Skip to content

Commit

Permalink
Adds spec for #17260 and #17231
Browse files Browse the repository at this point in the history
Adds specs for one-line pattern matching syntax in Ruby 3.0+, including
both Righthand Assignment (`=>`) and one-line match (`in`).
  • Loading branch information
baweaver committed Oct 6, 2021
1 parent ccf0d85 commit ae26511
Showing 1 changed file with 53 additions and 6 deletions.
59 changes: 53 additions & 6 deletions language/pattern_matching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,49 @@
end

ruby_version_is "3.0" do
it "can be standalone assoc operator that deconstructs value" do
suppress_warning do
eval(<<-RUBY).should == [0, 1]
[0, 1] => [a, b]
[a, b]
RUBY
describe "Rightward assignment (`=>`)" do
it "can be standalone assoc operator that deconstructs value" do
suppress_warning do
eval(<<~RUBY).should == [0, 1]
[0, 1] => [a, b]
[a, b]
RUBY
end
end

it "can work with keywords" do
suppress_warning do
eval(<<~RUBY).should == [0, 1]
{ a: 0, b: 1 } => { a:, b: }
[a, b]
RUBY
end
end
end

describe "One-line pattern matching" do
it "can be used to check if a pattern matches for Array-like entities" do
suppress_warning do
eval(<<~RUBY).should == true
[0, 1] in [a, b]
RUBY

eval(<<~RUBY).should == false
[0, 1] in [a, b, c]
RUBY
end
end

it "can be used to check if a pattern matches for Hash-like entities" do
suppress_warning do
eval(<<~RUBY).should == true
{ a: 0, b: 1 } in { a:, b: }
RUBY

eval(<<~RUBY).should == false
{ a: 0, b: 1 } in { a:, b:, c: }
RUBY
end
end
end
end
Expand Down Expand Up @@ -1136,6 +1173,16 @@ def ===(obj)
end
end

ruby_version_is ""..."2.7" do
it "raises NoMatchingPatternError if no pattern matches for one-line patterns" do
-> {
eval <<~RUBY
0 in 1
RUBY
}.should raise_error(NoMatchingPatternError, /\[0, 1\]/)
end
end

ruby_version_is "3.1" do
it "can omit parentheses in one line pattern matching" do
eval(<<~RUBY).should == [1, 2]
Expand Down

0 comments on commit ae26511

Please sign in to comment.