Commit | Line | Data |
87a42246 |
1 | #!./perl |
2 | |
3 | BEGIN { |
5638aaac |
4 | if ($ENV{PERL_CORE}){ |
5 | chdir('t') if -d 't'; |
6 | if ($^O eq 'MacOS') { |
7 | @INC = qw(: ::lib ::macos:lib); |
8 | } else { |
9 | @INC = '.'; |
10 | push @INC, '../lib'; |
11 | } |
87a42246 |
12 | } else { |
5638aaac |
13 | unshift @INC, 't'; |
87a42246 |
14 | } |
9cd8f857 |
15 | require Config; |
16 | if (($Config::Config{'extensions'} !~ /\bB\b/) ){ |
17 | print "1..0 # Skip -- Perl configured without B module\n"; |
18 | exit 0; |
19 | } |
87a42246 |
20 | } |
21 | |
87a42246 |
22 | use warnings; |
23 | use strict; |
e9c69003 |
24 | BEGIN { |
25 | # BEGIN block is acutally a subroutine :-) |
26 | return unless $] > 5.009; |
27 | require feature; |
28 | feature->import(':5.10'); |
29 | } |
71c4dbc3 |
30 | use Test::More tests => 68; |
87a42246 |
31 | |
32 | use B::Deparse; |
09d856fb |
33 | my $deparse = B::Deparse->new(); |
34 | ok($deparse); |
87a42246 |
35 | |
36 | # Tell B::Deparse about our ambient pragmas |
0ced6c29 |
37 | { my ($hint_bits, $warning_bits, $hinthash); |
38 | BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); } |
87a42246 |
39 | $deparse->ambient_pragmas ( |
40 | hint_bits => $hint_bits, |
41 | warning_bits => $warning_bits, |
0ced6c29 |
42 | '$[' => 0 + $[, |
43 | '%^H' => $hinthash, |
87a42246 |
44 | ); |
45 | } |
46 | |
ad46c0be |
47 | $/ = "\n####\n"; |
48 | while (<DATA>) { |
49 | chomp; |
e9c69003 |
50 | # This code is pinched from the t/lib/common.pl for TODO. |
51 | # It's not clear how to avoid duplication |
52 | my ($skip, $skip_reason); |
53 | s/^#\s*SKIP\s*(.*)\n//m and $skip_reason = $1; |
54 | # If the SKIP reason starts ? then it's taken as a code snippet to evaluate |
55 | # This provides the flexibility to have conditional SKIPs |
56 | if ($skip_reason && $skip_reason =~ s/^\?//) { |
57 | my $temp = eval $skip_reason; |
58 | if ($@) { |
59 | die "# In SKIP code reason:\n# $skip_reason\n$@"; |
60 | } |
61 | $skip_reason = $temp; |
62 | } |
63 | |
ec59cdf2 |
64 | s/#\s*(.*)$//mg; |
65 | my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/; |
e9c69003 |
66 | |
67 | if ($skip_reason) { |
68 | # Like this to avoid needing a label SKIP: |
69 | Test::More->builder->skip($skip_reason); |
70 | next; |
71 | } |
72 | |
ad46c0be |
73 | my ($input, $expected); |
74 | if (/(.*)\n>>>>\n(.*)/s) { |
75 | ($input, $expected) = ($1, $2); |
76 | } |
77 | else { |
78 | ($input, $expected) = ($_, $_); |
79 | } |
87a42246 |
80 | |
ad46c0be |
81 | my $coderef = eval "sub {$input}"; |
87a42246 |
82 | |
ad46c0be |
83 | if ($@) { |
ec59cdf2 |
84 | diag("$num deparsed: $@"); |
85 | ok(0, $testname); |
ad46c0be |
86 | } |
87 | else { |
88 | my $deparsed = $deparse->coderef2text( $coderef ); |
31c6271a |
89 | my $regex = $expected; |
90 | $regex =~ s/(\S+)/\Q$1/g; |
91 | $regex =~ s/\s+/\\s+/g; |
92 | $regex = '^\{\s*' . $regex . '\s*\}$'; |
ec59cdf2 |
93 | like($deparsed, qr/$regex/, $testname); |
87a42246 |
94 | } |
87a42246 |
95 | } |
96 | |
87a42246 |
97 | use constant 'c', 'stuff'; |
09d856fb |
98 | is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff'); |
87a42246 |
99 | |
09d856fb |
100 | my $a = 0; |
101 | is("{\n (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a })); |
87a42246 |
102 | |
d989cdac |
103 | use constant cr => ['hello']; |
104 | my $string = "sub " . $deparse->coderef2text(\&cr); |
0707d6cc |
105 | my $val = (eval $string)->() or diag $string; |
106 | is(ref($val), 'ARRAY'); |
107 | is($val->[0], 'hello'); |
87a42246 |
108 | |
87a42246 |
109 | my $Is_VMS = $^O eq 'VMS'; |
110 | my $Is_MacOS = $^O eq 'MacOS'; |
111 | |
112 | my $path = join " ", map { qq["-I$_"] } @INC; |
be708cc0 |
113 | $path .= " -MMac::err=unix" if $Is_MacOS; |
87a42246 |
114 | my $redir = $Is_MacOS ? "" : "2>&1"; |
115 | |
d2bc402e |
116 | $a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 $redir`; |
e69a2255 |
117 | $a =~ s/-e syntax OK\n//g; |
d2bc402e |
118 | $a =~ s/.*possible typo.*\n//; # Remove warning line |
87a42246 |
119 | $a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037 |
120 | $a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc' |
121 | $b = <<'EOF'; |
d2bc402e |
122 | BEGIN { $^I = ".bak"; } |
123 | BEGIN { $^W = 1; } |
124 | BEGIN { $/ = "\n"; $\ = "\n"; } |
87a42246 |
125 | LINE: while (defined($_ = <ARGV>)) { |
126 | chomp $_; |
f86ea535 |
127 | our(@F) = split(' ', $_, 0); |
87a42246 |
128 | '???'; |
129 | } |
87a42246 |
130 | EOF |
e69a2255 |
131 | $b =~ s/(LINE:)/sub BEGIN { |
132 | 'MacPerl'->bootstrap; |
133 | 'OSA'->bootstrap; |
134 | 'XL'->bootstrap; |
135 | } |
136 | $1/ if $Is_MacOS; |
09d856fb |
137 | is($a, $b); |
87a42246 |
138 | |
579a54dc |
139 | #Re: perlbug #35857, patch #24505 |
b3980c39 |
140 | #handle warnings::register-ed packages properly. |
141 | package B::Deparse::Wrapper; |
142 | use strict; |
143 | use warnings; |
144 | use warnings::register; |
145 | sub getcode { |
579a54dc |
146 | my $deparser = B::Deparse->new(); |
b3980c39 |
147 | return $deparser->coderef2text(shift); |
148 | } |
149 | |
150 | package main; |
151 | use strict; |
152 | use warnings; |
71c4dbc3 |
153 | use constant GLIPP => 'glipp'; |
b3980c39 |
154 | sub test { |
579a54dc |
155 | my $val = shift; |
156 | my $res = B::Deparse::Wrapper::getcode($val); |
09d856fb |
157 | like( $res, qr/use warnings/); |
b3980c39 |
158 | } |
159 | my ($q,$p); |
160 | my $x=sub { ++$q,++$p }; |
161 | test($x); |
162 | eval <<EOFCODE and test($x); |
163 | package bar; |
164 | use strict; |
165 | use warnings; |
166 | use warnings::register; |
167 | package main; |
168 | 1 |
169 | EOFCODE |
170 | |
ad46c0be |
171 | __DATA__ |
14a55f98 |
172 | # 2 |
ad46c0be |
173 | 1; |
174 | #### |
14a55f98 |
175 | # 3 |
ad46c0be |
176 | { |
177 | no warnings; |
178 | '???'; |
179 | 2; |
180 | } |
181 | #### |
14a55f98 |
182 | # 4 |
ad46c0be |
183 | my $test; |
184 | ++$test and $test /= 2; |
185 | >>>> |
186 | my $test; |
187 | $test /= 2 if ++$test; |
188 | #### |
14a55f98 |
189 | # 5 |
ad46c0be |
190 | -((1, 2) x 2); |
191 | #### |
14a55f98 |
192 | # 6 |
ad46c0be |
193 | { |
194 | my $test = sub : lvalue { |
195 | my $x; |
196 | } |
197 | ; |
198 | } |
199 | #### |
14a55f98 |
200 | # 7 |
ad46c0be |
201 | { |
202 | my $test = sub : method { |
203 | my $x; |
204 | } |
205 | ; |
206 | } |
207 | #### |
14a55f98 |
208 | # 8 |
ad46c0be |
209 | { |
210 | my $test = sub : locked method { |
211 | my $x; |
212 | } |
213 | ; |
214 | } |
215 | #### |
14a55f98 |
216 | # 9 |
87a42246 |
217 | { |
ad46c0be |
218 | 234; |
f99a63a2 |
219 | } |
ad46c0be |
220 | continue { |
221 | 123; |
87a42246 |
222 | } |
ce4e655d |
223 | #### |
14a55f98 |
224 | # 10 |
ce4e655d |
225 | my $x; |
226 | print $main::x; |
227 | #### |
14a55f98 |
228 | # 11 |
ce4e655d |
229 | my @x; |
230 | print $main::x[1]; |
14a55f98 |
231 | #### |
232 | # 12 |
233 | my %x; |
234 | $x{warn()}; |
ad8caead |
235 | #### |
236 | # 13 |
237 | my $foo; |
238 | $_ .= <ARGV> . <$foo>; |
cef22867 |
239 | #### |
240 | # 14 |
241 | my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ"; |
4ae52e81 |
242 | #### |
243 | # 15 |
244 | s/x/'y';/e; |
241416b8 |
245 | #### |
246 | # 16 - various lypes of loop |
247 | { my $x; } |
248 | #### |
249 | # 17 |
250 | while (1) { my $k; } |
251 | #### |
252 | # 18 |
253 | my ($x,@a); |
254 | $x=1 for @a; |
255 | >>>> |
256 | my($x, @a); |
0bb5f065 |
257 | $x = 1 foreach (@a); |
241416b8 |
258 | #### |
259 | # 19 |
260 | for (my $i = 0; $i < 2;) { |
261 | my $z = 1; |
262 | } |
263 | #### |
264 | # 20 |
265 | for (my $i = 0; $i < 2; ++$i) { |
266 | my $z = 1; |
267 | } |
268 | #### |
269 | # 21 |
270 | for (my $i = 0; $i < 2; ++$i) { |
271 | my $z = 1; |
272 | } |
273 | #### |
274 | # 22 |
275 | my $i; |
276 | while ($i) { my $z = 1; } continue { $i = 99; } |
277 | #### |
278 | # 23 |
09d856fb |
279 | foreach my $i (1, 2) { |
241416b8 |
280 | my $z = 1; |
281 | } |
282 | #### |
283 | # 24 |
284 | my $i; |
285 | foreach $i (1, 2) { |
286 | my $z = 1; |
287 | } |
288 | #### |
289 | # 25 |
290 | my $i; |
291 | foreach my $i (1, 2) { |
292 | my $z = 1; |
293 | } |
294 | #### |
295 | # 26 |
296 | foreach my $i (1, 2) { |
297 | my $z = 1; |
298 | } |
299 | #### |
300 | # 27 |
301 | foreach our $i (1, 2) { |
302 | my $z = 1; |
303 | } |
304 | #### |
305 | # 28 |
306 | my $i; |
307 | foreach our $i (1, 2) { |
308 | my $z = 1; |
309 | } |
3ac6e0f9 |
310 | #### |
311 | # 29 |
312 | my @x; |
313 | print reverse sort(@x); |
314 | #### |
315 | # 30 |
316 | my @x; |
317 | print((sort {$b cmp $a} @x)); |
318 | #### |
319 | # 31 |
320 | my @x; |
321 | print((reverse sort {$b <=> $a} @x)); |
36d57d93 |
322 | #### |
323 | # 32 |
324 | our @a; |
325 | print $_ foreach (reverse @a); |
aae53c41 |
326 | #### |
579a54dc |
327 | # 33 |
aae53c41 |
328 | our @a; |
329 | print $_ foreach (reverse 1, 2..5); |
f86ea535 |
330 | #### |
331 | # 34 (bug #38684) |
332 | our @ary; |
333 | @ary = split(' ', 'foo', 0); |
31c6271a |
334 | #### |
335 | # 35 (bug #40055) |
336 | do { () }; |
337 | #### |
338 | # 36 (ibid.) |
339 | do { my $x = 1; $x }; |
d9002312 |
340 | #### |
341 | # 37 <20061012113037.GJ25805@c4.convolution.nl> |
342 | my $f = sub { |
343 | +{[]}; |
344 | } ; |
8b2d6640 |
345 | #### |
346 | # 38 (bug #43010) |
347 | '!@$%'->(); |
348 | #### |
349 | # 39 (ibid.) |
350 | ::(); |
351 | #### |
352 | # 40 (ibid.) |
353 | '::::'->(); |
354 | #### |
355 | # 41 (ibid.) |
356 | &::::; |
09d856fb |
357 | #### |
358 | # 42 |
359 | my $bar; |
360 | 'Foo'->$bar('orz'); |
361 | #### |
362 | # 43 |
363 | 'Foo'->bar('orz'); |
364 | #### |
365 | # 44 |
366 | 'Foo'->bar; |
0ced6c29 |
367 | #### |
e9c69003 |
368 | # SKIP ?$] < 5.010 && "say not implemented on this Perl version" |
7ddd1a01 |
369 | # 45 say |
370 | say 'foo'; |
371 | #### |
e9c69003 |
372 | # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version" |
7ddd1a01 |
373 | # 46 state vars |
0ced6c29 |
374 | state $x = 42; |
375 | #### |
e9c69003 |
376 | # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version" |
7ddd1a01 |
377 | # 47 state var assignment |
378 | { |
379 | my $y = (state $x = 42); |
380 | } |
381 | #### |
e9c69003 |
382 | # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version" |
7ddd1a01 |
383 | # 48 state vars in anoymous subroutines |
384 | $a = sub { |
385 | state $x; |
386 | return $x++; |
387 | } |
388 | ; |
644741fd |
389 | #### |
390 | # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version' |
391 | # 49 each @array; |
392 | each @ARGV; |
393 | each @$a; |
394 | #### |
395 | # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version' |
396 | # 50 keys @array; values @array |
397 | keys @$a if keys @ARGV; |
398 | values @ARGV if values @$a; |
35925e80 |
399 | #### |
43b09ad7 |
400 | # 51 Anonymous arrays and hashes, and references to them |
35925e80 |
401 | my $a = {}; |
402 | my $b = \{}; |
403 | my $c = []; |
404 | my $d = \[]; |
9210de83 |
405 | #### |
406 | # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version" |
43b09ad7 |
407 | # 52 implicit smartmatch in given/when |
9210de83 |
408 | given ('foo') { |
409 | when ('bar') { continue; } |
410 | when ($_ ~~ 'quux') { continue; } |
411 | default { 0; } |
412 | } |
7ecdd211 |
413 | #### |
414 | # 53 conditions in elsifs (regression in change #33710 which fixed bug #37302) |
415 | if ($a) { x(); } |
416 | elsif ($b) { x(); } |
417 | elsif ($a and $b) { x(); } |
418 | elsif ($a or $b) { x(); } |
419 | else { x(); } |
03b22f1b |
420 | #### |
421 | # 54 interpolation in regexps |
422 | my($y, $t); |
423 | /x${y}z$t/; |
227375e1 |
424 | #### |
425 | # SKIP ?$B::Deparse::VERSION <= 0.87 && "TODO new undocumented cpan-bug #33708" |
426 | # 55 (cpan-bug #33708) |
427 | %{$_ || {}} |
428 | #### |
429 | # SKIP ?$B::Deparse::VERSION <= 0.87 && "TODO hash constants not yet fixed" |
430 | # 56 (cpan-bug #33708) |
431 | use constant H => { "#" => 1 }; H->{"#"} |
432 | #### |
433 | # SKIP ?$B::Deparse::VERSION <= 0.87 && "TODO optimized away 0 not yet fixed" |
434 | # 57 (cpan-bug #33708) |
435 | foreach my $i (@_) { 0 } |
edbe35ea |
436 | #### |
437 | # 58 tests with not, not optimized |
07f3cdf5 |
438 | my $c; |
edbe35ea |
439 | x() unless $a; |
440 | x() if not $a and $b; |
441 | x() if $a and not $b; |
442 | x() unless not $a and $b; |
443 | x() unless $a and not $b; |
444 | x() if not $a or $b; |
445 | x() if $a or not $b; |
446 | x() unless not $a or $b; |
447 | x() unless $a or not $b; |
07f3cdf5 |
448 | x() if $a and not $b and $c; |
449 | x() if not $a and $b and not $c; |
450 | x() unless $a and not $b and $c; |
451 | x() unless not $a and $b and not $c; |
452 | x() if $a or not $b or $c; |
453 | x() if not $a or $b or not $c; |
454 | x() unless $a or not $b or $c; |
455 | x() unless not $a or $b or not $c; |
edbe35ea |
456 | #### |
457 | # 59 tests with not, optimized |
07f3cdf5 |
458 | my $c; |
edbe35ea |
459 | x() if not $a; |
460 | x() unless not $a; |
461 | x() if not $a and not $b; |
462 | x() unless not $a and not $b; |
463 | x() if not $a or not $b; |
464 | x() unless not $a or not $b; |
07f3cdf5 |
465 | x() if not $a and not $b and $c; |
466 | x() unless not $a and not $b and $c; |
467 | x() if not $a or not $b or $c; |
468 | x() unless not $a or not $b or $c; |
469 | x() if not $a and not $b and not $c; |
470 | x() unless not $a and not $b and not $c; |
471 | x() if not $a or not $b or not $c; |
472 | x() unless not $a or not $b or not $c; |
473 | x() unless not $a or not $b or not $c; |
edbe35ea |
474 | >>>> |
07f3cdf5 |
475 | my $c; |
edbe35ea |
476 | x() unless $a; |
477 | x() if $a; |
478 | x() unless $a or $b; |
479 | x() if $a or $b; |
480 | x() unless $a and $b; |
07f3cdf5 |
481 | x() if $a and $b; |
482 | x() if not $a || $b and $c; |
483 | x() unless not $a || $b and $c; |
484 | x() if not $a && $b or $c; |
485 | x() unless not $a && $b or $c; |
486 | x() unless $a or $b or $c; |
487 | x() if $a or $b or $c; |
488 | x() unless $a and $b and $c; |
489 | x() if $a and $b and $c; |
490 | x() unless not $a && $b && $c; |
71c4dbc3 |
491 | #### |
492 | # 60 tests that should be constant folded |
493 | x() if 1; |
494 | x() if GLIPP; |
495 | x() if !GLIPP; |
496 | x() if GLIPP && GLIPP; |
497 | x() if !GLIPP || GLIPP; |
498 | x() if do { GLIPP }; |
499 | x() if do { no warnings 'void'; 5; GLIPP }; |
500 | x() if do { !GLIPP }; |
501 | if (GLIPP) { x() } else { z() } |
502 | if (!GLIPP) { x() } else { z() } |
503 | if (GLIPP) { x() } elsif (GLIPP) { z() } |
504 | if (!GLIPP) { x() } elsif (GLIPP) { z() } |
505 | if (GLIPP) { x() } elsif (!GLIPP) { z() } |
506 | if (!GLIPP) { x() } elsif (!GLIPP) { z() } |
507 | if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() } |
508 | if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() } |
509 | if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() } |
510 | >>>> |
511 | x(); |
512 | x(); |
513 | '???'; |
514 | x(); |
515 | x(); |
516 | x(); |
517 | x(); |
518 | do { |
519 | '???' |
520 | }; |
521 | do { |
522 | x() |
523 | }; |
524 | do { |
525 | z() |
526 | }; |
527 | do { |
528 | x() |
529 | }; |
530 | do { |
531 | z() |
532 | }; |
533 | do { |
534 | x() |
535 | }; |
536 | '???'; |
537 | do { |
538 | t() |
539 | }; |
540 | '???'; |
541 | !1; |
542 | #### |
543 | # 61 tests that shouldn't be constant folded |
544 | x() if $a; |
545 | if ($a == 1) { x() } elsif ($b == 2) { z() } |
546 | if (do { foo(); GLIPP }) { x() } |
547 | if (do { $a++; GLIPP }) { x() } |
548 | >>>> |
549 | x() if $a; |
550 | if ($a == 1) { x(); } elsif ($b == 2) { z(); } |
551 | if (do { foo(); 'glipp' }) { x(); } |
552 | if (do { ++$a; 'glipp' }) { x(); } |