Sha256: 27a87144ddc0fbc9bb39842dfb7cc51d18b4c547837c2bda7cb31441f400a218

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

require 'spec_helper'

describe Hashie::Extensions::Coercion do
  class Initializable
    def initialize(obj, coerced = false)
      @coerced = coerced
      @value = obj.class.to_s
    end
    def coerced?; @coerced end
    attr_reader :value
  end

  class Coercable < Initializable
    def self.coerce(obj)
      new(obj, true)
    end
  end

  before(:each) do
    class ExampleCoercableHash < Hash; include Hashie::Extensions::Coercion end
  end
  subject { ExampleCoercableHash }
  let(:instance){ subject.new }

  describe '.coerce_key' do
    it { subject.should be_respond_to(:coerce_key) }

    it 'should run through coerce on a specified key' do
      subject.coerce_key :foo, Coercable

      instance[:foo] = "bar"
      instance[:foo].should be_coerced
    end

    it 'should just call #new if no coerce method is available' do
      subject.coerce_key :foo, Initializable

      instance[:foo] = "bar"
      instance[:foo].value.should == "String"
      instance[:foo].should_not be_coerced
    end
  end

  describe '.coerce_value' do
    context 'with :strict => true' do
      it 'should coerce any value of the exact right class' do
        subject.coerce_value String, Coercable
        
        instance[:foo] = "bar"
        instance[:bar] = "bax"
        instance[:foo].should be_coerced
        instance[:bar].should be_coerced
      end

      it 'should not coerce superclasses' do
        klass = Class.new(String)
        subject.coerce_value klass, Coercable

        instance[:foo] = "bar"
        instance[:foo].should_not be_kind_of(Coercable)
        instance[:foo] = klass.new
        instance[:foo].should be_kind_of(Coercable)
      end
    end
  end

  after(:each) do
    Object.send(:remove_const, :ExampleCoercableHash)
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
cb_hashie-2.0.0.beta spec/hashie/extensions/coercion_spec.rb
hashie-pre-2.0.0.beta spec/hashie/extensions/coercion_spec.rb