|
10 | 10 | end
|
11 | 11 |
|
12 | 12 | 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 |
19 | 56 | end
|
20 | 57 | end
|
21 | 58 | end
|
@@ -1136,6 +1173,16 @@ def ===(obj)
|
1136 | 1173 | end
|
1137 | 1174 | end
|
1138 | 1175 |
|
| 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 | + |
1139 | 1186 | ruby_version_is "3.1" do
|
1140 | 1187 | it "can omit parentheses in one line pattern matching" do
|
1141 | 1188 | eval(<<~RUBY).should == [1, 2]
|
|
0 commit comments