summaryrefslogtreecommitdiff
path: root/test/ruby/test_hash.rb
diff options
context:
space:
mode:
authorKenichi Kamiya <kachick1@gmail.com>2021-03-20 19:35:36 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-03-20 20:46:15 +0900
commit7954bb056be30e86c419fe3792064d28990a4999 (patch)
treee69e50ed25911633d8193872b4c62a12a919babd /test/ruby/test_hash.rb
parent278522f0535d081ff6473f210cb45f923ff8810a (diff)
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
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4297
Diffstat (limited to 'test/ruby/test_hash.rb')
-rw-r--r--test/ruby/test_hash.rb42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index 3458053fea..849fd3a772 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -439,6 +439,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
@@ -446,6 +455,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
@@ -743,6 +760,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
@@ -1046,6 +1072,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
@@ -1098,6 +1132,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