diff options
author | gregory <greg2502@gmail.com> | 2014-06-26 09:28:11 -0700 |
---|---|---|
committer | gregory <greg2502@gmail.com> | 2014-06-26 15:46:17 -0700 |
commit | f09c5f68f907659a68fa759e7a7dcd225b7c2078 (patch) | |
tree | ca130ce7bc02b633796e25d5856b16f54412947e /spec/hashie/extensions/coercion_spec.rb | |
parent | 4ab9a3361a9d6a9fd958d7c7f0a159b236149f4d (diff) | |
download | hashie-f09c5f68f907659a68fa759e7a7dcd225b7c2078.tar.gz |
Added support for coercing enumerables and collections
Diffstat (limited to 'spec/hashie/extensions/coercion_spec.rb')
-rw-r--r-- | spec/hashie/extensions/coercion_spec.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/spec/hashie/extensions/coercion_spec.rb b/spec/hashie/extensions/coercion_spec.rb index 4aab298..d73e884 100644 --- a/spec/hashie/extensions/coercion_spec.rb +++ b/spec/hashie/extensions/coercion_spec.rb @@ -50,6 +50,48 @@ describe Hashie::Extensions::Coercion do expect(instance[:bar]).to be_coerced end + it 'supports coercion for Array' do + subject.coerce_key :foo, Array[Coercable] + + instance[:foo] = %w('bar', 'bar2') + expect(instance[:foo]).to all(be_coerced) + expect(instance[:foo]).to be_a(Array) + end + + it 'supports coercion for Set' do + subject.coerce_key :foo, Set[Coercable] + + instance[:foo] = Set.new(%w('bar', 'bar2')) + expect(instance[:foo]).to all(be_coerced) + expect(instance[:foo]).to be_a(Set) + end + + it 'supports coercion for Set of primitive' do + subject.coerce_key :foo, Set[Initializable] + + instance[:foo] = %w('bar', 'bar2') + expect(instance[:foo].map(&:value)).to all(eq 'String') + expect(instance[:foo]).to be_none { |v| v.coerced? } + expect(instance[:foo]).to be_a(Set) + end + + it 'supports coercion for Hash' do + subject.coerce_key :foo, Hash[Coercable => Coercable] + + instance[:foo] = { 'bar_key' => 'bar_value', 'bar2_key' => 'bar2_value' } + expect(instance[:foo].keys).to all(be_coerced) + expect(instance[:foo].values).to all(be_coerced) + expect(instance[:foo]).to be_a(Hash) + end + + it 'supports coercion for Hash with primitive as value' do + subject.coerce_key :foo, Hash[Coercable => Initializable] + + instance[:foo] = { 'bar_key' => '1', 'bar2_key' => '2' } + expect(instance[:foo].values.map(&:value)).to all(eq 'String') + expect(instance[:foo].keys).to all(be_coerced) + end + it 'calls #new if no coerce method is available' do subject.coerce_key :foo, Initializable |