diff options
Diffstat (limited to 'spec/hashie/dash_spec.rb')
-rw-r--r-- | spec/hashie/dash_spec.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb index 4299d97..6ed9c9a 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -151,6 +151,64 @@ describe DashTest do end end + describe '#merge' do + it 'creates a new instance of the Dash' do + new_dash = subject.merge(:first_name => 'Robert') + subject.object_id.should_not == new_dash.object_id + end + + it 'merges the given hash' do + new_dash = subject.merge(:first_name => 'Robert', :email => 'robert@example.com') + new_dash.first_name.should == 'Robert' + new_dash.email.should == 'robert@example.com' + end + + it 'fails with non-existent properties' do + expect { subject.merge(:middle_name => 'James') }.to raise_error(NoMethodError) + end + + it 'errors out when attempting to set a required property to nil' do + expect { subject.merge(:first_name => nil) }.to raise_error(ArgumentError) + end + + context "given a block" do + it "sets merged key's values to the block's return value" do + subject.merge(:first_name => 'Jim') do |key, oldval, newval| + "#{key}: #{newval} #{oldval}" + end.first_name.should == 'first_name: Jim Bob' + end + end + end + + describe '#merge!' do + it 'modifies the existing instance of the Dash' do + original_dash = subject.merge!(:first_name => 'Robert') + subject.object_id.should == original_dash.object_id + end + + it 'merges the given hash' do + subject.merge!(:first_name => 'Robert', :email => 'robert@example.com') + subject.first_name.should == 'Robert' + subject.email.should == 'robert@example.com' + end + + it 'fails with non-existent properties' do + expect { subject.merge!(:middle_name => 'James') }.to raise_error(NoMethodError) + end + + it 'errors out when attempting to set a required property to nil' do + expect { subject.merge!(:first_name => nil) }.to raise_error(ArgumentError) + end + + context "given a block" do + it "sets merged key's values to the block's return value" do + subject.merge!(:first_name => 'Jim') do |key, oldval, newval| + "#{key}: #{newval} #{oldval}" + end.first_name.should == 'first_name: Jim Bob' + end + end + end + describe 'properties' do it 'lists defined properties' do described_class.properties.should == Set.new([:first_name, :email, :count]) |