Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 350 lines (286 sloc) 9.96 kb
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
1 use v6;
2 use Test;
3
4 =begin desc
5
6 This tests proper return of values from subroutines.
7
f97d4fb [t/spec] fix two smartlinks
moritz authored
8 # L<S06/"The C<return> function">
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
9
10 See also t/blocks/return.t, which overlaps in scope.
11
12 =end desc
13
14 # NOTE: the smart link above actually doesn't go to a good
15 # reference for the spec for 'return', but I couldn't find
16 # one either.
17
1138a24 Tadeusz Sośnierz Tests for RT #61126
tadzik authored
18 plan 79;
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
19
20 # These test the returning of values from a subroutine.
21 # We test each data-type with 4 different styles of return.
22 #
23 # The datatypes are:
24 # Scalar
25 # Array
26 # Array-ref (aka List)
27 # Hash
28 # Hash-ref
29 #
30 # The 4 return styles are:
31 # create a variable, and return it with the return statement
32 # create a variable, and make it the last value in the sub (implied return)
33 # create the value inline and return it with the return statement
34 # create the value inline and make it the last value in the sub (implied return)
35 #
36 # NOTE:
37 # we do not really check return context here. That should be
38 # in it's own test file
39
40 # TODO-NOTE:
41 # Currently the Hash and Hash-ref tests are not complete, becuase hashes seem to be
42 # broken in a number of ways. I will get back to those later.
43
44 ## void
45 # ok(eval('sub ret { return }; 1'), "return without value parses ok");
46
47 sub bare_return { return };
48
49 ok(! bare_return(), "A bare return is a false value");
50
51 my @l = <some values>;
52 @l = bare_return();
53 is( @l, [], "A bare return is an empty list in array/list context");
54
55 my $s = "hello";
56 $s = bare_return();
6ea6c37 [t/spec] unTODO another test in return.t
moritz authored
57
08f1960 Moritz Lenz start to remove .notdef
moritz authored
58 nok($s.defined, "A bare return is undefined in scalar context");
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
59
60 ## scalars
61
62 sub foo_scalar {
63 my $foo = 'foo';
64 return $foo;
65 }
66 is(foo_scalar(), 'foo', 'got the right return value');
67
68 # ... w/out return statement
69
70 sub foo_scalar2 {
71 my $foo = 'foo';
72 $foo;
73 }
74 is(foo_scalar2(), 'foo', 'got the right return value');
75
76 # ... returning constant
77
78 sub foo_scalar3 {
79 return 'foo';
80 }
81 is(foo_scalar3(), 'foo', 'got the right return value');
82
83 # ... returning constant w/out return statement
84
85 sub foo_scalar4 {
86 'foo';
87 }
88 is(foo_scalar4(), 'foo', 'got the right return value');
89
90 ## arrays
91
92 sub foo_array {
93 my @foo = ('foo', 'bar', 'baz');
94 return @foo;
95 }
96 my @foo_array_return = foo_array();
97 isa_ok(@foo_array_return, Array);
98 is(+@foo_array_return, 3, 'got the right number of return value');
99 is(@foo_array_return[0], 'foo', 'got the right return value');
100 is(@foo_array_return[1], 'bar', 'got the right return value');
101 is(@foo_array_return[2], 'baz', 'got the right return value');
102
103 # ... without the last return statement
104
105 sub foo_array2 {
106 my @foo = ('foo', 'bar', 'baz');
107 @foo;
108 }
109 my @foo_array_return2 = foo_array2();
110 isa_ok(@foo_array_return2, Array);
111 is(+@foo_array_return2, 3, 'got the right number of return value');
112 is(@foo_array_return2[0], 'foo', 'got the right return value');
113 is(@foo_array_return2[1], 'bar', 'got the right return value');
114 is(@foo_array_return2[2], 'baz', 'got the right return value');
115
116 # ... returning an Array constructed "on the fly"
117
118 sub foo_array3 {
119 return ('foo', 'bar', 'baz');
120 }
121 my @foo_array_return3 = foo_array3();
122 isa_ok(@foo_array_return3, Array);
123 is(+@foo_array_return3, 3, 'got the right number of return value');
124 is(@foo_array_return3[0], 'foo', 'got the right return value');
125 is(@foo_array_return3[1], 'bar', 'got the right return value');
126 is(@foo_array_return3[2], 'baz', 'got the right return value');
127
128 # ... returning an Array constructed "on the fly" w/out return statement
129
130 sub foo_array4 {
131 ('foo', 'bar', 'baz');
132 }
133 my @foo_array_return4 = foo_array4();
134 isa_ok(@foo_array_return4, Array);
135 is(+@foo_array_return4, 3, 'got the right number of return value');
136 is(@foo_array_return4[0], 'foo', 'got the right return value');
137 is(@foo_array_return4[1], 'bar', 'got the right return value');
138 is(@foo_array_return4[2], 'baz', 'got the right return value');
139
140 ## Array Refs aka - Lists
141
142 sub foo_array_ref {
143 my $foo = ['foo', 'bar', 'baz'];
144 return $foo;
145 }
146 my $foo_array_ref_return = foo_array_ref();
eb0d6e5 [t/spec] Updates and re-fudging for some return tests.
jnthn authored
147 isa_ok($foo_array_ref_return, Array);
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
148 is(+$foo_array_ref_return, 3, 'got the right number of return value');
149 is($foo_array_ref_return[0], 'foo', 'got the right return value');
150 is($foo_array_ref_return[1], 'bar', 'got the right return value');
151 is($foo_array_ref_return[2], 'baz', 'got the right return value');
152
153 # ... w/out the return statement
154
155 sub foo_array_ref2 {
156 my $foo = ['foo', 'bar', 'baz'];
157 $foo;
158 }
159 my $foo_array_ref_return2 = foo_array_ref2();
eb0d6e5 [t/spec] Updates and re-fudging for some return tests.
jnthn authored
160 isa_ok($foo_array_ref_return2, Array);
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
161 is(+$foo_array_ref_return2, 3, 'got the right number of return value');
162 is($foo_array_ref_return2[0], 'foo', 'got the right return value');
163 is($foo_array_ref_return2[1], 'bar', 'got the right return value');
164 is($foo_array_ref_return2[2], 'baz', 'got the right return value');
165
166 # ... returning list constructed "on the fly"
167
168 sub foo_array_ref3 {
169 return ['foo', 'bar', 'baz'];
170 }
171 my $foo_array_ref_return3 = foo_array_ref3();
eb0d6e5 [t/spec] Updates and re-fudging for some return tests.
jnthn authored
172 isa_ok($foo_array_ref_return3, Array);
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
173 is(+$foo_array_ref_return3, 3, 'got the right number of return value');
174 is($foo_array_ref_return3[0], 'foo', 'got the right return value');
175 is($foo_array_ref_return3[1], 'bar', 'got the right return value');
176 is($foo_array_ref_return3[2], 'baz', 'got the right return value');
177
178 # ... returning list constructed "on the fly" w/out return statement
179
180 sub foo_array_ref4 {
181 ['foo', 'bar', 'baz'];
182 }
183 my $foo_array_ref_return4 = foo_array_ref4();
eb0d6e5 [t/spec] Updates and re-fudging for some return tests.
jnthn authored
184 isa_ok($foo_array_ref_return4, Array);
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
185 is(+$foo_array_ref_return4, 3, 'got the right number of return value');
186 is($foo_array_ref_return4[0], 'foo', 'got the right return value');
187 is($foo_array_ref_return4[1], 'bar', 'got the right return value');
188 is($foo_array_ref_return4[2], 'baz', 'got the right return value');
189
190 ## hashes
191
192 sub foo_hash {
193 my %foo = ('foo', 1, 'bar', 2, 'baz', 3);
194 return %foo;
195 }
196
197 my %foo_hash_return = foo_hash();
eb0d6e5 [t/spec] Updates and re-fudging for some return tests.
jnthn authored
198 ok(%foo_hash_return ~~ Hash);
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
199 is(+%foo_hash_return.keys, 3, 'got the right number of return value');
200 is(%foo_hash_return<foo>, 1, 'got the right return value');
201 is(%foo_hash_return<bar>, 2, 'got the right return value');
202 is(%foo_hash_return<baz>, 3, 'got the right return value');
203
204 my $keys;
205 lives_ok({ $keys = +(foo_hash().keys) },
206 "can call method on return value (hashref)");
207 is($keys, 3, "got right result");
208 lives_ok({ foo_hash()<foo> },
209 "can hash de-ref return value (hashref)");
210
211 # now hash refs
212
213 sub foo_hash_ref {
eb0d6e5 [t/spec] Updates and re-fudging for some return tests.
jnthn authored
214 my $foo = { 'foo' => 1, 'bar' => 2, 'baz' => 3 };
215 return $foo;
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
216 }
217
218 my $foo_hash_ref_return = foo_hash_ref();
eb0d6e5 [t/spec] Updates and re-fudging for some return tests.
jnthn authored
219 ok($foo_hash_ref_return ~~ Hash);
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
220 is(+$foo_hash_ref_return.keys, 3, 'got the right number of return value');
221 is($foo_hash_ref_return<foo>, 1, 'got the right return value');
222 is($foo_hash_ref_return<bar>, 2, 'got the right return value');
223 is($foo_hash_ref_return<baz>, 3, 'got the right return value');
224
225 lives_ok({ $keys = +(foo_hash_ref().keys) },
226 "can call method on return value (hashref)");
227 is($keys, 3, "got right result");
228 lives_ok({ foo_hash_ref()<foo> },
229 "can hash de-ref return value (hashref)");
1c55bcb [t/spec] merge return2.t into return.t
moritz authored
230
231 # from return2.t
232 {
233 sub userdefinedcontrol_a (&block) { block(); return 24 }
234 sub official_a {
235 userdefinedcontrol_a { return 42 };
236 }
237 is official_a(), 42, "bare blocks are invisible to return";
238 }
239
240 {
241 sub userdefinedcontrol_b (&block) { block(); return 24 }
242 sub official_b {
243 {
244 {
245 userdefinedcontrol_b { return 42 };
246 }
247 }
248 }
249 is official_b(), 42, "nested bare blocks are invisible to return";
250 }
251
252 {
253 sub userdefinedcontrol_c ($value, &block) { block($value); return 24 }
254 sub official_c($value) {
255 {
256 userdefinedcontrol_c $value, -> $x { return $x };
257 }
258 }
259 is official_c(42), 42, "pointy blocks are invisible to return";
260 }
261
262 # return should desugar to &?ROUTINE.leave, where &?ROUTINE is lexically scoped
263 # to mean the current "official" subroutine or method.
264
265 {
266 sub userdefinedcontrol3 (&block) { block(); return 36 }
267 sub userdefinedcontrol2 (&block) { userdefinedcontrol3(&block); return 24 }
268 sub userdefinedcontrol1 (&block) { userdefinedcontrol2(&block); return 12 }
269 sub official_d {
270 userdefinedcontrol1 { return 42 };
271 }
ba8e84a [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
272 #?pugs todo 'bug'
1c55bcb [t/spec] merge return2.t into return.t
moritz authored
273 is official_d(), 42,
ba8e84a [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
274 "subcalls in user-defined control flow are invisible to return";
1c55bcb [t/spec] merge return2.t into return.t
moritz authored
275 }
276
277 class Foo {
278 method userdefinedcontrol3 (&block) { block(); 36 }
279 submethod userdefinedcontrol2 (&block) { self.userdefinedcontrol3(&block); 24 }
280 method userdefinedcontrol1 (&block) { self.userdefinedcontrol2(&block); 12 }
281 method officialmeth {
282 self.userdefinedcontrol1({ return 42 });
283 }
284 submethod officialsubmeth {
285 self.userdefinedcontrol1({ return 43 });
286 }
b57d6ca [t/spec] Another missing our.
jnthn authored
287 our sub official {
1c55bcb [t/spec] merge return2.t into return.t
moritz authored
288 Foo.new.userdefinedcontrol1({ return 44 });
289 }
290 }
291
292 #?pugs 3 todo 'return(), blocks and methods'
293 is Foo.new.officialmeth(), 42,
294 "return correctly from official method only";
295 is Foo.new.officialsubmeth(), 43,
296 "return correctly from official submethod only";
297 is Foo::official(), 44,
298 "return correctly from official sub only";
299
3aac6a6 [t/spec] tests for RT #75118, named arguments to return()
moritz authored
300 # RT #75118
6dc8424 Moritz Lenz fudge return.t for rakudo
moritz authored
301 #?rakudo skip 'RT 75118'
3aac6a6 [t/spec] tests for RT #75118, named arguments to return()
moritz authored
302 {
303 sub named() {
304 return 1, 2, :c(3);
305 }
306 is named().elems, 3, 'return with named arguments';
307 is named().[2].key, 'c', ' ... correct key';
308 #?rakudo todo 'named argument to return()'
309 is named().[2].value, '3', ' ... correct value';
310 }
311
ce08eb0 [t/spec] Test for RT #61732
kyle authored
312 # RT #61732
313 {
314 sub rt61732_c { 1; CATCH {} }
6dc8424 Moritz Lenz fudge return.t for rakudo
moritz authored
315 #?rakudo skip 'RT 61732'
ea4d235 [t/spec] begin cleansing of eval and todos that should skip
kyle authored
316 is rt61732_c(), 1, 'sub with empty catch block returns value before block';
ce08eb0 [t/spec] Test for RT #61732
kyle authored
317 }
318
ea4d235 [t/spec] begin cleansing of eval and todos that should skip
kyle authored
319 {
ce08eb0 [t/spec] Test for RT #61732
kyle authored
320 sub rt61732_d { 1;; }
ea4d235 [t/spec] begin cleansing of eval and todos that should skip
kyle authored
321 is rt61732_d(), 1, 'get right value from sub with double ;';
ce08eb0 [t/spec] Test for RT #61732
kyle authored
322 }
323
cd6d374 [t/spec] Test for RT #63912
kyle authored
324 # RT #63912
325 {
326 sub rt63912 { return 1, 2; }
327 lives_ok { rt63912() }, 'can call sub that returns two things (no parens)';
328 }
329
1d82de7 [t/spec] tests for # RT #72836 Null PMC access when using value from emp...
bbkr authored
330 # RT #72836
331 {
332 class RT72836 {
333 method new() { }
334 }
335 lives_ok {my $c = RT72836.new},
336 'can use value returned from empty routine';
337 }
338
1138a24 Tadeusz Sośnierz Tests for RT #61126
tadzik authored
339 # RT #61126
340 {
341 sub bar61126($code) { $code() }; sub foo61126 { bar61126 { return 1 }; return 2; };
342 is foo61126, 1;
343
2d28aa2 Tadeusz Sośnierz Fix tests for RT #61126, jnthn++
tadzik authored
344 sub baz61126 { eager map { return 1 }, 1; return 2 };
1138a24 Tadeusz Sośnierz Tests for RT #61126
tadzik authored
345 is baz61126, 1;
346
347 }
348
2a8608d [t] move sub_return_values.t to spec, corrected isa_ok calls to use type...
moritz authored
349 # vim: ft=perl6
Something went wrong with that request. Please try again.