diff options
Diffstat (limited to 'spec/hashie/extensions')
-rw-r--r-- | spec/hashie/extensions/coercion_spec.rb | 21 | ||||
-rw-r--r-- | spec/hashie/extensions/indifferent_access_spec.rb | 10 | ||||
-rw-r--r-- | spec/hashie/extensions/key_conversion_spec.rb | 42 |
3 files changed, 68 insertions, 5 deletions
diff --git a/spec/hashie/extensions/coercion_spec.rb b/spec/hashie/extensions/coercion_spec.rb index c5d932a..8dcf6fd 100644 --- a/spec/hashie/extensions/coercion_spec.rb +++ b/spec/hashie/extensions/coercion_spec.rb @@ -17,7 +17,10 @@ describe Hashie::Extensions::Coercion do end before(:each) do - class ExampleCoercableHash < Hash; include Hashie::Extensions::Coercion end + class ExampleCoercableHash < Hash + include Hashie::Extensions::Coercion + include Hashie::Extensions::MergeInitializer + end end subject { ExampleCoercableHash } let(:instance){ subject.new } @@ -32,6 +35,15 @@ describe Hashie::Extensions::Coercion do instance[:foo].should be_coerced end + it "should support an array of keys" do + subject.coerce_keys :foo, :bar, Coercable + + instance[:foo] = "bar" + instance[:bar] = "bax" + instance[:foo].should be_coerced + instance[:bar].should be_coerced + end + it 'should just call #new if no coerce method is available' do subject.coerce_key :foo, Initializable @@ -39,6 +51,13 @@ describe Hashie::Extensions::Coercion do instance[:foo].value.should == "String" instance[:foo].should_not be_coerced end + + it "should coerce when the merge initializer is used" do + subject.coerce_key :foo, Coercable + instance = subject.new(:foo => "bar") + + instance[:foo].should be_coerced + end end describe '.coerce_value' do diff --git a/spec/hashie/extensions/indifferent_access_spec.rb b/spec/hashie/extensions/indifferent_access_spec.rb index 7c421cb..382d930 100644 --- a/spec/hashie/extensions/indifferent_access_spec.rb +++ b/spec/hashie/extensions/indifferent_access_spec.rb @@ -38,11 +38,19 @@ describe Hashie::Extensions::IndifferentAccess do end describe '#key?' do + let(:h) { subject.new(:foo => 'bar') } + it 'should find it indifferently' do - h = subject.new(:foo => 'bar') h.should be_key(:foo) h.should be_key('foo') end + + %w(include? member? has_key?).each do |key_alias| + it "should be aliased as #{key_alias}" do + h.send(key_alias.to_sym, :foo).should be(true) + h.send(key_alias.to_sym, 'foo').should be(true) + end + end end describe '#update' do diff --git a/spec/hashie/extensions/key_conversion_spec.rb b/spec/hashie/extensions/key_conversion_spec.rb index 72e1614..4edb022 100644 --- a/spec/hashie/extensions/key_conversion_spec.rb +++ b/spec/hashie/extensions/key_conversion_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe Hashie::Extensions::KeyConversion do subject do - klass = Class.new(Hash) + klass = Class.new(::Hash) klass.send :include, Hashie::Extensions::KeyConversion klass end @@ -16,6 +16,24 @@ describe Hashie::Extensions::KeyConversion do (instance.keys & %w(abc 123)).size.should == 2 end + it 'should do deep conversion within nested hashes' do + instance[:ab] = subject.new + instance[:ab][:cd] = subject.new + instance[:ab][:cd][:ef] = 'abcdef' + instance.stringify_keys! + instance.should == {'ab' => {'cd' => {'ef' => 'abcdef'}}} + end + + it 'should do deep conversion within nested arrays' do + instance[:ab] = [] + instance[:ab] << subject.new + instance[:ab] << subject.new + instance[:ab][0][:cd] = 'abcd' + instance[:ab][1][:ef] = 'abef' + instance.stringify_keys! + instance.should == {'ab' => [{'cd' => 'abcd'}, {'ef' => 'abef'}]} + end + it 'should return itself' do instance.stringify_keys!.should == instance end @@ -44,13 +62,31 @@ describe Hashie::Extensions::KeyConversion do (instance.keys & [:abc, :def]).size.should == 2 end + it 'should do deep conversion within nested hashes' do + instance['ab'] = subject.new + instance['ab']['cd'] = subject.new + instance['ab']['cd']['ef'] = 'abcdef' + instance.symbolize_keys! + instance.should == {:ab => {:cd => {:ef => 'abcdef'}}} + end + + it 'should do deep conversion within nested arrays' do + instance['ab'] = [] + instance['ab'] << subject.new + instance['ab'] << subject.new + instance['ab'][0]['cd'] = 'abcd' + instance['ab'][1]['ef'] = 'abef' + instance.symbolize_keys! + instance.should == {:ab => [{:cd => 'abcd'}, {:ef => 'abef'}]} + end + it 'should return itself' do instance.symbolize_keys!.should == instance end end - describe '#stringify_keys' do - it 'should convert keys to strings' do + describe '#symbolize_keys' do + it 'should convert keys to symbols' do instance['abc'] = 'def' copy = instance.symbolize_keys copy[:abc].should == 'def' |