Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 159 lines (129 sloc) 4.736 kB
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
1 use v6;
2
3 use Test;
4
2098af2 [spec] S02-builtin_data_types/*
moritz authored
5 =begin description
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
6
7 Block tests
8
9 This covers anonymous blocks and subs, as well as pointy blocks
189b4db [t/spec] cosmetic cleaning: remove trailing space
wollmers authored
10 (with and without args) and bare blocks.
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
11
2098af2 [spec] S02-builtin_data_types/*
moritz authored
12 =end description
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
13
53e37b5 [t/spec] Test for RT #68116
kyle authored
14 plan 42;
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
15
8268066 [gsoc_spectest] simple smartlink cleanup
Auzon authored
16 # L<S04/"The Relationship of Blocks and Declarations">
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
17 # L<S06/"Anonymous subroutines">
18 # anon blocks
19 my $anon_sub = sub { 1 };
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
20 isa_ok($anon_sub, Sub);
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
21 is($anon_sub(), 1, 'sub { } works');
22
23 my $anon_sub_w_arg = sub ($arg) { 1 + $arg };
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
24 isa_ok($anon_sub_w_arg, Sub);
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
25 is($anon_sub_w_arg(3), 4, 'sub ($arg) {} works');
26
27 # L<S06/"Blocks">
28 # anon blocks
29 my $anon_block = { 1 };
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
30 isa_ok($anon_block, Block);
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
31 is($anon_block(), 1, '{} <anon block> works');
32
8efee97 [t/spec] Tests for RT #64844
kyle authored
33 # RT #64844
cb88c13 @colomon Fudge for niecza.
colomon authored
34 #?niecza skip "Exception NYI"
b1c8167 @coke pugs fudge
coke authored
35 #?pugs skip "Exception NYI"
8efee97 [t/spec] Tests for RT #64844
kyle authored
36 {
37 eval '$anon_block( 1 )';
38 #?rakudo todo 'Parrot support for zero-arg subs?'
39 ok $! ~~ Exception, 'too many parameters';
40
41 if $! !~~ Exception {
8b5a87e @moritz reverse order of skip() arguments all over the test suite
moritz authored
42 skip q{tests don't work if previous test fails}, 2;
8efee97 [t/spec] Tests for RT #64844
kyle authored
43 }
44 else {
45 my $errmsg = ~$!;
46
47 eval '$anon_block( foo => "RT #64844" )';
48 ok $! ~~ Exception, 'too many parameters';
49 is ~$!, $errmsg, 'same error for named param as positional';
50 }
51 }
52
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
53 # L<S06/""Pointy blocks"">
189b4db [t/spec] cosmetic cleaning: remove trailing space
wollmers authored
54 {
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
55 # pointy subs
56 my $pointy_block = -> { 1 };
207f13f [t] and [t/spec]
moritz authored
57 isa_ok($pointy_block, Block);
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
58 is($pointy_block(), 1, '-> {} <"pointy" block> works');
59
60 my $pointy_block_w_arg = -> $arg { 1 + $arg };
207f13f [t] and [t/spec]
moritz authored
61 isa_ok($pointy_block_w_arg, Block);
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
62 is($pointy_block_w_arg(3), 4, '-> $arg {} <"pointy" block w/args> works');
63
64 my $pointy_block_w_multiple_args = -> $arg1, $arg2 { $arg1 + $arg2 };
207f13f [t] and [t/spec]
moritz authored
65 isa_ok($pointy_block_w_multiple_args, Block);
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
66 is($pointy_block_w_multiple_args(3, 4), 7, '-> $arg1, $arg2 {} <"pointy" block w/multiple args> works');
67
68 my $pointy_block_nested = -> $a { -> $b { $a + $b }};
69 isa_ok($pointy_block_nested, Block);
70 isa_ok($pointy_block_nested(5), Block);
71 is $pointy_block_nested(5)(6), 11, '-> $a { -> $b { $a+$b }} nested <"pointy" block> works';
72 }
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
73
74 # L<S06/"Blocks">
75 # bare blocks
76
77 my $foo;
78 {$foo = "blah"};
79 is($foo, "blah", "lone block actually executes it's content");
80
81 my $foo2;
82 {$foo2 = "blah"};
83 is($foo2, "blah", "lone block w/out a semicolon actually executes it's content");
84
85 my $foo4;
86 ({$foo4 = "blah"},);
87 ok(!defined($foo4), "block enclosed by parentheses should not auto-execute (2)");
88
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
89 my $one;
90 my $two;
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
91 # The try's here because it should die: $foo{...} should only work if $foo isa
92 # Hash (or sth. which provides appropriate tieing/&postcircumfix:<{
93 # }>/whatever, but a Code should surely not support hash access).
94 # Additionally, a smart compiler will detect thus errors at compile-time, so I
95 # added an eval(). --iblech
96 try { eval '0,{$one = 1}{$two = 2}' };
8c8fd13 [t/spec] many test fixes, mostly related to undef
moritz authored
97 ok(!defined($one), 'two blocks ({} {}) no semicolon after either,.. first block does not execute');
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
98 is($two, 2, '... but second block does (parsed as hash subscript)');
99
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
100 my $one_a;
101 my $two_a;
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
102 {$one_a = 1}; {$two_a = 2}
103 is($one_a, 1, '... two blocks ({}; {}) semicolon after the first only,.. first block does execute');
104 is($two_a, 2, '... and second block does too');
105
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
106 my $one_b;
107 my $two_b;
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
108 {
109 $one_b = 1
110 }
111 {
112 $two_b = 2
113 };
114 is($one_b, 1, '... two stand-alone blocks ({\n...\n}\n{\n...\n}),.. first block does execute');
115 is($two_b, 2, '... and second block does too');
116
63c4f60 [spec] more fudging in S02-builtin_data_types/ - some if it successful
moritz authored
117 my $one_c;
118 my $two_c;
0ca4c11 [gsoc_spectest] t/data_types/* -> t/spec/S02-builtin_data_types/
Auzon authored
119 {$one_c = 1}; {$two_c = 2};
120 is($one_c, 1, '... two blocks ({}; {};) semicolon after both,.. first block does execute');
121 is($two_c, 2, '... and second block does too');
122
123 sub f { { 3 } }
124 is(f(), 3, 'bare blocks immediately runs even as the last statement');
125 is((sub { { 3 } }).(), 3, 'ditto for anonymous subs');
126 is((sub { { { 3 } } }).(), 3, 'ditto, even if nested');
127 dies_ok({(sub { { $^x } }).()}, 'implicit params become errors');
128 isnt((sub { -> { 3 } }).(), 3, 'as are pointies');
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
129
53e37b5 [t/spec] Test for RT #68116
kyle authored
130 # RT #68116
131 {
132 sub rt68116 { 68116 }
133 is &rt68116(), 68116, 'check that sub is callable via &';
b1c8167 @coke pugs fudge
coke authored
134 #?pugs 2 skip "is multi"
53e37b5 [t/spec] Test for RT #68116
kyle authored
135 is { &^x() }.( &rt68116 ), 68116,
136 'call via { &^pos() }( &s ) works for sub';
137 is -> &x { &x() }.( &rt68116 ), 68116,
138 'call via -> &point { &point() }.( &s ) works for sub';
139 is (sub (&x) { &x() }).( &rt68116 ), 68116,
140 'call via (sub (&x) { &x() }).( &s ) works for sub';
cb88c13 @colomon Fudge for niecza.
colomon authored
141 }
53e37b5 [t/spec] Test for RT #68116
kyle authored
142
cb88c13 @colomon Fudge for niecza.
colomon authored
143 #?niecza skip 'No candidates for dispatch to mone'
b1c8167 @coke pugs fudge
coke authored
144 #?pugs skip 'parsefail'
cb88c13 @colomon Fudge for niecza.
colomon authored
145 {
7fa87bc @pmichaud Update use of multi to include proto declaration.
pmichaud authored
146 proto mone(|$) { * }
53e37b5 [t/spec] Test for RT #68116
kyle authored
147 multi mone { 'one' }
148 is &mone(), 'one', 'check that mutli is callable via &';
149 is { &^x() }.( &mone ), 'one',
150 'call via { &^pos() }( &s ) works for multi';
151 is -> &x { &x() }.( &mone ), 'one',
152 'call via -> &point { &point() }.( &s ) works for multi';
153 is (sub (&x) { &x() }).( &mone ), 'one',
154 'call via (sub (&x) { &x() }).( &s ) works for multi';
155
156 }
157
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
158 # vim: ft=perl6
Something went wrong with that request. Please try again.