diff options
Diffstat (limited to 'test/ruby/test_pattern_matching.rb')
-rw-r--r-- | test/ruby/test_pattern_matching.rb | 62 |
1 files changed, 55 insertions, 7 deletions
diff --git a/test/ruby/test_pattern_matching.rb b/test/ruby/test_pattern_matching.rb index 7531466f91..cfe3bd1e19 100644 --- a/test/ruby/test_pattern_matching.rb +++ b/test/ruby/test_pattern_matching.rb @@ -9,14 +9,14 @@ class TestPatternMatching < Test::Unit::TestCase end def setup - if defined?(DidYouMean) + if defined?(DidYouMean.formatter=nil) @original_formatter = DidYouMean.formatter DidYouMean.formatter = NullFormatter.new end end def teardown - if defined?(DidYouMean) + if defined?(DidYouMean.formatter=nil) DidYouMean.formatter = @original_formatter end end @@ -109,16 +109,12 @@ class TestPatternMatching < Test::Unit::TestCase end assert_block do - # suppress "warning: Pattern matching is experimental, and the behavior may change in future versions of Ruby!" - experimental, Warning[:experimental] = Warning[:experimental], false eval(%q{ case true in a a end }) - ensure - Warning[:experimental] = experimental end assert_block do @@ -358,6 +354,14 @@ END end assert_block do + a = "abc" + case 'abc' + in /#{a}/o + true + end + end + + assert_block do case 0 in ->(i) { i == 0 } true @@ -464,6 +468,8 @@ END true end end + + assert_valid_syntax("1 in ^(1\n)") end def test_array_pattern @@ -798,6 +804,10 @@ END true end end + + assert_syntax_error(%q{ + 0 => [a, *a] + }, /duplicated variable name/) end def test_find_pattern @@ -866,6 +876,10 @@ END false end end + + assert_syntax_error(%q{ + 0 => [*a, a, b, *b] + }, /duplicated variable name/) end def test_hash_pattern @@ -1155,6 +1169,28 @@ END end end + bug18890 = assert_warning(/(?:.*:[47]: warning: possibly useless use of a literal in void context\n){2}/) do + eval("#{<<~';;;'}") + proc do |i| + case i + in a: + 0 # line 4 + a + in "b": + 0 # line 7 + b + else + false + end + end + ;;; + end + [{a: 42}, {b: 42}].each do |i| + assert_block('newline should be significant after pattern label') do + bug18890.call(i) + end + end + assert_syntax_error(%q{ case _ in a:, a: @@ -1295,7 +1331,7 @@ END end assert_block do - case {} + case C.new({}) in {} C.keys == nil end @@ -1548,6 +1584,18 @@ END assert_equal false, (1 in 2) end + def test_bug18990 + {a: 0} => a: + assert_equal 0, a + {a: 0} => a: + assert_equal 0, a + + {a: 0} in a: + assert_equal 0, a + {a: 0} in a: + assert_equal 0, a + end + ################################################################ def test_single_pattern_error_value_pattern |