diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_pattern_matching.rb | 24 | ||||
-rw-r--r-- | test/ruby/test_struct.rb | 12 |
2 files changed, 36 insertions, 0 deletions
diff --git a/test/ruby/test_pattern_matching.rb b/test/ruby/test_pattern_matching.rb index 0338f90a91..9af091a5e5 100644 --- a/test/ruby/test_pattern_matching.rb +++ b/test/ruby/test_pattern_matching.rb @@ -1230,6 +1230,30 @@ END true end end + + s = Struct.new(:a, :b, keyword_init: true) + assert_block do + case s[a: 0, b: 1] + in **r + r == {a: 0, b: 1} + end + end + assert_block do + s = Struct.new(:a, :b, keyword_init: true) + case s[a: 0, b: 1] + in a:, b: + a == 0 && b == 1 + end + end + assert_block do + s = Struct.new(:a, :b, keyword_init: true) + case s[a: 0, b: 1] + in a:, c: + flunk + in b: + b == 1 + end + end end ################################################################ diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index 22a6ce8241..06a8a1bb92 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -431,6 +431,18 @@ module TestStruct } end + def test_deconstruct_keys + klass = @Struct.new(:a, :b) + o = klass.new(1, 2) + assert_equal({a: 1, b: 2}, o.deconstruct_keys(nil)) + assert_equal({a: 1, b: 2}, o.deconstruct_keys([:b, :a])) + assert_equal({a: 1}, o.deconstruct_keys([:a])) + assert_equal({}, o.deconstruct_keys([:a, :c])) + assert_raise(TypeError) { + o.deconstruct_keys(0) + } + end + class TopStruct < Test::Unit::TestCase include TestStruct |