This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
The tests for the -ostash option to B::Concise will have been failing
[perl5.git] / ext / B / t / concise.t
1 #!./perl
2
3 BEGIN {
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     }
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     }
16     require 'test.pl';          # we use runperl from 'test.pl', so can't use Test::More
17 }
18
19 plan tests => 157;
20
21 require_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
28 ($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
29
30 is($op_base, 1, "Smallest OP sequence number");
31
32 ($op_base_p1, $cop_base)
33   = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
34
35 is($op_base_p1, 2, "Second-smallest OP sequence number");
36
37 is($cop_base, 1, "Smallest COP sequence number");
38
39 # test that with -exec B::Concise navigates past logops (bug #18175)
40
41 $out = runperl(
42     switches => ["-MO=Concise,-exec"],
43     prog => q{$a=$b && print q/foo/},
44     stderr => 1,
45 );
46 #diag($out);
47 like($out, qr/print/, "'-exec' option output has print opcode");
48
49 ######## API tests v.60 
50
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 ));
54
55 ## walk_output argument checking
56
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
62 }
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)");
67 }
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 }
74 my $foo = new Hugo;     # suggested this API fix
75 eval {  walk_output($foo) };
76 is ($@, '', "walk_output() accepts obj that can print");
77
78 # test that walk_output accepts a HANDLE arg
79 SKIP: {
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 }
96
97 ## add_style
98 my @stylespec;
99 $@='';
100 eval { add_style ('junk_B' => @stylespec) };
101 like ($@, 'expecting 3 style-format args',
102     "add_style rejects insufficient args");
103
104 @stylespec = (0,0,0); # right length, invalid values
105 $@='';
106 eval { add_style ('junk' => @stylespec) };
107 is ($@, '', "add_style accepts: stylename => 3-arg-array");
108
109 $@='';
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" );
113
114 # test new arg-checks on set_style
115 $@='';
116 eval { set_style (@stylespec) };
117 is ($@, '', "set_style accepts 3 style-format args");
118
119 @stylespec = (); # bad style
120
121 eval { set_style (@stylespec) };
122 like ($@, qr/expecting 3 style-format args/,
123       "set_style rejects bad style-format args");
124
125 #### for content with doc'd options
126
127 our($a, $b);
128 my $func = sub{ $a = $b+42 };   # canonical example asub
129
130 sub 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
138 SKIP: {
139     # tests output to GLOB, using perlio feature directly
140     skip "no perlio on this build", 127
141         unless $Config::Config{useperlio};
142     
143     set_style_standard('concise');  # MUST CALL before output needed
144     
145     @options = qw(
146                   -basic -exec -tree -compact -loose -vt -ascii
147                   -base10 -bigendian -littleendian
148                   );
149     foreach $opt (@options) {
150         ($out) = render($opt, $func);
151         isnt($out, '', "got output with option $opt");
152     }
153     
154     ## test output control via walk_output
155     
156     my $treegen = B::Concise::compile('-basic', $func); # reused
157     
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     
165     # test walkoutput acceptance of a scalar-bound IO handle
166     open (my $fh, '>', \my $buf);
167     walk_output($fh);
168     $treegen->();
169     ok($buf, "walk_output to GLOB, output seen");
170     
171     ## test B::Concise::compile error checking
172     
173     # call compile on non-CODE ref items
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");
183             is($out,'', "no output when errd"); # announcement prints
184         }
185     }
186     
187     # test against a bogus autovivified subref.
188     # in debugger, it should look like:
189     #  1  CODE(0x84840cc)
190     #      -> &CODE(0x84840cc) in ???
191
192     my ($res,$err);
193     TODO: {
194         #local $TODO = "\tdoes this handling make sense ?";
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);
203         my @lines = split(/\n/, $res);
204         is(scalar @lines, 3,
205            "'sub defd_empty {}' seen as 3 liner");
206
207         is(1, $res =~ /leavesub/ && $res =~ /(next|db)state/,
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';
217             sub AUTOLOAD { print "# in AUTOLOAD body: $AUTOLOAD\n" }
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);
228         like ($res, qr/in AUTOLOAD body: /, "found body of Bar::AUTOLOAD");
229
230     }
231     ($res,$err) = render('-basic', Foo::bar);
232     like ($res, qr/unknown function \(Foo::bar\)/,
233           "BC::compile detects fn-name as unknown function");
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;
293     %combos = ();       # outputs for $mode=any($order) and any($style)
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
339     %combos = (%combos, %save);
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     }
362 }
363
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
379 like($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
386 like($out, qr/Config::AUTOLOAD exists in stash, but has no START/,
387     "coderef properly undefined");
388
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
395 like($out, qr/FUNC: \*B::Concise::concise_cv_obj/,
396      "stash rendering of B::Concise includes Concise::concise_cv_obj");
397
398 like($out, qr/FUNC: \*B::Concise::walk_output/,
399      "stash rendering includes Concise::walk_output");
400
401 like($out, qr/FUNC: \*B::Concise::PAD_FAKELEX_MULTI/,
402      "stash rendering includes constant sub: PAD_FAKELEX_MULTI");
403
404 like($out, qr/PAD_FAKELEX_MULTI is a constant sub, optimized to a IV/,
405      "stash rendering identifies it as constant");
406
407 like($out, qr/\# 4\d\d: \s+ \$l->concise\(\$level\);/,
408      "src-line rendering works");
409
410 $out = runperl ( switches => ["-MO=Concise,-stash=ExtUtils::Mksymlists,-src,-exec"],
411                  prog => '-e 1', stderr => 1 );
412
413 like($out, qr/FUNC: \*ExtUtils::Mksymlists::_write_vms/,
414      "stash rendering loads package as needed");
415
416 $out = runperl ( switches => ["-MO=Concise,-stash=Data::Dumper,-src,-exec"],
417                  prog => '-e 1', stderr => 1 );
418
419 {
420     local $TODO = q(require $package unless ${$package.'::'}; doesn't do what you want under static linking) unless $Config{usedl};
421     like($out, qr/FUNC: \*Data::Dumper::format_refaddr/,
422          "stash rendering loads package as needed");
423 }
424
425 my $prog = q{package FOO; sub bar { print "bar" } package main; FOO::bar(); };
426
427 # this would fail if %INC used for -stash test
428 $out = runperl ( switches => ["-MO=Concise,-src,-stash=FOO,-main"],
429                  prog => $prog, stderr => 1 );
430
431 like($out, qr/FUNC: \*FOO::bar/,
432      "stash rendering works on inlined package");
433
434 __END__