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
17 sub diag { print "# @_\n" } # but this is still handy
22 require_ok("B::Concise");
24 $out = runperl(switches => ["-MO=Concise"], prog => '$a', stderr => 1);
26 # If either of the next two tests fail, it probably means you need to
27 # fix the section labeled 'fragile kludge' in Concise.pm
29 ($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
31 is($op_base, 1, "Smallest OP sequence number");
33 ($op_base_p1, $cop_base)
34 = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
36 is($op_base_p1, 2, "Second-smallest OP sequence number");
38 is($cop_base, 1, "Smallest COP sequence number");
40 # test that with -exec B::Concise navigates past logops (bug #18175)
43 switches => ["-MO=Concise,-exec"],
44 prog => q{$a=$b && print q/foo/},
48 like($out, qr/print/, "'-exec' option output has print opcode");
50 ######## API tests v.60
52 use Config; # used for perlio check
53 B::Concise->import(qw( set_style set_style_standard add_callback
54 add_style walk_output reset_sequence ));
56 ## walk_output argument checking
58 # test that walk_output rejects non-HANDLE args
59 foreach my $foo ("string", [], {}) {
60 eval { walk_output($foo) };
61 isnt ($@, '', "walk_output() rejects arg '$foo'");
62 $@=''; # clear the fail for next test
64 # test accessor mode when arg undefd or 0
65 foreach my $foo (undef, 0) {
66 my $handle = walk_output($foo);
67 is ($handle, \*STDOUT, "walk_output set to STDOUT (default)");
70 { # any object that can print should be ok for walk_output
72 sub new { my $foo = bless {} };
73 sub print { CORE::print @_ }
75 my $foo = new Hugo; # suggested this API fix
76 eval { walk_output($foo) };
77 is ($@, '', "walk_output() accepts obj that can print");
79 # test that walk_output accepts a HANDLE arg
81 skip("no perlio in this build", 4)
82 unless $Config::Config{useperlio};
84 foreach my $foo (\*STDOUT, \*STDERR) {
85 eval { walk_output($foo) };
86 is ($@, '', "walk_output() accepts STD* " . ref $foo);
89 # now test a ref to scalar
90 eval { walk_output(\my $junk) };
91 is ($@, '', "walk_output() accepts ref-to-sprintf target");
94 eval { walk_output(\$junk) };
95 is ($@, '', "walk_output() accepts ref-to-non-empty-scalar");
101 eval { add_style ('junk_B' => @stylespec) };
102 like ($@, 'expecting 3 style-format args',
103 "add_style rejects insufficient args");
105 @stylespec = (0,0,0); # right length, invalid values
107 eval { add_style ('junk' => @stylespec) };
108 is ($@, '', "add_style accepts: stylename => 3-arg-array");
111 eval { add_style (junk => @stylespec) };
112 like ($@, qr/style 'junk' already exists, choose a new name/,
113 "add_style correctly disallows re-adding same style-name" );
115 # test new arg-checks on set_style
117 eval { set_style (@stylespec) };
118 is ($@, '', "set_style accepts 3 style-format args");
120 @stylespec = (); # bad style
122 eval { set_style (@stylespec) };
123 like ($@, qr/expecting 3 style-format args/,
124 "set_style rejects bad style-format args");
126 #### for content with doc'd options
129 my $func = sub{ $a = $b+42 }; # canonical example asub
132 walk_output(\my $out);
133 eval { B::Concise::compile(@_)->() };
134 # diag "rendering $@\n";
135 return ($out, $@) if wantarray;
140 # tests output to GLOB, using perlio feature directly
141 skip "no perlio on this build", 127
142 unless $Config::Config{useperlio};
144 set_style_standard('concise'); # MUST CALL before output needed
147 -basic -exec -tree -compact -loose -vt -ascii
148 -base10 -bigendian -littleendian
150 foreach $opt (@options) {
151 ($out) = render($opt, $func);
152 isnt($out, '', "got output with option $opt");
155 ## test output control via walk_output
157 my $treegen = B::Concise::compile('-basic', $func); # reused
159 { # test output into a package global string (sprintf-ish)
161 walk_output(\$thing);
163 ok($thing, "walk_output to our SCALAR, output seen");
166 # test walkoutput acceptance of a scalar-bound IO handle
167 open (my $fh, '>', \my $buf);
170 ok($buf, "walk_output to GLOB, output seen");
172 ## test B::Concise::compile error checking
174 # call compile on non-CODE ref items
176 # pending STASH splaying
178 foreach my $ref ([], {}) {
180 walk_output(\my $out);
181 eval { B::Concise::compile('-basic', $ref)->() };
182 like ($@, qr/^err: not a coderef: $typ/,
183 "compile detects $typ-ref where expecting subref");
184 is($out,'', "no output when errd"); # announcement prints
188 # test against a bogus autovivified subref.
189 # in debugger, it should look like:
191 # -> &CODE(0x84840cc) in ???
195 #local $TODO = "\tdoes this handling make sense ?";
198 ($res,$err) = render('-basic', \&declared_only);
199 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
200 "'sub decl_only' seen as having no START");
203 ($res,$err) = render('-basic', \&defd_empty);
204 is(scalar split(/\n/, $res), 3,
205 "'sub defd_empty {}' seen as 3 liner");
207 is(1, $res =~ /leavesub/ && $res =~ /nextstate/,
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");