@@ -4,61 +4,62 @@ use v6;
4
4
5
5
use Test ;
6
6
7
+ plan 35 ;
7
8
8
9
# L<S04/Phasers/Code "generated at run time" "still fire off"
9
10
# "can't" "travel back in time" >
10
-
11
- my ($ handle );
12
-
13
- our $ h ;
14
-
15
11
{
16
12
my $ h ;
13
+ my $ handle ;
17
14
18
- eval ' $handle = { $h ~= "1"; START { $h ~= "F" }; $h ~= "2" }' ;
19
- ok $! ! ~~ Exception , ' eval START {...} works' ;
15
+ eval ' $handle = { $h ~= "1"; once { $h ~= "F" }; $h ~= "2" }' ;
16
+ ok $! ! ~~ Exception , ' eval once {...} works' ;
20
17
21
- nok $ h . defined , ' START {...} has not run yet' ;
22
- lives_ok { $ handle () }, ' can run code with START block' ;
23
- is $ h , ' 1F2' , ' START {...} fired' ;
24
- lives_ok { $ handle () }, ' can run code with START block again' ;
25
- is $ h , ' 1F212' , ' START {...} fired only once' ;
18
+ nok $ h . defined , ' once {...} has not run yet' ;
19
+ lives_ok { $ handle () }, ' can run code with once block' ;
20
+ is $ h , ' 1F2' , ' once {...} fired' ;
21
+ lives_ok { $ handle () }, ' can run code with once block again' ;
22
+ is $ h , ' 1F212' , ' once {...} fired only once' ;
26
23
27
24
# test that it runs again for a clone of $handle
28
25
$ h = ' ' ;
29
- my $ start_clone = $ handle . clone ;
26
+ my $ clone = $ handle . clone ;
30
27
is $ h , ' ' , ' cloning code does not run anything' ;
31
- lives_ok { $ start_clone () }, ' can run clone of code with START block' ;
32
- # ?rakudo todo 'clone of code with START should run START again'
33
- is $ h , ' 1F2' , ' START {...} fired again for the clone' ;
34
- lives_ok { $ start_clone () }, ' can run clone of START block code again' ;
35
- # ?rakudo todo 'clone of code with START should not run START again'
36
- is $ h , ' 1F212' , ' cloned START {...} fired only once' ;
28
+ lives_ok { $ clone () }, ' can run clone of code with once block' ;
29
+ is $ h , ' 1F2' , ' once {...} fired again for the clone' ;
30
+ lives_ok { $ clone () }, ' can run clone of once block code again' ;
31
+ is $ h , ' 1F212' , ' cloned once {...} fired only once' ;
37
32
}
38
33
39
34
{
40
35
my $ h ;
36
+ my $ handle ;
41
37
42
- eval ' $handle = { $h =~ "r"; INIT { $h ~= "I" }; $h ~= "R" }' ;
38
+ eval ' $handle = { $h ~= "r"; INIT { $h ~= "I" }; $h ~= "R" }' ;
43
39
ok $! ! ~~ Exception , ' eval INIT {...} works' ;
40
+ # ?rakudo todo 'not sure'
44
41
nok $ h . defined , ' INIT did not run at compile time' ;
45
- # ?rakudo 4 todo 'Could not find non-existent sub INIT'
46
42
lives_ok { $ handle () }, ' can run code with INIT block' ;
47
43
is $ h , ' IrR' , ' INIT {...} fires at run-time' ;
48
44
lives_ok { $ handle () }, ' can run code with INIT block again' ;
49
45
is $ h , ' IrRrR' , ' INIT runs only once' ;
50
46
51
- # TODO: test that it does not run again for a clone of $handle (?)
47
+ # test that it runs again for a clone of $handle
48
+ $ h = ' ' ;
49
+ my $ clone = $ handle . clone ;
50
+ is $ h , ' ' , ' cloning code does not run anything' ;
51
+ lives_ok { $ clone () }, ' can run clone of code with INIT block' ;
52
+ is $ h , ' rR' , ' INIT {...} did not fire again for the clone' ;
52
53
}
53
54
54
55
{
55
- our $ h = Mu ;
56
+ my $ h ;
57
+ my $ handle ;
56
58
57
- eval ' $handle = { our $h ~= "1"; CHECK { our $h ~= "C" };'
58
- ~ ' our $h ~= "2"; BEGIN { our $h ~= "B" }; our $h ~= "3" }' ;
59
+ eval ' $handle = { $h ~= "1"; CHECK { $h ~= "C" };'
60
+ ~ ' $h ~= "2"; BEGIN { $h ~= "B" }; $h ~= "3" }' ;
59
61
ok $! ! ~~ Exception , ' eval CHECK {...} (and BEGIN {...}) works' ;
60
62
61
- # ?rakudo 5 todo 'Could not find non-existent sub CHECK'
62
63
is $ h , ' BC' , ' CHECK and BEGIN blocks ran before run time' ;
63
64
lives_ok { $ handle () }, ' can run code with CHECK and BEGIN blocks' ;
64
65
is $ h , ' BC123' , ' CHECK {...} runs at compile time after BEGIN' ;
@@ -67,32 +68,31 @@ our $h;
67
68
}
68
69
69
70
{
70
- our $ h = Mu ;
71
+ my $ h ;
72
+ my $ handle ;
71
73
72
- eval ' $handle = { our $h ~= "1"; BEGIN { our $h ~= "B" }; our $h ~= "2" }' ;
74
+ eval ' $handle = { $h ~= "1"; BEGIN { $h ~= "B" }; $h ~= "2" }' ;
73
75
ok $! ! ~~ Exception , ' eval BEGIN {...} works' ;
74
76
75
77
is $ h , ' B' , ' BEGIN ran before run time' ;
76
78
lives_ok { $ handle () }, ' can run code with BEGIN block' ;
77
79
is $ h , ' B12' , ' BEGIN does not run again at run time' ;
78
80
}
79
81
80
- # ?rakudo skip 'test harness does not see test result in END'
81
82
{
83
+ my $ h = ' ' ;
84
+ my $ handle ;
85
+
82
86
END {
83
- is our $ end , ' 12E' , ' the END {...} in eval has run already' ;
87
+ is $ h , ' 12E' , ' the END {...} in eval has run already' ;
84
88
}
85
- }
86
-
87
- {
88
- our $ end = Mu ;
89
89
90
- eval ' $handle = { our $end ~= "1"; END { our $end ~= "E" }; our $end ~= "2" }' ;
90
+ eval ' $handle = { $h ~= "1"; END { $h ~= "E" }; $h ~= "2" }' ;
91
91
ok $! ! ~~ Exception , ' eval END {...} works' ;
92
92
93
- nok $ end . defined , ' END {} has not run yet' ;
93
+ is $ h , ' ' , ' END {} has not run yet' ;
94
94
lives_ok { $ handle () }, ' can call code with END block' ;
95
- is $ end , ' 12' , ' END {} does not run at run time either' ;
95
+ is $ h , ' 12' , ' END {} does not run at run time either' ;
96
96
}
97
97
98
98
done ;
0 commit comments