1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add Hash#{update, merge!} test to ensure receiver modifiable in block

This commit is contained in:
Kenichi Kamiya 2021-03-21 01:57:46 +09:00 committed by Nobuyoshi Nakada
parent 54bfa0570d
commit d319eb602d
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
Notes: git 2021-03-21 11:14:50 +09:00

View file

@ -1232,6 +1232,14 @@ class TestHash < Test::Unit::TestCase
assert_equal({1=>8, 2=>4, 3=>4, 5=>7}, h1)
end
def test_update5
h = @cls[a: 1, b: 2, c: 3]
assert_raise(FrozenError) do
h.update({a: 10, b: 20}){ |key, v1, v2| key == :b && h.freeze; v2 }
end
assert_equal(@cls[a: 10, b: 2, c: 3], h)
end
def test_merge
h1 = @cls[1=>2, 3=>4]
h2 = {1=>3, 5=>7}
@ -1243,6 +1251,14 @@ class TestHash < Test::Unit::TestCase
assert_equal({1=>8, 2=>4, 3=>4, 5=>7}, h1.merge(h2, h3) {|k, v1, v2| k + v1 + v2 })
end
def test_merge!
h = @cls[a: 1, b: 2, c: 3]
assert_raise(FrozenError) do
h.merge!({a: 10, b: 20}){ |key, v1, v2| key == :b && h.freeze; v2 }
end
assert_equal(@cls[a: 10, b: 2, c: 3], h)
end
def test_assoc
assert_equal([3,4], @cls[1=>2, 3=>4, 5=>6].assoc(3))
assert_nil(@cls[1=>2, 3=>4, 5=>6].assoc(4))