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