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