/
return.t
356 lines (293 loc) · 9.88 KB
/
return.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use v6;
use Test;
=begin desc
This tests proper return of values from subroutines.
# L<S06/"The C<return> function">
See also t/blocks/return.t, which overlaps in scope.
=end desc
# NOTE: the smart link above actually doesn't go to a good
# reference for the spec for 'return', but I couldn't find
# one either.
plan 79;
# These test the returning of values from a subroutine.
# We test each data-type with 4 different styles of return.
#
# The datatypes are:
# Scalar
# Array
# Array-ref (aka List)
# Hash
# Hash-ref
#
# The 4 return styles are:
# create a variable, and return it with the return statement
# create a variable, and make it the last value in the sub (implied return)
# create the value inline and return it with the return statement
# create the value inline and make it the last value in the sub (implied return)
#
# NOTE:
# we do not really check return context here. That should be
# in it's own test file
# TODO-NOTE:
# Currently the Hash and Hash-ref tests are not complete, becuase hashes seem to be
# broken in a number of ways. I will get back to those later.
## void
# ok(eval('sub ret { return }; 1'), "return without value parses ok");
sub bare_return { return };
ok(! bare_return(), "A bare return is a false value");
my @l = <some values>;
@l = bare_return();
is( @l, [], "A bare return is an empty list in array/list context");
my $s = "hello";
$s = bare_return();
nok($s.defined, "A bare return is undefined in scalar context");
## scalars
sub foo_scalar {
my $foo = 'foo';
return $foo;
}
is(foo_scalar(), 'foo', 'got the right return value');
# ... w/out return statement
sub foo_scalar2 {
my $foo = 'foo';
$foo;
}
is(foo_scalar2(), 'foo', 'got the right return value');
# ... returning constant
sub foo_scalar3 {
return 'foo';
}
is(foo_scalar3(), 'foo', 'got the right return value');
# ... returning constant w/out return statement
sub foo_scalar4 {
'foo';
}
is(foo_scalar4(), 'foo', 'got the right return value');
## arrays
sub foo_array {
my @foo = ('foo', 'bar', 'baz');
return @foo;
}
my @foo_array_return = foo_array();
isa_ok(@foo_array_return, Array);
is(+@foo_array_return, 3, 'got the right number of return value');
is(@foo_array_return[0], 'foo', 'got the right return value');
is(@foo_array_return[1], 'bar', 'got the right return value');
is(@foo_array_return[2], 'baz', 'got the right return value');
# ... without the last return statement
sub foo_array2 {
my @foo = ('foo', 'bar', 'baz');
@foo;
}
my @foo_array_return2 = foo_array2();
isa_ok(@foo_array_return2, Array);
is(+@foo_array_return2, 3, 'got the right number of return value');
is(@foo_array_return2[0], 'foo', 'got the right return value');
is(@foo_array_return2[1], 'bar', 'got the right return value');
is(@foo_array_return2[2], 'baz', 'got the right return value');
# ... returning an Array constructed "on the fly"
sub foo_array3 {
return ('foo', 'bar', 'baz');
}
my @foo_array_return3 = foo_array3();
isa_ok(@foo_array_return3, Array);
is(+@foo_array_return3, 3, 'got the right number of return value');
is(@foo_array_return3[0], 'foo', 'got the right return value');
is(@foo_array_return3[1], 'bar', 'got the right return value');
is(@foo_array_return3[2], 'baz', 'got the right return value');
# ... returning an Array constructed "on the fly" w/out return statement
sub foo_array4 {
('foo', 'bar', 'baz');
}
my @foo_array_return4 = foo_array4();
isa_ok(@foo_array_return4, Array);
is(+@foo_array_return4, 3, 'got the right number of return value');
is(@foo_array_return4[0], 'foo', 'got the right return value');
is(@foo_array_return4[1], 'bar', 'got the right return value');
is(@foo_array_return4[2], 'baz', 'got the right return value');
## Array Refs aka - Lists
sub foo_array_ref {
my $foo = ['foo', 'bar', 'baz'];
return $foo;
}
my $foo_array_ref_return = foo_array_ref();
isa_ok($foo_array_ref_return, Array);
is(+$foo_array_ref_return, 3, 'got the right number of return value');
is($foo_array_ref_return[0], 'foo', 'got the right return value');
is($foo_array_ref_return[1], 'bar', 'got the right return value');
is($foo_array_ref_return[2], 'baz', 'got the right return value');
# ... w/out the return statement
sub foo_array_ref2 {
my $foo = ['foo', 'bar', 'baz'];
$foo;
}
my $foo_array_ref_return2 = foo_array_ref2();
isa_ok($foo_array_ref_return2, Array);
is(+$foo_array_ref_return2, 3, 'got the right number of return value');
is($foo_array_ref_return2[0], 'foo', 'got the right return value');
is($foo_array_ref_return2[1], 'bar', 'got the right return value');
is($foo_array_ref_return2[2], 'baz', 'got the right return value');
# ... returning list constructed "on the fly"
sub foo_array_ref3 {
return ['foo', 'bar', 'baz'];
}
my $foo_array_ref_return3 = foo_array_ref3();
isa_ok($foo_array_ref_return3, Array);
is(+$foo_array_ref_return3, 3, 'got the right number of return value');
is($foo_array_ref_return3[0], 'foo', 'got the right return value');
is($foo_array_ref_return3[1], 'bar', 'got the right return value');
is($foo_array_ref_return3[2], 'baz', 'got the right return value');
# ... returning list constructed "on the fly" w/out return statement
sub foo_array_ref4 {
['foo', 'bar', 'baz'];
}
my $foo_array_ref_return4 = foo_array_ref4();
isa_ok($foo_array_ref_return4, Array);
is(+$foo_array_ref_return4, 3, 'got the right number of return value');
is($foo_array_ref_return4[0], 'foo', 'got the right return value');
is($foo_array_ref_return4[1], 'bar', 'got the right return value');
is($foo_array_ref_return4[2], 'baz', 'got the right return value');
## hashes
sub foo_hash {
my %foo = ('foo', 1, 'bar', 2, 'baz', 3);
return %foo;
}
my %foo_hash_return = foo_hash();
ok(%foo_hash_return ~~ Hash);
is(+%foo_hash_return.keys, 3, 'got the right number of return value');
is(%foo_hash_return<foo>, 1, 'got the right return value');
is(%foo_hash_return<bar>, 2, 'got the right return value');
is(%foo_hash_return<baz>, 3, 'got the right return value');
my $keys;
lives_ok({ $keys = +(foo_hash().keys) },
"can call method on return value (hashref)");
is($keys, 3, "got right result");
lives_ok({ foo_hash()<foo> },
"can hash de-ref return value (hashref)");
# now hash refs
sub foo_hash_ref {
my $foo = { 'foo' => 1, 'bar' => 2, 'baz' => 3 };
return $foo;
}
my $foo_hash_ref_return = foo_hash_ref();
ok($foo_hash_ref_return ~~ Hash);
is(+$foo_hash_ref_return.keys, 3, 'got the right number of return value');
is($foo_hash_ref_return<foo>, 1, 'got the right return value');
is($foo_hash_ref_return<bar>, 2, 'got the right return value');
is($foo_hash_ref_return<baz>, 3, 'got the right return value');
lives_ok({ $keys = +(foo_hash_ref().keys) },
"can call method on return value (hashref)");
is($keys, 3, "got right result");
lives_ok({ foo_hash_ref()<foo> },
"can hash de-ref return value (hashref)");
# from return2.t
{
sub userdefinedcontrol_a (&block) { block(); return 24 }
sub official_a {
userdefinedcontrol_a { return 42 };
}
is official_a(), 42, "bare blocks are invisible to return";
}
{
sub userdefinedcontrol_b (&block) { block(); return 24 }
sub official_b {
{
{
userdefinedcontrol_b { return 42 };
}
}
}
is official_b(), 42, "nested bare blocks are invisible to return";
}
{
sub userdefinedcontrol_c ($value, &block) { block($value); return 24 }
sub official_c($value) {
{
userdefinedcontrol_c $value, -> $x { return $x };
}
}
is official_c(42), 42, "pointy blocks are invisible to return";
}
# return should desugar to &?ROUTINE.leave, where &?ROUTINE is lexically scoped
# to mean the current "official" subroutine or method.
#?niecza todo
{
sub userdefinedcontrol3 (&block) { block(); return 36 }
sub userdefinedcontrol2 (&block) { userdefinedcontrol3(&block); return 24 }
sub userdefinedcontrol1 (&block) { userdefinedcontrol2(&block); return 12 }
sub official_d {
userdefinedcontrol1 { return 42 };
}
#?pugs todo 'bug'
is official_d(), 42,
"subcalls in user-defined control flow are invisible to return";
}
class Foo {
method userdefinedcontrol3 (&block) { block(); 36 }
submethod userdefinedcontrol2 (&block) { self.userdefinedcontrol3(&block); 24 }
method userdefinedcontrol1 (&block) { self.userdefinedcontrol2(&block); 12 }
method officialmeth {
self.userdefinedcontrol1({ return 42 });
}
submethod officialsubmeth {
self.userdefinedcontrol1({ return 43 });
}
our sub official {
Foo.new.userdefinedcontrol1({ return 44 });
}
}
#?pugs 3 todo 'return(), blocks and methods'
#?niecza 3 todo
is Foo.new.officialmeth(), 42,
"return correctly from official method only";
is Foo.new.officialsubmeth(), 43,
"return correctly from official submethod only";
is Foo::official(), 44,
"return correctly from official sub only";
# RT #75118
#?rakudo skip 'RT 75118'
#?niecza skip "Excess arguments to return, unused named c"
{
sub named() {
return 1, 2, :c(3);
}
is named().elems, 3, 'return with named arguments';
is named().[2].key, 'c', ' ... correct key';
#?rakudo todo 'named argument to return()'
is named().[2].value, '3', ' ... correct value';
}
# RT #61732
#?niecza todo
{
sub rt61732_c { 1; CATCH {} }
#?rakudo todo 'RT 61732'
is rt61732_c(), 1, 'sub with empty catch block returns value before block';
}
#?niecza todo
{
sub rt61732_d { 1;; }
is rt61732_d(), 1, 'get right value from sub with double ;';
}
# RT #63912
{
sub rt63912 { return 1, 2; }
lives_ok { rt63912() }, 'can call sub that returns two things (no parens)';
}
# RT #72836
{
class RT72836 {
method new() { }
}
lives_ok {my $c = RT72836.new},
'can use value returned from empty routine';
}
# RT #61126
#?niecza skip "eager NYI"
{
sub bar61126($code) { $code() }; sub foo61126 { bar61126 { return 1 }; return 2; };
#?pugs todo
is foo61126, 1;
sub baz61126 { eager map { return 1 }, 1; return 2 };
is baz61126, 1;
}
# vim: ft=perl6