6 @INC = ('.', '../lib');
12 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
13 print "1..0 # Skip -- Perl configured without B module\n";
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 # tests output to GLOB, using perlio feature directly
132 skip "no perlio on this build", 122
133 unless $Config::Config{useperlio};
135 set_style_standard('concise'); # MUST CALL before output needed
138 -basic -exec -tree -compact -loose -vt -ascii
139 -base10 -bigendian -littleendian
141 foreach $opt (@options) {
142 walk_output(\my $out);
143 my $treegen = B::Concise::compile($opt, $func);
146 isnt($out, '', "got output with option $opt");
149 ## test output control via walk_output
151 my $treegen = B::Concise::compile('-basic', $func); # reused
153 { # test output into a package global string (sprintf-ish)
155 walk_output(\$thing);
157 ok($thing, "walk_output to our SCALAR, output seen");
160 # test walkoutput acceptance of a scalar-bound IO handle
161 open (my $fh, '>', \my $buf);
164 ok($buf, "walk_output to GLOB, output seen");
166 ## Test B::Concise::compile error checking
168 # call compile on non-CODE ref items
170 # pending STASH splaying
172 foreach my $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
182 # test against a bogus autovivified subref.
183 # in debugger, it should look like:
185 # -> &CODE(0x84840cc) in ???
187 eval { B::Concise::compile('-basic', \&nosuchfunc)->() };
188 like ($@, qr/^err: coderef has no START/,
189 "compile detects CODE-ref w/o actual code");
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");
199 pass ("TEST POST-COMPILE OPTION-HANDLING IN WALKER SUBROUTINE");
203 my $walker = B::Concise::compile('-basic', $func);
204 walk_output(\$sample);
206 like($sample, qr/goto/m, "post-compile -exec");
208 walk_output(\$sample);
210 unlike($sample, qr/goto/m, "post-compile -basic");
213 # bang at it combinatorically
215 my @modes = qw( -basic -exec );
216 my @styles = qw( -concise -debug -linenoise -terse );
219 for $style (@styles) {
221 walk_output(\$sample);
223 $walker->($style, $mode);
224 $combos{"$style$mode"} = $sample;
227 # crosscheck that samples are all text-different
228 @list = sort keys %combos;
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");
236 # add samples with styles in different order
238 for $style (@styles) {
240 walk_output(\$sample);
241 $walker->($mode, $style);
242 $combos{"$mode$style"} = $sample;
245 # test commutativity of flags, ie that AB == BA
247 for $style (@styles) {
248 is ( $combos{"$style$mode"},
249 $combos{"$mode$style"},
250 "results for $style$mode vs $mode$style are the same" );
255 %combos = (); # outputs for $mode=any($order) and any($style)
257 # add more samples with switching modes & sticky styles
258 for $style (@styles) {
259 walk_output(\$sample);
263 walk_output(\$sample);
266 $combos{"$style/$mode"} = $sample;
269 # crosscheck that samples are all text-different
270 @nm = sort keys %combos;
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");
278 # add samples with switching styles & sticky modes
280 walk_output(\$sample);
283 for $style (@styles) {
284 walk_output(\$sample);
287 $combos{"$mode/$style"} = $sample;
290 # test commutativity of flags, ie that AB == BA
292 for $style (@styles) {
293 is ( $combos{"$style/$mode"},
294 $combos{"$mode/$style"},
295 "results for $style/$mode vs $mode/$style are the same" );
300 #now do double crosschecks: commutativity across stick / nostick
301 %combos = (%combos, %save);
303 # test commutativity of flags, ie that AB == BA
305 for $style (@styles) {
307 is ( $combos{"$style$mode"},
308 $combos{"$style/$mode"},
309 "$style$mode VS $style/$mode are the same" );
311 is ( $combos{"$mode$style"},
312 $combos{"$mode/$style"},
313 "$mode$style VS $mode/$style are the same" );
315 is ( $combos{"$style$mode"},
316 $combos{"$mode/$style"},
317 "$style$mode VS $mode/$style are the same" );
319 is ( $combos{"$mode$style"},
320 $combos{"$style/$mode"},
321 "$mode$style VS $style/$mode are the same" );