This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
stab at UNITCHECK blocks
[perl5.git] / t / op / blocks.t
CommitLineData
3c10abe3
AG
1#!./perl
2
3BEGIN {
4 chdir 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
9plan tests => 3;
10
11my @expect = qw(
12b1
13b2
14b3
15b4
16b6
17u5
18b7
19u6
20u1
21c3
22c2
23c1
24i1
25i2
26b5
27u2
28u3
29u4
30e2
31e1
32 );
33my $expect = ":" . join(":", @expect);
34
35fresh_perl_is(<<'SCRIPT', $expect,{switches => [''], stdin => '', stderr => 1 },'Order of execution of special blocks');
36BEGIN {print ":b1"}
37END {print ":e1"}
38BEGIN {print ":b2"}
39{
40 BEGIN {BEGIN {print ":b3"}; print ":b4"}
41}
42CHECK {print ":c1"}
43INIT {print ":i1"}
44UNITCHECK {print ":u1"}
45eval 'BEGIN {print ":b5"}';
46eval 'UNITCHECK {print ":u2"}';
47eval 'UNITCHECK {print ":u3"; UNITCHECK {print ":u4"}}';
48"a" =~ /(?{UNITCHECK {print ":u5"};
49 CHECK {print ":c2"};
50 BEGIN {print ":b6"}})/x;
51eval {BEGIN {print ":b7"}};
52eval {UNITCHECK {print ":u6"}};
53eval {INIT {print ":i2"}};
54eval {CHECK {print ":c3"}};
55END {print ":e2"}
56SCRIPT
57
58@expect =(
59# BEGIN
60qw( main bar myfoo foo ),
61# UNITCHECK
62qw( foo myfoo bar main ),
63# CHECK
64qw( foo myfoo bar main ),
65# INIT
66qw( main bar myfoo foo ),
67# END
68qw(foo myfoo bar main ));
69
70$expect = ":" . join(":", @expect);
71fresh_perl_is(<<'SCRIPT2', $expect,{switches => [''], stdin => '', stderr => 1 },'blocks interact with packages/scopes');
72BEGIN {$f = 'main'; print ":$f"}
73UNITCHECK {print ":$f"}
74CHECK {print ":$f"}
75INIT {print ":$f"}
76END {print ":$f"}
77package bar;
78BEGIN {$f = 'bar';print ":$f"}
79UNITCHECK {print ":$f"}
80CHECK {print ":$f"}
81INIT {print ":$f"}
82END {print ":$f"}
83package foo;
84{
85 my $f;
86 BEGIN {$f = 'myfoo'; print ":$f"}
87 UNITCHECK {print ":$f"}
88 CHECK {print ":$f"}
89 INIT {print ":$f"}
90 END {print ":$f"}
91}
92BEGIN {$f = "foo";print ":$f"}
93UNITCHECK {print ":$f"}
94CHECK {print ":$f"}
95INIT {print ":$f"}
96END {print ":$f"}
97SCRIPT2
98
99@expect = qw(begin unitcheck check init end);
100$expect = ":" . join(":", @expect);
101fresh_perl_is(<<'SCRIPT3', $expect,{switches => [''], stdin => '', stderr => 1 },'can name blocks as sub FOO');
102sub BEGIN {print ":begin"}
103sub UNITCHECK {print ":unitcheck"}
104sub CHECK {print ":check"}
105sub INIT {print ":init"}
106sub END {print ":end"}
107SCRIPT3