D'oh. Don't turn on warnings on the #! line without actually testing
[p5sagit/p5-mst-13.2.git] / ext / B / t / concise.t
CommitLineData
c517cc47 1#!./perl
2
3BEGIN {
4 chdir 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
cc02ea56 9plan tests => 142;
c517cc47 10
11require_ok("B::Concise");
12
13$out = runperl(switches => ["-MO=Concise"], prog => '$a', stderr => 1);
14
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
17
c33fe613 18($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
c517cc47 19
c33fe613 20is($op_base, 1, "Smallest OP sequence number");
c517cc47 21
c27ea44e 22($op_base_p1, $cop_base)
23 = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
c517cc47 24
c33fe613 25is($op_base_p1, 2, "Second-smallest OP sequence number");
26
27is($cop_base, 1, "Smallest COP sequence number");
62e36f8a 28
29# test that with -exec B::Concise navigates past logops (bug #18175)
30
31$out = runperl(
32 switches => ["-MO=Concise,-exec"],
cc02ea56 33 prog => q{$a=$b && print q/foo/},
62e36f8a 34 stderr => 1,
35);
36
724aa791 37like($out, qr/print/, "'-exec' option output has print opcode");
38
39######## API tests v.60
40
41use Config; # used for perlio check
cc02ea56 42B::Concise->import(qw( set_style set_style_standard add_callback
43 add_style walk_output reset_sequence ));
724aa791 44
45## walk_output argument checking
46
724aa791 47# test that walk_output rejects non-HANDLE args
cc02ea56 48foreach my $foo ("string", [], {}) {
724aa791 49 eval { walk_output($foo) };
50 isnt ($@, '', "walk_output() rejects arg '$foo'");
51 $@=''; # clear the fail for next test
52}
cc02ea56 53# test accessor mode when arg undefd or 0
54foreach my $foo (undef, 0) {
55 my $handle = walk_output($foo);
56 is ($handle, \*STDOUT, "walk_output set to STDOUT (default)");
57}
724aa791 58
59{ # any object that can print should be ok for walk_output
60 package Hugo;
61 sub new { my $foo = bless {} };
62 sub print { CORE::print @_ }
63}
64my $foo = new Hugo; # suggested this API fix
65eval { walk_output($foo) };
66is ($@, '', "walk_output() accepts obj that can print");
67
2ce64696 68# test that walk_output accepts a HANDLE arg
69SKIP: {
70 skip("no perlio in this build", 4)
71 unless $Config::Config{useperlio};
72
73 foreach my $foo (\*STDOUT, \*STDERR) {
74 eval { walk_output($foo) };
75 is ($@, '', "walk_output() accepts STD* " . ref $foo);
76 }
77
78 # now test a ref to scalar
79 eval { walk_output(\my $junk) };
80 is ($@, '', "walk_output() accepts ref-to-sprintf target");
81
82 $junk = "non-empty";
83 eval { walk_output(\$junk) };
84 is ($@, '', "walk_output() accepts ref-to-non-empty-scalar");
85}
724aa791 86
87## add_style
88my @stylespec;
89$@='';
90eval { add_style ('junk_B' => @stylespec) };
91like ($@, 'expecting 3 style-format args',
92 "add_style rejects insufficient args");
93
94@stylespec = (0,0,0); # right length, invalid values
95$@='';
96eval { add_style ('junk' => @stylespec) };
97is ($@, '', "add_style accepts: stylename => 3-arg-array");
98
99$@='';
100eval { add_style (junk => @stylespec) };
101like ($@, qr/style 'junk' already exists, choose a new name/,
102 "add_style correctly disallows re-adding same style-name" );
103
104# test new arg-checks on set_style
105$@='';
106eval { set_style (@stylespec) };
107is ($@, '', "set_style accepts 3 style-format args");
108
109@stylespec = (); # bad style
110
111eval { set_style (@stylespec) };
112like ($@, qr/expecting 3 style-format args/,
113 "set_style rejects bad style-format args");
114
724aa791 115#### for content with doc'd options
2ce64696 116
cc02ea56 117my $func = sub{ $a = $b+42 }; # canonical example asub
2ce64696 118
cc02ea56 119SKIP: {
120 # tests output to GLOB, using perlio feature directly
121 skip "no perlio on this build", 122
122 unless $Config::Config{useperlio};
123
124 set_style_standard('concise'); # MUST CALL before output needed
125
2ce64696 126 @options = qw(
cc02ea56 127 -basic -exec -tree -compact -loose -vt -ascii
2ce64696 128 -base10 -bigendian -littleendian
129 );
130 foreach $opt (@options) {
131 walk_output(\my $out);
132 my $treegen = B::Concise::compile($opt, $func);
133 $treegen->();
134 #print "foo:$out\n";
135 isnt($out, '', "got output with option $opt");
136 }
cc02ea56 137
2ce64696 138 ## test output control via walk_output
cc02ea56 139
2ce64696 140 my $treegen = B::Concise::compile('-basic', $func); # reused
cc02ea56 141
2ce64696 142 { # test output into a package global string (sprintf-ish)
143 our $thing;
144 walk_output(\$thing);
145 $treegen->();
146 ok($thing, "walk_output to our SCALAR, output seen");
147 }
148
cc02ea56 149 # test walkoutput acceptance of a scalar-bound IO handle
724aa791 150 open (my $fh, '>', \my $buf);
151 walk_output($fh);
152 $treegen->();
153 ok($buf, "walk_output to GLOB, output seen");
cc02ea56 154
2ce64696 155 ## Test B::Concise::compile error checking
cc02ea56 156
2ce64696 157 # call compile on non-CODE ref items
cc02ea56 158 if (0) {
159 # pending STASH splaying
160
161 foreach my $ref ([], {}) {
162 my $typ = ref $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
168 }
2ce64696 169 }
cc02ea56 170
2ce64696 171 # test against a bogus autovivified subref.
172 # in debugger, it should look like:
173 # 1 CODE(0x84840cc)
174 # -> &CODE(0x84840cc) in ???
175 sub nosuchfunc;
cc02ea56 176 eval { B::Concise::compile('-basic', \&nosuchfunc)->() };
2ce64696 177 like ($@, qr/^err: coderef has no START/,
178 "compile detects CODE-ref w/o actual code");
179
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");
184 }
cc02ea56 185
186 # v.62 tests
187
188 pass ("TEST POST-COMPILE OPTION-HANDLING IN WALKER SUBROUTINE");
189
190 my $sample;
191
192 my $walker = B::Concise::compile('-basic', $func);
193 walk_output(\$sample);
194 $walker->('-exec');
195 like($sample, qr/goto/m, "post-compile -exec");
196
197 walk_output(\$sample);
198 $walker->('-basic');
199 unlike($sample, qr/goto/m, "post-compile -basic");
200
201
202 # bang at it combinatorically
203 my %combos;
204 my @modes = qw( -basic -exec );
205 my @styles = qw( -concise -debug -linenoise -terse );
206
207 # prep samples
208 for $style (@styles) {
209 for $mode (@modes) {
210 walk_output(\$sample);
211 reset_sequence();
212 $walker->($style, $mode);
213 $combos{"$style$mode"} = $sample;
214 }
215 }
216 # crosscheck that samples are all text-different
217 @list = sort keys %combos;
218 for $i (0..$#list) {
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");
222 }
223 }
224
225 # add samples with styles in different order
226 for $mode (@modes) {
227 for $style (@styles) {
228 reset_sequence();
229 walk_output(\$sample);
230 $walker->($mode, $style);
231 $combos{"$mode$style"} = $sample;
232 }
233 }
234 # test commutativity of flags, ie that AB == BA
235 for $mode (@modes) {
236 for $style (@styles) {
237 is ( $combos{"$style$mode"},
238 $combos{"$mode$style"},
239 "results for $style$mode vs $mode$style are the same" );
240 }
241 }
242
243 my %save = %combos;
244 my %combos; # outputs for $mode=any($order) and any($style)
245
246 # add more samples with switching modes & sticky styles
247 for $style (@styles) {
248 walk_output(\$sample);
249 reset_sequence();
250 $walker->($style);
251 for $mode (@modes) {
252 walk_output(\$sample);
253 reset_sequence();
254 $walker->($mode);
255 $combos{"$style/$mode"} = $sample;
256 }
257 }
258 # crosscheck that samples are all text-different
259 @nm = sort keys %combos;
260 for $i (0..$#nm) {
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");
264 }
265 }
266
267 # add samples with switching styles & sticky modes
268 for $mode (@modes) {
269 walk_output(\$sample);
270 reset_sequence();
271 $walker->($mode);
272 for $style (@styles) {
273 walk_output(\$sample);
274 reset_sequence();
275 $walker->($style);
276 $combos{"$mode/$style"} = $sample;
277 }
278 }
279 # test commutativity of flags, ie that AB == BA
280 for $mode (@modes) {
281 for $style (@styles) {
282 is ( $combos{"$style/$mode"},
283 $combos{"$mode/$style"},
284 "results for $style/$mode vs $mode/$style are the same" );
285 }
286 }
287
288
289 #now do double crosschecks: commutativity across stick / nostick
290 my %combos = (%combos, %save);
291
292 # test commutativity of flags, ie that AB == BA
293 for $mode (@modes) {
294 for $style (@styles) {
295
296 is ( $combos{"$style$mode"},
297 $combos{"$style/$mode"},
298 "$style$mode VS $style/$mode are the same" );
299
300 is ( $combos{"$mode$style"},
301 $combos{"$mode/$style"},
302 "$mode$style VS $mode/$style are the same" );
303
304 is ( $combos{"$style$mode"},
305 $combos{"$mode/$style"},
306 "$style$mode VS $mode/$style are the same" );
307
308 is ( $combos{"$mode$style"},
309 $combos{"$style/$mode"},
310 "$mode$style VS $style/$mode are the same" );
311 }
312 }
724aa791 313}
cc02ea56 314
315__END__
316