diff options
author | Pranas Kiziela <pranas.kiziela@gmail.com> | 2012-09-13 21:49:49 +0300 |
---|---|---|
committer | Pranas Kiziela <pranas.kiziela@gmail.com> | 2012-09-13 23:01:18 +0300 |
commit | 54575d87973b95cbd278fbe086b70761584d286d (patch) | |
tree | 7c93bc193b5606c061f19fed793ca9cd62055849 /activesupport/test | |
parent | 8692db59af8f0dc468b588963622e61ff982a925 (diff) | |
download | rails-54575d87973b95cbd278fbe086b70761584d286d.tar.gz rails-54575d87973b95cbd278fbe086b70761584d286d.tar.bz2 rails-54575d87973b95cbd278fbe086b70761584d286d.zip |
Allow passing block to deep_merge and deep_merge!
Hash#merge accepts block that you can use to customize how hash values
are merged. This change makes merge and deep_merge compatible.
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 94463cc311..01934dd2c3 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -582,6 +582,16 @@ class HashExtTest < ActiveSupport::TestCase assert_equal expected, hash_1 end + def test_deep_merge_with_block + hash_1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } } + hash_2 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } } + expected = { :a => [:a, "a", 1], :b => "b", :c => { :c1 => [:c1, "c1", 2], :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } } + assert_equal(expected, hash_1.deep_merge(hash_2) { |k,o,n| [k, o, n] }) + + hash_1.deep_merge!(hash_2) { |k,o,n| [k, o, n] } + assert_equal expected, hash_1 + end + def test_deep_merge_on_indifferent_access hash_1 = HashWithIndifferentAccess.new({ :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } }) hash_2 = HashWithIndifferentAccess.new({ :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }) |