summaryrefslogtreecommitdiff
path: root/spec/ruby/language/pattern_matching_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/language/pattern_matching_spec.rb')
-rw-r--r--spec/ruby/language/pattern_matching_spec.rb374
1 files changed, 187 insertions, 187 deletions
diff --git a/spec/ruby/language/pattern_matching_spec.rb b/spec/ruby/language/pattern_matching_spec.rb
index e34ff2db22..1596b2b8d2 100644
--- a/spec/ruby/language/pattern_matching_spec.rb
+++ b/spec/ruby/language/pattern_matching_spec.rb
@@ -23,10 +23,10 @@ ruby_version_is "2.7" do
it "extends case expression with case/in construction" do
eval(<<~RUBY).should == :bar
case [0, 1]
- in [0]
- :foo
- in [0, 1]
- :bar
+ in [0]
+ :foo
+ in [0, 1]
+ :bar
end
RUBY
end
@@ -34,8 +34,8 @@ ruby_version_is "2.7" do
it "allows using then operator" do
eval(<<~RUBY).should == :bar
case [0, 1]
- in [0] then :foo
- in [0, 1] then :bar
+ in [0] then :foo
+ in [0, 1] then :bar
end
RUBY
end
@@ -59,8 +59,8 @@ ruby_version_is "2.7" do
it "binds variables" do
eval(<<~RUBY).should == 1
case [0, 1]
- in [0, a]
- a
+ in [0, a]
+ a
end
RUBY
end
@@ -69,8 +69,8 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case []
- when 1 == 1
- in []
+ when 1 == 1
+ in []
end
RUBY
}.should raise_error(SyntaxError, /syntax error, unexpected `in'/)
@@ -78,8 +78,8 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case []
- in []
- when 1 == 1
+ in []
+ when 1 == 1
end
RUBY
}.should raise_error(SyntaxError, /syntax error, unexpected `when'/)
@@ -88,12 +88,12 @@ ruby_version_is "2.7" do
it "checks patterns until the first matching" do
eval(<<~RUBY).should == :bar
case [0, 1]
- in [0]
- :foo
- in [0, 1]
- :bar
- in [0, 1]
- :baz
+ in [0]
+ :foo
+ in [0, 1]
+ :bar
+ in [0, 1]
+ :baz
end
RUBY
end
@@ -101,10 +101,10 @@ ruby_version_is "2.7" do
it "executes else clause if no pattern matches" do
eval(<<~RUBY).should == false
case [0, 1]
- in [0]
- true
- else
- false
+ in [0]
+ true
+ else
+ false
end
RUBY
end
@@ -113,7 +113,7 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case [0, 1]
- in [0]
+ in [0]
end
RUBY
}.should raise_error(NoMatchingPatternError, /\[0, 1\]/)
@@ -123,8 +123,8 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case 0
- in 1 + 1
- true
+ in 1 + 1
+ true
end
RUBY
}.should raise_error(SyntaxError, /unexpected/)
@@ -134,8 +134,8 @@ ruby_version_is "2.7" do
it "supports if guard" do
eval(<<~RUBY).should == false
case 0
- in 0 if false
- true
+ in 0 if false
+ true
else
false
end
@@ -143,8 +143,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == true
case 0
- in 0 if true
- true
+ in 0 if true
+ true
else
false
end
@@ -154,8 +154,8 @@ ruby_version_is "2.7" do
it "supports unless guard" do
eval(<<~RUBY).should == false
case 0
- in 0 unless true
- true
+ in 0 unless true
+ true
else
false
end
@@ -163,8 +163,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == true
case 0
- in 0 unless false
- true
+ in 0 unless false
+ true
else
false
end
@@ -174,8 +174,8 @@ ruby_version_is "2.7" do
it "makes bound variables visible in guard" do
eval(<<~RUBY).should == true
case [0, 1]
- in [a, 1] if a >= 0
- true
+ in [a, 1] if a >= 0
+ true
end
RUBY
end
@@ -183,7 +183,7 @@ ruby_version_is "2.7" do
it "does not evaluate guard if pattern does not match" do
eval <<~RUBY
case 0
- in 1 if (ScratchPad << :foo) || true
+ in 1 if (ScratchPad << :foo) || true
else
end
RUBY
@@ -194,10 +194,10 @@ ruby_version_is "2.7" do
it "takes guards into account when there are several matching patterns" do
eval(<<~RUBY).should == :bar
case 0
- in 0 if false
- :foo
- in 0 if true
- :bar
+ in 0 if false
+ :foo
+ in 0 if true
+ :bar
end
RUBY
end
@@ -205,8 +205,8 @@ ruby_version_is "2.7" do
it "executes else clause if no guarded pattern matches" do
eval(<<~RUBY).should == false
case 0
- in 0 if false
- true
+ in 0 if false
+ true
else
false
end
@@ -217,7 +217,7 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case [0, 1]
- in [0, 1] if false
+ in [0, 1] if false
end
RUBY
}.should raise_error(NoMatchingPatternError, /\[0, 1\]/)
@@ -228,36 +228,36 @@ ruby_version_is "2.7" do
it "matches an object such that pattern === object" do
eval(<<~RUBY).should == true
case 0
- in 0
- true
+ in 0
+ true
end
RUBY
eval(<<~RUBY).should == true
case 0
- in (-1..1)
- true
+ in (-1..1)
+ true
end
RUBY
eval(<<~RUBY).should == true
case 0
- in Integer
- true
+ in Integer
+ true
end
RUBY
eval(<<~RUBY).should == true
case "0"
- in /0/
- true
+ in /0/
+ true
end
RUBY
eval(<<~RUBY).should == true
case "0"
- in ->(s) { s == "0" }
- true
+ in ->(s) { s == "0" }
+ true
end
RUBY
end
@@ -267,8 +267,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == true
case "x"
- in "#{x + ""}"
- true
+ in "#{x + ""}"
+ true
end
RUBY
end
@@ -278,8 +278,8 @@ ruby_version_is "2.7" do
it "matches a value and binds variable name to this value" do
eval(<<~RUBY).should == 0
case 0
- in a
- a
+ in a
+ a
end
RUBY
end
@@ -287,7 +287,7 @@ ruby_version_is "2.7" do
it "makes bounded variable visible outside a case statement scope" do
eval(<<~RUBY).should == 0
case 0
- in a
+ in a
end
a
@@ -297,9 +297,9 @@ ruby_version_is "2.7" do
it "create local variables even if a pattern doesn't match" do
eval(<<~RUBY).should == [0, nil, nil]
case 0
- in a
- in b
- in c
+ in a
+ in b
+ in c
end
[a, b, c]
@@ -309,8 +309,8 @@ ruby_version_is "2.7" do
it "allow using _ name to drop values" do
eval(<<~RUBY).should == 0
case [0, 1]
- in [a, _]
- a
+ in [a, _]
+ a
end
RUBY
end
@@ -318,8 +318,8 @@ ruby_version_is "2.7" do
it "supports using _ in a pattern several times" do
eval(<<~RUBY).should == true
case [0, 1, 2]
- in [0, _, _]
- true
+ in [0, _, _]
+ true
end
RUBY
end
@@ -327,15 +327,15 @@ ruby_version_is "2.7" do
it "supports using any name with _ at the beginning in a pattern several times" do
eval(<<~RUBY).should == true
case [0, 1, 2]
- in [0, _x, _x]
- true
+ in [0, _x, _x]
+ true
end
RUBY
eval(<<~RUBY).should == true
case {a: 0, b: 1, c: 2}
- in {a: 0, b: _x, c: _x}
- true
+ in {a: 0, b: _x, c: _x}
+ true
end
RUBY
end
@@ -344,7 +344,7 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case [0]
- in [a, a]
+ in [a, a]
end
RUBY
}.should raise_error(SyntaxError, /duplicated variable name/)
@@ -355,8 +355,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == true
case 0
- in ^a
- true
+ in ^a
+ true
end
RUBY
end
@@ -364,15 +364,15 @@ ruby_version_is "2.7" do
it "allows applying ^ operator to bound variables" do
eval(<<~RUBY).should == 1
case [1, 1]
- in [n, ^n]
- n
+ in [n, ^n]
+ n
end
RUBY
eval(<<~RUBY).should == false
case [1, 2]
- in [n, ^n]
- true
+ in [n, ^n]
+ true
else
false
end
@@ -383,8 +383,8 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case [1, 2]
- in [^n, n]
- true
+ in [^n, n]
+ true
else
false
end
@@ -397,8 +397,8 @@ ruby_version_is "2.7" do
it "matches if any of patterns matches" do
eval(<<~RUBY).should == true
case 0
- in 0 | 1 | 2
- true
+ in 0 | 1 | 2
+ true
end
RUBY
end
@@ -407,7 +407,7 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case [0, 1]
- in [0, 0] | [0, a]
+ in [0, 0] | [0, a]
end
RUBY
}.should raise_error(SyntaxError, /illegal variable in alternative pattern/)
@@ -416,10 +416,10 @@ ruby_version_is "2.7" do
it "support underscore prefixed variables in alternation" do
eval(<<~RUBY).should == true
case [0, 1]
- in [1, _]
- false
- in [0, 0] | [0, _a]
- true
+ in [1, _]
+ false
+ in [0, 0] | [0, _a]
+ true
end
RUBY
end
@@ -429,8 +429,8 @@ ruby_version_is "2.7" do
it "binds a variable to a value if pattern matches" do
eval(<<~RUBY).should == 0
case 0
- in Integer => n
- n
+ in Integer => n
+ n
end
RUBY
end
@@ -438,8 +438,8 @@ ruby_version_is "2.7" do
it "can be used as a nested pattern" do
eval(<<~RUBY).should == [2, 3]
case [1, [2, 3]]
- in [1, Array => ary]
- ary
+ in [1, Array => ary]
+ ary
end
RUBY
end
@@ -449,8 +449,8 @@ ruby_version_is "2.7" do
it "supports form Constant(pat, pat, ...)" do
eval(<<~RUBY).should == true
case [0, 1, 2]
- in Array(0, 1, 2)
- true
+ in Array(0, 1, 2)
+ true
end
RUBY
end
@@ -458,8 +458,8 @@ ruby_version_is "2.7" do
it "supports form Constant[pat, pat, ...]" do
eval(<<~RUBY).should == true
case [0, 1, 2]
- in Array[0, 1, 2]
- true
+ in Array[0, 1, 2]
+ true
end
RUBY
end
@@ -467,8 +467,8 @@ ruby_version_is "2.7" do
it "supports form [pat, pat, ...]" do
eval(<<~RUBY).should == true
case [0, 1, 2]
- in [0, 1, 2]
- true
+ in [0, 1, 2]
+ true
end
RUBY
end
@@ -476,22 +476,22 @@ ruby_version_is "2.7" do
it "supports form pat, pat, ..." do
eval(<<~RUBY).should == true
case [0, 1, 2]
- in 0, 1, 2
- true
+ in 0, 1, 2
+ true
end
RUBY
eval(<<~RUBY).should == 1
case [0, 1, 2]
- in 0, a, 2
- a
+ in 0, a, 2
+ a
end
RUBY
eval(<<~RUBY).should == [1, 2]
case [0, 1, 2]
- in 0, *rest
- rest
+ in 0, *rest
+ rest
end
RUBY
end
@@ -502,8 +502,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == true
case obj
- in [Integer, Integer]
- true
+ in [Integer, Integer]
+ true
end
RUBY
end
@@ -511,8 +511,8 @@ ruby_version_is "2.7" do
it "does not match object if Constant === object returns false" do
eval(<<~RUBY).should == false
case [0, 1, 2]
- in String[0, 1, 2]
- true
+ in String[0, 1, 2]
+ true
else
false
end
@@ -524,8 +524,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == false
case obj
- in Object[]
- true
+ in Object[]
+ true
else
false
end
@@ -539,7 +539,7 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case obj
- in Object[]
+ in Object[]
else
end
RUBY
@@ -552,8 +552,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == false
case obj
- in Object[0]
- true
+ in Object[0]
+ true
else
false
end
@@ -563,8 +563,8 @@ ruby_version_is "2.7" do
it "binds variables" do
eval(<<~RUBY).should == [0, 1, 2]
case [0, 1, 2]
- in [a, b, c]
- [a, b, c]
+ in [a, b, c]
+ [a, b, c]
end
RUBY
end
@@ -572,8 +572,8 @@ ruby_version_is "2.7" do
it "supports splat operator *rest" do
eval(<<~RUBY).should == [1, 2]
case [0, 1, 2]
- in [0, *rest]
- rest
+ in [0, *rest]
+ rest
end
RUBY
end
@@ -581,8 +581,8 @@ ruby_version_is "2.7" do
it "does not match partially by default" do
eval(<<~RUBY).should == false
case [0, 1, 2, 3]
- in [1, 2]
- true
+ in [1, 2]
+ true
else
false
end
@@ -592,15 +592,15 @@ ruby_version_is "2.7" do
it "does match partially from the array beginning if list + , syntax used" do
eval(<<~RUBY).should == true
case [0, 1, 2, 3]
- in [0, 1,]
- true
+ in [0, 1,]
+ true
end
RUBY
eval(<<~RUBY).should == true
case [0, 1, 2, 3]
- in 0, 1,;
- true
+ in 0, 1,;
+ true
end
RUBY
end
@@ -608,8 +608,8 @@ ruby_version_is "2.7" do
it "matches [] with []" do
eval(<<~RUBY).should == true
case []
- in []
- true
+ in []
+ true
end
RUBY
end
@@ -617,8 +617,8 @@ ruby_version_is "2.7" do
it "matches anything with *" do
eval(<<~RUBY).should == true
case [0, 1]
- in *;
- true
+ in *;
+ true
end
RUBY
end
@@ -628,8 +628,8 @@ ruby_version_is "2.7" do
it "supports form Constant(id: pat, id: pat, ...)" do
eval(<<~RUBY).should == true
case {a: 0, b: 1}
- in Hash(a: 0, b: 1)
- true
+ in Hash(a: 0, b: 1)
+ true
end
RUBY
end
@@ -637,8 +637,8 @@ ruby_version_is "2.7" do
it "supports form Constant[id: pat, id: pat, ...]" do
eval(<<~RUBY).should == true
case {a: 0, b: 1}
- in Hash[a: 0, b: 1]
- true
+ in Hash[a: 0, b: 1]
+ true
end
RUBY
end
@@ -646,8 +646,8 @@ ruby_version_is "2.7" do
it "supports form {id: pat, id: pat, ...}" do
eval(<<~RUBY).should == true
case {a: 0, b: 1}
- in {a: 0, b: 1}
- true
+ in {a: 0, b: 1}
+ true
end
RUBY
end
@@ -655,22 +655,22 @@ ruby_version_is "2.7" do
it "supports form id: pat, id: pat, ..." do
eval(<<~RUBY).should == true
case {a: 0, b: 1}
- in a: 0, b: 1
- true
+ in a: 0, b: 1
+ true
end
RUBY
eval(<<~RUBY).should == [0, 1]
case {a: 0, b: 1}
- in a: a, b: b
- [a, b]
+ in a: a, b: b
+ [a, b]
end
RUBY
eval(<<~RUBY).should == { b: 1, c: 2 }
case {a: 0, b: 1, c: 2}
- in a: 0, **rest
- rest
+ in a: 0, **rest
+ rest
end
RUBY
end
@@ -678,40 +678,40 @@ ruby_version_is "2.7" do
it "supports a: which means a: a" do
eval(<<~RUBY).should == [0, 1]
case {a: 0, b: 1}
- in Hash(a:, b:)
- [a, b]
+ in Hash(a:, b:)
+ [a, b]
end
RUBY
a = b = nil
eval(<<~RUBY).should == [0, 1]
case {a: 0, b: 1}
- in Hash[a:, b:]
- [a, b]
+ in Hash[a:, b:]
+ [a, b]
end
RUBY
a = b = nil
eval(<<~RUBY).should == [0, 1]
case {a: 0, b: 1}
- in {a:, b:}
- [a, b]
+ in {a:, b:}
+ [a, b]
end
RUBY
a = nil
eval(<<~RUBY).should == [0, {b: 1, c: 2}]
case {a: 0, b: 1, c: 2}
- in {a:, **rest}
- [a, rest]
+ in {a:, **rest}
+ [a, rest]
end
RUBY
a = b = nil
eval(<<~RUBY).should == [0, 1]
case {a: 0, b: 1}
- in a:, b:
- [a, b]
+ in a:, b:
+ [a, b]
end
RUBY
end
@@ -719,8 +719,8 @@ ruby_version_is "2.7" do
it "can mix key (a:) and key-value (a: b) declarations" do
eval(<<~RUBY).should == [0, 1]
case {a: 0, b: 1}
- in Hash(a:, b: x)
- [a, x]
+ in Hash(a:, b: x)
+ [a, x]
end
RUBY
end
@@ -728,8 +728,8 @@ ruby_version_is "2.7" do
it "supports 'string': key literal" do
eval(<<~RUBY).should == true
case {a: 0}
- in {"a": 0}
- true
+ in {"a": 0}
+ true
end
RUBY
end
@@ -738,7 +738,7 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case {a: 1}
- in {"a" => 1}
+ in {"a" => 1}
end
RUBY
}.should raise_error(SyntaxError, /unexpected/)
@@ -750,7 +750,7 @@ ruby_version_is "2.7" do
-> {
eval <<~'RUBY'
case {a: 1}
- in {"#{x}": 1}
+ in {"#{x}": 1}
end
RUBY
}.should raise_error(SyntaxError, /symbol literal with interpolation is not allowed/)
@@ -760,7 +760,7 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case {a: 1}
- in {a: 1, b: 2, a: 3}
+ in {a: 1, b: 2, a: 3}
end
RUBY
}.should raise_error(SyntaxError, /duplicated key name/)
@@ -772,8 +772,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == true
case obj
- in {a: 1}
- true
+ in {a: 1}
+ true
end
RUBY
end
@@ -781,8 +781,8 @@ ruby_version_is "2.7" do
it "does not match object if Constant === object returns false" do
eval(<<~RUBY).should == false
case {a: 1}
- in String[a: 1]
- true
+ in String[a: 1]
+ true
else
false
end
@@ -794,8 +794,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == false
case obj
- in Object[a: 1]
- true
+ in Object[a: 1]
+ true
else
false
end
@@ -809,7 +809,7 @@ ruby_version_is "2.7" do
-> {
eval <<~RUBY
case obj
- in Object[a: 1]
+ in Object[a: 1]
end
RUBY
}.should raise_error(TypeError, /deconstruct_keys must return Hash/)
@@ -821,8 +821,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == false
case obj
- in Object[a: 1]
- true
+ in Object[a: 1]
+ true
else
false
end
@@ -835,8 +835,8 @@ ruby_version_is "2.7" do
eval(<<~RUBY).should == false
case obj
- in Object[a: 2]
- true
+ in Object[a: 2]
+ true
else
false
end
@@ -853,7 +853,7 @@ ruby_version_is "2.7" do
eval <<~RUBY
case obj
- in Object[a: 1, b: 2, c: 3]
+ in Object[a: 1, b: 2, c: 3]
end
RUBY
@@ -870,7 +870,7 @@ ruby_version_is "2.7" do
eval <<~RUBY
case obj
- in Object[a: 1, b: 2, **]
+ in Object[a: 1, b: 2, **]
end
RUBY
@@ -887,7 +887,7 @@ ruby_version_is "2.7" do
eval <<~RUBY
case obj
- in Object[a: 1, **rest]
+ in Object[a: 1, **rest]
end
RUBY
@@ -897,8 +897,8 @@ ruby_version_is "2.7" do
it "binds variables" do
eval(<<~RUBY).should == [0, 1, 2]
case {a: 0, b: 1, c: 2}
- in {a: x, b: y, c: z}
- [x, y, z]
+ in {a: x, b: y, c: z}
+ [x, y, z]
end
RUBY
end
@@ -906,8 +906,8 @@ ruby_version_is "2.7" do
it "supports double splat operator **rest" do
eval(<<~RUBY).should == {b: 1, c: 2}
case {a: 0, b: 1, c: 2}
- in {a: 0, **rest}
- rest
+ in {a: 0, **rest}
+ rest
end
RUBY
end
@@ -915,15 +915,15 @@ ruby_version_is "2.7" do
it "treats **nil like there should not be any other keys in a matched Hash" do
eval(<<~RUBY).should == true
case {a: 1, b: 2}
- in {a: 1, b: 2, **nil}
- true
+ in {a: 1, b: 2, **nil}
+ true
end
RUBY
eval(<<~RUBY).should == false
case {a: 1, b: 2}
- in {a: 1, **nil}
- true
+ in {a: 1, **nil}
+ true
else
false
end
@@ -933,8 +933,8 @@ ruby_version_is "2.7" do
it "can match partially" do
eval(<<~RUBY).should == true
case {a: 1, b: 2}
- in {a: 1}
- true
+ in {a: 1}
+ true
end
RUBY
end
@@ -942,8 +942,8 @@ ruby_version_is "2.7" do
it "matches {} with {}" do
eval(<<~RUBY).should == true
case {}
- in {}
- true
+ in {}
+ true
end
RUBY
end
@@ -951,8 +951,8 @@ ruby_version_is "2.7" do
it "matches anything with **" do
eval(<<~RUBY).should == true
case {a: 1}
- in **;
- true
+ in **;
+ true
end
RUBY
end
@@ -974,8 +974,8 @@ ruby_version_is "2.7" do
result = eval(<<~RUBY)
case []
- in [0]
- true
+ in [0]
+ true
end
RUBY
end
@@ -998,8 +998,8 @@ ruby_version_is "2.7" do
result = eval(<<~RUBY)
case {}
- in a: 0
- true
+ in a: 0
+ true
end
RUBY
end
@@ -1022,8 +1022,8 @@ ruby_version_is "2.7" do
result = eval(<<~RUBY)
case {}
- in Array
- true
+ in Array
+ true
end
RUBY
end