diff options
author | nagachika <nagachika@ruby-lang.org> | 2021-05-23 15:51:10 +0900 |
---|---|---|
committer | nagachika <nagachika@ruby-lang.org> | 2021-05-23 15:51:10 +0900 |
commit | 44b87adc07621b6a8eddfcf4aaff34ce634179d4 (patch) | |
tree | 3751a240810290e1cc80c0334daf9284ad8c705b /test/ruby/test_hash.rb | |
parent | 86f7e55dfb5938e0c617b8629a1fbb4d24341dc0 (diff) |
merge revision(s) e019dd24df4ed7063ad80d4c2e4070141793f598,7954bb056be30e86c419fe3792064d28990a4999,7d3fdfb27dac456827b004d9e66a44b15f8cd762: [Backport #17736]
Ensure the receiver is modifiable before shrinking [Bug #17736]
* Ensure the receiver is modifiable before shinking [Bug #17736]
* Assert the receivers are not modified
---
array.c | 1 +
test/ruby/test_array.rb | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 37 insertions(+)
Some Hash destructive methods ensure the receiver modifiable [Bug
#17736]
refs:
* https://bugs.ruby-lang.org/issues/17736
* https://github.com/ruby/ruby/pull/4296
This commit aims to cover following methods
* Hash#select!
* Hash#filter!
* Hash#keep_if
* Hash#reject!
* Hash#delete_if
I think these are not all.
---
* Ensure the receiver is modifiable or not
* Assert the receiver is not modified
---
hash.c | 2 ++
test/ruby/test_hash.rb | 42 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+)
Hash#transform_values! ensures receiver modifiable in block [Bug
#17736]
---
hash.c | 1 +
test/ruby/test_hash.rb | 9 +++++++++
2 files changed, 10 insertions(+)
Diffstat (limited to 'test/ruby/test_hash.rb')
-rw-r--r-- | test/ruby/test_hash.rb | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 812b9e60ff..aaf92d4f06 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -419,6 +419,15 @@ class TestHash < Test::Unit::TestCase true } assert_equal(base.size, n) + + h = base.dup + assert_raise(FrozenError) do + h.delete_if do + h.freeze + true + end + end + assert_equal(base.dup, h) end def test_keep_if @@ -426,6 +435,14 @@ class TestHash < Test::Unit::TestCase assert_equal({3=>4,5=>6}, h.keep_if {|k, v| k + v >= 7 }) h = @cls[1=>2,3=>4,5=>6] assert_equal({1=>2,3=>4,5=>6}, h.keep_if{true}) + h = @cls[1=>2,3=>4,5=>6] + assert_raise(FrozenError) do + h.keep_if do + h.freeze + false + end + end + assert_equal(@cls[1=>2,3=>4,5=>6], h) end def test_compact @@ -722,6 +739,15 @@ class TestHash < Test::Unit::TestCase h = base.dup assert_equal(h3, h.reject! {|k,v| v }) assert_equal(h3, h) + + h = base.dup + assert_raise(FrozenError) do + h.reject! do + h.freeze + true + end + end + assert_equal(base.dup, h) end def test_replace @@ -1025,6 +1051,14 @@ class TestHash < Test::Unit::TestCase assert_equal({3=>4,5=>6}, h) h = @cls[1=>2,3=>4,5=>6] assert_equal(nil, h.select!{true}) + h = @cls[1=>2,3=>4,5=>6] + assert_raise(FrozenError) do + h.select! do + h.freeze + false + end + end + assert_equal(@cls[1=>2,3=>4,5=>6], h) end def test_slice @@ -1077,6 +1111,14 @@ class TestHash < Test::Unit::TestCase assert_equal({3=>4,5=>6}, h) h = @cls[1=>2,3=>4,5=>6] assert_equal(nil, h.filter!{true}) + h = @cls[1=>2,3=>4,5=>6] + assert_raise(FrozenError) do + h.filter! do + h.freeze + false + end + end + assert_equal(@cls[1=>2,3=>4,5=>6], h) end def test_clear2 @@ -1720,6 +1762,15 @@ class TestHash < Test::Unit::TestCase x = @cls[a: 1, b: 2, c: 3] y = x.transform_values!.with_index {|v, i| "#{v}.#{i}" } assert_equal(%w(1.0 2.1 3.2), y.values_at(:a, :b, :c)) + + x = @cls[a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10] + assert_raise(FrozenError) do + x.transform_values!() do |v| + x.freeze if v == 2 + v.succ + end + end + assert_equal(@cls[a: 2, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10], x) end def test_broken_hash_value |