Skip to content

Commit c53448d

Browse files
authored
Drop assert_expand_second and assert_expand_third helpers (#16244)
The AST node of interest is always the last one, the previous elements of an `Expressions` node only serve to turn specific `Call`s into `Var`s. It suffices to have `assert_expand` always expand the last element of any `Expressions` node.
1 parent 93c8072 commit c53448d

File tree

6 files changed

+42
-48
lines changed

6 files changed

+42
-48
lines changed

spec/compiler/normalize/and_spec.cr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ describe "Normalize: and" do
66
end
77

88
it "normalizes and with variable on the left" do
9-
assert_expand_second "a = 1; a && b", "if a\n b\nelse\n a\nend"
9+
assert_expand "a = 1; a && b", "if a\n b\nelse\n a\nend"
1010
end
1111

1212
it "normalizes and with is_a? on var" do
13-
assert_expand_second "a = 1; a.is_a?(Foo) && b", "if a.is_a?(Foo)\n b\nelse\n a.is_a?(Foo)\nend"
13+
assert_expand "a = 1; a.is_a?(Foo) && b", "if a.is_a?(Foo)\n b\nelse\n a.is_a?(Foo)\nend"
1414
end
1515

1616
it "normalizes and with ! on var" do
17-
assert_expand_second "a = 1; !a && b", "if !a\n b\nelse\n !a\nend"
17+
assert_expand "a = 1; !a && b", "if !a\n b\nelse\n !a\nend"
1818
end
1919

2020
it "normalizes and with ! on var.is_a?(...)" do
21-
assert_expand_second "a = 1; !a.is_a?(Int32) && b", "if !a.is_a?(Int32)\n b\nelse\n !a.is_a?(Int32)\nend"
21+
assert_expand "a = 1; !a.is_a?(Int32) && b", "if !a.is_a?(Int32)\n b\nelse\n !a.is_a?(Int32)\nend"
2222
end
2323

2424
it "normalizes and with is_a? on exp" do
25-
assert_expand_second "a = 1; 1.is_a?(Foo) && b", "if __temp_1 = 1.is_a?(Foo)\n b\nelse\n __temp_1\nend"
25+
assert_expand "a = 1; 1.is_a?(Foo) && b", "if __temp_1 = 1.is_a?(Foo)\n b\nelse\n __temp_1\nend"
2626
end
2727

2828
it "normalizes and with assignment" do

spec/compiler/normalize/case_spec.cr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ describe "Normalize: case" do
66
end
77

88
it "normalizes case with var in cond" do
9-
assert_expand_second "x = 1; case x; when 1; 'b'; end", "if 1 === x\n 'b'\nend"
9+
assert_expand "x = 1; case x; when 1; 'b'; end", "if 1 === x\n 'b'\nend"
1010
end
1111

1212
it "normalizes case with Path to is_a?" do
13-
assert_expand_second "x = 1; case x; when Foo; 'b'; end", "if x.is_a?(Foo)\n 'b'\nend"
13+
assert_expand "x = 1; case x; when Foo; 'b'; end", "if x.is_a?(Foo)\n 'b'\nend"
1414
end
1515

1616
it "normalizes case with generic to is_a?" do
17-
assert_expand_second "x = 1; case x; when Foo(T); 'b'; end", "if x.is_a?(Foo(T))\n 'b'\nend"
17+
assert_expand "x = 1; case x; when Foo(T); 'b'; end", "if x.is_a?(Foo(T))\n 'b'\nend"
1818
end
1919

2020
it "normalizes case with Path.class to is_a?" do
21-
assert_expand_second "x = 1; case x; when Foo.class; 'b'; end", "if x.is_a?(Foo.class)\n 'b'\nend"
21+
assert_expand "x = 1; case x; when Foo.class; 'b'; end", "if x.is_a?(Foo.class)\n 'b'\nend"
2222
end
2323

2424
it "normalizes case with Generic.class to is_a?" do
25-
assert_expand_second "x = 1; case x; when Foo(T).class; 'b'; end", "if x.is_a?(Foo(T).class)\n 'b'\nend"
25+
assert_expand "x = 1; case x; when Foo(T).class; 'b'; end", "if x.is_a?(Foo(T).class)\n 'b'\nend"
2626
end
2727

2828
it "normalizes case with many expressions in when" do
29-
assert_expand_second "x = 1; case x; when 1, 2; 'b'; end", "if (1 === x) || (2 === x)\n 'b'\nend"
29+
assert_expand "x = 1; case x; when 1, 2; 'b'; end", "if (1 === x) || (2 === x)\n 'b'\nend"
3030
end
3131

3232
it "normalizes case with implicit call" do
@@ -70,39 +70,39 @@ describe "Normalize: case" do
7070
end
7171

7272
it "normalizes case with nil to is_a?" do
73-
assert_expand_second "x = 1; case x; when nil; 'b'; end", "if x.is_a?(::Nil)\n 'b'\nend"
73+
assert_expand "x = 1; case x; when nil; 'b'; end", "if x.is_a?(::Nil)\n 'b'\nend"
7474
end
7575

7676
it "normalizes case with multiple expressions" do
77-
assert_expand_second "x, y = 1, 2; case {x, y}; when {2, 3}; 4; end", "if (2 === x) && (3 === y)\n 4\nend"
77+
assert_expand "x, y = 1, 2; case {x, y}; when {2, 3}; 4; end", "if (2 === x) && (3 === y)\n 4\nend"
7878
end
7979

8080
it "normalizes case with multiple expressions and types" do
81-
assert_expand_second "x, y = 1, 2; case {x, y}; when {Int32, Float64}; 4; end", "if x.is_a?(Int32) && y.is_a?(Float64)\n 4\nend"
81+
assert_expand "x, y = 1, 2; case {x, y}; when {Int32, Float64}; 4; end", "if x.is_a?(Int32) && y.is_a?(Float64)\n 4\nend"
8282
end
8383

8484
it "normalizes case with multiple expressions and implicit obj" do
85-
assert_expand_second "x, y = 1, 2; case {x, y}; when {.foo, .bar}; 4; end", "if x.foo && y.bar\n 4\nend"
85+
assert_expand "x, y = 1, 2; case {x, y}; when {.foo, .bar}; 4; end", "if x.foo && y.bar\n 4\nend"
8686
end
8787

8888
it "normalizes case with multiple expressions and comma" do
89-
assert_expand_second "x, y = 1, 2; case {x, y}; when {2, 3}, {4, 5}; 6; end", "if ((2 === x) && (3 === y)) || ((4 === x) && (5 === y))\n 6\nend"
89+
assert_expand "x, y = 1, 2; case {x, y}; when {2, 3}, {4, 5}; 6; end", "if ((2 === x) && (3 === y)) || ((4 === x) && (5 === y))\n 6\nend"
9090
end
9191

9292
it "normalizes case with multiple expressions with underscore" do
93-
assert_expand_second "x, y = 1, 2; case {x, y}; when {2, _}; 4; end", "if 2 === x\n 4\nend"
93+
assert_expand "x, y = 1, 2; case {x, y}; when {2, _}; 4; end", "if 2 === x\n 4\nend"
9494
end
9595

9696
it "normalizes case with multiple expressions with all underscores" do
97-
assert_expand_second "x, y = 1, 2; case {x, y}; when {_, _}; 4; end", "if true\n 4\nend"
97+
assert_expand "x, y = 1, 2; case {x, y}; when {_, _}; 4; end", "if true\n 4\nend"
9898
end
9999

100100
it "normalizes case with multiple expressions with all underscores twice" do
101-
assert_expand_second "x, y = 1, 2; case {x, y}; when {_, _}, {_, _}; 4; end", "if true\n 4\nend"
101+
assert_expand "x, y = 1, 2; case {x, y}; when {_, _}, {_, _}; 4; end", "if true\n 4\nend"
102102
end
103103

104104
it "normalizes case with multiple expressions and non-tuple" do
105-
assert_expand_second "x, y = 1, 2; case {x, y}; when 1; 4; end", "if 1 === {x, y}\n 4\nend"
105+
assert_expand "x, y = 1, 2; case {x, y}; when 1; 4; end", "if 1 === {x, y}\n 4\nend"
106106
end
107107

108108
it "normalizes case without when and else" do
@@ -122,6 +122,6 @@ describe "Normalize: case" do
122122
end
123123

124124
it "normalizes case with Path.class to is_a? (in)" do
125-
assert_expand_second "x = 1; case x; in Foo.class; 'b'; end", "if x.is_a?(Foo.class)\n 'b'\nelse\n raise \"unreachable\"\nend"
125+
assert_expand "x = 1; case x; in Foo.class; 'b'; end", "if x.is_a?(Foo.class)\n 'b'\nelse\n raise \"unreachable\"\nend"
126126
end
127127
end

spec/compiler/normalize/multi_assign_spec.cr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe "Normalize: multi assign" do
1313
end
1414

1515
it "normalizes n to n with []" do
16-
assert_expand_third "a = 1; b = 2; a[0], b[1] = 2, 3", <<-CRYSTAL
16+
assert_expand "a = 1; b = 2; a[0], b[1] = 2, 3", <<-CRYSTAL
1717
__temp_1 = 2
1818
__temp_2 = 3
1919
a[0] = __temp_1
@@ -22,7 +22,7 @@ describe "Normalize: multi assign" do
2222
end
2323

2424
it "normalizes n to n with call" do
25-
assert_expand_third "a = 1; b = 2; a.foo, b.bar = 2, 3", <<-CRYSTAL
25+
assert_expand "a = 1; b = 2; a.foo, b.bar = 2, 3", <<-CRYSTAL
2626
__temp_1 = 2
2727
__temp_2 = 3
2828
a.foo = __temp_1
@@ -32,7 +32,7 @@ describe "Normalize: multi assign" do
3232

3333
context "without strict_multi_assign" do
3434
it "normalizes 1 to n" do
35-
assert_expand_second "d = 1; a, b, c = d", <<-CRYSTAL
35+
assert_expand "d = 1; a, b, c = d", <<-CRYSTAL
3636
__temp_1 = d
3737
a = __temp_1[0]
3838
b = __temp_1[1]
@@ -41,15 +41,15 @@ describe "Normalize: multi assign" do
4141
end
4242

4343
it "normalizes 1 to n with []" do
44-
assert_expand_third "a = 1; b = 2; a[0], b[1] = 2", <<-CRYSTAL
44+
assert_expand "a = 1; b = 2; a[0], b[1] = 2", <<-CRYSTAL
4545
__temp_1 = 2
4646
a[0] = __temp_1[0]
4747
b[1] = __temp_1[1]
4848
CRYSTAL
4949
end
5050

5151
it "normalizes 1 to n with call" do
52-
assert_expand_third "a = 1; b = 2; a.foo, b.bar = 2", <<-CRYSTAL
52+
assert_expand "a = 1; b = 2; a.foo, b.bar = 2", <<-CRYSTAL
5353
__temp_1 = 2
5454
a.foo = __temp_1[0]
5555
b.bar = __temp_1[1]
@@ -59,7 +59,7 @@ describe "Normalize: multi assign" do
5959

6060
context "strict_multi_assign" do
6161
it "normalizes 1 to n" do
62-
assert_expand_second "d = 1; a, b, c = d", <<-CRYSTAL, flags: "strict_multi_assign"
62+
assert_expand "d = 1; a, b, c = d", <<-CRYSTAL, flags: "strict_multi_assign"
6363
__temp_1 = d
6464
if __temp_1.size != 3
6565
::raise(::IndexError.new("Multiple assignment count mismatch"))
@@ -71,7 +71,7 @@ describe "Normalize: multi assign" do
7171
end
7272

7373
it "normalizes 1 to n with []" do
74-
assert_expand_third "a = 1; b = 2; a[0], b[1] = 2", <<-CRYSTAL, flags: "strict_multi_assign"
74+
assert_expand "a = 1; b = 2; a[0], b[1] = 2", <<-CRYSTAL, flags: "strict_multi_assign"
7575
__temp_1 = 2
7676
if __temp_1.size != 2
7777
::raise(::IndexError.new("Multiple assignment count mismatch"))
@@ -82,7 +82,7 @@ describe "Normalize: multi assign" do
8282
end
8383

8484
it "normalizes 1 to n with call" do
85-
assert_expand_third "a = 1; b = 2; a.foo, b.bar = 2", <<-CRYSTAL, flags: "strict_multi_assign"
85+
assert_expand "a = 1; b = 2; a.foo, b.bar = 2", <<-CRYSTAL, flags: "strict_multi_assign"
8686
__temp_1 = 2
8787
if __temp_1.size != 2
8888
::raise(::IndexError.new("Multiple assignment count mismatch"))
@@ -94,7 +94,7 @@ describe "Normalize: multi assign" do
9494
end
9595

9696
it "normalizes m to n, with splat on left-hand side, splat is empty" do
97-
assert_expand_third "a = 1; b = 2; *a[0], b.foo, c = 3, 4", <<-CRYSTAL
97+
assert_expand "a = 1; b = 2; *a[0], b.foo, c = 3, 4", <<-CRYSTAL
9898
__temp_1 = ::Tuple.new
9999
__temp_2 = 3
100100
__temp_3 = 4
@@ -105,7 +105,7 @@ describe "Normalize: multi assign" do
105105
end
106106

107107
it "normalizes m to n, with splat on left-hand side, splat is non-empty" do
108-
assert_expand_third "a = 1; b = 2; a[0], *b.foo, c = 3, 4, 5, 6, 7", <<-CRYSTAL
108+
assert_expand "a = 1; b = 2; a[0], *b.foo, c = 3, 4, 5, 6, 7", <<-CRYSTAL
109109
__temp_1 = 3
110110
__temp_2 = ::Tuple.new(4, 5, 6)
111111
__temp_3 = 7
@@ -155,7 +155,7 @@ describe "Normalize: multi assign" do
155155
end
156156

157157
it "normalizes 1 to n, with splat on left-hand side" do
158-
assert_expand_third "c = 1; d = 2; a, b, *c.foo, d[0], e, f = 3", <<-CRYSTAL
158+
assert_expand "c = 1; d = 2; a, b, *c.foo, d[0], e, f = 3", <<-CRYSTAL
159159
__temp_1 = 3
160160
if __temp_1.size < 5
161161
::raise(::IndexError.new("Multiple assignment count mismatch"))

spec/compiler/normalize/or_spec.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ describe "Normalize: or" do
66
end
77

88
it "normalizes or with variable on the left" do
9-
assert_expand_second "a = 1; a || b", "if a\n a\nelse\n b\nend"
9+
assert_expand "a = 1; a || b", "if a\n a\nelse\n b\nend"
1010
end
1111

1212
it "normalizes or with assignment on the left" do
1313
assert_expand "(a = 1) || b", "if a = 1\n a\nelse\n b\nend"
1414
end
1515

1616
it "normalizes or with is_a? on var" do
17-
assert_expand_second "a = 1; a.is_a?(Foo) || b", "if a.is_a?(Foo)\n a.is_a?(Foo)\nelse\n b\nend"
17+
assert_expand "a = 1; a.is_a?(Foo) || b", "if a.is_a?(Foo)\n a.is_a?(Foo)\nelse\n b\nend"
1818
end
1919

2020
it "normalizes or with ! on var" do
21-
assert_expand_second "a = 1; !a || b", "if !a\n !a\nelse\n b\nend"
21+
assert_expand "a = 1; !a || b", "if !a\n !a\nelse\n b\nend"
2222
end
2323

2424
it "normalizes or with ! on var.is_a?(...)" do
25-
assert_expand_second "a = 1; !a.is_a?(Int32) || b", "if !a.is_a?(Int32)\n !a.is_a?(Int32)\nelse\n b\nend"
25+
assert_expand "a = 1; !a.is_a?(Int32) || b", "if !a.is_a?(Int32)\n !a.is_a?(Int32)\nelse\n b\nend"
2626
end
2727
end

spec/compiler/normalize/proc_pointer_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe "Normalize: proc pointer" do
4242
end
4343

4444
it "normalizes proc pointer with variable receiver" do
45-
assert_expand_second "foo = 1; ->foo.bar(Int32)", <<-CRYSTAL
45+
assert_expand "foo = 1; ->foo.bar(Int32)", <<-CRYSTAL
4646
__temp_1 = foo
4747
->(__temp_2 : Int32) do
4848
__temp_1.bar(__temp_2)

spec/spec_helper.cr

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ def assert_normalize(from, to, flags = nil, *, filename = nil, file = __FILE__,
122122
end
123123

124124
def assert_expand(from : String, to, *, flags = nil, file = __FILE__, line = __LINE__)
125-
assert_expand Parser.parse(from), to, flags: flags, file: file, line: line
125+
node = Parser.parse(from)
126+
if node.is_a?(Expressions)
127+
node = node.last
128+
end
129+
assert_expand node, to, flags: flags, file: file, line: line
126130
end
127131

128132
def assert_expand(from_nodes : ASTNode, to, *, flags = nil, file = __FILE__, line = __LINE__)
@@ -138,16 +142,6 @@ def assert_expand(from_nodes : ASTNode, *, flags = nil, file = __FILE__, line =
138142
yield to_nodes, program
139143
end
140144

141-
def assert_expand_second(from : String, to, *, flags = nil, file = __FILE__, line = __LINE__)
142-
node = (Parser.parse(from).as(Expressions))[1]
143-
assert_expand node, to, flags: flags, file: file, line: line
144-
end
145-
146-
def assert_expand_third(from : String, to, *, flags = nil, file = __FILE__, line = __LINE__)
147-
node = (Parser.parse(from).as(Expressions))[2]
148-
assert_expand node, to, flags: flags, file: file, line: line
149-
end
150-
151145
def assert_expand_named(from : String, to, *, generic = nil, flags = nil, file = __FILE__, line = __LINE__)
152146
program = new_program
153147
program.flags.concat(flags.split) if flags

0 commit comments

Comments
 (0)