From 6b1192b8cdf2989beff7f21656206a75aca19ece Mon Sep 17 00:00:00 2001 From: dblock Date: Thu, 1 May 2014 08:20:09 -0400 Subject: [PATCH] Spec for issue #149. --- lib/hashie/extensions/ignore_undeclared.rb | 2 +- .../extensions/ignore_undeclared_spec.rb | 49 ++++++++++++++----- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/hashie/extensions/ignore_undeclared.rb b/lib/hashie/extensions/ignore_undeclared.rb index 3f9e16c4..8c7bfe9f 100644 --- a/lib/hashie/extensions/ignore_undeclared.rb +++ b/lib/hashie/extensions/ignore_undeclared.rb @@ -5,7 +5,7 @@ module Extensions # raising an error. This is useful when using a Trash to # capture a subset of a larger hash. # - # Note that attempting to retrieve an undeclared property + # Note that attempting to retrieve or set an undeclared property # will still raise a NoMethodError, even if a value for # that property was provided at initialization. # diff --git a/spec/hashie/extensions/ignore_undeclared_spec.rb b/spec/hashie/extensions/ignore_undeclared_spec.rb index 4a6d19c9..aef289c3 100644 --- a/spec/hashie/extensions/ignore_undeclared_spec.rb +++ b/spec/hashie/extensions/ignore_undeclared_spec.rb @@ -1,23 +1,46 @@ require 'spec_helper' describe Hashie::Extensions::IgnoreUndeclared do - class ForgivingTrash < Hashie::Trash - include Hashie::Extensions::IgnoreUndeclared - property :city - property :state, from: :provence - end + context 'included in Trash' do + class ForgivingTrash < Hashie::Trash + include Hashie::Extensions::IgnoreUndeclared + property :city + property :state, from: :provence + end - subject { ForgivingTrash } + subject { ForgivingTrash } - it 'silently ignores undeclared properties on initialization' do - expect { subject.new(city: 'Toronto', provence: 'ON', country: 'Canada') }.to_not raise_error - end + it 'silently ignores undeclared properties on initialization' do + expect { subject.new(city: 'Toronto', provence: 'ON', country: 'Canada') }.to_not raise_error + end + + it 'works with translated properties (with symbol keys)' do + expect(subject.new(provence: 'Ontario').state).to eq('Ontario') + end - it 'works with translated properties (with symbol keys)' do - expect(subject.new(provence: 'Ontario').state).to eq('Ontario') + it 'works with translated properties (with string keys)' do + expect(subject.new(provence: 'Ontario').state).to eq('Ontario') + end end - it 'works with translated properties (with string keys)' do - expect(subject.new(provence: 'Ontario').state).to eq('Ontario') + context 'combined with DeepMerge' do + class ForgivingTrashWithMerge < Hashie::Trash + include Hashie::Extensions::DeepMerge + include Hashie::Extensions::IgnoreUndeclared + property :some_key + end + + it 'requires properties to be declared on assignment' do + hash = ForgivingTrashWithMerge.new(some_ignored_key: 17, some_key: 12) + expect { hash.deep_merge(some_other_key: 55) }.to raise_error(NoMethodError) + end + + it 'deep merges' do + class ForgivingTrashWithMergeAndProperty < ForgivingTrashWithMerge + property :some_other_key + end + hash = ForgivingTrashWithMergeAndProperty.new(some_ignored_key: 17, some_key: 12) + expect(hash.deep_merge(some_other_key: 55)).to eq("some_key" => 12, "some_other_key" => 55) + end end end