/
return.t
71 lines (55 loc) 路 2.13 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
use v6;
use Test;
plan 17;
# Is there a better reference for the spec for how return return works?
# There is "return function" but that's a more advanced feature.
#L<S04/"Control Exceptions">
=begin pod
Basic tests for "return"
=end pod
sub bar { return }
is(bar(), Nil, '... bare return statement returned Nil');
sub bar2 { return() }
is(bar2(), Nil, '... bare return statement w/ parens returned Nil');
sub baz { return 10 if 1; }
is(baz(), 10, '... return worked with a statement modifier');
sub foobar { return if 1; };
is(foobar(), Nil, '... bare return worked with a statement modifier');
sub foobar2 { return() if 1; }
is(foobar2(), Nil, '... bare return worked with a statement modifier');
my $should_ret_empty_list1 = sub { return; 1 };
is $should_ret_empty_list1().elems, 0, "our sub returned an empty list (1)";
sub return_1 { return 1; }
is(return_1(), 1, '... return_1() returned 1 correctly');
is( try { sub foo { my $x = 1; while $x-- { return 24; }; return 42; }; foo() }, 24, 'return in while');
# S04: "A return always exits from the lexically surrounding sub or method definition"
{
eval_dies_ok('return 1', 'bare return fails');
eval_dies_ok('for 1 {return 2}', 'cannot return out of a bare for block');
eval_dies_ok('my $i = 1; while $i-- {return 3}', 'cannot return out of a bare while');
eval_dies_ok('my $i = 0; until $i++ {return 4}', 'cannot return out of a bare until');
eval_dies_ok('loop (my $i = 0; $i < 1; $i++) {return 5}', 'cannot return out of a bare loop');
# XXX: Not 100% sure on this one
eval_dies_ok('do {return 5}', 'cannot return out of a do block');
}
{
# In an ancient version of pugs the sub below didn't return anything
sub list_return { return (42, 1) }
my $bar = ~list_return();
is($bar, '42 1', 'Should not return empty string');
}
# RT #81962
{
my $tracker = '';
my &r = &return;
sub f {
my &return := -> $v {
$tracker ~= 'LOL';
&r($v * 2);
};
return(21);
}
is f(), 42, 'proxied return produces the correct value';
is $tracker, 'LOL', 'proxied return produced the right side effect';
}
# vim: ft=perl6