Skip to content

Commit ae26511

Browse files
committed
Adds spec for #17260 and #17231
Adds specs for one-line pattern matching syntax in Ruby 3.0+, including both Righthand Assignment (`=>`) and one-line match (`in`).
1 parent ccf0d85 commit ae26511

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

Diff for: language/pattern_matching_spec.rb

+53-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,49 @@
1010
end
1111

1212
ruby_version_is "3.0" do
13-
it "can be standalone assoc operator that deconstructs value" do
14-
suppress_warning do
15-
eval(<<-RUBY).should == [0, 1]
16-
[0, 1] => [a, b]
17-
[a, b]
18-
RUBY
13+
describe "Rightward assignment (`=>`)" do
14+
it "can be standalone assoc operator that deconstructs value" do
15+
suppress_warning do
16+
eval(<<~RUBY).should == [0, 1]
17+
[0, 1] => [a, b]
18+
[a, b]
19+
RUBY
20+
end
21+
end
22+
23+
it "can work with keywords" do
24+
suppress_warning do
25+
eval(<<~RUBY).should == [0, 1]
26+
{ a: 0, b: 1 } => { a:, b: }
27+
[a, b]
28+
RUBY
29+
end
30+
end
31+
end
32+
33+
describe "One-line pattern matching" do
34+
it "can be used to check if a pattern matches for Array-like entities" do
35+
suppress_warning do
36+
eval(<<~RUBY).should == true
37+
[0, 1] in [a, b]
38+
RUBY
39+
40+
eval(<<~RUBY).should == false
41+
[0, 1] in [a, b, c]
42+
RUBY
43+
end
44+
end
45+
46+
it "can be used to check if a pattern matches for Hash-like entities" do
47+
suppress_warning do
48+
eval(<<~RUBY).should == true
49+
{ a: 0, b: 1 } in { a:, b: }
50+
RUBY
51+
52+
eval(<<~RUBY).should == false
53+
{ a: 0, b: 1 } in { a:, b:, c: }
54+
RUBY
55+
end
1956
end
2057
end
2158
end
@@ -1136,6 +1173,16 @@ def ===(obj)
11361173
end
11371174
end
11381175

1176+
ruby_version_is ""..."2.7" do
1177+
it "raises NoMatchingPatternError if no pattern matches for one-line patterns" do
1178+
-> {
1179+
eval <<~RUBY
1180+
0 in 1
1181+
RUBY
1182+
}.should raise_error(NoMatchingPatternError, /\[0, 1\]/)
1183+
end
1184+
end
1185+
11391186
ruby_version_is "3.1" do
11401187
it "can omit parentheses in one line pattern matching" do
11411188
eval(<<~RUBY).should == [1, 2]

0 commit comments

Comments
 (0)