11 require_ok("B::Concise");
13 $out = runperl(switches => ["-MO=Concise"], prog => '$a', stderr => 1);
15 # If either of the next two tests fail, it probably means you need to
16 # fix the section labeled 'fragile kludge' in Concise.pm
18 ($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
20 is($op_base, 1, "Smallest OP sequence number");
22 ($op_base_p1, $cop_base)
23 = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
25 is($op_base_p1, 2, "Second-smallest OP sequence number");
27 is($cop_base, 1, "Smallest COP sequence number");
29 # test that with -exec B::Concise navigates past logops (bug #18175)
32 switches => ["-MO=Concise,-exec"],
33 prog => q{$a=$b && print q/foo/},
37 like($out, qr/print/, "'-exec' option output has print opcode");
39 ######## API tests v.60
41 use Config; # used for perlio check
42 B::Concise->import(qw( set_style set_style_standard add_callback
43 add_style walk_output reset_sequence ));
45 ## walk_output argument checking
47 # test that walk_output rejects non-HANDLE args
48 foreach my $foo ("string", [], {}) {
49 eval { walk_output($foo) };
50 isnt ($@, '', "walk_output() rejects arg '$foo'");
51 $@=''; # clear the fail for next test
53 # test accessor mode when arg undefd or 0
54 foreach my $foo (undef, 0) {
55 my $handle = walk_output($foo);
56 is ($handle, \*STDOUT, "walk_output set to STDOUT (default)");
59 { # any object that can print should be ok for walk_output
61 sub new { my $foo = bless {} };
62 sub print { CORE::print @_ }
64 my $foo = new Hugo; # suggested this API fix
65 eval { walk_output($foo) };
66 is ($@, '', "walk_output() accepts obj that can print");
68 # test that walk_output accepts a HANDLE arg
70 skip("no perlio in this build", 4)
71 unless $Config::Config{useperlio};
73 foreach my $foo (\*STDOUT, \*STDERR) {
74 eval { walk_output($foo) };
75 is ($@, '', "walk_output() accepts STD* " . ref $foo);
78 # now test a ref to scalar
79 eval { walk_output(\my $junk) };
80 is ($@, '', "walk_output() accepts ref-to-sprintf target");
83 eval { walk_output(\$junk) };
84 is ($@, '', "walk_output() accepts ref-to-non-empty-scalar");
90 eval { add_style ('junk_B' => @stylespec) };
91 like ($@, 'expecting 3 style-format args',
92 "add_style rejects insufficient args");
94 @stylespec = (0,0,0); # right length, invalid values
96 eval { add_style ('junk' => @stylespec) };
97 is ($@, '', "add_style accepts: stylename => 3-arg-array");
100 eval { add_style (junk => @stylespec) };
101 like ($@, qr/style 'junk' already exists, choose a new name/,
102 "add_style correctly disallows re-adding same style-name" );
104 # test new arg-checks on set_style
106 eval { set_style (@stylespec) };
107 is ($@, '', "set_style accepts 3 style-format args");
109 @stylespec = (); # bad style
111 eval { set_style (@stylespec) };
112 like ($@, qr/expecting 3 style-format args/,
113 "set_style rejects bad style-format args");
115 #### for content with doc'd options
117 my $func = sub{ $a = $b+42 }; # canonical example asub
120 # tests output to GLOB, using perlio feature directly
121 skip "no perlio on this build", 122
122 unless $Config::Config{useperlio};
124 set_style_standard('concise'); # MUST CALL before output needed
127 -basic -exec -tree -compact -loose -vt -ascii
128 -base10 -bigendian -littleendian
130 foreach $opt (@options) {
131 walk_output(\my $out);
132 my $treegen = B::Concise::compile($opt, $func);
135 isnt($out, '', "got output with option $opt");
138 ## test output control via walk_output
140 my $treegen = B::Concise::compile('-basic', $func); # reused
142 { # test output into a package global string (sprintf-ish)
144 walk_output(\$thing);
146 ok($thing, "walk_output to our SCALAR, output seen");
149 # test walkoutput acceptance of a scalar-bound IO handle
150 open (my $fh, '>', \my $buf);
153 ok($buf, "walk_output to GLOB, output seen");
155 ## Test B::Concise::compile error checking
157 # call compile on non-CODE ref items
159 # pending STASH splaying
161 foreach my $ref ([], {}) {
163 walk_output(\my $out);
164 eval { B::Concise::compile('-basic', $ref)->() };
165 like ($@, qr/^err: not a coderef: $typ/,
166 "compile detects $typ-ref where expecting subref");
167 # is($out,'', "no output when errd"); # announcement prints
171 # test against a bogus autovivified subref.
172 # in debugger, it should look like:
174 # -> &CODE(0x84840cc) in ???
176 eval { B::Concise::compile('-basic', \&nosuchfunc)->() };
177 like ($@, qr/^err: coderef has no START/,
178 "compile detects CODE-ref w/o actual code");
180 foreach my $opt (qw( -concise -exec )) {
181 eval { B::Concise::compile($opt,'non_existent_function')->() };
182 like ($@, qr/unknown function \(main::non_existent_function\)/,
183 "'$opt' reports non-existent-function properly");
188 pass ("TEST POST-COMPILE OPTION-HANDLING IN WALKER SUBROUTINE");
192 my $walker = B::Concise::compile('-basic', $func);
193 walk_output(\$sample);
195 like($sample, qr/goto/m, "post-compile -exec");
197 walk_output(\$sample);
199 unlike($sample, qr/goto/m, "post-compile -basic");
202 # bang at it combinatorically
204 my @modes = qw( -basic -exec );
205 my @styles = qw( -concise -debug -linenoise -terse );
208 for $style (@styles) {
210 walk_output(\$sample);
212 $walker->($style, $mode);
213 $combos{"$style$mode"} = $sample;
216 # crosscheck that samples are all text-different
217 @list = sort keys %combos;
219 for $j ($i+1..$#list) {
220 isnt ($combos{$list[$i]}, $combos{$list[$j]},
221 "combos for $list[$i] and $list[$j] are different, as expected");
225 # add samples with styles in different order
227 for $style (@styles) {
229 walk_output(\$sample);
230 $walker->($mode, $style);
231 $combos{"$mode$style"} = $sample;
234 # test commutativity of flags, ie that AB == BA
236 for $style (@styles) {
237 is ( $combos{"$style$mode"},
238 $combos{"$mode$style"},
239 "results for $style$mode vs $mode$style are the same" );
244 my %combos; # outputs for $mode=any($order) and any($style)
246 # add more samples with switching modes & sticky styles
247 for $style (@styles) {
248 walk_output(\$sample);
252 walk_output(\$sample);
255 $combos{"$style/$mode"} = $sample;
258 # crosscheck that samples are all text-different
259 @nm = sort keys %combos;
261 for $j ($i+1..$#nm) {
262 isnt ($combos{$nm[$i]}, $combos{$nm[$j]},
263 "results for $nm[$i] and $nm[$j] are different, as expected");
267 # add samples with switching styles & sticky modes
269 walk_output(\$sample);
272 for $style (@styles) {
273 walk_output(\$sample);
276 $combos{"$mode/$style"} = $sample;
279 # test commutativity of flags, ie that AB == BA
281 for $style (@styles) {
282 is ( $combos{"$style/$mode"},
283 $combos{"$mode/$style"},
284 "results for $style/$mode vs $mode/$style are the same" );
289 #now do double crosschecks: commutativity across stick / nostick
290 my %combos = (%combos, %save);
292 # test commutativity of flags, ie that AB == BA
294 for $style (@styles) {
296 is ( $combos{"$style$mode"},
297 $combos{"$style/$mode"},
298 "$style$mode VS $style/$mode are the same" );
300 is ( $combos{"$mode$style"},
301 $combos{"$mode/$style"},
302 "$mode$style VS $mode/$style are the same" );
304 is ( $combos{"$style$mode"},
305 $combos{"$mode/$style"},
306 "$style$mode VS $mode/$style are the same" );
308 is ( $combos{"$mode$style"},
309 $combos{"$style/$mode"},
310 "$mode$style VS $style/$mode are the same" );