/
funcs.t
271 lines (220 loc) · 5.02 KB
/
funcs.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use v6;
use Test;
use _007::Test;
{
my $program = q:to/./;
func f() { say("OH HAI from inside sub") }
.
outputs $program, "", "subs are not immediate";
}
{
my $program = q:to/./;
my x = "one";
say(x);
func f() {
my x = "two";
say(x);
}
f();
say(x);
.
outputs $program, "one\ntwo\none\n", "subs have their own variable scope";
}
{
my $program = q:to/./;
func f(name) {
say("Good evening, Mr " ~ name);
}
f("Bond");
.
outputs $program, "Good evening, Mr Bond\n", "calling a func with parameters works";
}
{
my $program = q:to/./;
func f(x, y) {
say(x ~ y);
}
my y = "y";
f("X", y ~ y);
.
outputs $program, "Xyy\n", "arguments are evaluated before parameters are bound";
}
{
my $program = q:to/./;
func f(callback) {
my scoping = "dynamic";
callback();
}
my scoping = "lexical";
f(func() { say(scoping) });
.
outputs $program, "lexical\n", "scoping is lexical";
}
{
my $program = q:to/./;
f();
func f() {
say("OH HAI from inside sub");
}
.
outputs $program, "OH HAI from inside sub\n", "call a func before declaring it";
}
{
my $program = q:to/./;
f();
my x = "X";
func f() {
say(x);
}
.
outputs $program, "none\n", "using an outer lexical in a func that's called before the outer lexical's declaration";
}
{
my $program = q:to/./;
func f() { say("OH HAI") }
func g() { return f };
g()();
.
outputs $program, "OH HAI\n",
"left hand of a call doesn't have to be an identifier, just has to resolve to a callable";
}
{
my $program = 'f("Bond"); func f(name) { say("Good evening, Mr " ~ name) }';
outputs $program, "Good evening, Mr Bond\n", "calling a post-declared func works";
}
{
my $program = 'my b = 42; func g() { say(b) }; g()';
outputs $program, "42\n", "lexical scope works correctly from inside a sub";
}
{
my $program = q:to/./;
func f() {}
f = 5;
.
parse-error
$program,
X::Assignment::ReadOnly,
"cannot assign to a subroutine (#68)";
}
{
my $program = q:to/./;
func f() {}
func h(a, b, f) {
f = 17;
say(f == 17);
}
h(0, 0, 7);
say(f == 17);
.
outputs $program,
"true\nfalse\n",
"can assign to a parameter which hides a subroutine (#68)";
}
{
my $program = q:to/./;
my f = func (x) { say(x) };
f("Mr Bond");
.
outputs $program,
"Mr Bond\n",
"expression subs work";
}
{
my $program = q:to/./;
my f = func g(x) { say(x) };
f("Mr Bond");
.
outputs $program,
"Mr Bond\n",
"expression subs can be named, too";
}
{
my $program = q:to/./;
my f = func g(x) {};
say(f);
.
outputs $program,
"<func g(x)>\n",
"...and they know their own name";
}
{
my $program = q:to/./;
my f = func g() { say(g) };
f();
.
outputs $program,
"<func g()>\n",
"the name of a func is visible inside the sub...";
}
{
my $program = q:to/./;
my f = func g() {};
g();
.
parse-error $program,
X::Undeclared,
"...but not outside of the sub";
}
{
my $program = q:to/./;
my f = func () {
my c = "Goldfinger";
say(c);
};
f();
.
outputs $program,
"Goldfinger\n",
"can declare and use a variable in a term sub";
}
{
my $program = q:to/./;
func f(x,) { }
func g(x,y,) { }
.
outputs $program, "", "trailing commas are allowed in parameterlist";
}
{
my $program = q:to/./;
func f(x) { say(1) }
func g(x,y) { say(2) }
f(4,);
g(4,5,);
.
outputs $program, "1\n2\n", "...and in argumentlist";
}
{
my $program = 'func subtract(x) { say(x) }; subtract("Mr Bond")';
outputs $program, "Mr Bond\n", "it's OK to call your func 'subtract'";
}
{
my $program = q:to/./;
func fn()
.
my subset missing-block of X::Syntax::Missing where {
is(.what, "block", "got the right missing thing");
.what eq "block";
};
parse-error $program,
missing-block,
"parse error 'missing block' on missing block (#48)";
}
{
my $program = q:to/./;
func b(count) {
if count {
b(count - 1);
say(count);
}
}
b(4);
.
outputs $program, "1\n2\n3\n4\n", "each func invocation gets its own callframe/scope";
}
{
my $program = q:to/./;
say(func () {});
.
outputs $program, "<func ()>\n", "an anonymous func stringifies without a name";
}
done-testing;