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

[Ruby 2.7] Pattern matching (continuation) #752

Merged
merged 3 commits into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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