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
23 require_ok("B::Concise");
25 $out = runperl(switches => ["-MO=Concise"], prog => '$a', stderr => 1);
27 # If either of the next two tests fail, it probably means you need to
28 # fix the section labeled 'fragile kludge' in Concise.pm
30 ($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
32 is($op_base, 1, "Smallest OP sequence number");
34 ($op_base_p1, $cop_base)
35 = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
37 is($op_base_p1, 2, "Second-smallest OP sequence number");
39 is($cop_base, 1, "Smallest COP sequence number");
41 # test that with -exec B::Concise navigates past logops (bug #18175)
44 switches => ["-MO=Concise,-exec"],
45 prog => q{$a=$b && print q/foo/},
49 like($out, qr/print/, "'-exec' option output has print opcode");
51 ######## API tests v.60
53 use Config; # used for perlio check
54 B::Concise->import(qw( set_style set_style_standard add_callback
55 add_style walk_output reset_sequence ));
57 ## walk_output argument checking
59 # test that walk_output rejects non-HANDLE args
60 foreach my $foo ("string", [], {}) {
61 eval { walk_output($foo) };
62 isnt ($@, '', "walk_output() rejects arg '$foo'");
63 $@=''; # clear the fail for next test
65 # test accessor mode when arg undefd or 0
66 foreach my $foo (undef, 0) {
67 my $handle = walk_output($foo);
68 is ($handle, \*STDOUT, "walk_output set to STDOUT (default)");
71 { # any object that can print should be ok for walk_output
73 sub new { my $foo = bless {} };
74 sub print { CORE::print @_ }
76 my $foo = new Hugo; # suggested this API fix
77 eval { walk_output($foo) };
78 is ($@, '', "walk_output() accepts obj that can print");
80 # test that walk_output accepts a HANDLE arg
82 skip("no perlio in this build", 4)
83 unless $Config::Config{useperlio};
85 foreach my $foo (\*STDOUT, \*STDERR) {
86 eval { walk_output($foo) };
87 is ($@, '', "walk_output() accepts STD* " . ref $foo);
90 # now test a ref to scalar
91 eval { walk_output(\my $junk) };
92 is ($@, '', "walk_output() accepts ref-to-sprintf target");
95 eval { walk_output(\$junk) };
96 is ($@, '', "walk_output() accepts ref-to-non-empty-scalar");
102 eval { add_style ('junk_B' => @stylespec) };
103 like ($@, 'expecting 3 style-format args',
104 "add_style rejects insufficient args");
106 @stylespec = (0,0,0); # right length, invalid values
108 eval { add_style ('junk' => @stylespec) };
109 is ($@, '', "add_style accepts: stylename => 3-arg-array");
112 eval { add_style (junk => @stylespec) };
113 like ($@, qr/style 'junk' already exists, choose a new name/,
114 "add_style correctly disallows re-adding same style-name" );
116 # test new arg-checks on set_style
118 eval { set_style (@stylespec) };
119 is ($@, '', "set_style accepts 3 style-format args");
121 @stylespec = (); # bad style
123 eval { set_style (@stylespec) };
124 like ($@, qr/expecting 3 style-format args/,
125 "set_style rejects bad style-format args");
127 #### for content with doc'd options
130 my $func = sub{ $a = $b+42 }; # canonical example asub
133 walk_output(\my $out);
134 eval { B::Concise::compile(@_)->() };
135 # diag "rendering $@\n";
136 return ($out, $@) if wantarray;
141 # tests output to GLOB, using perlio feature directly
142 skip "no perlio on this build", 122
143 unless $Config::Config{useperlio};
145 set_style_standard('concise'); # MUST CALL before output needed
148 -basic -exec -tree -compact -loose -vt -ascii
149 -base10 -bigendian -littleendian
151 foreach $opt (@options) {
152 ($out) = render($opt, $func);
153 isnt($out, '', "got output with option $opt");
156 ## test output control via walk_output
158 my $treegen = B::Concise::compile('-basic', $func); # reused
160 { # test output into a package global string (sprintf-ish)
162 walk_output(\$thing);
164 ok($thing, "walk_output to our SCALAR, output seen");
167 # test walkoutput acceptance of a scalar-bound IO handle
168 open (my $fh, '>', \my $buf);
171 ok($buf, "walk_output to GLOB, output seen");
173 ## test B::Concise::compile error checking
175 # call compile on non-CODE ref items
177 # pending STASH splaying
179 foreach my $ref ([], {}) {
181 walk_output(\my $out);
182 eval { B::Concise::compile('-basic', $ref)->() };
183 like ($@, qr/^err: not a coderef: $typ/,
184 "compile detects $typ-ref where expecting subref");
185 is($out,'', "no output when errd"); # announcement prints
189 # test against a bogus autovivified subref.
190 # in debugger, it should look like:
192 # -> &CODE(0x84840cc) in ???
196 local $TODO = "\tdoes this handling make sense ?";
199 ($res,$err) = render('-basic', \&declared_only);
200 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
201 "'sub decl_only' seen as having no START");
204 ($res,$err) = render('-basic', \&defd_empty);
205 is(scalar split(/\n/, $res), 3,
206 "'sub defd_empty {}' seen as 3 liner");
208 is(1, $res =~ /leavesub/ && $res =~ /nextstate/,
209 "'sub defd_empty {}' seen as 2 ops: leavesub,nextstate");
211 ($res,$err) = render('-basic', \¬_even_declared);
212 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
213 "'\¬_even_declared' seen as having no START");
217 our $AUTOLOAD = 'garbage';
218 sub AUTOLOAD { print "# in AUTOLOAD: $AUTOLOAD\n" }
220 ($res,$err) = render('-basic', Bar::auto_func);
221 like ($res, qr/unknown function \(Bar::auto_func\)/,
222 "Bar::auto_func seen as unknown function");
224 ($res,$err) = render('-basic', \&Bar::auto_func);
225 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
226 "'\&Bar::auto_func' seen as having no START");
228 ($res,$err) = render('-basic', \&Bar::AUTOLOAD);
229 like ($res, qr/called Bar::AUTOLOAD/, "found body of Bar::AUTOLOAD");
232 ($res,$err) = render('-basic', Foo::bar);
233 like ($res, qr/unknown function \(Foo::bar\)/,
234 "BC::compile detects fn-name as unknown function");
238 pass ("TEST POST-COMPILE OPTION-HANDLING IN WALKER SUBROUTINE");
242 my $walker = B::Concise::compile('-basic', $func);
243 walk_output(\$sample);
245 like($sample, qr/goto/m, "post-compile -exec");
247 walk_output(\$sample);
249 unlike($sample, qr/goto/m, "post-compile -basic");
252 # bang at it combinatorically
254 my @modes = qw( -basic -exec );
255 my @styles = qw( -concise -debug -linenoise -terse );
258 for $style (@styles) {
260 walk_output(\$sample);
262 $walker->($style, $mode);
263 $combos{"$style$mode"} = $sample;
266 # crosscheck that samples are all text-different
267 @list = sort keys %combos;
269 for $j ($i+1..$#list) {
270 isnt ($combos{$list[$i]}, $combos{$list[$j]},
271 "combos for $list[$i] and $list[$j] are different, as expected");
275 # add samples with styles in different order
277 for $style (@styles) {
279 walk_output(\$sample);
280 $walker->($mode, $style);
281 $combos{"$mode$style"} = $sample;
284 # test commutativity of flags, ie that AB == BA
286 for $style (@styles) {
287 is ( $combos{"$style$mode"},
288 $combos{"$mode$style"},
289 "results for $style$mode vs $mode$style are the same" );
294 %combos = (); # outputs for $mode=any($order) and any($style)
296 # add more samples with switching modes & sticky styles
297 for $style (@styles) {
298 walk_output(\$sample);
302 walk_output(\$sample);
305 $combos{"$style/$mode"} = $sample;
308 # crosscheck that samples are all text-different
309 @nm = sort keys %combos;
311 for $j ($i+1..$#nm) {
312 isnt ($combos{$nm[$i]}, $combos{$nm[$j]},
313 "results for $nm[$i] and $nm[$j] are different, as expected");
317 # add samples with switching styles & sticky modes
319 walk_output(\$sample);
322 for $style (@styles) {
323 walk_output(\$sample);
326 $combos{"$mode/$style"} = $sample;
329 # test commutativity of flags, ie that AB == BA
331 for $style (@styles) {
332 is ( $combos{"$style/$mode"},
333 $combos{"$mode/$style"},
334 "results for $style/$mode vs $mode/$style are the same" );
339 #now do double crosschecks: commutativity across stick / nostick
340 %combos = (%combos, %save);
342 # test commutativity of flags, ie that AB == BA
344 for $style (@styles) {
346 is ( $combos{"$style$mode"},
347 $combos{"$style/$mode"},
348 "$style$mode VS $style/$mode are the same" );
350 is ( $combos{"$mode$style"},
351 $combos{"$mode/$style"},
352 "$mode$style VS $mode/$style are the same" );
354 is ( $combos{"$style$mode"},
355 $combos{"$mode/$style"},
356 "$style$mode VS $mode/$style are the same" );
358 is ( $combos{"$mode$style"},
359 $combos{"$style/$mode"},
360 "$mode$style VS $style/$mode are the same" );