diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/hashie/extensions/mash/symbolize_keys_spec.rb | 24 | ||||
-rw-r--r-- | spec/hashie/extensions/symbolize_keys_spec.rb | 5 | ||||
-rw-r--r-- | spec/hashie/hash_spec.rb | 6 | ||||
-rw-r--r-- | spec/hashie/mash_spec.rb | 22 |
4 files changed, 40 insertions, 17 deletions
diff --git a/spec/hashie/extensions/mash/symbolize_keys_spec.rb b/spec/hashie/extensions/mash/symbolize_keys_spec.rb index 9846737..bf58ea1 100644 --- a/spec/hashie/extensions/mash/symbolize_keys_spec.rb +++ b/spec/hashie/extensions/mash/symbolize_keys_spec.rb @@ -9,12 +9,30 @@ RSpec.describe Hashie::Extensions::Mash::SymbolizeKeys do end.to raise_error(ArgumentError) end - it 'symbolizes all keys in the Mash' do - my_mash = Class.new(Hashie::Mash) do + context 'when included in a Mash' do + class SymbolizedMash < Hashie::Mash include Hashie::Extensions::Mash::SymbolizeKeys end - expect(my_mash.new('test' => 'value').to_h).to eq(test: 'value') + it 'symbolizes string keys in the Mash' do + my_mash = SymbolizedMash.new('test' => 'value') + expect(my_mash.to_h).to eq(test: 'value') + end + + it 'preserves keys which cannot be symbolized' do + my_mash = SymbolizedMash.new( + '1' => 'symbolizable one', + 1 => 'one', + [1, 2, 3] => 'testing', + { 'test' => 'value' } => 'value' + ) + expect(my_mash.to_h).to eq( + :'1' => 'symbolizable one', + 1 => 'one', + [1, 2, 3] => 'testing', + { 'test' => 'value' } => 'value' + ) + end end context 'implicit to_hash on double splat' do diff --git a/spec/hashie/extensions/symbolize_keys_spec.rb b/spec/hashie/extensions/symbolize_keys_spec.rb index be345ea..34ac8d8 100644 --- a/spec/hashie/extensions/symbolize_keys_spec.rb +++ b/spec/hashie/extensions/symbolize_keys_spec.rb @@ -89,9 +89,10 @@ describe Hashie::Extensions::SymbolizeKeys do context 'singleton methods' do subject { Hash } let(:object) do - subject.new.merge('a' => 1, 'b' => { 'c' => 2 }).extend(Hashie::Extensions::SymbolizeKeys) + subject.new.merge('a' => 1, 'b' => { 'c' => 2 }, 1 => 'numeric key') + .extend(Hashie::Extensions::SymbolizeKeys) end - let(:expected_hash) { { a: 1, b: { c: 2 } } } + let(:expected_hash) { { a: 1, b: { c: 2 }, 1 => 'numeric key' } } describe '.symbolize_keys' do it 'does not raise error' do diff --git a/spec/hashie/hash_spec.rb b/spec/hashie/hash_spec.rb index 0282258..9e2b4e0 100644 --- a/spec/hashie/hash_spec.rb +++ b/spec/hashie/hash_spec.rb @@ -41,7 +41,7 @@ describe Hash do it '#to_hash with symbolize_keys set to true returns a hash with symbolized keys' do hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]] symbolized_hash = hash.to_hash(symbolize_keys: true) - expect(symbolized_hash).to eq(a: 'hey', :"123" => 'bob', array: [1, 2, 3]) + expect(symbolized_hash).to eq(a: 'hey', 123 => 'bob', array: [1, 2, 3]) end it "#to_hash should not blow up when #to_hash doesn't accept arguments" do @@ -112,9 +112,9 @@ describe Hash do expected = { a: 'hey', - :"123" => 'bob', + 123 => 'bob', array: [1, 2, 3], - subhash: { a: 'hey', b: 'bar', :'123' => 'bob', array: [1, 2, 3] } + subhash: { a: 'hey', b: 'bar', 123 => 'bob', array: [1, 2, 3] } } expect(symbolized_hash).to eq(expected) diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index b0aef57..4731985 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -613,6 +613,18 @@ describe Hashie::Mash do expect(converted.to_hash['a'].first.is_a?(Hash)).to be_truthy expect(converted.to_hash['a'].first['c'].first.is_a?(Hashie::Mash)).to be_falsy end + + it 'only stringifies keys which can be converted to symbols' do + initial_hash = { 1 => 'a', ['b'] => 2, 'c' => 3, d: 4 } + converted = Hashie::Mash.new(initial_hash) + expect(converted).to eq(1 => 'a', ['b'] => 2, 'c' => 3, 'd' => 4) + end + + it 'preserves keys which cannot be converted to symbols' do + initial_hash = { 1 => 'a', '1' => 'b', :'1' => 'c' } + converted = Hashie::Mash.new(initial_hash) + expect(converted).to eq(1 => 'a', '1' => 'c') + end end describe '#fetch' do @@ -900,7 +912,7 @@ describe Hashie::Mash do end it 'returns a mash with the keys and values inverted' do - expect(mash.invert).to eq('apple' => 'a', '4' => 'b') + expect(mash.invert).to eq('apple' => 'a', 4 => 'b') end context 'when using with subclass' do @@ -977,14 +989,6 @@ describe Hashie::Mash do expect(subject.dig('a', 'b')).to eq(1) end - context 'with numeric key' do - subject { described_class.new('1' => { b: 1 }) } - it 'accepts a numeric value as key' do - expect(subject.dig(1, :b)).to eq(1) - expect(subject.dig('1', :b)).to eq(1) - end - end - context 'when the Mash wraps a Hashie::Array' do it 'handles digging into an array' do mash = described_class.new(alphabet: { first_three: Hashie::Array['a', 'b', 'c'] }) |