Upgrade to Win32API-File 0.1101
[p5sagit/p5-mst-13.2.git] / ext / B / t / concise.t
CommitLineData
c517cc47 1#!./perl
2
3BEGIN {
5638aaac 4 if ($ENV{PERL_CORE}){
5 chdir('t') if -d 't';
6 @INC = ('.', '../lib');
7 } else {
8 unshift @INC, 't';
9 push @INC, "../../t";
10 }
9cd8f857 11 require Config;
12 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
13 print "1..0 # Skip -- Perl configured without B module\n";
14 exit 0;
15 }
c0939cee 16 require 'test.pl'; # we use runperl from 'test.pl', so can't use Test::More
c517cc47 17}
18
4d3af52d 19plan tests => 157;
c517cc47 20
21require_ok("B::Concise");
22
23$out = runperl(switches => ["-MO=Concise"], prog => '$a', stderr => 1);
24
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
27
c33fe613 28($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
c517cc47 29
c33fe613 30is($op_base, 1, "Smallest OP sequence number");
c517cc47 31
c27ea44e 32($op_base_p1, $cop_base)
33 = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
c517cc47 34
c33fe613 35is($op_base_p1, 2, "Second-smallest OP sequence number");
36
37is($cop_base, 1, "Smallest COP sequence number");
62e36f8a 38
39# test that with -exec B::Concise navigates past logops (bug #18175)
40
41$out = runperl(
42 switches => ["-MO=Concise,-exec"],
cc02ea56 43 prog => q{$a=$b && print q/foo/},
62e36f8a 44 stderr => 1,
45);
c0939cee 46#diag($out);
724aa791 47like($out, qr/print/, "'-exec' option output has print opcode");
48
49######## API tests v.60
50
51use Config; # used for perlio check
cc02ea56 52B::Concise->import(qw( set_style set_style_standard add_callback
53 add_style walk_output reset_sequence ));
724aa791 54
55## walk_output argument checking
56
724aa791 57# test that walk_output rejects non-HANDLE args
cc02ea56 58foreach my $foo ("string", [], {}) {
724aa791 59 eval { walk_output($foo) };
60 isnt ($@, '', "walk_output() rejects arg '$foo'");
61 $@=''; # clear the fail for next test
62}
cc02ea56 63# test accessor mode when arg undefd or 0
64foreach my $foo (undef, 0) {
65 my $handle = walk_output($foo);
66 is ($handle, \*STDOUT, "walk_output set to STDOUT (default)");
67}
724aa791 68
69{ # any object that can print should be ok for walk_output
70 package Hugo;
71 sub new { my $foo = bless {} };
72 sub print { CORE::print @_ }
73}
74my $foo = new Hugo; # suggested this API fix
75eval { walk_output($foo) };
76is ($@, '', "walk_output() accepts obj that can print");
77
2ce64696 78# test that walk_output accepts a HANDLE arg
79SKIP: {
80 skip("no perlio in this build", 4)
81 unless $Config::Config{useperlio};
82
83 foreach my $foo (\*STDOUT, \*STDERR) {
84 eval { walk_output($foo) };
85 is ($@, '', "walk_output() accepts STD* " . ref $foo);
86 }
87
88 # now test a ref to scalar
89 eval { walk_output(\my $junk) };
90 is ($@, '', "walk_output() accepts ref-to-sprintf target");
91
92 $junk = "non-empty";
93 eval { walk_output(\$junk) };
94 is ($@, '', "walk_output() accepts ref-to-non-empty-scalar");
95}
724aa791 96
97## add_style
98my @stylespec;
99$@='';
100eval { add_style ('junk_B' => @stylespec) };
101like ($@, 'expecting 3 style-format args',
102 "add_style rejects insufficient args");
103
104@stylespec = (0,0,0); # right length, invalid values
105$@='';
106eval { add_style ('junk' => @stylespec) };
107is ($@, '', "add_style accepts: stylename => 3-arg-array");
108
109$@='';
110eval { add_style (junk => @stylespec) };
111like ($@, qr/style 'junk' already exists, choose a new name/,
112 "add_style correctly disallows re-adding same style-name" );
113
114# test new arg-checks on set_style
115$@='';
116eval { set_style (@stylespec) };
117is ($@, '', "set_style accepts 3 style-format args");
118
119@stylespec = (); # bad style
120
121eval { set_style (@stylespec) };
122like ($@, qr/expecting 3 style-format args/,
c0939cee 123 "set_style rejects bad style-format args");
724aa791 124
724aa791 125#### for content with doc'd options
2ce64696 126
5638aaac 127our($a, $b);
cc02ea56 128my $func = sub{ $a = $b+42 }; # canonical example asub
2ce64696 129
c0939cee 130sub render {
131 walk_output(\my $out);
132 eval { B::Concise::compile(@_)->() };
133 # diag "rendering $@\n";
134 return ($out, $@) if wantarray;
135 return $out;
136}
137
cc02ea56 138SKIP: {
139 # tests output to GLOB, using perlio feature directly
28380c63 140 skip "no perlio on this build", 127
cc02ea56 141 unless $Config::Config{useperlio};
142
143 set_style_standard('concise'); # MUST CALL before output needed
144
2ce64696 145 @options = qw(
cc02ea56 146 -basic -exec -tree -compact -loose -vt -ascii
2ce64696 147 -base10 -bigendian -littleendian
148 );
149 foreach $opt (@options) {
c0939cee 150 ($out) = render($opt, $func);
2ce64696 151 isnt($out, '', "got output with option $opt");
152 }
cc02ea56 153
2ce64696 154 ## test output control via walk_output
cc02ea56 155
2ce64696 156 my $treegen = B::Concise::compile('-basic', $func); # reused
cc02ea56 157
2ce64696 158 { # test output into a package global string (sprintf-ish)
159 our $thing;
160 walk_output(\$thing);
161 $treegen->();
162 ok($thing, "walk_output to our SCALAR, output seen");
163 }
164
cc02ea56 165 # test walkoutput acceptance of a scalar-bound IO handle
724aa791 166 open (my $fh, '>', \my $buf);
167 walk_output($fh);
168 $treegen->();
169 ok($buf, "walk_output to GLOB, output seen");
cc02ea56 170
c0939cee 171 ## test B::Concise::compile error checking
cc02ea56 172
2ce64696 173 # call compile on non-CODE ref items
cc02ea56 174 if (0) {
175 # pending STASH splaying
176
177 foreach my $ref ([], {}) {
178 my $typ = ref $ref;
179 walk_output(\my $out);
180 eval { B::Concise::compile('-basic', $ref)->() };
181 like ($@, qr/^err: not a coderef: $typ/,
182 "compile detects $typ-ref where expecting subref");
c0939cee 183 is($out,'', "no output when errd"); # announcement prints
cc02ea56 184 }
2ce64696 185 }
cc02ea56 186
2ce64696 187 # test against a bogus autovivified subref.
188 # in debugger, it should look like:
189 # 1 CODE(0x84840cc)
190 # -> &CODE(0x84840cc) in ???
c0939cee 191
192 my ($res,$err);
193 TODO: {
e75702e9 194 #local $TODO = "\tdoes this handling make sense ?";
c0939cee 195
196 sub declared_only;
197 ($res,$err) = render('-basic', \&declared_only);
198 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
199 "'sub decl_only' seen as having no START");
200
201 sub defd_empty {};
202 ($res,$err) = render('-basic', \&defd_empty);
dd48e7ab 203 my @lines = split(/\n/, $res);
204 is(scalar @lines, 3,
c0939cee 205 "'sub defd_empty {}' seen as 3 liner");
206
9e0f9750 207 is(1, $res =~ /leavesub/ && $res =~ /(next|db)state/,
c0939cee 208 "'sub defd_empty {}' seen as 2 ops: leavesub,nextstate");
209
210 ($res,$err) = render('-basic', \&not_even_declared);
211 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
212 "'\&not_even_declared' seen as having no START");
213
214 {
215 package Bar;
216 our $AUTOLOAD = 'garbage';
e75702e9 217 sub AUTOLOAD { print "# in AUTOLOAD body: $AUTOLOAD\n" }
c0939cee 218 }
219 ($res,$err) = render('-basic', Bar::auto_func);
220 like ($res, qr/unknown function \(Bar::auto_func\)/,
221 "Bar::auto_func seen as unknown function");
222
223 ($res,$err) = render('-basic', \&Bar::auto_func);
224 like ($res, qr/coderef CODE\(0x[0-9a-fA-F]+\) has no START/,
225 "'\&Bar::auto_func' seen as having no START");
226
227 ($res,$err) = render('-basic', \&Bar::AUTOLOAD);
e75702e9 228 like ($res, qr/in AUTOLOAD body: /, "found body of Bar::AUTOLOAD");
c0939cee 229
2ce64696 230 }
c0939cee 231 ($res,$err) = render('-basic', Foo::bar);
232 like ($res, qr/unknown function \(Foo::bar\)/,
233 "BC::compile detects fn-name as unknown function");
cc02ea56 234
235 # v.62 tests
236
237 pass ("TEST POST-COMPILE OPTION-HANDLING IN WALKER SUBROUTINE");
238
239 my $sample;
240
241 my $walker = B::Concise::compile('-basic', $func);
242 walk_output(\$sample);
243 $walker->('-exec');
244 like($sample, qr/goto/m, "post-compile -exec");
245
246 walk_output(\$sample);
247 $walker->('-basic');
248 unlike($sample, qr/goto/m, "post-compile -basic");
249
250
251 # bang at it combinatorically
252 my %combos;
253 my @modes = qw( -basic -exec );
254 my @styles = qw( -concise -debug -linenoise -terse );
255
256 # prep samples
257 for $style (@styles) {
258 for $mode (@modes) {
259 walk_output(\$sample);
260 reset_sequence();
261 $walker->($style, $mode);
262 $combos{"$style$mode"} = $sample;
263 }
264 }
265 # crosscheck that samples are all text-different
266 @list = sort keys %combos;
267 for $i (0..$#list) {
268 for $j ($i+1..$#list) {
269 isnt ($combos{$list[$i]}, $combos{$list[$j]},
270 "combos for $list[$i] and $list[$j] are different, as expected");
271 }
272 }
273
274 # add samples with styles in different order
275 for $mode (@modes) {
276 for $style (@styles) {
277 reset_sequence();
278 walk_output(\$sample);
279 $walker->($mode, $style);
280 $combos{"$mode$style"} = $sample;
281 }
282 }
283 # test commutativity of flags, ie that AB == BA
284 for $mode (@modes) {
285 for $style (@styles) {
286 is ( $combos{"$style$mode"},
287 $combos{"$mode$style"},
288 "results for $style$mode vs $mode$style are the same" );
289 }
290 }
291
292 my %save = %combos;
5638aaac 293 %combos = (); # outputs for $mode=any($order) and any($style)
cc02ea56 294
295 # add more samples with switching modes & sticky styles
296 for $style (@styles) {
297 walk_output(\$sample);
298 reset_sequence();
299 $walker->($style);
300 for $mode (@modes) {
301 walk_output(\$sample);
302 reset_sequence();
303 $walker->($mode);
304 $combos{"$style/$mode"} = $sample;
305 }
306 }
307 # crosscheck that samples are all text-different
308 @nm = sort keys %combos;
309 for $i (0..$#nm) {
310 for $j ($i+1..$#nm) {
311 isnt ($combos{$nm[$i]}, $combos{$nm[$j]},
312 "results for $nm[$i] and $nm[$j] are different, as expected");
313 }
314 }
315
316 # add samples with switching styles & sticky modes
317 for $mode (@modes) {
318 walk_output(\$sample);
319 reset_sequence();
320 $walker->($mode);
321 for $style (@styles) {
322 walk_output(\$sample);
323 reset_sequence();
324 $walker->($style);
325 $combos{"$mode/$style"} = $sample;
326 }
327 }
328 # test commutativity of flags, ie that AB == BA
329 for $mode (@modes) {
330 for $style (@styles) {
331 is ( $combos{"$style/$mode"},
332 $combos{"$mode/$style"},
333 "results for $style/$mode vs $mode/$style are the same" );
334 }
335 }
336
337
338 #now do double crosschecks: commutativity across stick / nostick
5638aaac 339 %combos = (%combos, %save);
cc02ea56 340
341 # test commutativity of flags, ie that AB == BA
342 for $mode (@modes) {
343 for $style (@styles) {
344
345 is ( $combos{"$style$mode"},
346 $combos{"$style/$mode"},
347 "$style$mode VS $style/$mode are the same" );
348
349 is ( $combos{"$mode$style"},
350 $combos{"$mode/$style"},
351 "$mode$style VS $mode/$style are the same" );
352
353 is ( $combos{"$style$mode"},
354 $combos{"$mode/$style"},
355 "$style$mode VS $mode/$style are the same" );
356
357 is ( $combos{"$mode$style"},
358 $combos{"$style/$mode"},
359 "$mode$style VS $style/$mode are the same" );
360 }
361 }
724aa791 362}
cc02ea56 363
e75702e9 364
365# test proper NULLING of pointer, derefd by CvSTART, when a coderef is
366# undefd. W/o this, the pointer can dangle into freed and reused
367# optree mem, which no longer points to opcodes.
368
369# Using B::Concise to render Config::AUTOLOAD's optree at BEGIN-time
370# triggers this obscure bug, cuz AUTOLOAD has a bootstrap version,
371# which is used at load-time then undeffed. It is normally
372# re-vivified later, but not in time for this (BEGIN/CHECK)-time
373# rendering.
374
375$out = runperl ( switches => ["-MO=Concise,Config::AUTOLOAD"],
376 prog => 'use Config; BEGIN { $Config{awk} }',
377 stderr => 1 );
378
379like($out, qr/Config::AUTOLOAD exists in stash, but has no START/,
380 "coderef properly undefined");
381
382$out = runperl ( switches => ["-MO=Concise,Config::AUTOLOAD"],
383 prog => 'use Config; CHECK { $Config{awk} }',
384 stderr => 1 );
385
386like($out, qr/Config::AUTOLOAD exists in stash, but has no START/,
387 "coderef properly undefined");
388
9e0f9750 389# test -stash and -src rendering
390# todo: stderr=1 puts '-e syntax OK' into $out,
391# conceivably fouling one of the lines that are tested
392$out = runperl ( switches => ["-MO=Concise,-stash=B::Concise,-src"],
393 prog => '-e 1', stderr => 1 );
394
395like($out, qr/FUNC: \*B::Concise::concise_cv_obj/,
396 "stash rendering of B::Concise includes Concise::concise_cv_obj");
397
398like($out, qr/FUNC: \*B::Concise::walk_output/,
399 "stash rendering includes Concise::walk_output");
400
401like($out, qr/FUNC: \*B::Concise::PAD_FAKELEX_MULTI/,
402 "stash rendering includes constant sub: PAD_FAKELEX_MULTI");
403
404like($out, qr/PAD_FAKELEX_MULTI is a constant sub, optimized to a IV/,
405 "stash rendering identifies it as constant");
406
407like($out, qr/\# 4\d\d: \s+ \$l->concise\(\$level\);/,
408 "src-line rendering works");
cc02ea56 409
4d3af52d 410$out = runperl ( switches => ["-MO=Concise,-stash=ExtUtils::Mksymlists,-src,-exec"],
9e0f9750 411 prog => '-e 1', stderr => 1 );
412
4d3af52d 413like($out, qr/FUNC: \*ExtUtils::Mksymlists::_write_vms/,
9e0f9750 414 "stash rendering loads package as needed");
415
4d3af52d 416$out = runperl ( switches => ["-MO=Concise,-stash=Data::Dumper,-src,-exec"],
417 prog => '-e 1', stderr => 1 );
418
f667a15a 419like($out, qr/FUNC: \*Data::Dumper::format_refaddr/,
420 "stash rendering loads package as needed");
4d3af52d 421
bcfa18ea 422my $prog = q{package FOO; sub bar { print "bar" } package main; FOO::bar(); };
423
9e0f9750 424# this would fail if %INC used for -stash test
425$out = runperl ( switches => ["-MO=Concise,-src,-stash=FOO,-main"],
426 prog => $prog, stderr => 1 );
427
428like($out, qr/FUNC: \*FOO::bar/,
429 "stash rendering works on inlined package");
430
431__END__