This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move all the xxxpvs() macros to handy.h.
[perl5.git] / ext / B / t / optree_check.t
1 #!perl
2
3 BEGIN {
4     if ($ENV{PERL_CORE}){
5         chdir('t') if -d 't';
6         @INC = ('.', '../lib', '../ext/B/t');
7     } else {
8         unshift @INC, 't';
9         push @INC, "../../t";
10     }
11     require Config;
12     if (($Config::Config{'extensions'} !~ /\bB\b/) ){
13         print "1..0 # Skip -- Perl configured without B module\n";
14         exit 0;
15     }
16     # require 'test.pl'; # now done by OptreeCheck
17 }
18
19 use OptreeCheck;
20
21 =head1 OptreeCheck selftest harness
22
23 This file is primarily to test services of OptreeCheck itself, ie
24 checkOptree().  %gOpts provides test-state info, it is 'exported' into
25 main::  
26
27 doing use OptreeCheck runs import(), which processes @ARGV to process
28 cmdline args in 'standard' way across all clients of OptreeCheck.
29
30 =cut
31
32 my $tests = 5 + 15 + 16 * $gOpts{selftest};     # pass()s + $#tests
33 plan tests => $tests;
34
35 SKIP: {
36     skip "no perlio in this build", $tests
37     unless $Config::Config{useperlio};
38
39
40 pass("REGEX TEST HARNESS SELFTEST");
41
42 checkOptree ( name      => "bare minimum opcode search",
43               bcopts    => '-exec',
44               code      => sub {my $a},
45               noanchors => 1, # unanchored match
46               expect    => 'leavesub',
47               expect_nt => 'leavesub');
48
49 checkOptree ( name      => "found print opcode",
50               bcopts    => '-exec',
51               code      => sub {print 1},
52               noanchors => 1, # unanchored match
53               expect    => 'print',
54               expect_nt => 'leavesub');
55
56 checkOptree ( name      => 'test skip itself',
57               skip      => 'this is skip-reason',
58               bcopts    => '-exec',
59               code      => sub {print 1},
60               expect    => 'dont-care, skipping',
61               expect_nt => 'this insures failure');
62
63 # This test 'unexpectedly succeeds', but that is "expected".  Theres
64 # no good way to expect a successful todo, and inducing a failure
65 # causes the harness to print verbose errors, which is NOT helpful.
66
67 checkOptree ( name      => 'test todo itself',
68               todo      => "your excuse here ;-)",
69               bcopts    => '-exec',
70               code      => sub {print 1},
71               noanchors => 1, # unanchored match
72               expect    => 'print',
73               expect_nt => 'print') if 0;
74
75 checkOptree ( name      => 'impossible match, remove skip to see failure',
76               todo      => "see! it breaks!",
77               skip      => 'skip the failure',
78               code      => sub {print 1},
79               expect    => 'look out ! Boy Wonder',
80               expect_nt => 'holy near earth asteroid Batman !');
81
82 pass ("TEST FATAL ERRS");
83
84 if (1) {
85     # test for fatal errors. Im unsettled on fail vs die.
86     # calling fail isnt good enough by itself.
87
88     $@='';
89     eval {
90         checkOptree ( name      => 'test against empty expectations',
91                       bcopts    => '-exec',
92                       code      => sub {print 1},
93                       expect    => '',
94                       expect_nt => '');
95     };
96     like($@, /no '\w+' golden-sample found/, "empty expectations prevented");
97     
98     $@='';
99     eval {
100         checkOptree ( name      => 'prevent whitespace only expectations',
101                       bcopts    => '-exec',
102                       code      => sub {my $a},
103                       #skip     => 1,
104                       expect_nt => "\n",
105                       expect    => "\n");
106     };
107     like($@, /no '\w+' golden-sample found/,
108          "just whitespace expectations prevented");
109 }
110     
111 pass ("TEST -e \$srcCode");
112
113 checkOptree ( name      => 'empty code or prog',
114               skip      => 'or fails',
115               todo      => "your excuse here ;-)",
116               code      => '',
117               prog      => '',
118               );
119     
120 checkOptree
121     (  name     => "self strict, catch err",
122        prog     => 'use strict; bogus',
123        errs     => 'Bareword "bogus" not allowed while "strict subs" in use at -e line 1.',
124        expect   => "nextstate", # simple expectations
125        expect_nt => "nextstate",
126        noanchors => 1,          # allow them to work
127        );
128     
129 checkOptree ( name      => "sort lK - flag specific search",
130               prog      => 'our (@a,@b); @b = sort @a',
131               noanchors => 1,
132               expect    => '<@> sort lK ',
133               expect_nt => '<@> sort lK ');
134
135 checkOptree ( name      => "sort vK - flag specific search",
136               prog      => 'sort our @a',
137               errs      => 'Useless use of sort in void context at -e line 1.',
138               noanchors => 1,
139               expect    => '<@> sort vK',
140               expect_nt => '<@> sort vK');
141
142 checkOptree ( name      => "'code' => 'sort our \@a'",
143               code      => 'sort our @a',
144               noanchors => 1,
145               expect    => '<@> sort K',
146               expect_nt => '<@> sort K');
147
148 pass ("REFTEXT FIXUP TESTS");
149
150 checkOptree ( name      => 'fixup nextstate (in reftext)',
151               bcopts    => '-exec',
152               code      => sub {my $a},
153               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
154 # 1  <;> nextstate( NOTE THAT THIS CAN BE ANYTHING ) v
155 # 2  <0> padsv[$a:54,55] M/LVINTRO
156 # 3  <1> leavesub[1 ref] K/REFC,1
157 EOT_EOT
158 # 1  <;> nextstate(main 54 optree_concise.t:84) v
159 # 2  <0> padsv[$a:54,55] M/LVINTRO
160 # 3  <1> leavesub[1 ref] K/REFC,1
161 EONT_EONT
162
163 checkOptree ( name      => 'fixup opcode args',
164               bcopts    => '-exec',
165               #fail     => 1, # uncomment to see real padsv args: [$a:491,492] 
166               code      => sub {my $a},
167               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
168 # 1  <;> nextstate(main 56 optree_concise.t:96) v
169 # 2  <0> padsv[$a:56,57] M/LVINTRO
170 # 3  <1> leavesub[1 ref] K/REFC,1
171 EOT_EOT
172 # 1  <;> nextstate(main 56 optree_concise.t:96) v
173 # 2  <0> padsv[$a:56,57] M/LVINTRO
174 # 3  <1> leavesub[1 ref] K/REFC,1
175 EONT_EONT
176
177 #################################
178 pass("CANONICAL B::Concise EXAMPLE");
179
180 checkOptree ( name      => 'canonical example w -basic',
181               bcopts    => '-basic',
182               code      =>  sub{$a=$b+42},
183               crossfail => 1,
184               debug     => 1,
185               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
186 # 7  <1> leavesub[1 ref] K/REFC,1 ->(end)
187 # -     <@> lineseq KP ->7
188 # 1        <;> nextstate(main 380 optree_selftest.t:139) v ->2
189 # 6        <2> sassign sKS/2 ->7
190 # 4           <2> add[t3] sK/2 ->5
191 # -              <1> ex-rv2sv sK/1 ->3
192 # 2                 <#> gvsv[*b] s ->3
193 # 3              <$> const[IV 42] s ->4
194 # -           <1> ex-rv2sv sKRM*/1 ->6
195 # 5              <#> gvsv[*a] s ->6
196 EOT_EOT
197 # 7  <1> leavesub[1 ref] K/REFC,1 ->(end)
198 # -     <@> lineseq KP ->7
199 # 1        <;> nextstate(main 60 optree_concise.t:122) v ->2
200 # 6        <2> sassign sKS/2 ->7
201 # 4           <2> add[t1] sK/2 ->5
202 # -              <1> ex-rv2sv sK/1 ->3
203 # 2                 <$> gvsv(*b) s ->3
204 # 3              <$> const(IV 42) s ->4
205 # -           <1> ex-rv2sv sKRM*/1 ->6
206 # 5              <$> gvsv(*a) s ->6
207 EONT_EONT
208
209 checkOptree ( code      => '$a=$b+42',
210               bcopts    => '-exec',
211               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
212 # 1  <;> nextstate(main 61 optree_concise.t:139) v
213 # 2  <#> gvsv[*b] s
214 # 3  <$> const[IV 42] s
215 # 4  <2> add[t3] sK/2
216 # 5  <#> gvsv[*a] s
217 # 6  <2> sassign sKS/2
218 # 7  <1> leavesub[1 ref] K/REFC,1
219 EOT_EOT
220 # 1  <;> nextstate(main 61 optree_concise.t:139) v
221 # 2  <$> gvsv(*b) s
222 # 3  <$> const(IV 42) s
223 # 4  <2> add[t1] sK/2
224 # 5  <$> gvsv(*a) s
225 # 6  <2> sassign sKS/2
226 # 7  <1> leavesub[1 ref] K/REFC,1
227 EONT_EONT
228
229 } # skip
230
231 __END__
232