Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 109 lines (89 sloc) 2.611 kb
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
1 use v6;
2
3 use Test;
4
5 plan 21;
6
4e5f604 [t/spec] fix some smartlinks
moritz authored
7 # L<S04/"Phasers"/START "runs separately for each clone">
2d14223 [t/spec] Unskip term:START tests, and s/skip/todo/ for a soft failure.
jnthn authored
8 #?rakudo todo '$_ inside START has some issues, it seems'
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
9 {
297e783 [t] and [t/spec]
moritz authored
10 is(eval(q{{
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
11 my $str;
12 for 1..2 {
13 my $sub = {
14 START { $str ~= $_ };
15 };
16 $sub();
17 $sub();
18 }
19 $str;
297e783 [t] and [t/spec]
moritz authored
20 }}), '12');
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
21 };
22
4e5f604 [t/spec] fix some smartlinks
moritz authored
23 # L<S04/"Phasers"/START "puts off" initialization till
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
24 # "last possible moment">
25 {
26 my $var;
27 my $sub = sub ($x) { START { $var += $x } };
28
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
29 ok $var.notdef, 'START {...} has not run yet';
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
30
31 $sub(2);
32 is $var, 2, 'START {} has executed';
33
34 $sub(3);
35 is $var, 2, "START {} only runs once for each clone";
36 }
37
4e5f604 [t/spec] fix some smartlinks
moritz authored
38 # L<S04/"Phasers"/START "on first ever execution">
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
39 {
40 my $str ~= 'o';
41 {
42 START { $str ~= 'i' }
43 }
44 is $str, 'oi', 'START {} runs when we first try to use a block';
45 }
46
4e5f604 [t/spec] fix some smartlinks
moritz authored
47 # L<S04/"Phasers"/START "executes inline">
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
48
49 # Execute the tests twice to make sure that START binds to
50 # the lexical scope, not the lexical position.
51 for <first second> {
52 my $sub = {
53 my $str = 'o';
54 START { $str ~= 'I' };
55 START { $str ~= 'i' };
56 ":$str";
57 };
58
59 is $sub(), ':oIi', "START block set \$str to 3 ($_ time)";
60 is $sub(), ':o', "START wasn't invoked again (1-1) ($_ time)";
61 is $sub(), ':o', "START wasn't invoked again (1-2) ($_ time)";
62 }
63
64 # Some behavior occurs where START does not close over the correct
65 # pad when closures are cloned
66
67 my $ran;
68 for <first second> {
69 my $str = 'bana';
70 $ran = 0;
71 my $sub = {
72 START { $ran++; $str ~= 'na' };
73 };
74
75 $sub(); $sub();
76 is $ran, 1, "START block ran exactly once ($_ time)";
77 is $str, 'banana', "START block modified the correct variable ($_ time)";
78 }
79
4e5f604 [t/spec] fix some smartlinks
moritz authored
80 # L<S04/"Phasers"/START "caches its value for all subsequent calls">
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
81 {
82 my $was_in_start;
83 my $sub = {
84 my $var = START { $was_in_start++; 23 };
85 $var //= 42;
86 $var;
87 };
88
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
89 ok $was_in_start.notdef, 'START {} has not run yet';
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
90 is $sub(), 23, 'START {} block set our variable (2)';
91 is $sub(), 23, 'the returned value of START {} still there';
92 is $was_in_start, 1, 'our START {} block was invoked exactly once';
93 }
94
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
95 # Test that START {} blocks are executed only once even if they return undefined
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
96 # (the first implementation ran them twice instead).
97 {
98 my $was_in_start;
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
99 my $sub = { START { $was_in_start++; Mu } };
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
100
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
101 ok $sub().notdef, 'START {} returned undefined';
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
102 $sub();
103 $sub();
104 is $was_in_start, 1,
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
105 'our START { ...; Mu } block was invoked exactly once';
3766e25 [t] moved some closure_traits/* tests to spec/, some small simplificatio...
moritz authored
106 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
107
108 # vim: ft=perl6
Something went wrong with that request. Please try again.