6 @INC = ('.', '../lib');
12 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
13 print "1..0 # Skip -- Perl configured without B module\n";
16 require 'test.pl'; # we use runperl from 'test.pl', so can't use Test::More
21 require_ok("B::Concise");
23 $out = runperl(switches => ["-MO=Concise"], prog => '$a', stderr => 1);
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
28 ($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
30 is($op_base, 1, "Smallest OP sequence number");
32 ($op_base_p1, $cop_base)
33 = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
35 is($op_base_p1, 2, "Second-smallest OP sequence number");
37 is($cop_base, 1, "Smallest COP sequence number");
39 # test that with -exec B::Concise navigates past logops (bug #18175)
42 switches => ["-MO=Concise,-exec"],
43 prog => q{$a=$b && print q/foo/},
47 like($out, qr/print/, "'-exec' option output has print opcode");
49 ######## API tests v.60
51 use Config; # used for perlio check
52 B::Concise->import(qw( set_style set_style_standard add_callback
53 add_style walk_output reset_sequence ));
55 ## walk_output argument checking
57 # test that walk_output rejects non-HANDLE args
58 foreach my $foo ("string", [], {}) {
59 eval { walk_output($foo) };
60 isnt ($@, '', "walk_output() rejects arg '$foo'");
61 $@=''; # clear the fail for next test
63 # test accessor mode when arg undefd or 0
64 foreach my $foo (undef, 0) {
65 my $handle = walk_output($foo);
66 is ($handle, \*STDOUT, "walk_output set to STDOUT (default)");
69 { # any object that can print should be ok for walk_output
71 sub new { my $foo = bless {} };
72 sub print { CORE::print @_ }
74 my $foo = new Hugo; # suggested this API fix
75 eval { walk_output($foo) };
76 is ($@, '', "walk_output() accepts obj that can print");
78 # test that walk_output accepts a HANDLE arg
80 skip("no perlio in this build", 4)
81 unless $Config::Config{useperlio};
83 foreach my $foo (\*STDOUT, \*STDERR) {
84 eval { walk_output($foo) };
85 is ($@, '', "walk_output() accepts STD* " . ref $foo);
88 # now test a ref to scalar
89 eval { walk_output(\my $junk) };
90 is ($@, '', "walk_output() accepts ref-to-sprintf target");
93 eval { walk_output(\$junk) };
94 is ($@, '', "walk_output() accepts ref-to-non-empty-scalar");
100 eval { add_style ('junk_B' => @stylespec) };
101 like ($@, 'expecting 3 style-format args',
102 "add_style rejects insufficient args");
104 @stylespec = (0,0,0); # right length, invalid values
106 eval { add_style ('junk' => @stylespec) };
107 is ($@, '', "add_style accepts: stylename => 3-arg-array");
110 eval { add_style (junk => @stylespec) };
111 like ($@, qr/style 'junk' already exists, choose a new name/,
112 "add_style correctly disallows re-adding same style-name" );
114 # test new arg-checks on set_style
116 eval { set_style (@stylespec) };
117 is ($@, '', "set_style accepts 3 style-format args");
119 @stylespec = (); # bad style
121 eval { set_style (@stylespec) };
122 like ($@, qr/expecting 3 style-format args/,
123 "set_style rejects bad style-format args");
125 #### for content with doc'd options
128 my $func = sub{ $a = $b+42 }; # canonical example asub
131 walk_output(\my $out);
132 eval { B::Concise::compile(@_)->() };
133 # diag "rendering $@\n";
134 return ($out, $@) if wantarray;
139 # tests output to GLOB, using perlio feature directly
140 skip "no perlio on this build", 127
141 unless $Config::Config{useperlio};
143 set_style_standard('concise'); # MUST CALL before output needed
146 -basic -exec -tree -compact -loose -vt -ascii
147 -base10 -bigendian -littleendian
149 foreach $opt (@options) {
150 ($out) = render($opt, $func);
151 isnt($out, '', "got output with option $opt");
154 ## test output control via walk_output
156 my $treegen = B::Concise::compile('-basic', $func); # reused
158 { # test output into a package global string (sprintf-ish)
160 walk_output(\$thing);
162 ok($thing, "walk_output to our SCALAR, output seen");
165 # test walkoutput acceptance of a scalar-bound IO handle
166 open (my $fh, '>', \my $buf);
169 ok($buf, "walk_output to GLOB, output seen");
171 ## test B::Concise::compile error checking
173 # call compile on non-CODE ref items
175 # pending STASH splaying
177 foreach my $ref ([], {}) {
179 walk_output(\my $out);
180 eval { B::Concise::compile('-basic', $ref)->() };
181 like ($@, qr/^err: not a coderef: $typ/,
182 "compile detects $typ-ref where expecting subref");
183 is($out,'', "no output when errd"); # announcement prints
187 # test against a bogus autovivified subref.
188 # in debugger, it should look like:
190 # -> &CODE(0x84840cc) in ???
194 #local $TODO = "\tdoes this handling make sense ?";
197 ($res,$err) = render('-basic', \&declared_only);
198 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
199 "'sub decl_only' seen as having no START");
202 ($res,$err) = render('-basic', \&defd_empty);
203 my @lines = split(/\n/, $res);
205 "'sub defd_empty {}' seen as 3 liner");
207 is(1, $res =~ /leavesub/ && $res =~ /(next|db)state/,
208 "'sub defd_empty {}' seen as 2 ops: leavesub,nextstate");
210 ($res,$err) = render('-basic', \¬_even_declared);
211 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
212 "'\¬_even_declared' seen as having no START");
216 our $AUTOLOAD = 'garbage';
217 sub AUTOLOAD { print "# in AUTOLOAD body: $AUTOLOAD\n" }
219 ($res,$err) = render('-basic', Bar::auto_func);
220 like ($res, qr/unknown function \(Bar::auto_func\)/,
221 "Bar::auto_func seen as unknown function");
223 ($res,$err) = render('-basic', \&Bar::auto_func);
224 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
225 "'\&Bar::auto_func' seen as having no START");
227 ($res,$err) = render('-basic', \&Bar::AUTOLOAD);
228 like ($res, qr/in AUTOLOAD body: /, "found body of Bar::AUTOLOAD");
231 ($res,$err) = render('-basic', Foo::bar);
232 like ($res, qr/unknown function \(Foo::bar\)/,
233 "BC::compile detects fn-name as unknown function");
237 pass ("TEST POST-COMPILE OPTION-HANDLING IN WALKER SUBROUTINE");
241 my $walker = B::Concise::compile('-basic', $func);
242 walk_output(\$sample);
244 like($sample, qr/goto/m, "post-compile -exec");
246 walk_output(\$sample);
248 unlike($sample, qr/goto/m, "post-compile -basic");
251 # bang at it combinatorically
253 my @modes = qw( -basic -exec );
254 my @styles = qw( -concise -debug -linenoise -terse );
257 for $style (@styles) {
259 walk_output(\$sample);
261 $walker->($style, $mode);
262 $combos{"$style$mode"} = $sample;
265 # crosscheck that samples are all text-different
266 @list = sort keys %combos;
268 for $j ($i+1..$#list) {
269 isnt ($combos{$list[$i]}, $combos{$list[$j]},
270 "combos for $list[$i] and $list[$j] are different, as expected");
274 # add samples with styles in different order
276 for $style (@styles) {
278 walk_output(\$sample);
279 $walker->($mode, $style);
280 $combos{"$mode$style"} = $sample;
283 # test commutativity of flags, ie that AB == BA
285 for $style (@styles) {
286 is ( $combos{"$style$mode"},
287 $combos{"$mode$style"},
288 "results for $style$mode vs $mode$style are the same" );
293 %combos = (); # outputs for $mode=any($order) and any($style)
295 # add more samples with switching modes & sticky styles
296 for $style (@styles) {
297 walk_output(\$sample);
301 walk_output(\$sample);
304 $combos{"$style/$mode"} = $sample;
307 # crosscheck that samples are all text-different
308 @nm = sort keys %combos;
310 for $j ($i+1..$#nm) {
311 isnt ($combos{$nm[$i]}, $combos{$nm[$j]},
312 "results for $nm[$i] and $nm[$j] are different, as expected");
316 # add samples with switching styles & sticky modes
318 walk_output(\$sample);
321 for $style (@styles) {
322 walk_output(\$sample);
325 $combos{"$mode/$style"} = $sample;
328 # test commutativity of flags, ie that AB == BA
330 for $style (@styles) {
331 is ( $combos{"$style/$mode"},
332 $combos{"$mode/$style"},
333 "results for $style/$mode vs $mode/$style are the same" );
338 #now do double crosschecks: commutativity across stick / nostick
339 %combos = (%combos, %save);
341 # test commutativity of flags, ie that AB == BA
343 for $style (@styles) {
345 is ( $combos{"$style$mode"},
346 $combos{"$style/$mode"},
347 "$style$mode VS $style/$mode are the same" );
349 is ( $combos{"$mode$style"},
350 $combos{"$mode/$style"},
351 "$mode$style VS $mode/$style are the same" );
353 is ( $combos{"$style$mode"},
354 $combos{"$mode/$style"},
355 "$style$mode VS $mode/$style are the same" );
357 is ( $combos{"$mode$style"},
358 $combos{"$style/$mode"},
359 "$mode$style VS $style/$mode are the same" );
365 # test proper NULLING of pointer, derefd by CvSTART, when a coderef is
366 # undefd. W/o this, the pointer can dangle into freed and reused
367 # optree mem, which no longer points to opcodes.
369 # Using B::Concise to render Config::AUTOLOAD's optree at BEGIN-time
370 # triggers this obscure bug, cuz AUTOLOAD has a bootstrap version,
371 # which is used at load-time then undeffed. It is normally
372 # re-vivified later, but not in time for this (BEGIN/CHECK)-time
375 $out = runperl ( switches => ["-MO=Concise,Config::AUTOLOAD"],
376 prog => 'use Config; BEGIN { $Config{awk} }',
379 like($out, qr/Config::AUTOLOAD exists in stash, but has no START/,
380 "coderef properly undefined");
382 $out = runperl ( switches => ["-MO=Concise,Config::AUTOLOAD"],
383 prog => 'use Config; CHECK { $Config{awk} }',
386 like($out, qr/Config::AUTOLOAD exists in stash, but has no START/,
387 "coderef properly undefined");
389 # test -stash and -src rendering
390 # todo: stderr=1 puts '-e syntax OK' into $out,
391 # conceivably fouling one of the lines that are tested
392 $out = runperl ( switches => ["-MO=Concise,-stash=B::Concise,-src"],
393 prog => '-e 1', stderr => 1 );
395 like($out, qr/FUNC: \*B::Concise::concise_cv_obj/,
396 "stash rendering of B::Concise includes Concise::concise_cv_obj");
398 like($out, qr/FUNC: \*B::Concise::walk_output/,
399 "stash rendering includes Concise::walk_output");
401 like($out, qr/FUNC: \*B::Concise::PAD_FAKELEX_MULTI/,
402 "stash rendering includes constant sub: PAD_FAKELEX_MULTI");
404 like($out, qr/PAD_FAKELEX_MULTI is a constant sub, optimized to a IV/,
405 "stash rendering identifies it as constant");
407 like($out, qr/\# 4\d\d: \s+ \$l->concise\(\$level\);/,
408 "src-line rendering works");
410 $out = runperl ( switches => ["-MO=Concise,-stash=ExtUtils::Mksymlists,-src,-exec"],
411 prog => '-e 1', stderr => 1 );
413 like($out, qr/FUNC: \*ExtUtils::Mksymlists::_write_vms/,
414 "stash rendering loads package as needed");
416 $out = runperl ( switches => ["-MO=Concise,-stash=Data::Dumper,-src,-exec"],
417 prog => '-e 1', stderr => 1 );
419 like($out, qr/FUNC: \*Data::Dumper::format_refaddr/,
420 "stash rendering loads package as needed");
422 my $prog = q{package FOO; sub bar { print "bar" } package main; FOO::bar(); };
424 # this would fail if %INC used for -stash test
425 $out = runperl ( switches => ["-MO=Concise,-src,-stash=FOO,-main"],
426 prog => $prog, stderr => 1 );
428 like($out, qr/FUNC: \*FOO::bar/,
429 "stash rendering works on inlined package");