This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [perl #38710] localised stash slice
[perl5.git] / t / comp / assertions.t
CommitLineData
cb8340be
SF
1#!./perl
2
e76bdc3c
SP
3BEGIN { $^W=0 }
4
5use base 'assertions::compat';
6
8c63d938
SF
7sub callme ($ ) : assertion {
8 return shift;
9}
10
11# select STDERR; $|=1;
12
13my @expr=( '1' => 1,
14 '0' => 0,
15 '1 && 1' => 1,
16 '1 && 0' => 0,
17 '0 && 1' => 0,
18 '0 && 0' => 0,
19 '1 || 1' => 1,
20 '1 || 0' => 1,
21 '0 || 1' => 1,
22 '0 || 0' => 0,
23 '(1)' => 1,
24 '(0)' => 0,
25 '1 && ((1) && 1)' => 1,
26 '1 && (0 || 1)' => 1,
27 '1 && ( 0' => undef,
28 '1 &&' => undef,
29 '&& 1' => undef,
30 '1 && || 1' => undef,
31 '(1 && 1) && 1)' => undef,
32 'one && two' => 1,
33 '_ && one' => 0,
34 'one && three' => 0,
35 '1 ' => 1,
36 ' 1' => 1,
37 ' 1 ' => 1,
38 ' ( 1 && 1 ) ' => 1,
39 ' ( 1 && 0 ) ' => 0,
40 '(( 1 && 1) && ( 1 || 0)) || _ && one && ( one || three)' => 1 );
41
e76bdc3c
SP
42my $supported = assertions::compat::supported();
43
44my $n=@expr/2 + ($supported ? 10 : 0);
cb8340be 45my $i=1;
8c63d938 46print "1..$n\n";
cb8340be 47
8c63d938
SF
48use assertions::activate 'one', 'two';
49require assertions;
50
51while (@expr) {
52 my $expr=shift @expr;
53 my $expected=shift @expr;
aefc56c5 54 my $result=eval {assertions::_calc_expr($expr)};
8c63d938
SF
55 if (defined $expected) {
56 unless (defined $result and $result == $expected) {
aefc56c5 57 print STDERR "assertions::_calc_expr($expr) failed,".
8c63d938
SF
58 " expected '$expected' but '$result' obtained (\$@=$@)\n";
59 print "not ";
60 }
61 }
62 else {
63 if (defined $result) {
aefc56c5 64 print STDERR "assertions::_calc_expr($expr) failed,".
8c63d938
SF
65 " expected undef but '$result' obtained\n";
66 print "not ";
67 }
68 }
69 print "ok ", $i++, "\n";
cb8340be
SF
70}
71
e76bdc3c 72if ($supported) {
cb8340be 73
e76bdc3c
SP
74 # @expr/2+1
75 if (callme(1)) {
76 print STDERR "assertions called by default\n";
77 print "not ";
78 }
79 print "ok ", $i++, "\n";
80
81 # 2
82 use assertions::activate 'mine';
83 {
84 package mine;
85 use base 'assertions::compat';
86 sub callme ($) : assertion {
87 return shift;
88 }
89 use assertions;
90 unless (callme(1)) {
91 print STDERR "'use assertions;' doesn't active assertions based on package name\n";
92 print "not ";
93 }
94 }
95 print "ok ", $i++, "\n";
96
97 # 3
98 use assertions 'foo';
99 if (callme(1)) {
100 print STDERR "assertion deselection doesn't work\n";
101 print "not ";
102 }
103 print "ok ", $i++, "\n";
104
105 # 4
106 use assertions::activate 'bar', 'doz';
cb8340be 107 use assertions 'bar';
e76bdc3c
SP
108 unless (callme(1)) {
109 print STDERR "assertion selection doesn't work\n";
cb8340be
SF
110 print "not ";
111 }
e76bdc3c
SP
112 print "ok ", $i++, "\n";
113
114 # 5
115 use assertions q(_ && doz);
116 unless (callme(1)) {
117 print STDERR "assertion activation filtering doesn't work\n";
118 print "not ";
119 }
120 print "ok ", $i++, "\n";
121
122 # 6
123 use assertions q(_ && foo);
124 if (callme(1)) {
125 print STDERR "assertion deactivation filtering doesn't work\n";
126 print "not ";
127 }
128 print "ok ", $i++, "\n";
129
130 # 7
131 if (1) {
132 use assertions 'bar';
133 }
134 if (callme(1)) {
135 print STDERR "assertion scoping doesn't work\n";
136 print "not ";
137 }
138 print "ok ", $i++, "\n";
cb8340be 139
e76bdc3c
SP
140 # 8
141 use assertions::activate 're.*';
142 use assertions 'reassert';
143 unless (callme(1)) {
144 print STDERR "assertion selection with re failed\n";
cb8340be
SF
145 print "not ";
146 }
e76bdc3c
SP
147 print "ok ", $i++, "\n";
148
149 # 9
150 my $b=12;
151 {
152 use assertions 'bar';
153 callme(my $b=45);
154 unless ($b == 45) {
155 print STDERR "this shouldn't fail ever (b=$b)\n";
156 print "not ";
157 }
158 }
159 print "ok ", $i++, "\n";
160
161 # 10
162 {
163 no assertions;
164 callme(my $b=46);
165 if (defined $b) {
166 print STDERR "lexical declaration in assertion arg ignored (b=$b\n";
167 print "not ";
168 }
169 }
170 print "ok ", $i++, "\n";
cb8340be 171}