Sha256: 7f64dac7e235e72193c77e6e492a19ae316840f594b179a3549ecb44b6341314

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

RSpec.describe Necromancer::Conversions, '.register' do
  it "allows to register converter" do
    context = described_class.new
    converter = double(:converter, {source: :string, target: :numeric})
    expect(context.register(converter)).to eq(true)
    expect(context[:string, :numeric]).to eq(converter)
  end

  it "allows to register converter with no source" do
    context = described_class.new
    converter = double(:converter, {source: nil, target: :numeric})
    expect(context.register(converter)).to eq(true)
    expect(context[:none, :numeric]).to eq(converter)
  end

  it "allows to register converter with no target" do
    context = described_class.new
    converter = double(:converter, {source: :string, target: nil})
    expect(context.register(converter)).to eq(true)
    expect(context[:string, :none]).to eq(converter)
  end

  it "allows to register anonymous converter" do
    conversions = described_class.new

    conversions.register do |c|
      c.source= :string
      c.target= :upcase
      c.convert = proc { |value| value.to_s.upcase }
    end
    expect(conversions[:string, :upcase].call('magic')).to eq('MAGIC')
  end

  it "allows to register anonymous converter with class names" do
    conversions = described_class.new

    conversions.register do |c|
      c.source= String
      c.target= Array
      c.convert = proc { |value| Array(value) }
    end
    expect(conversions[String, Array].call('magic')).to eq(['magic'])
  end

  it "allows to register custom converter" do
    conversions = described_class.new
    UpcaseConverter = Struct.new(:source, :target) do
      def call(value)
        value.to_s.upcase
      end
    end
    upcase_converter = UpcaseConverter.new(:string, :upcase)
    expect(conversions.register(upcase_converter)).to be(true)
    expect(conversions[:string, :upcase].call('magic')).to eq('MAGIC')
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
pokedex-terminal-0.2.8 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/conversions/register_spec.rb
pokedex-terminal-0.2.7 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/conversions/register_spec.rb
pokedex-terminal-0.2.6 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/conversions/register_spec.rb
pokedex-terminal-0.2.5 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/conversions/register_spec.rb
pokedex-terminal-0.2.4 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/conversions/register_spec.rb
necromancer-0.5.1 spec/unit/conversions/register_spec.rb
necromancer-0.5.0 spec/unit/conversions/register_spec.rb