Commit | Line | Data |
c517cc47 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't'; |
5 | @INC = '../lib'; |
9cd8f857 |
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 | } |
c517cc47 |
11 | require './test.pl'; |
12 | } |
13 | |
cc02ea56 |
14 | plan tests => 142; |
c517cc47 |
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 | |
c33fe613 |
23 | ($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m); |
c517cc47 |
24 | |
c33fe613 |
25 | is($op_base, 1, "Smallest OP sequence number"); |
c517cc47 |
26 | |
c27ea44e |
27 | ($op_base_p1, $cop_base) |
28 | = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m); |
c517cc47 |
29 | |
c33fe613 |
30 | is($op_base_p1, 2, "Second-smallest OP sequence number"); |
31 | |
32 | is($cop_base, 1, "Smallest COP sequence number"); |
62e36f8a |
33 | |
34 | # test that with -exec B::Concise navigates past logops (bug #18175) |
35 | |
36 | $out = runperl( |
37 | switches => ["-MO=Concise,-exec"], |
cc02ea56 |
38 | prog => q{$a=$b && print q/foo/}, |
62e36f8a |
39 | stderr => 1, |
40 | ); |
41 | |
724aa791 |
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 |
cc02ea56 |
47 | B::Concise->import(qw( set_style set_style_standard add_callback |
48 | add_style walk_output reset_sequence )); |
724aa791 |
49 | |
50 | ## walk_output argument checking |
51 | |
724aa791 |
52 | # test that walk_output rejects non-HANDLE args |
cc02ea56 |
53 | foreach my $foo ("string", [], {}) { |
724aa791 |
54 | eval { walk_output($foo) }; |
55 | isnt ($@, '', "walk_output() rejects arg '$foo'"); |
56 | $@=''; # clear the fail for next test |
57 | } |
cc02ea56 |
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 | } |
724aa791 |
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 | |
2ce64696 |
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 | } |
724aa791 |
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 | |
724aa791 |
120 | #### for content with doc'd options |
2ce64696 |
121 | |
cc02ea56 |
122 | my $func = sub{ $a = $b+42 }; # canonical example asub |
2ce64696 |
123 | |
cc02ea56 |
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 | |
2ce64696 |
131 | @options = qw( |
cc02ea56 |
132 | -basic -exec -tree -compact -loose -vt -ascii |
2ce64696 |
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 | } |
cc02ea56 |
142 | |
2ce64696 |
143 | ## test output control via walk_output |
cc02ea56 |
144 | |
2ce64696 |
145 | my $treegen = B::Concise::compile('-basic', $func); # reused |
cc02ea56 |
146 | |
2ce64696 |
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 | |
cc02ea56 |
154 | # test walkoutput acceptance of a scalar-bound IO handle |
724aa791 |
155 | open (my $fh, '>', \my $buf); |
156 | walk_output($fh); |
157 | $treegen->(); |
158 | ok($buf, "walk_output to GLOB, output seen"); |
cc02ea56 |
159 | |
2ce64696 |
160 | ## Test B::Concise::compile error checking |
cc02ea56 |
161 | |
2ce64696 |
162 | # call compile on non-CODE ref items |
cc02ea56 |
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 | } |
2ce64696 |
174 | } |
cc02ea56 |
175 | |
2ce64696 |
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; |
cc02ea56 |
181 | eval { B::Concise::compile('-basic', \&nosuchfunc)->() }; |
2ce64696 |
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 | } |
cc02ea56 |
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 | } |
724aa791 |
318 | } |
cc02ea56 |
319 | |
320 | __END__ |
321 | |