Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds specs for Feature#16828 - Find Pattern #856

Merged
merged 2 commits into from
Oct 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
eregon marked this conversation as resolved.
Show resolved Hide resolved
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