diff options
author | dblock <dblock@dblock.org> | 2021-11-07 22:21:45 -0500 |
---|---|---|
committer | dblock <dblock@dblock.org> | 2021-11-07 22:21:45 -0500 |
commit | 25a3ff6e0498234d25de6a02bdbd3e508ade5898 (patch) | |
tree | 3ae1add7a8731daf46264b7a78cea9ac8b3278d0 /spec | |
parent | 796f9446fe3313685fe1a5c74052e1e942d170a2 (diff) | |
parent | 98df995e2f0433d3d1abe46efd732bc8e20e31cc (diff) | |
download | hashie-25a3ff6e0498234d25de6a02bdbd3e508ade5898.tar.gz |
Merge branch 'dash-nil-keys' of https://github.com/michaelherold/hashie into michaelherold-dash-nil-keys
Diffstat (limited to 'spec')
-rw-r--r-- | spec/hashie/dash_spec.rb | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb index 3adfc8f..e75ad1a 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -344,7 +344,7 @@ describe DashTest do before { subject.replace(first_name: 'Cain') } it 'return self' do - expect(subject.replace(email: 'bar').to_hash).to eq(email: 'bar', count: 0) + expect(subject.replace(email: 'bar').object_id).to eq subject.object_id end it 'sets all specified keys to their corresponding values' do @@ -416,6 +416,7 @@ describe DashTest do Class.new(Hashie::Dash) do property :a, required: -> { b.nil? }, message: 'is required if b is not set.' property :b, required: -> { a.nil? }, message: 'is required if a is not set.' + property :c, default: -> { 'c' } end end @@ -434,6 +435,44 @@ describe DashTest do it 'raises an error when neither property is set' do expect { codependent.new(a: nil, b: nil) }.to raise_error(ArgumentError) end + + context 'exporting nil values' do + describe '#to_h' do + it 'does not prune nil values' do + expect(codependent.new(a: 'hi', b: nil).to_h).to eq(a: 'hi', b: nil, c: 'c') + expect(codependent.new(a: 'hi', b: nil, c: nil).to_hash).to eq(a: 'hi', b: nil, c: 'c') + expect(codependent.new(a: 'hi', b: nil).merge(c: nil).to_h).to( + eq(a: 'hi', b: nil, c: nil) + ) + end + end + + describe '#to_hash' do + it 'does not prune nil values' do + expect(codependent.new(a: 'hi', b: nil).to_hash).to eq(a: 'hi', b: nil, c: 'c') + expect(codependent.new(a: 'hi', b: nil, c: nil).to_hash).to eq(a: 'hi', b: nil, c: 'c') + expect(codependent.new(a: 'hi', b: nil).merge(c: nil).to_hash).to( + eq(a: 'hi', b: nil, c: nil) + ) + end + end + + describe '**' do + # Note: This test is an implementation detail of MRI and may not hold for + # other Ruby interpreters. But it's important to note in the test suite + # because it can be surprising for people unfamiliar with the semantics of + # double-splatting. + # + # For more information, see [this link][1]: + # + # [1]: https://github.com/hashie/hashie/issues/353#issuecomment-363294886 + it 'prunes nil values because they are not set in the dash' do + dash = codependent.new(a: 'hi', b: nil) + + expect(**dash).to eq(a: 'hi', c: 'c') + end + end + end end end end @@ -482,6 +521,43 @@ describe Hashie::Dash, 'inheritance' do expect(@bottom.new).to have_key(:echo) expect(@bottom.new).to_not have_key('echo') end + + context 'exporting nil values' do + let(:test) do + Class.new(Hashie::Dash) do + property :foo + property :bar + end + end + + describe '#to_h' do + it 'does not prune nil values' do + expect(test.new(foo: 'hi', bar: nil).to_h).to eq(foo: 'hi', bar: nil) + end + end + + describe '#to_hash' do + it 'does not prune nil values' do + expect(test.new(foo: 'hi', bar: nil).to_hash).to eq(foo: 'hi', bar: nil) + end + end + + describe '**' do + # Note: This test is an implementation detail of MRI and may not hold for + # other Ruby interpreters. But it's important to note in the test suite + # because it can be surprising for people unfamiliar with the semantics of + # double-splatting. + # + # For more information, see [this link][1]: + # + # [1]: https://github.com/hashie/hashie/issues/353#issuecomment-363294886 + it 'prunes nil values because they are not set in the dash' do + dash = test.new(foo: 'hi', bar: nil) + + expect(**dash).to eq(foo: 'hi') + end + end + end end describe SubclassedTest do |