Skip to content

Commit

Permalink
Merge pull request #752 from ruby/andrykonchin-ruby-2-7-continue
Browse files Browse the repository at this point in the history
[Ruby 2.7] Pattern matching (continuation)
  • Loading branch information
andrykonchin authored Jan 26, 2020
2 parents a8dc71f + 98a538a commit 351cfcb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
22 changes: 21 additions & 1 deletion core/struct/deconstruct_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
end

it "requires one argument" do
struct = Struct.new(:x)
obj = struct.new(1)

-> {
Struct.new(:x).new(1).deconstruct_keys
obj.deconstruct_keys
}.should raise_error(ArgumentError, /wrong number of arguments \(given 0, expected 1\)/)
end

Expand Down Expand Up @@ -46,5 +49,22 @@

s.deconstruct_keys([:a, :b, :c]).should == {}
end

it "accepts nil argument and return all the attributes" do
struct = Struct.new(:x, :y)
obj = struct.new(1, 2)

obj.deconstruct_keys(nil).should == {x: 1, y: 2}
end

it "raise TypeError if passed anything accept nil or array" do
struct = Struct.new(:x, :y)
s = struct.new(1, 2)

-> { s.deconstruct_keys('x') }.should raise_error(TypeError, /expected Array or nil/)
-> { s.deconstruct_keys(1) }.should raise_error(TypeError, /expected Array or nil/)
-> { s.deconstruct_keys(:x) }.should raise_error(TypeError, /expected Array or nil/)
-> { s.deconstruct_keys({}) }.should raise_error(TypeError, /expected Array or nil/)
end
end
end
65 changes: 64 additions & 1 deletion language/pattern_matching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@
}.should raise_error(NoMatchingPatternError, /\[0, 1\]/)
end

it "does not allow calculation or method calls in a pattern" do
-> {
eval <<~RUBY
case 0
in 1 + 1
true
end
RUBY
}.should raise_error(SyntaxError, /unexpected/)
end

describe "guards" do
it "supports if guard" do
eval(<<~RUBY).should == false
Expand Down Expand Up @@ -273,6 +284,18 @@
RUBY
end

it "create local variables even if a pattern doesn't match" do
eval(<<~RUBY).should == [0, nil, nil]
case 0
in a
in b
in c
end
[a, b, c]
RUBY
end

it "allow using _ name to drop values" do
eval(<<~RUBY).should == 0
case [0, 1]
Expand Down Expand Up @@ -345,6 +368,19 @@
end
RUBY
end

it "requires bound variable to be specified in a pattern before ^ operator when it relies on a bound variable" do
-> {
eval <<~RUBY
case [1, 2]
in [^n, n]
true
else
false
end
RUBY
}.should raise_error(SyntaxError, /n: no such local variable/)
end
end

describe "alternative pattern" do
Expand Down Expand Up @@ -569,6 +605,15 @@ def obj.deconstruct; [1] end
end
RUBY
end

it "matches anything with *" do
eval(<<~RUBY).should == true
case [0, 1]
in *;
true
end
RUBY
end
end

describe "Hash pattern" do
Expand Down Expand Up @@ -663,7 +708,16 @@ def obj.deconstruct; [1] end
RUBY
end

it 'supports "str": key literal' do
it "can mix key (a:) and key-value (a: b) declarations" do
eval(<<~RUBY).should == [0, 1]
case {a: 0, b: 1}
in Hash(a:, b: x)
[a, x]
end
RUBY
end

it "supports 'string': key literal" do
eval(<<~RUBY).should == true
case {a: 0}
in {"a": 0}
Expand Down Expand Up @@ -898,6 +952,15 @@ def obj.deconstruct_keys(*args)
end
RUBY
end

it "matches anything with **" do
eval(<<~RUBY).should == true
case {a: 1}
in **;
true
end
RUBY
end
end
end
end
7 changes: 7 additions & 0 deletions language/range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@
eval("(1...)").should == Range.new(1, nil, true)
end
end

ruby_version_is "2.7" do
it "creates beginless ranges" do
eval("(..1)").should == Range.new(nil, 1)
eval("(...1)").should == Range.new(nil, 1, true)
end
end
end

0 comments on commit 351cfcb

Please sign in to comment.