Skip to content

Commit c8668bf

Browse files
eregonandrykonchin
authored andcommitted
Add specs for value in pattern pattern matching
1 parent 38a9dfa commit c8668bf

File tree

1 file changed

+92
-64
lines changed

1 file changed

+92
-64
lines changed

spec/ruby/language/pattern_matching_spec.rb

Lines changed: 92 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,87 +1336,115 @@ def ===(obj)
13361336
end
13371337
end
13381338

1339-
ruby_version_is "3.1" do
1340-
it "can omit parentheses in one line pattern matching" do
1341-
eval(<<~RUBY).should == [1, 2]
1342-
[1, 2] => a, b
1343-
[a, b]
1344-
RUBY
1339+
describe "Ruby 3.1 improvements" do
1340+
ruby_version_is "3.1" do
1341+
it "can omit parentheses in one line pattern matching" do
1342+
eval(<<~RUBY).should == [1, 2]
1343+
[1, 2] => a, b
1344+
[a, b]
1345+
RUBY
13451346

1346-
eval(<<~RUBY).should == 1
1347-
{a: 1} => a:
1348-
a
1349-
RUBY
1350-
end
1347+
eval(<<~RUBY).should == 1
1348+
{a: 1} => a:
1349+
a
1350+
RUBY
1351+
end
13511352

1352-
it "supports pinning instance variables" do
1353-
eval(<<~RUBY).should == true
1354-
@a = /a/
1355-
case 'abc'
1356-
in ^@a
1357-
true
1353+
it "supports pinning instance variables" do
1354+
eval(<<~RUBY).should == true
1355+
@a = /a/
1356+
case 'abc'
1357+
in ^@a
1358+
true
1359+
end
1360+
RUBY
1361+
end
1362+
1363+
it "supports pinning class variables" do
1364+
result = nil
1365+
Module.new do
1366+
result = module_eval(<<~RUBY)
1367+
@@a = 0..10
1368+
1369+
case 2
1370+
in ^@@a
1371+
true
1372+
end
1373+
RUBY
13581374
end
1359-
RUBY
1360-
end
13611375

1362-
it "supports pinning class variables" do
1363-
result = nil
1364-
Module.new do
1365-
result = module_eval(<<~RUBY)
1366-
@@a = 0..10
1376+
result.should == true
1377+
end
13671378

1368-
case 2
1369-
in ^@@a
1379+
it "supports pinning global variables" do
1380+
eval(<<~RUBY).should == true
1381+
$a = /a/
1382+
case 'abc'
1383+
in ^$a
13701384
true
13711385
end
13721386
RUBY
13731387
end
13741388

1375-
result.should == true
1376-
end
1389+
it "supports pinning expressions" do
1390+
eval(<<~RUBY).should == true
1391+
case 'abc'
1392+
in ^(/a/)
1393+
true
1394+
end
1395+
RUBY
13771396

1378-
it "supports pinning global variables" do
1379-
eval(<<~RUBY).should == true
1380-
$a = /a/
1381-
case 'abc'
1382-
in ^$a
1383-
true
1384-
end
1385-
RUBY
1386-
end
1397+
eval(<<~RUBY).should == true
1398+
case 0
1399+
in ^(0+0)
1400+
true
1401+
end
1402+
RUBY
1403+
end
13871404

1388-
it "supports pinning expressions" do
1389-
eval(<<~RUBY).should == true
1390-
case 'abc'
1391-
in ^(/a/)
1392-
true
1393-
end
1394-
RUBY
1405+
it "supports pinning expressions in array pattern" do
1406+
eval(<<~RUBY).should == true
1407+
case [3]
1408+
in [^(1+2)]
1409+
true
1410+
end
1411+
RUBY
1412+
end
13951413

1396-
eval(<<~RUBY).should == true
1397-
case 0
1398-
in ^(0+0)
1399-
true
1400-
end
1401-
RUBY
1414+
it "supports pinning expressions in hash pattern" do
1415+
eval(<<~RUBY).should == true
1416+
case {name: '2.6', released_at: Time.new(2018, 12, 25)}
1417+
in {released_at: ^(Time.new(2010)..Time.new(2020))}
1418+
true
1419+
end
1420+
RUBY
1421+
end
14021422
end
1423+
end
14031424

1404-
it "supports pinning expressions in array pattern" do
1405-
eval(<<~RUBY).should == true
1406-
case [3]
1407-
in [^(1+2)]
1408-
true
1409-
end
1410-
RUBY
1425+
describe "value in pattern" do
1426+
it "returns true if the pattern matches" do
1427+
eval("1 in 1").should == true
1428+
1429+
eval("1 in Integer").should == true
1430+
1431+
e = nil
1432+
eval("[1, 2] in [1, e]").should == true
1433+
e.should == 2
1434+
1435+
k = nil
1436+
eval("{k: 1} in {k:}").should == true
1437+
k.should == 1
14111438
end
14121439

1413-
it "supports pinning expressions in hash pattern" do
1414-
eval(<<~RUBY).should == true
1415-
case {name: '2.6', released_at: Time.new(2018, 12, 25)}
1416-
in {released_at: ^(Time.new(2010)..Time.new(2020))}
1417-
true
1418-
end
1419-
RUBY
1440+
it "returns false if the pattern does not match" do
1441+
eval("1 in 2").should == false
1442+
1443+
eval("1 in Float").should == false
1444+
1445+
eval("[1, 2] in [2, e]").should == false
1446+
1447+
eval("{k: 1} in {k: 2}").should == false
14201448
end
14211449
end
14221450
end

0 commit comments

Comments
 (0)