This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
s/PERL_COPY_ON_WRITE/PERL_OLD_COPY_ON_WRITE/g
[perl5.git] / ext / B / t / optree_check.t
CommitLineData
724aa791
JC
1#!perl
2
3BEGIN {
5638aaac
SM
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
NC
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
JC
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
NC
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
JC
37 unless $Config::Config{useperlio};
38
39
724aa791
JC
40pass("REGEX TEST HARNESS SELFTEST");
41
42checkOptree ( name => "bare minimum opcode search",
43 bcopts => '-exec',
44 code => sub {my $a},
cc02ea56 45 noanchors => 1, # unanchored match
724aa791
JC
46 expect => 'leavesub',
47 expect_nt => 'leavesub');
48
49checkOptree ( name => "found print opcode",
50 bcopts => '-exec',
51 code => sub {print 1},
cc02ea56 52 noanchors => 1, # unanchored match
724aa791
JC
53 expect => 'print',
54 expect_nt => 'leavesub');
55
56checkOptree ( name => 'test skip itself',
19e169bf 57 skip => 'this is skip-reason',
724aa791
JC
58 bcopts => '-exec',
59 code => sub {print 1},
60 expect => 'dont-care, skipping',
61 expect_nt => 'this insures failure');
62
181f6ff5
JC
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
724aa791
JC
67checkOptree ( name => 'test todo itself',
68 todo => "your excuse here ;-)",
69 bcopts => '-exec',
70 code => sub {print 1},
cc02ea56 71 noanchors => 1, # unanchored match
724aa791 72 expect => 'print',
19e169bf 73 expect_nt => 'print') if 0;
724aa791
JC
74
75checkOptree ( name => 'impossible match, remove skip to see failure',
76 todo => "see! it breaks!",
19e169bf 77 skip => 'skip the failure',
724aa791
JC
78 code => sub {print 1},
79 expect => 'look out ! Boy Wonder',
80 expect_nt => 'holy near earth asteroid Batman !');
81
82pass ("TEST FATAL ERRS");
83
84if (1) {
85 # test for fatal errors. Im unsettled on fail vs die.
86 # calling fail isnt good enough by itself.
19e169bf 87
724aa791
JC
88 $@='';
89 eval {
90 checkOptree ( name => 'test against empty expectations',
91 bcopts => '-exec',
92 code => sub {print 1},
93 expect => '',
94 expect_nt => '');
95 };
19e169bf 96 like($@, /no '\w+' golden-sample found/, "empty expectations prevented");
724aa791
JC
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 };
19e169bf
JC
107 like($@, /no '\w+' golden-sample found/,
108 "just whitespace expectations prevented");
724aa791 109}
19e169bf 110
724aa791
JC
111pass ("TEST -e \$srcCode");
112
19e169bf
JC
113checkOptree ( name => 'empty code or prog',
114 skip => 'or fails',
115 todo => "your excuse here ;-)",
116 code => '',
117 prog => '',
118 );
5e251bf1
JC
119
120checkOptree
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.',
19e169bf
JC
124 expect => "nextstate", # simple expectations
125 expect_nt => "nextstate",
126 noanchors => 1, # allow them to work
5e251bf1
JC
127 );
128
19e169bf
JC
129checkOptree ( name => "sort lK - flag specific search",
130 prog => 'our (@a,@b); @b = sort @a',
cc02ea56 131 noanchors => 1,
19e169bf
JC
132 expect => '<@> sort lK ',
133 expect_nt => '<@> sort lK ');
724aa791 134
19e169bf 135checkOptree ( name => "sort vK - flag specific search",
724aa791 136 prog => 'sort our @a',
19e169bf 137 errs => 'Useless use of sort in void context at -e line 1.',
cc02ea56 138 noanchors => 1,
724aa791
JC
139 expect => '<@> sort vK',
140 expect_nt => '<@> sort vK');
141
142checkOptree ( name => "'code' => 'sort our \@a'",
143 code => 'sort our @a',
cc02ea56 144 noanchors => 1,
724aa791
JC
145 expect => '<@> sort K',
146 expect_nt => '<@> sort K');
147
148pass ("REFTEXT FIXUP TESTS");
149
150checkOptree ( name => 'fixup nextstate (in reftext)',
151 bcopts => '-exec',
152 code => sub {my $a},
153 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791
JC
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
157EOT_EOT
724aa791
JC
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
161EONT_EONT
162
181f6ff5 163checkOptree ( name => 'fixup opcode args',
724aa791 164 bcopts => '-exec',
181f6ff5 165 #fail => 1, # uncomment to see real padsv args: [$a:491,492]
724aa791 166 code => sub {my $a},
724aa791 167 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791
JC
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
171EOT_EOT
724aa791
JC
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
175EONT_EONT
176
724aa791
JC
177#################################
178pass("CANONICAL B::Concise EXAMPLE");
179
180checkOptree ( 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
196EOT_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
207EONT_EONT
208
19e169bf 209checkOptree ( code => '$a=$b+42',
724aa791 210 bcopts => '-exec',
724aa791 211 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791
JC
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
219EOT_EOT
724aa791
JC
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
227EONT_EONT
228
2ce64696 229} # skip
724aa791
JC
230
231__END__
232