diff options
author | Guillermo Iguaran <guilleiguaran@gmail.com> | 2019-02-08 12:28:12 -0500 |
---|---|---|
committer | Guillermo Iguaran <guilleiguaran@gmail.com> | 2019-02-08 12:28:38 -0500 |
commit | b8dc06b8fdc16874160f61dcf58743fcc10e57db (patch) | |
tree | 4dd55334bdc2c7a30073f5b52b7a3cc4a5e3c94d /activesupport/test/core_ext | |
parent | 5f9e05048d409b9c3fb35a3620fe361fb03dd4e1 (diff) | |
download | rails-b8dc06b8fdc16874160f61dcf58743fcc10e57db.tar.gz rails-b8dc06b8fdc16874160f61dcf58743fcc10e57db.tar.bz2 rails-b8dc06b8fdc16874160f61dcf58743fcc10e57db.zip |
Add 'Hash#deep_transform_values', and 'Hash#deep_transform_values!'
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r-- | activesupport/test/core_ext/hash/deep_transform_values_test.rb | 0 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 27 |
2 files changed, 27 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/hash/deep_transform_values_test.rb b/activesupport/test/core_ext/hash/deep_transform_values_test.rb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/activesupport/test/core_ext/hash/deep_transform_values_test.rb diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index e8e0a1ae72..90a5f2dcd1 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -33,6 +33,8 @@ class HashExtTest < ActiveSupport::TestCase h = {} assert_respond_to h, :deep_transform_keys assert_respond_to h, :deep_transform_keys! + assert_respond_to h, :deep_transform_values + assert_respond_to h, :deep_transform_values! assert_respond_to h, :symbolize_keys assert_respond_to h, :symbolize_keys! assert_respond_to h, :deep_symbolize_keys @@ -78,6 +80,31 @@ class HashExtTest < ActiveSupport::TestCase assert_equal({ "a" => { b: { "c" => 3 } } }, @nested_mixed) end + def test_deep_transform_values + assert_equal({ "a" => "1", "b" => "2" }, @strings.deep_transform_values{ |value| value.to_s }) + assert_equal({ "a" => { "b" => { "c" => "3" } } }, @nested_strings.deep_transform_values { |value| value.to_s }) + assert_equal({ "a" => [ { "b" => "2" }, { "c" => "3" }, "4" ] }, @string_array_of_hashes.deep_transform_values { |value| value.to_s }) + end + + def test_deep_transform_values_not_mutates + transformed_hash = @nested_mixed.deep_dup + transformed_hash.deep_transform_values { |value| value.to_s } + assert_equal @nested_mixed, transformed_hash + end + + def test_deep_transform_values! + assert_equal({ "a" => "1", "b" => "2" }, @strings.deep_transform_values!{ |value| value.to_s }) + assert_equal({ "a" => { "b" => { "c" => "3" } } }, @nested_strings.deep_transform_values! { |value| value.to_s }) + assert_equal({ "a" => [ { "b" => "2" }, { "c" => "3" }, "4" ] }, @string_array_of_hashes.deep_transform_values! { |value| value.to_s }) + end + + def test_deep_transform_values_with_bang_mutates + transformed_hash = @nested_mixed.deep_dup + transformed_hash.deep_transform_values! { |value| value.to_s } + assert_equal({ "a" => { b: { "c" => "3" } } }, transformed_hash) + assert_equal({ "a" => { b: { "c" => 3 } } }, @nested_mixed) + end + def test_symbolize_keys assert_equal @symbols, @symbols.symbolize_keys assert_equal @symbols, @strings.symbolize_keys |