This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Stop eval "BEGIN{die}" from leaking
[perl5.git] / dist / B-Deparse / t / deparse.t
1 #!./perl
2
3 BEGIN {
4     unshift @INC, 't';
5     require Config;
6     if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7         print "1..0 # Skip -- Perl configured without B module\n";
8         exit 0;
9     }
10 }
11
12 use warnings;
13 use strict;
14 BEGIN {
15     # BEGIN block is acutally a subroutine :-)
16     return unless $] > 5.009;
17     require feature;
18     feature->import(':5.10');
19 }
20 use Test::More tests => 94;
21 use Config ();
22
23 use B::Deparse;
24 my $deparse = B::Deparse->new();
25 ok($deparse);
26
27 # Tell B::Deparse about our ambient pragmas
28 { my ($hint_bits, $warning_bits, $hinthash);
29  BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
30  $deparse->ambient_pragmas (
31      hint_bits    => $hint_bits,
32      warning_bits => $warning_bits,
33      '$['         => 0 + $[,
34      '%^H'        => $hinthash,
35  );
36 }
37
38 $/ = "\n####\n";
39 while (<DATA>) {
40     chomp;
41     # This code is pinched from the t/lib/common.pl for TODO.
42     # It's not clear how to avoid duplication
43     # Now tweaked a bit to do skip or todo
44     my %reason;
45     foreach my $what (qw(skip todo)) {
46         s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
47         # If the SKIP reason starts ? then it's taken as a code snippet to
48         # evaluate. This provides the flexibility to have conditional SKIPs
49         if ($reason{$what} && $reason{$what} =~ s/^\?//) {
50             my $temp = eval $reason{$what};
51             if ($@) {
52                 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
53             }
54             $reason{$what} = $temp;
55         }
56     }
57
58     s/^\s*#\s*(.*)$//mg;
59     my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;
60
61     if ($reason{skip}) {
62         # Like this to avoid needing a label SKIP:
63        Test::More->builder->skip($reason{skip});
64         next;
65     }
66
67     my ($input, $expected);
68     if (/(.*)\n>>>>\n(.*)/s) {
69         ($input, $expected) = ($1, $2);
70     }
71     else {
72         ($input, $expected) = ($_, $_);
73     }
74
75     my $coderef = eval "sub {$input}";
76
77     if ($@) {
78         diag("$num deparsed: $@");
79         ok(0, $testname);
80     }
81     else {
82         my $deparsed = $deparse->coderef2text( $coderef );
83         my $regex = $expected;
84         $regex =~ s/(\S+)/\Q$1/g;
85         $regex =~ s/\s+/\\s+/g;
86         $regex = '^\{\s*' . $regex . '\s*\}$';
87
88         local $::TODO = $reason{todo};
89         like($deparsed, qr/$regex/, $testname);
90     }
91 }
92
93 use constant 'c', 'stuff';
94 is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff');
95
96 my $a = 0;
97 is("{\n    (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a }));
98
99 use constant cr => ['hello'];
100 my $string = "sub " . $deparse->coderef2text(\&cr);
101 my $val = (eval $string)->() or diag $string;
102 is(ref($val), 'ARRAY');
103 is($val->[0], 'hello');
104
105 my $path = join " ", map { qq["-I$_"] } @INC;
106
107 $a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
108 $a =~ s/-e syntax OK\n//g;
109 $a =~ s/.*possible typo.*\n//;     # Remove warning line
110 $a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
111 $a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
112 $b = <<'EOF';
113 BEGIN { $^I = ".bak"; }
114 BEGIN { $^W = 1; }
115 BEGIN { $/ = "\n"; $\ = "\n"; }
116 LINE: while (defined($_ = <ARGV>)) {
117     chomp $_;
118     our(@F) = split(' ', $_, 0);
119     '???';
120 }
121 EOF
122 is($a, $b);
123
124 $a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
125 $a =~ s/-e syntax OK\n//g;
126 is($a, "use constant ('PI', 4);\n",
127    "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
128
129 #Re: perlbug #35857, patch #24505
130 #handle warnings::register-ed packages properly.
131 package B::Deparse::Wrapper;
132 use strict;
133 use warnings;
134 use warnings::register;
135 sub getcode {
136    my $deparser = B::Deparse->new();
137    return $deparser->coderef2text(shift);
138 }
139
140 package Moo;
141 use overload '0+' => sub { 42 };
142
143 package main;
144 use strict;
145 use warnings;
146 use constant GLIPP => 'glipp';
147 use constant PI => 4;
148 use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
149 use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
150 BEGIN { delete $::Fcntl::{O_APPEND}; }
151 use POSIX qw/O_CREAT/;
152 sub test {
153    my $val = shift;
154    my $res = B::Deparse::Wrapper::getcode($val);
155    like( $res, qr/use warnings/);
156 }
157 my ($q,$p);
158 my $x=sub { ++$q,++$p };
159 test($x);
160 eval <<EOFCODE and test($x);
161    package bar;
162    use strict;
163    use warnings;
164    use warnings::register;
165    package main;
166    1
167 EOFCODE
168
169 # [perl #33752]
170 {
171   my $code = <<"EOCODE";
172 {
173     our \$\x{1e1f}\x{14d}\x{14d};
174 }
175 EOCODE
176   my $deparsed
177    = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
178   s/$ \n//x for $deparsed, $code;
179   is $deparsed, $code, 'our $funny_Unicode_chars';
180 }
181
182 __DATA__
183 # 2
184 1;
185 ####
186 # 3
187 {
188     no warnings;
189     '???';
190     2;
191 }
192 ####
193 # 4
194 my $test;
195 ++$test and $test /= 2;
196 >>>>
197 my $test;
198 $test /= 2 if ++$test;
199 ####
200 # 5
201 -((1, 2) x 2);
202 ####
203 # 6
204 {
205     my $test = sub : lvalue {
206         my $x;
207     }
208     ;
209 }
210 ####
211 # 7
212 {
213     my $test = sub : method {
214         my $x;
215     }
216     ;
217 }
218 ####
219 # 8
220 # Was sub : locked method { ... }
221 # This number could be re-used.
222 ####
223 # 9
224 {
225     234;
226 }
227 continue {
228     123;
229 }
230 ####
231 # 10
232 my $x;
233 print $main::x;
234 ####
235 # 11
236 my @x;
237 print $main::x[1];
238 ####
239 # 12
240 my %x;
241 $x{warn()};
242 ####
243 # 13
244 my $foo;
245 $_ .= <ARGV> . <$foo>;
246 ####
247 # 14
248 my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
249 ####
250 # 15
251 s/x/'y';/e;
252 ####
253 # 16 - various lypes of loop
254 { my $x; }
255 ####
256 # 17
257 while (1) { my $k; }
258 ####
259 # 18
260 my ($x,@a);
261 $x=1 for @a;
262 >>>>
263 my($x, @a);
264 $x = 1 foreach (@a);
265 ####
266 # 19
267 for (my $i = 0; $i < 2;) {
268     my $z = 1;
269 }
270 ####
271 # 20
272 for (my $i = 0; $i < 2; ++$i) {
273     my $z = 1;
274 }
275 ####
276 # 21
277 for (my $i = 0; $i < 2; ++$i) {
278     my $z = 1;
279 }
280 ####
281 # 22
282 my $i;
283 while ($i) { my $z = 1; } continue { $i = 99; }
284 ####
285 # 23
286 foreach my $i (1, 2) {
287     my $z = 1;
288 }
289 ####
290 # 24
291 my $i;
292 foreach $i (1, 2) {
293     my $z = 1;
294 }
295 ####
296 # 25
297 my $i;
298 foreach my $i (1, 2) {
299     my $z = 1;
300 }
301 ####
302 # 26
303 foreach my $i (1, 2) {
304     my $z = 1;
305 }
306 ####
307 # 27
308 foreach our $i (1, 2) {
309     my $z = 1;
310 }
311 ####
312 # 28
313 my $i;
314 foreach our $i (1, 2) {
315     my $z = 1;
316 }
317 ####
318 # 29
319 my @x;
320 print reverse sort(@x);
321 ####
322 # 30
323 my @x;
324 print((sort {$b cmp $a} @x));
325 ####
326 # 31
327 my @x;
328 print((reverse sort {$b <=> $a} @x));
329 ####
330 # 32
331 our @a;
332 print $_ foreach (reverse @a);
333 ####
334 # 33
335 our @a;
336 print $_ foreach (reverse 1, 2..5);
337 ####
338 # 34  (bug #38684)
339 our @ary;
340 @ary = split(' ', 'foo', 0);
341 ####
342 # 35 (bug #40055)
343 do { () }; 
344 ####
345 # 36 (ibid.)
346 do { my $x = 1; $x }; 
347 ####
348 # 37 <20061012113037.GJ25805@c4.convolution.nl>
349 my $f = sub {
350     +{[]};
351 } ;
352 ####
353 # 38 (bug #43010)
354 '!@$%'->();
355 ####
356 # 39 (ibid.)
357 ::();
358 ####
359 # 40 (ibid.)
360 '::::'->();
361 ####
362 # 41 (ibid.)
363 &::::;
364 ####
365 # 42
366 my $bar;
367 'Foo'->$bar('orz');
368 ####
369 # 43
370 'Foo'->bar('orz');
371 ####
372 # 44
373 'Foo'->bar;
374 ####
375 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
376 # 45 say
377 say 'foo';
378 ####
379 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
380 # 46 state vars
381 state $x = 42;
382 ####
383 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
384 # 47 state var assignment
385 {
386     my $y = (state $x = 42);
387 }
388 ####
389 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
390 # 48 state vars in anoymous subroutines
391 $a = sub {
392     state $x;
393     return $x++;
394 }
395 ;
396 ####
397 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
398 # 49 each @array;
399 each @ARGV;
400 each @$a;
401 ####
402 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
403 # 50 keys @array; values @array
404 keys @$a if keys @ARGV;
405 values @ARGV if values @$a;
406 ####
407 # 51 Anonymous arrays and hashes, and references to them
408 my $a = {};
409 my $b = \{};
410 my $c = [];
411 my $d = \[];
412 ####
413 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
414 # 52 implicit smartmatch in given/when
415 given ('foo') {
416     when ('bar') { continue; }
417     when ($_ ~~ 'quux') { continue; }
418     default { 0; }
419 }
420 ####
421 # 53 conditions in elsifs (regression in change #33710 which fixed bug #37302)
422 if ($a) { x(); }
423 elsif ($b) { x(); }
424 elsif ($a and $b) { x(); }
425 elsif ($a or $b) { x(); }
426 else { x(); }
427 ####
428 # 54 interpolation in regexps
429 my($y, $t);
430 /x${y}z$t/;
431 ####
432 # TODO new undocumented cpan-bug #33708
433 # 55  (cpan-bug #33708)
434 %{$_ || {}}
435 ####
436 # TODO hash constants not yet fixed
437 # 56  (cpan-bug #33708)
438 use constant H => { "#" => 1 }; H->{"#"}
439 ####
440 # TODO optimized away 0 not yet fixed
441 # 57  (cpan-bug #33708)
442 foreach my $i (@_) { 0 }
443 ####
444 # 58 tests with not, not optimized
445 my $c;
446 x() unless $a;
447 x() if not $a and $b;
448 x() if $a and not $b;
449 x() unless not $a and $b;
450 x() unless $a and not $b;
451 x() if not $a or $b;
452 x() if $a or not $b;
453 x() unless not $a or $b;
454 x() unless $a or not $b;
455 x() if $a and not $b and $c;
456 x() if not $a and $b and not $c;
457 x() unless $a and not $b and $c;
458 x() unless not $a and $b and not $c;
459 x() if $a or not $b or $c;
460 x() if not $a or $b or not $c;
461 x() unless $a or not $b or $c;
462 x() unless not $a or $b or not $c;
463 ####
464 # 59 tests with not, optimized
465 my $c;
466 x() if not $a;
467 x() unless not $a;
468 x() if not $a and not $b;
469 x() unless not $a and not $b;
470 x() if not $a or not $b;
471 x() unless not $a or not $b;
472 x() if not $a and not $b and $c;
473 x() unless not $a and not $b and $c;
474 x() if not $a or not $b or $c;
475 x() unless not $a or not $b or $c;
476 x() if not $a and not $b and not $c;
477 x() unless not $a and not $b and not $c;
478 x() if not $a or not $b or not $c;
479 x() unless not $a or not $b or not $c;
480 x() unless not $a or not $b or not $c;
481 >>>>
482 my $c;
483 x() unless $a;
484 x() if $a;
485 x() unless $a or $b;
486 x() if $a or $b;
487 x() unless $a and $b;
488 x() if $a and $b;
489 x() if not $a || $b and $c;
490 x() unless not $a || $b and $c;
491 x() if not $a && $b or $c;
492 x() unless not $a && $b or $c;
493 x() unless $a or $b or $c;
494 x() if $a or $b or $c;
495 x() unless $a and $b and $c;
496 x() if $a and $b and $c;
497 x() unless not $a && $b && $c;
498 ####
499 # 60 tests that should be constant folded
500 x() if 1;
501 x() if GLIPP;
502 x() if !GLIPP;
503 x() if GLIPP && GLIPP;
504 x() if !GLIPP || GLIPP;
505 x() if do { GLIPP };
506 x() if do { no warnings 'void'; 5; GLIPP };
507 x() if do { !GLIPP };
508 if (GLIPP) { x() } else { z() }
509 if (!GLIPP) { x() } else { z() }
510 if (GLIPP) { x() } elsif (GLIPP) { z() }
511 if (!GLIPP) { x() } elsif (GLIPP) { z() }
512 if (GLIPP) { x() } elsif (!GLIPP) { z() }
513 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
514 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
515 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
516 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
517 >>>>
518 x();
519 x();
520 '???';
521 x();
522 x();
523 x();
524 x();
525 do {
526     '???'
527 };
528 do {
529     x()
530 };
531 do {
532     z()
533 };
534 do {
535     x()
536 };
537 do {
538     z()
539 };
540 do {
541     x()
542 };
543 '???';
544 do {
545     t()
546 };
547 '???';
548 !1;
549 ####
550 # TODO constant deparsing has been backed out for 5.12
551 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
552 # 61 tests that shouldn't be constant folded
553 # It might be fundamentally impossible to make this work on ithreads, in which
554 # case the TODO should become a SKIP
555 x() if $a;
556 if ($a == 1) { x() } elsif ($b == 2) { z() }
557 if (do { foo(); GLIPP }) { x() }
558 if (do { $a++; GLIPP }) { x() }
559 >>>>
560 x() if $a;
561 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
562 if (do { foo(); GLIPP }) { x(); }
563 if (do { ++$a; GLIPP }) { x(); }
564 ####
565 # TODO constant deparsing has been backed out for 5.12
566 # 62 tests for deparsing constants
567 warn PI;
568 ####
569 # TODO constant deparsing has been backed out for 5.12
570 # 63 tests for deparsing imported constants
571 warn O_TRUNC;
572 ####
573 # TODO constant deparsing has been backed out for 5.12
574 # 64 tests for deparsing re-exported constants
575 warn O_CREAT;
576 ####
577 # TODO constant deparsing has been backed out for 5.12
578 # 65 tests for deparsing imported constants that got deleted from the original namespace
579 warn O_APPEND;
580 ####
581 # TODO constant deparsing has been backed out for 5.12
582 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
583 # 66 tests for deparsing constants which got turned into full typeglobs
584 # It might be fundamentally impossible to make this work on ithreads, in which
585 # case the TODO should become a SKIP
586 warn O_EXCL;
587 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
588 warn O_EXCL;
589 ####
590 # TODO constant deparsing has been backed out for 5.12
591 # 67 tests for deparsing of blessed constant with overloaded numification
592 warn OVERLOADED_NUMIFICATION;
593 ####
594 # TODO Only strict 'refs' currently supported
595 # 68 strict
596 no strict;
597 $x;
598 ####
599 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
600 no warnings 'deprecated';
601 my $x;
602 ####
603 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
604 use strict;
605 no warnings;
606
607 foreach (0..3) {
608     my $x = 2;
609     {
610         my $x if 0;
611         print ++$x, "\n";
612     }
613 }
614 ####
615 my $pi = 4;
616 ####
617 no warnings;
618 my $pi := 4;
619 >>>>
620 no warnings;
621 my $pi = 4;
622 ####
623 my $pi : = 4;
624 >>>>
625 my $pi = 4;
626 ####
627 our @a;
628 my @b;
629 @a = sort @a;
630 @b = sort @b;
631 ();
632 ####
633 our @a;
634 my @b;
635 @a = reverse @a;
636 @b = reverse @b;
637 ();
638 ####
639 my($r, $s, @a);
640 @a = split(/foo/, $s, 0);
641 $r = qr/foo/;
642 @a = split(/$r/, $s, 0);
643 ();
644 ####
645 {
646     package Foo;
647     label: print 123;
648 }
649 ####
650 shift;
651 >>>>
652 shift();
653 ####
654 shift @_;
655 ####
656 pop;
657 >>>>
658 pop();
659 ####
660 pop @_;
661 ####
662 # 82 [perl #20444]
663 "foo" =~ (1 ? /foo/ : /bar/);
664 "foo" =~ (1 ? y/foo// : /bar/);
665 "foo" =~ (1 ? s/foo// : /bar/);
666 >>>>
667 'foo' =~ ($_ =~ /foo/);
668 'foo' =~ ($_ =~ tr/fo//);
669 'foo' =~ ($_ =~ s/foo//);
670 ####
671 # Test @threadsv_names under 5005threads
672 foreach $' (1, 2) {
673     sleep $';
674 }
675 ####
676 # y///r
677 tr/a/b/r;
678 ####
679 # y/uni/code/
680 tr/\x{345}/\x{370}/;