Skip to content

Commit a9789cf

Browse files
authored
Add tests for block_given? desugaring (sorbet#9723)
1 parent 8c73044 commit a9789cf

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# typed: false
2+
3+
class SomethingElse; end
4+
5+
def method_with_block_param(&the_block_name)
6+
# Valid cases that we should detect as special calls to `block_given?`
7+
block_given?
8+
self.block_given?
9+
Kernel.block_given?
10+
::Kernel.block_given?
11+
12+
# Due to the current structure of Desugar.cc, we still detect this as a special `block_given?` call.
13+
block_given? { "with a block?!" }
14+
# ^^^^^^^^^^^^^^^^^^^^ error: No body in block
15+
16+
# Cases we should not detect as special calls to `block_given?`
17+
Object.block_given? # Normally not allowed: private method `block_given?' called for Object:Class
18+
SomethingElse.block_given?
19+
block_given?("with parameter")
20+
end
21+
22+
def method_with_anonymous_block_param(&)
23+
# Valid cases that we should detect as special calls to `block_given?`
24+
block_given?
25+
self.block_given?
26+
Kernel.block_given?
27+
::Kernel.block_given?
28+
29+
# Due to the current structure of Desugar.cc, we still detect this as a special `block_given?` call.
30+
block_given? { "with a block?!" }
31+
# ^^^^^^^^^^^^^^^^^^^^ error: No body in block
32+
33+
# Cases we should not detect as special calls to `block_given?`
34+
Object.block_given? # Normally not allowed: private method `block_given?' called for Object:Class
35+
SomethingElse.block_given?
36+
block_given?("with parameter")
37+
end
38+
39+
def method_with_implicit_block_param()
40+
# Valid cases that we should detect as special calls to `block_given?`
41+
block_given?
42+
self.block_given?
43+
Kernel.block_given?
44+
::Kernel.block_given?
45+
46+
# Due to the current structure of Desugar.cc, we still detect this as a special `block_given?` call.
47+
block_given? { "with a block?!" }
48+
# ^^^^^^^^^^^^^^^^^^^^ error: No body in block
49+
50+
# Cases we should not detect as special calls to `block_given?`
51+
Object.block_given? # Normally not allowed: private method `block_given?' called for Object:Class
52+
SomethingElse.block_given?
53+
block_given?("with parameter")
54+
end
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
class <emptyTree><<C <root>>> < (::<todo sym>)
2+
class <emptyTree>::<C SomethingElse><<C <todo sym>>> < (::<todo sym>)
3+
<emptyTree>
4+
end
5+
6+
def method_with_block_param<<todo method>>(&the_block_name)
7+
begin
8+
if the_block_name
9+
<self>.block_given?()
10+
else
11+
false
12+
end
13+
if the_block_name
14+
<self>.block_given?()
15+
else
16+
false
17+
end
18+
if the_block_name
19+
<emptyTree>::<C Kernel>.block_given?()
20+
else
21+
false
22+
end
23+
if the_block_name
24+
::<root>::<C Kernel>.block_given?()
25+
else
26+
false
27+
end
28+
<emptyTree>
29+
if the_block_name
30+
<emptyTree>::<C Object>.block_given?()
31+
else
32+
false
33+
end
34+
if the_block_name
35+
<emptyTree>::<C SomethingElse>.block_given?()
36+
else
37+
false
38+
end
39+
<self>.block_given?("with parameter")
40+
end
41+
end
42+
43+
def method_with_anonymous_block_param<<todo method>>(&&$2)
44+
begin
45+
if &$2
46+
<self>.block_given?()
47+
else
48+
false
49+
end
50+
if &$2
51+
<self>.block_given?()
52+
else
53+
false
54+
end
55+
if &$2
56+
<emptyTree>::<C Kernel>.block_given?()
57+
else
58+
false
59+
end
60+
if &$2
61+
::<root>::<C Kernel>.block_given?()
62+
else
63+
false
64+
end
65+
<emptyTree>
66+
if &$2
67+
<emptyTree>::<C Object>.block_given?()
68+
else
69+
false
70+
end
71+
if &$2
72+
<emptyTree>::<C SomethingElse>.block_given?()
73+
else
74+
false
75+
end
76+
<self>.block_given?("with parameter")
77+
end
78+
end
79+
80+
def method_with_implicit_block_param<<todo method>>(&<blk>)
81+
begin
82+
if <blk>
83+
<self>.block_given?()
84+
else
85+
false
86+
end
87+
if <blk>
88+
<self>.block_given?()
89+
else
90+
false
91+
end
92+
if <blk>
93+
<emptyTree>::<C Kernel>.block_given?()
94+
else
95+
false
96+
end
97+
if <blk>
98+
::<root>::<C Kernel>.block_given?()
99+
else
100+
false
101+
end
102+
<emptyTree>
103+
if <blk>
104+
<emptyTree>::<C Object>.block_given?()
105+
else
106+
false
107+
end
108+
if <blk>
109+
<emptyTree>::<C SomethingElse>.block_given?()
110+
else
111+
false
112+
end
113+
<self>.block_given?("with parameter")
114+
end
115+
end
116+
end

0 commit comments

Comments
 (0)