Skip to content

Commit 351cfcb

Browse files
authored
Merge pull request #752 from ruby/andrykonchin-ruby-2-7-continue
[Ruby 2.7] Pattern matching (continuation)
2 parents a8dc71f + 98a538a commit 351cfcb

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

core/struct/deconstruct_keys_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
end
1111

1212
it "requires one argument" do
13+
struct = Struct.new(:x)
14+
obj = struct.new(1)
15+
1316
-> {
14-
Struct.new(:x).new(1).deconstruct_keys
17+
obj.deconstruct_keys
1518
}.should raise_error(ArgumentError, /wrong number of arguments \(given 0, expected 1\)/)
1619
end
1720

@@ -46,5 +49,22 @@
4649

4750
s.deconstruct_keys([:a, :b, :c]).should == {}
4851
end
52+
53+
it "accepts nil argument and return all the attributes" do
54+
struct = Struct.new(:x, :y)
55+
obj = struct.new(1, 2)
56+
57+
obj.deconstruct_keys(nil).should == {x: 1, y: 2}
58+
end
59+
60+
it "raise TypeError if passed anything accept nil or array" do
61+
struct = Struct.new(:x, :y)
62+
s = struct.new(1, 2)
63+
64+
-> { s.deconstruct_keys('x') }.should raise_error(TypeError, /expected Array or nil/)
65+
-> { s.deconstruct_keys(1) }.should raise_error(TypeError, /expected Array or nil/)
66+
-> { s.deconstruct_keys(:x) }.should raise_error(TypeError, /expected Array or nil/)
67+
-> { s.deconstruct_keys({}) }.should raise_error(TypeError, /expected Array or nil/)
68+
end
4969
end
5070
end

language/pattern_matching_spec.rb

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@
109109
}.should raise_error(NoMatchingPatternError, /\[0, 1\]/)
110110
end
111111

112+
it "does not allow calculation or method calls in a pattern" do
113+
-> {
114+
eval <<~RUBY
115+
case 0
116+
in 1 + 1
117+
true
118+
end
119+
RUBY
120+
}.should raise_error(SyntaxError, /unexpected/)
121+
end
122+
112123
describe "guards" do
113124
it "supports if guard" do
114125
eval(<<~RUBY).should == false
@@ -273,6 +284,18 @@
273284
RUBY
274285
end
275286

287+
it "create local variables even if a pattern doesn't match" do
288+
eval(<<~RUBY).should == [0, nil, nil]
289+
case 0
290+
in a
291+
in b
292+
in c
293+
end
294+
295+
[a, b, c]
296+
RUBY
297+
end
298+
276299
it "allow using _ name to drop values" do
277300
eval(<<~RUBY).should == 0
278301
case [0, 1]
@@ -345,6 +368,19 @@
345368
end
346369
RUBY
347370
end
371+
372+
it "requires bound variable to be specified in a pattern before ^ operator when it relies on a bound variable" do
373+
-> {
374+
eval <<~RUBY
375+
case [1, 2]
376+
in [^n, n]
377+
true
378+
else
379+
false
380+
end
381+
RUBY
382+
}.should raise_error(SyntaxError, /n: no such local variable/)
383+
end
348384
end
349385

350386
describe "alternative pattern" do
@@ -569,6 +605,15 @@ def obj.deconstruct; [1] end
569605
end
570606
RUBY
571607
end
608+
609+
it "matches anything with *" do
610+
eval(<<~RUBY).should == true
611+
case [0, 1]
612+
in *;
613+
true
614+
end
615+
RUBY
616+
end
572617
end
573618

574619
describe "Hash pattern" do
@@ -663,7 +708,16 @@ def obj.deconstruct; [1] end
663708
RUBY
664709
end
665710

666-
it 'supports "str": key literal' do
711+
it "can mix key (a:) and key-value (a: b) declarations" do
712+
eval(<<~RUBY).should == [0, 1]
713+
case {a: 0, b: 1}
714+
in Hash(a:, b: x)
715+
[a, x]
716+
end
717+
RUBY
718+
end
719+
720+
it "supports 'string': key literal" do
667721
eval(<<~RUBY).should == true
668722
case {a: 0}
669723
in {"a": 0}
@@ -898,6 +952,15 @@ def obj.deconstruct_keys(*args)
898952
end
899953
RUBY
900954
end
955+
956+
it "matches anything with **" do
957+
eval(<<~RUBY).should == true
958+
case {a: 1}
959+
in **;
960+
true
961+
end
962+
RUBY
963+
end
901964
end
902965
end
903966
end

language/range_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@
1616
eval("(1...)").should == Range.new(1, nil, true)
1717
end
1818
end
19+
20+
ruby_version_is "2.7" do
21+
it "creates beginless ranges" do
22+
eval("(..1)").should == Range.new(nil, 1)
23+
eval("(...1)").should == Range.new(nil, 1, true)
24+
end
25+
end
1926
end

0 commit comments

Comments
 (0)