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