diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-07-14 06:44:00 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-07-14 06:44:00 +0000 |
commit | 1405111722af070f443e32d31d96177dac21b782 (patch) | |
tree | 676151764f3753acba21e2dd6114c2a8a3de33f9 /test/ruby | |
parent | 0b396d588060707e227f21b80e964180674c8a50 (diff) | |
download | ruby-1405111722af070f443e32d31d96177dac21b782.tar.gz |
hash.c: Add Hash#transform_keys and Hash#transform_keys!
* hash.c (transform_keys_i, rb_hash_transform_keys): Add Hash#transform_keys.
[Feature #13583] [ruby-core:81290]
* hash.c (rb_hash_transform_keys_bang): Add Hash#transform_keys!.
[Feature #13583] [ruby-core:81290]
* test/ruby/test_hash.rb: Add tests for above changes.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59328 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_hash.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index d915e52bde..8fb1c17003 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1530,6 +1530,34 @@ class TestHash < Test::Unit::TestCase assert_equal([10, 20, 30], [1, 2, 3].map(&h)) end + def test_transform_keys + x = @cls[a: 1, b: 2, c: 3] + y = x.transform_keys {|k| :"#{k}!" } + assert_equal({a: 1, b: 2, c: 3}, x) + assert_equal({a!: 1, b!: 2, c!: 3}, y) + + enum = x.transform_keys + assert_equal(x.size, enum.size) + assert_instance_of(Enumerator, enum) + + y = x.transform_keys.with_index {|k, i| "#{k}.#{i}" } + assert_equal(%w(a.0 b.1 c.2), y.keys) + end + + def test_transform_keys_bang + x = @cls[a: 1, b: 2, c: 3] + y = x.transform_keys! {|k| :"#{k}!" } + assert_equal({a!: 1, b!: 2, c!: 3}, x) + assert_same(x, y) + + enum = x.transform_keys! + assert_equal(x.size, enum.size) + assert_instance_of(Enumerator, enum) + + x.transform_keys!.with_index {|k, i| "#{k}.#{i}" } + assert_equal(%w(a!.0 b!.1 c!.2), x.keys) + end + def test_transform_values x = @cls[a: 1, b: 2, c: 3] y = x.transform_values {|v| v ** 2 } |