diff options
Diffstat (limited to 'test/ruby/test_pattern_matching.rb')
| -rw-r--r-- | test/ruby/test_pattern_matching.rb | 143 |
1 files changed, 114 insertions, 29 deletions
diff --git a/test/ruby/test_pattern_matching.rb b/test/ruby/test_pattern_matching.rb index 277a0dcc51..96aa2a7fd6 100644 --- a/test/ruby/test_pattern_matching.rb +++ b/test/ruby/test_pattern_matching.rb @@ -1,9 +1,26 @@ # frozen_string_literal: true require 'test/unit' -experimental, Warning[:experimental] = Warning[:experimental], false # suppress "warning: Pattern matching is experimental, and the behavior may change in future versions of Ruby!" -eval "\n#{<<~'END_of_GUARD'}", binding, __FILE__, __LINE__ class TestPatternMatching < Test::Unit::TestCase + class NullFormatter + def message_for(corrections) + "" + end + end + + def setup + if defined?(DidYouMean.formatter=nil) + @original_formatter = DidYouMean.formatter + DidYouMean.formatter = NullFormatter.new + end + end + + def teardown + if defined?(DidYouMean.formatter=nil) + DidYouMean.formatter = @original_formatter + end + end + class C class << self attr_accessor :keys @@ -92,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 @@ -184,11 +197,49 @@ class TestPatternMatching < Test::Unit::TestCase end end - assert_syntax_error(%q{ + assert_valid_syntax(%{ + case 0 + in [ :a | :b, x] + true + end + }) + + assert_in_out_err(['-c'], %q{ case 0 in a | 0 end - }, /illegal variable in alternative pattern/) + }, [], /alternative pattern/, + success: false) + + assert_in_out_err(['-c'], %q{ + case 0 + in 0 | a + end + }, [], /alternative pattern/, + success: false) + end + + def test_alternative_pattern_nested + assert_in_out_err(['-c'], %q{ + case 0 + in [a] | 1 + end + }, [], /alternative pattern/, + success: false) + + assert_in_out_err(['-c'], %q{ + case 0 + in { a: b } | 1 + end + }, [], /alternative pattern/, + success: false) + + assert_in_out_err(['-c'], %q{ + case 0 + in [{ a: [{ b: [{ c: }] }] }] | 1 + end + }, [], /alternative pattern/, + success: false) end def test_var_pattern @@ -341,6 +392,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 @@ -447,6 +506,8 @@ END true end end + + assert_valid_syntax("1 in ^(1\n)") end def test_array_pattern @@ -781,6 +842,10 @@ END true end end + + assert_syntax_error(%q{ + 0 => [a, *a] + }, /duplicated variable name/) end def test_find_pattern @@ -849,6 +914,10 @@ END false end end + + assert_syntax_error(%q{ + 0 => [*a, a, b, *b] + }, /duplicated variable name/) end def test_hash_pattern @@ -1138,6 +1207,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: @@ -1278,7 +1369,7 @@ END end assert_block do - case {} + case C.new({}) in {} C.keys == nil end @@ -1531,20 +1622,16 @@ END assert_equal false, (1 in 2) end - def assert_experimental_warning(code) - w = Warning[:experimental] - - Warning[:experimental] = false - assert_warn('') {eval(code)} - - Warning[:experimental] = true - assert_warn(/is experimental/) {eval(code)} - ensure - Warning[:experimental] = w - end + def test_bug18990 + {a: 0} => a: + assert_equal 0, a + {a: 0} => a: + assert_equal 0, a - def test_experimental_warning - assert_experimental_warning("case [0]; in [*, 0, *]; end") + {a: 0} in a: + assert_equal 0, a + {a: 0} in a: + assert_equal 0, a end ################################################################ @@ -1615,7 +1702,7 @@ END raise a # suppress "unused variable: a" warning end - assert_raise_with_message(NoMatchingPatternKeyError, "{:a=>0}: key not found: :aa") do + assert_raise_with_message(NoMatchingPatternKeyError, "{a: 0}: key not found: :aa") do {a: 0} => {aa:} raise aa # suppress "unused variable: aa" warning rescue NoMatchingPatternKeyError => e @@ -1624,7 +1711,7 @@ END raise e end - assert_raise_with_message(NoMatchingPatternKeyError, "{:a=>{:b=>0}}: key not found: :bb") do + assert_raise_with_message(NoMatchingPatternKeyError, "{a: {b: 0}}: key not found: :bb") do {a: {b: 0}} => {a: {bb:}} raise bb # suppress "unused variable: bb" warning rescue NoMatchingPatternKeyError => e @@ -1633,15 +1720,15 @@ END raise e end - assert_raise_with_message(NoMatchingPatternError, "{:a=>0}: 1 === 0 does not return true") do + assert_raise_with_message(NoMatchingPatternError, "{a: 0}: 1 === 0 does not return true") do {a: 0} => {a: 1} end - assert_raise_with_message(NoMatchingPatternError, "{:a=>0}: {:a=>0} is not empty") do + assert_raise_with_message(NoMatchingPatternError, "{a: 0}: {a: 0} is not empty") do {a: 0} => {} end - assert_raise_with_message(NoMatchingPatternError, "[{:a=>0}]: rest of {:a=>0} is not empty") do + assert_raise_with_message(NoMatchingPatternError, "[{a: 0}]: rest of {a: 0} is not empty") do [{a: 0}] => [{**nil}] end end @@ -1674,5 +1761,3 @@ END end end end -END_of_GUARD -Warning[:experimental] = experimental |
