Permalink
Newer
Older
100644 111 lines (82 sloc) 2.84 KB
1
use v6;
2
3
use Test;
4
6
7
=begin description
8
9
Test pointy sub behaviour described in S06
10
11
=end description
12
13
# L<S06/""Pointy blocks""/"parameter list of a pointy block does not require
14
# parentheses">
15
my ($sub, $got);
16
17
$got = '';
18
$sub = -> $x { $got = "x $x" };
19
$sub.(123);
20
is $got, 'x 123', 'pointy sub without param parens';
21
22
$got = '';
23
-> $x { $got = "x $x" }.(123);
24
is $got, 'x 123', 'called pointy immediately: -> $x { ... }.(...)';
25
26
$got = '';
27
-> $x { $got = "x $x" }(123);
28
is $got, 'x 123', 'called pointy immediately: -> $x { ... }(...)';
29
30
31
# L<S04/Statement-ending blocks/End-of-statement cannot occur within a bracketed expression>
32
my @a;
moritz
Jan 5, 2009
33
lives_ok { @a = ("one", -> $x { $x**2 }, "three")} ,
34
'pointy sub without preceding comma';
35
is @a[0], 'one', 'pointy sub in list previous argument';
moritz
Dec 25, 2008
36
isa_ok @a[1], Code, 'pointy sub in list';
37
is @a[2], 'three', 'pointy sub in list following argument';
38
39
40
# L<S06/""Pointy blocks""/behaves like a block with respect to control exceptions>
41
my $n = 1;
42
my $s = -> {
43
last if $n == 10;
44
$n++;
45
redo if $n < 10;
46
};
47
try { $s.() };
48
#?pugs todo 'feature'
moritz
Jan 5, 2009
49
ok(!defined($!), 'pointy with block control exceptions');
Oct 24, 2011
50
#?rakudo todo 'pointy blocks and last/redo'
51
is $n, 10, "pointy control exceptions ran";
52
53
# L<S06/""Pointy blocks""/will return from the innermost enclosing sub or method>
54
my $str = '';
55
56
sub outer {
57
my $s = -> {
moritz
Jan 5, 2009
58
#?rakudo skip '&?ROUTINE'
59
is(&?ROUTINE.name, '&Main::outer', 'pointy still sees outer\'s &?ROUTINE');
60
61
$str ~= 'inner';
62
return 'inner ret';
63
};
64
$s.();
65
$str ~= 'outer';
66
return 'outer ret';
67
}
68
69
is outer(), 'inner ret', 'return in pointy returns from enclosing sub';
70
is $str, 'inner', 'return in pointy returns from enclosing sub';
71
72
# What about nested pointies -> { ... -> {} }?
73
74
75
# L<S06/""Pointy blocks""/It is referenced>
76
# Coming soon...
77
78
79
# -> { $^a, $^b } is illegal; you can't mix real sigs with placeholders,
80
# and the -> introduces a sig of (). TimToady #perl6 2008-May-24
moritz
Jan 5, 2009
81
eval_dies_ok(q{{ -> { $^a, $^b } }}, '-> { $^a, $^b } is illegal');
83
# RT #61034
84
85
lives_ok {my $x = -> {}; my $y = $x(); },
86
'can define and execute empty pointy block';
88
# The default type of pointy blocks is Mu, not Any. See
89
# http://www.nntp.perl.org/group/perl.perl6.language/2009/03/msg31181.html
90
# L<S02/Undefined types/default block parameter type>
91
# this means that junctions don't autothread over pointy blocks
93
#?rakudo skip 'Could not find non-existent sub junction'
94
{
95
my @a = any(3, 4);
96
my $ok = 0;
97
my $iterations = 0;
98
for @a -> $x {
102
ok $ok, 'Blocks receive junctions without autothreading';
103
is $iterations, 1, 'no autothreading happened';
104
my $b = -> $x { ... };
105
ok $b.signature.perl !~~ /Any/,
106
'The .signature of a block does not contain Any';
107
}
108
109
# vim: ft=perl6