Skip to content

HTTPS clone URL

Subversion checkout URL

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