Skip to content

Commit

Permalink
Adds specs for Feature#16828 - Find Pattern
Browse files Browse the repository at this point in the history
Refers to #823

Adds specs for Feature#16828, the find pattern, (`[1, 2, 3] in [*pre, 2,
*post]`) in Ruby 3.0+.
  • Loading branch information
baweaver authored and eregon committed Oct 6, 2021
1 parent ccf0d85 commit 5753fff
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions language/pattern_matching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,63 @@
RUBY
end
end

describe "find pattern" do
it "captures preceding elements to the pattern" do
eval(<<~RUBY).should == [0, 1]
case [0, 1, 2, 3]
in [*pre, 2, 3]
pre
else
false
end
RUBY
end

it "captures following elements to the pattern" do
eval(<<~RUBY).should == [2, 3]
case [0, 1, 2, 3]
in [0, 1, *post]
post
else
false
end
RUBY
end

it "captures both predecing and following elements to the pattern" do
eval(<<~RUBY).should == [[0, 1], [3, 4]]
case [0, 1, 2, 3, 4]
in [*pre, 2, *post]
[pre, post]
else
false
end
RUBY
end

it "can capture the entirety of the pattern" do
eval(<<~RUBY).should == [0, 1, 2, 3, 4]
case [0, 1, 2, 3, 4]
in [*everything]
everything
else
false
end
RUBY
end

it "will match an empty Array-like structure" do
eval(<<~RUBY).should == []
case []
in [*everything]
everything
else
false
end
RUBY
end
end
end

it "extends case expression with case/in construction" do
Expand Down

0 comments on commit 5753fff

Please sign in to comment.