spec/hashie/extensions/indifferent_access_spec.rb in hashie-3.5.7 vs spec/hashie/extensions/indifferent_access_spec.rb in hashie-3.6.0

- old
+ new

@@ -4,27 +4,27 @@ class IndifferentHashWithMergeInitializer < Hash include Hashie::Extensions::MergeInitializer include Hashie::Extensions::IndifferentAccess class << self - alias_method :build, :new + alias build new end end class IndifferentHashWithArrayInitializer < Hash include Hashie::Extensions::IndifferentAccess class << self - alias_method :build, :[] + alias build [] end end class IndifferentHashWithTryConvertInitializer < Hash include Hashie::Extensions::IndifferentAccess class << self - alias_method :build, :try_convert + alias build try_convert end end class IndifferentHashWithDash < Hashie::Dash include Hashie::Extensions::IndifferentAccess @@ -42,24 +42,37 @@ it 'indifferently merges in a hash' do indifferent_hash = Class.new(::Hash) do include Hashie::Extensions::IndifferentAccess end.new - merged_hash = indifferent_hash.merge(:cat => 'meow') + merged_hash = indifferent_hash.merge(cat: 'meow') expect(merged_hash[:cat]).to eq('meow') expect(merged_hash['cat']).to eq('meow') end + + it 'injects the resulting new Hash with IndifferentAccess' do + hash = IndifferentHashWithMergeInitializer.new( + :cat => 'meow', + 'dog' => { name: 'Mango', sound: 'woof' } + ) + + dog = hash[:dog] + merged = dog.merge(foo: 'bar') + + expect(merged[:foo]).to eq('bar') + expect(merged['foo']).to eq('bar') + end end describe '#merge!' do it 'indifferently merges in a hash' do indifferent_hash = Class.new(::Hash) do include Hashie::Extensions::IndifferentAccess end.new - indifferent_hash.merge!(:cat => 'meow') + indifferent_hash[:cat] = 'meow' expect(indifferent_hash[:cat]).to eq('meow') expect(indifferent_hash['cat']).to eq('meow') end end @@ -111,11 +124,11 @@ end describe '#values_at' do it 'indifferently finds values' do h = subject.build(:foo => 'bar', 'baz' => 'qux') - expect(h.values_at('foo', :baz)).to eq %w(bar qux) + expect(h.values_at('foo', :baz)).to eq %w[bar qux] end it 'returns the same instance of the hash that was set' do hash = {} h = subject.build(foo: hash) @@ -193,10 +206,10 @@ it 'finds it indifferently' do expect(h).to be_key(:foo) expect(h).to be_key('foo') end - %w(include? member? has_key?).each do |key_alias| + %w[include? member? has_key?].each do |key_alias| it "is aliased as #{key_alias}" do expect(h.send(key_alias.to_sym, :foo)).to be(true) expect(h.send(key_alias.to_sym, 'foo')).to be(true) end end