7 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
8 print "1..0 # Skip -- Perl configured without B module\n";
16 require_ok("B::Concise");
18 $out = runperl(switches => ["-MO=Concise"], prog => '$a', stderr => 1);
20 # If either of the next two tests fail, it probably means you need to
21 # fix the section labeled 'fragile kludge' in Concise.pm
23 ($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
25 is($op_base, 1, "Smallest OP sequence number");
27 ($op_base_p1, $cop_base)
28 = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
30 is($op_base_p1, 2, "Second-smallest OP sequence number");
32 is($cop_base, 1, "Smallest COP sequence number");
34 # test that with -exec B::Concise navigates past logops (bug #18175)
37 switches => ["-MO=Concise,-exec"],
38 prog => q{$a=$b && print q/foo/},
42 like($out, qr/print/, "'-exec' option output has print opcode");
44 ######## API tests v.60
46 use Config; # used for perlio check
47 B::Concise->import(qw( set_style set_style_standard add_callback
48 add_style walk_output reset_sequence ));
50 ## walk_output argument checking
52 # test that walk_output rejects non-HANDLE args
53 foreach my $foo ("string", [], {}) {
54 eval { walk_output($foo) };
55 isnt ($@, '', "walk_output() rejects arg '$foo'");
56 $@=''; # clear the fail for next test
58 # test accessor mode when arg undefd or 0
59 foreach my $foo (undef, 0) {
60 my $handle = walk_output($foo);
61 is ($handle, \*STDOUT, "walk_output set to STDOUT (default)");
64 { # any object that can print should be ok for walk_output
66 sub new { my $foo = bless {} };
67 sub print { CORE::print @_ }
69 my $foo = new Hugo; # suggested this API fix
70 eval { walk_output($foo) };
71 is ($@, '', "walk_output() accepts obj that can print");
73 # test that walk_output accepts a HANDLE arg
75 skip("no perlio in this build", 4)
76 unless $Config::Config{useperlio};
78 foreach my $foo (\*STDOUT, \*STDERR) {
79 eval { walk_output($foo) };
80 is ($@, '', "walk_output() accepts STD* " . ref $foo);
83 # now test a ref to scalar
84 eval { walk_output(\my $junk) };
85 is ($@, '', "walk_output() accepts ref-to-sprintf target");
88 eval { walk_output(\$junk) };
89 is ($@, '', "walk_output() accepts ref-to-non-empty-scalar");
95 eval { add_style ('junk_B' => @stylespec) };
96 like ($@, 'expecting 3 style-format args',
97 "add_style rejects insufficient args");
99 @stylespec = (0,0,0); # right length, invalid values
101 eval { add_style ('junk' => @stylespec) };
102 is ($@, '', "add_style accepts: stylename => 3-arg-array");
105 eval { add_style (junk => @stylespec) };
106 like ($@, qr/style 'junk' already exists, choose a new name/,
107 "add_style correctly disallows re-adding same style-name" );
109 # test new arg-checks on set_style
111 eval { set_style (@stylespec) };
112 is ($@, '', "set_style accepts 3 style-format args");
114 @stylespec = (); # bad style
116 eval { set_style (@stylespec) };
117 like ($@, qr/expecting 3 style-format args/,
118 "set_style rejects bad style-format args");
120 #### for content with doc'd options
122 my $func = sub{ $a = $b+42 }; # canonical example asub
125 # tests output to GLOB, using perlio feature directly
126 skip "no perlio on this build", 122
127 unless $Config::Config{useperlio};
129 set_style_standard('concise'); # MUST CALL before output needed
132 -basic -exec -tree -compact -loose -vt -ascii
133 -base10 -bigendian -littleendian
135 foreach $opt (@options) {
136 walk_output(\my $out);
137 my $treegen = B::Concise::compile($opt, $func);
140 isnt($out, '', "got output with option $opt");
143 ## test output control via walk_output
145 my $treegen = B::Concise::compile('-basic', $func); # reused
147 { # test output into a package global string (sprintf-ish)
149 walk_output(\$thing);
151 ok($thing, "walk_output to our SCALAR, output seen");
154 # test walkoutput acceptance of a scalar-bound IO handle
155 open (my $fh, '>', \my $buf);
158 ok($buf, "walk_output to GLOB, output seen");
160 ## Test B::Concise::compile error checking
162 # call compile on non-CODE ref items
164 # pending STASH splaying
166 foreach my $ref ([], {}) {
168 walk_output(\my $out);
169 eval { B::Concise::compile('-basic', $ref)->() };
170 like ($@, qr/^err: not a coderef: $typ/,
171 "compile detects $typ-ref where expecting subref");
172 # is($out,'', "no output when errd"); # announcement prints
176 # test against a bogus autovivified subref.
177 # in debugger, it should look like:
179 # -> &CODE(0x84840cc) in ???
181 eval { B::Concise::compile('-basic', \&nosuchfunc)->() };
182 like ($@, qr/^err: coderef has no START/,
183 "compile detects CODE-ref w/o actual code");
185 foreach my $opt (qw( -concise -exec )) {
186 eval { B::Concise::compile($opt,'non_existent_function')->() };
187 like ($@, qr/unknown function \(main::non_existent_function\)/,
188 "'$opt' reports non-existent-function properly");
193 pass ("TEST POST-COMPILE OPTION-HANDLING IN WALKER SUBROUTINE");
197 my $walker = B::Concise::compile('-basic', $func);
198 walk_output(\$sample);
200 like($sample, qr/goto/m, "post-compile -exec");
202 walk_output(\$sample);
204 unlike($sample, qr/goto/m, "post-compile -basic");
207 # bang at it combinatorically
209 my @modes = qw( -basic -exec );
210 my @styles = qw( -concise -debug -linenoise -terse );
213 for $style (@styles) {
215 walk_output(\$sample);
217 $walker->($style, $mode);
218 $combos{"$style$mode"} = $sample;
221 # crosscheck that samples are all text-different
222 @list = sort keys %combos;
224 for $j ($i+1..$#list) {
225 isnt ($combos{$list[$i]}, $combos{$list[$j]},
226 "combos for $list[$i] and $list[$j] are different, as expected");
230 # add samples with styles in different order
232 for $style (@styles) {
234 walk_output(\$sample);
235 $walker->($mode, $style);
236 $combos{"$mode$style"} = $sample;
239 # test commutativity of flags, ie that AB == BA
241 for $style (@styles) {
242 is ( $combos{"$style$mode"},
243 $combos{"$mode$style"},
244 "results for $style$mode vs $mode$style are the same" );
249 my %combos; # outputs for $mode=any($order) and any($style)
251 # add more samples with switching modes & sticky styles
252 for $style (@styles) {
253 walk_output(\$sample);
257 walk_output(\$sample);
260 $combos{"$style/$mode"} = $sample;
263 # crosscheck that samples are all text-different
264 @nm = sort keys %combos;
266 for $j ($i+1..$#nm) {
267 isnt ($combos{$nm[$i]}, $combos{$nm[$j]},
268 "results for $nm[$i] and $nm[$j] are different, as expected");
272 # add samples with switching styles & sticky modes
274 walk_output(\$sample);
277 for $style (@styles) {
278 walk_output(\$sample);
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 #now do double crosschecks: commutativity across stick / nostick
295 my %combos = (%combos, %save);
297 # test commutativity of flags, ie that AB == BA
299 for $style (@styles) {
301 is ( $combos{"$style$mode"},
302 $combos{"$style/$mode"},
303 "$style$mode VS $style/$mode are the same" );
305 is ( $combos{"$mode$style"},
306 $combos{"$mode/$style"},
307 "$mode$style VS $mode/$style are the same" );
309 is ( $combos{"$style$mode"},
310 $combos{"$mode/$style"},
311 "$style$mode VS $mode/$style are the same" );
313 is ( $combos{"$mode$style"},
314 $combos{"$style/$mode"},
315 "$mode$style VS $style/$mode are the same" );