Cleanup ext/B tests for -w and to run separately
[p5sagit/p5-mst-13.2.git] / ext / B / t / concise.t
CommitLineData
c517cc47 1#!./perl
2
3BEGIN {
5638aaac 4 if ($ENV{PERL_CORE}){
5 chdir('t') if -d 't';
6 @INC = ('.', '../lib');
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 }
5638aaac 16 require 'test.pl';
c517cc47 17}
18
cc02ea56 19plan tests => 142;
c517cc47 20
21require_ok("B::Concise");
22
23$out = runperl(switches => ["-MO=Concise"], prog => '$a', stderr => 1);
24
25# If either of the next two tests fail, it probably means you need to
26# fix the section labeled 'fragile kludge' in Concise.pm
27
c33fe613 28($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
c517cc47 29
c33fe613 30is($op_base, 1, "Smallest OP sequence number");
c517cc47 31
c27ea44e 32($op_base_p1, $cop_base)
33 = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
c517cc47 34
c33fe613 35is($op_base_p1, 2, "Second-smallest OP sequence number");
36
37is($cop_base, 1, "Smallest COP sequence number");
62e36f8a 38
39# test that with -exec B::Concise navigates past logops (bug #18175)
40
41$out = runperl(
42 switches => ["-MO=Concise,-exec"],
cc02ea56 43 prog => q{$a=$b && print q/foo/},
62e36f8a 44 stderr => 1,
45);
46
724aa791 47like($out, qr/print/, "'-exec' option output has print opcode");
48
49######## API tests v.60
50
51use Config; # used for perlio check
cc02ea56 52B::Concise->import(qw( set_style set_style_standard add_callback
53 add_style walk_output reset_sequence ));
724aa791 54
55## walk_output argument checking
56
724aa791 57# test that walk_output rejects non-HANDLE args
cc02ea56 58foreach my $foo ("string", [], {}) {
724aa791 59 eval { walk_output($foo) };
60 isnt ($@, '', "walk_output() rejects arg '$foo'");
61 $@=''; # clear the fail for next test
62}
cc02ea56 63# test accessor mode when arg undefd or 0
64foreach my $foo (undef, 0) {
65 my $handle = walk_output($foo);
66 is ($handle, \*STDOUT, "walk_output set to STDOUT (default)");
67}
724aa791 68
69{ # any object that can print should be ok for walk_output
70 package Hugo;
71 sub new { my $foo = bless {} };
72 sub print { CORE::print @_ }
73}
74my $foo = new Hugo; # suggested this API fix
75eval { walk_output($foo) };
76is ($@, '', "walk_output() accepts obj that can print");
77
2ce64696 78# test that walk_output accepts a HANDLE arg
79SKIP: {
80 skip("no perlio in this build", 4)
81 unless $Config::Config{useperlio};
82
83 foreach my $foo (\*STDOUT, \*STDERR) {
84 eval { walk_output($foo) };
85 is ($@, '', "walk_output() accepts STD* " . ref $foo);
86 }
87
88 # now test a ref to scalar
89 eval { walk_output(\my $junk) };
90 is ($@, '', "walk_output() accepts ref-to-sprintf target");
91
92 $junk = "non-empty";
93 eval { walk_output(\$junk) };
94 is ($@, '', "walk_output() accepts ref-to-non-empty-scalar");
95}
724aa791 96
97## add_style
98my @stylespec;
99$@='';
100eval { add_style ('junk_B' => @stylespec) };
101like ($@, 'expecting 3 style-format args',
102 "add_style rejects insufficient args");
103
104@stylespec = (0,0,0); # right length, invalid values
105$@='';
106eval { add_style ('junk' => @stylespec) };
107is ($@, '', "add_style accepts: stylename => 3-arg-array");
108
109$@='';
110eval { add_style (junk => @stylespec) };
111like ($@, qr/style 'junk' already exists, choose a new name/,
112 "add_style correctly disallows re-adding same style-name" );
113
114# test new arg-checks on set_style
115$@='';
116eval { set_style (@stylespec) };
117is ($@, '', "set_style accepts 3 style-format args");
118
119@stylespec = (); # bad style
120
121eval { set_style (@stylespec) };
122like ($@, qr/expecting 3 style-format args/,
123 "set_style rejects bad style-format args");
124
724aa791 125#### for content with doc'd options
2ce64696 126
5638aaac 127our($a, $b);
cc02ea56 128my $func = sub{ $a = $b+42 }; # canonical example asub
2ce64696 129
cc02ea56 130SKIP: {
131 # tests output to GLOB, using perlio feature directly
132 skip "no perlio on this build", 122
133 unless $Config::Config{useperlio};
134
135 set_style_standard('concise'); # MUST CALL before output needed
136
2ce64696 137 @options = qw(
cc02ea56 138 -basic -exec -tree -compact -loose -vt -ascii
2ce64696 139 -base10 -bigendian -littleendian
140 );
141 foreach $opt (@options) {
142 walk_output(\my $out);
143 my $treegen = B::Concise::compile($opt, $func);
144 $treegen->();
145 #print "foo:$out\n";
146 isnt($out, '', "got output with option $opt");
147 }
cc02ea56 148
2ce64696 149 ## test output control via walk_output
cc02ea56 150
2ce64696 151 my $treegen = B::Concise::compile('-basic', $func); # reused
cc02ea56 152
2ce64696 153 { # test output into a package global string (sprintf-ish)
154 our $thing;
155 walk_output(\$thing);
156 $treegen->();
157 ok($thing, "walk_output to our SCALAR, output seen");
158 }
159
cc02ea56 160 # test walkoutput acceptance of a scalar-bound IO handle
724aa791 161 open (my $fh, '>', \my $buf);
162 walk_output($fh);
163 $treegen->();
164 ok($buf, "walk_output to GLOB, output seen");
cc02ea56 165
2ce64696 166 ## Test B::Concise::compile error checking
cc02ea56 167
2ce64696 168 # call compile on non-CODE ref items
cc02ea56 169 if (0) {
170 # pending STASH splaying
171
172 foreach my $ref ([], {}) {
173 my $typ = ref $ref;
174 walk_output(\my $out);
175 eval { B::Concise::compile('-basic', $ref)->() };
176 like ($@, qr/^err: not a coderef: $typ/,
177 "compile detects $typ-ref where expecting subref");
178 # is($out,'', "no output when errd"); # announcement prints
179 }
2ce64696 180 }
cc02ea56 181
2ce64696 182 # test against a bogus autovivified subref.
183 # in debugger, it should look like:
184 # 1 CODE(0x84840cc)
185 # -> &CODE(0x84840cc) in ???
186 sub nosuchfunc;
cc02ea56 187 eval { B::Concise::compile('-basic', \&nosuchfunc)->() };
2ce64696 188 like ($@, qr/^err: coderef has no START/,
189 "compile detects CODE-ref w/o actual code");
190
191 foreach my $opt (qw( -concise -exec )) {
192 eval { B::Concise::compile($opt,'non_existent_function')->() };
193 like ($@, qr/unknown function \(main::non_existent_function\)/,
194 "'$opt' reports non-existent-function properly");
195 }
cc02ea56 196
197 # v.62 tests
198
199 pass ("TEST POST-COMPILE OPTION-HANDLING IN WALKER SUBROUTINE");
200
201 my $sample;
202
203 my $walker = B::Concise::compile('-basic', $func);
204 walk_output(\$sample);
205 $walker->('-exec');
206 like($sample, qr/goto/m, "post-compile -exec");
207
208 walk_output(\$sample);
209 $walker->('-basic');
210 unlike($sample, qr/goto/m, "post-compile -basic");
211
212
213 # bang at it combinatorically
214 my %combos;
215 my @modes = qw( -basic -exec );
216 my @styles = qw( -concise -debug -linenoise -terse );
217
218 # prep samples
219 for $style (@styles) {
220 for $mode (@modes) {
221 walk_output(\$sample);
222 reset_sequence();
223 $walker->($style, $mode);
224 $combos{"$style$mode"} = $sample;
225 }
226 }
227 # crosscheck that samples are all text-different
228 @list = sort keys %combos;
229 for $i (0..$#list) {
230 for $j ($i+1..$#list) {
231 isnt ($combos{$list[$i]}, $combos{$list[$j]},
232 "combos for $list[$i] and $list[$j] are different, as expected");
233 }
234 }
235
236 # add samples with styles in different order
237 for $mode (@modes) {
238 for $style (@styles) {
239 reset_sequence();
240 walk_output(\$sample);
241 $walker->($mode, $style);
242 $combos{"$mode$style"} = $sample;
243 }
244 }
245 # test commutativity of flags, ie that AB == BA
246 for $mode (@modes) {
247 for $style (@styles) {
248 is ( $combos{"$style$mode"},
249 $combos{"$mode$style"},
250 "results for $style$mode vs $mode$style are the same" );
251 }
252 }
253
254 my %save = %combos;
5638aaac 255 %combos = (); # outputs for $mode=any($order) and any($style)
cc02ea56 256
257 # add more samples with switching modes & sticky styles
258 for $style (@styles) {
259 walk_output(\$sample);
260 reset_sequence();
261 $walker->($style);
262 for $mode (@modes) {
263 walk_output(\$sample);
264 reset_sequence();
265 $walker->($mode);
266 $combos{"$style/$mode"} = $sample;
267 }
268 }
269 # crosscheck that samples are all text-different
270 @nm = sort keys %combos;
271 for $i (0..$#nm) {
272 for $j ($i+1..$#nm) {
273 isnt ($combos{$nm[$i]}, $combos{$nm[$j]},
274 "results for $nm[$i] and $nm[$j] are different, as expected");
275 }
276 }
277
278 # add samples with switching styles & sticky modes
279 for $mode (@modes) {
280 walk_output(\$sample);
281 reset_sequence();
282 $walker->($mode);
283 for $style (@styles) {
284 walk_output(\$sample);
285 reset_sequence();
286 $walker->($style);
287 $combos{"$mode/$style"} = $sample;
288 }
289 }
290 # test commutativity of flags, ie that AB == BA
291 for $mode (@modes) {
292 for $style (@styles) {
293 is ( $combos{"$style/$mode"},
294 $combos{"$mode/$style"},
295 "results for $style/$mode vs $mode/$style are the same" );
296 }
297 }
298
299
300 #now do double crosschecks: commutativity across stick / nostick
5638aaac 301 %combos = (%combos, %save);
cc02ea56 302
303 # test commutativity of flags, ie that AB == BA
304 for $mode (@modes) {
305 for $style (@styles) {
306
307 is ( $combos{"$style$mode"},
308 $combos{"$style/$mode"},
309 "$style$mode VS $style/$mode are the same" );
310
311 is ( $combos{"$mode$style"},
312 $combos{"$mode/$style"},
313 "$mode$style VS $mode/$style are the same" );
314
315 is ( $combos{"$style$mode"},
316 $combos{"$mode/$style"},
317 "$style$mode VS $mode/$style are the same" );
318
319 is ( $combos{"$mode$style"},
320 $combos{"$style/$mode"},
321 "$mode$style VS $style/$mode are the same" );
322 }
323 }
724aa791 324}
cc02ea56 325
326__END__
327