This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
util.c:get_db_sub: correct comment
[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 actually a subroutine :-)
16     return unless $] > 5.009;
17     require feature;
18     feature->import(':5.10');
19 }
20 use Test::More;
21 use Config ();
22
23 use B::Deparse;
24 my $deparse = B::Deparse->new();
25 isa_ok($deparse, 'B::Deparse', 'instantiate a B::Deparse object');
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      '%^H'        => $hinthash,
34  );
35 }
36
37 $/ = "\n####\n";
38 while (<DATA>) {
39     chomp;
40     # This code is pinched from the t/lib/common.pl for TODO.
41     # It's not clear how to avoid duplication
42     # Now tweaked a bit to do skip or todo
43     my %reason;
44     foreach my $what (qw(skip todo)) {
45         s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
46         # If the SKIP reason starts ? then it's taken as a code snippet to
47         # evaluate. This provides the flexibility to have conditional SKIPs
48         if ($reason{$what} && $reason{$what} =~ s/^\?//) {
49             my $temp = eval $reason{$what};
50             if ($@) {
51                 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
52             }
53             $reason{$what} = $temp;
54         }
55     }
56
57     s/^\s*#\s*(.*)$//mg;
58     my $desc = $1;
59     die "Missing name in test $_" unless defined $desc;
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         is($@, "", "compilation of $desc");
79     }
80     else {
81         my $deparsed = $deparse->coderef2text( $coderef );
82         my $regex = $expected;
83         $regex =~ s/(\S+)/\Q$1/g;
84         $regex =~ s/\s+/\\s+/g;
85         $regex = '^\{\s*' . $regex . '\s*\}$';
86
87         local $::TODO = $reason{todo};
88         like($deparsed, qr/$regex/, $desc);
89     }
90 }
91
92 use constant 'c', 'stuff';
93 is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',
94    'the subroutine generated by use constant deparses');
95
96 my $a = 0;
97 is($deparse->coderef2text(sub{(-1) ** $a }), "{\n    (-1) ** \$a;\n}",
98    'anon sub capturing an external lexical');
99
100 use constant cr => ['hello'];
101 my $string = "sub " . $deparse->coderef2text(\&cr);
102 my $val = (eval $string)->() or diag $string;
103 is(ref($val), 'ARRAY', 'constant array references deparse');
104 is($val->[0], 'hello', 'and return the correct value');
105
106 my $path = join " ", map { qq["-I$_"] } @INC;
107
108 $a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
109 $a =~ s/-e syntax OK\n//g;
110 $a =~ s/.*possible typo.*\n//;     # Remove warning line
111 $a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
112 $a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
113 $b = <<'EOF';
114 BEGIN { $^I = ".bak"; }
115 BEGIN { $^W = 1; }
116 BEGIN { $/ = "\n"; $\ = "\n"; }
117 LINE: while (defined($_ = <ARGV>)) {
118     chomp $_;
119     our(@F) = split(' ', $_, 0);
120     '???';
121 }
122 EOF
123 is($a, $b,
124    'command line flags deparse as BEGIN blocks setting control variables');
125
126 $a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
127 $a =~ s/-e syntax OK\n//g;
128 is($a, "use constant ('PI', 4);\n",
129    "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
130
131 #Re: perlbug #35857, patch #24505
132 #handle warnings::register-ed packages properly.
133 package B::Deparse::Wrapper;
134 use strict;
135 use warnings;
136 use warnings::register;
137 sub getcode {
138    my $deparser = B::Deparse->new();
139    return $deparser->coderef2text(shift);
140 }
141
142 package Moo;
143 use overload '0+' => sub { 42 };
144
145 package main;
146 use strict;
147 use warnings;
148 use constant GLIPP => 'glipp';
149 use constant PI => 4;
150 use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
151 use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
152 BEGIN { delete $::Fcntl::{O_APPEND}; }
153 use POSIX qw/O_CREAT/;
154 sub test {
155    my $val = shift;
156    my $res = B::Deparse::Wrapper::getcode($val);
157    like($res, qr/use warnings/,
158         '[perl #35857] [PATCH] B::Deparse doesnt handle warnings register properly');
159 }
160 my ($q,$p);
161 my $x=sub { ++$q,++$p };
162 test($x);
163 eval <<EOFCODE and test($x);
164    package bar;
165    use strict;
166    use warnings;
167    use warnings::register;
168    package main;
169    1
170 EOFCODE
171
172 # Exotic sub declarations
173 $a = `$^X $path "-MO=Deparse" -e "sub ::::{}sub ::::::{}" 2>&1`;
174 $a =~ s/-e syntax OK\n//g;
175 is($a, <<'EOCODG', "sub :::: and sub ::::::");
176 sub :::: {
177     
178 }
179 sub :::::: {
180     
181 }
182 EOCODG
183
184 # [perl #33752]
185 {
186   my $code = <<"EOCODE";
187 {
188     our \$\x{1e1f}\x{14d}\x{14d};
189 }
190 EOCODE
191   my $deparsed
192    = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
193   s/$ \n//x for $deparsed, $code;
194   is $deparsed, $code, 'our $funny_Unicode_chars';
195 }
196
197 # [perl #62500]
198 $a =
199   `$^X $path "-MO=Deparse" -e "BEGIN{*CORE::GLOBAL::require=sub{1}}" 2>&1`;
200 $a =~ s/-e syntax OK\n//g;
201 is($a, <<'EOCODF', "CORE::GLOBAL::require override causing panick");
202 sub BEGIN {
203     *CORE::GLOBAL::require = sub {
204         1;
205     }
206     ;
207 }
208 EOCODF
209
210 # [perl #93990]
211 @* = ();
212 is($deparse->coderef2text(sub{ print "@{*}" }),
213 q<{
214     print "@{*}";
215 }>, 'curly around to interpolate "@{*}"');
216 is($deparse->coderef2text(sub{ print "@{-}" }),
217 q<{
218     print "@-";
219 }>, 'no need to curly around to interpolate "@-"');
220
221 done_testing();
222
223 __DATA__
224 # A constant
225 1;
226 ####
227 # Constants in a block
228 {
229     no warnings;
230     '???';
231     2;
232 }
233 ####
234 # Lexical and simple arithmetic
235 my $test;
236 ++$test and $test /= 2;
237 >>>>
238 my $test;
239 $test /= 2 if ++$test;
240 ####
241 # list x
242 -((1, 2) x 2);
243 ####
244 # lvalue sub
245 {
246     my $test = sub : lvalue {
247         my $x;
248     }
249     ;
250 }
251 ####
252 # method
253 {
254     my $test = sub : method {
255         my $x;
256     }
257     ;
258 }
259 ####
260 # block with continue
261 {
262     234;
263 }
264 continue {
265     123;
266 }
267 ####
268 # lexical and package scalars
269 my $x;
270 print $main::x;
271 ####
272 # lexical and package arrays
273 my @x;
274 print $main::x[1];
275 ####
276 # lexical and package hashes
277 my %x;
278 $x{warn()};
279 ####
280 # <>
281 my $foo;
282 $_ .= <ARGV> . <$foo>;
283 ####
284 # \x{}
285 my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
286 ####
287 # s///e
288 s/x/'y';/e;
289 ####
290 # block
291 { my $x; }
292 ####
293 # while 1
294 while (1) { my $k; }
295 ####
296 # trailing for
297 my ($x,@a);
298 $x=1 for @a;
299 >>>>
300 my($x, @a);
301 $x = 1 foreach (@a);
302 ####
303 # 2 arguments in a 3 argument for
304 for (my $i = 0; $i < 2;) {
305     my $z = 1;
306 }
307 ####
308 # 3 argument for
309 for (my $i = 0; $i < 2; ++$i) {
310     my $z = 1;
311 }
312 ####
313 # 3 argument for again
314 for (my $i = 0; $i < 2; ++$i) {
315     my $z = 1;
316 }
317 ####
318 # while/continue
319 my $i;
320 while ($i) { my $z = 1; } continue { $i = 99; }
321 ####
322 # foreach with my
323 foreach my $i (1, 2) {
324     my $z = 1;
325 }
326 ####
327 # foreach
328 my $i;
329 foreach $i (1, 2) {
330     my $z = 1;
331 }
332 ####
333 # foreach, 2 mys
334 my $i;
335 foreach my $i (1, 2) {
336     my $z = 1;
337 }
338 ####
339 # foreach
340 foreach my $i (1, 2) {
341     my $z = 1;
342 }
343 ####
344 # foreach with our
345 foreach our $i (1, 2) {
346     my $z = 1;
347 }
348 ####
349 # foreach with my and our
350 my $i;
351 foreach our $i (1, 2) {
352     my $z = 1;
353 }
354 ####
355 # reverse sort
356 my @x;
357 print reverse sort(@x);
358 ####
359 # sort with cmp
360 my @x;
361 print((sort {$b cmp $a} @x));
362 ####
363 # reverse sort with block
364 my @x;
365 print((reverse sort {$b <=> $a} @x));
366 ####
367 # foreach reverse
368 our @a;
369 print $_ foreach (reverse @a);
370 ####
371 # foreach reverse (not inplace)
372 our @a;
373 print $_ foreach (reverse 1, 2..5);
374 ####
375 # bug #38684
376 our @ary;
377 @ary = split(' ', 'foo', 0);
378 ####
379 # bug #40055
380 do { () }; 
381 ####
382 # bug #40055
383 do { my $x = 1; $x }; 
384 ####
385 # <20061012113037.GJ25805@c4.convolution.nl>
386 my $f = sub {
387     +{[]};
388 } ;
389 ####
390 # bug #43010
391 '!@$%'->();
392 ####
393 # bug #43010
394 ::();
395 ####
396 # bug #43010
397 '::::'->();
398 ####
399 # bug #43010
400 &::::;
401 ####
402 # variables as method names
403 my $bar;
404 'Foo'->$bar('orz');
405 'Foo'->$bar('orz') = 'a stranger stranger than before';
406 ####
407 # constants as method names
408 'Foo'->bar('orz');
409 ####
410 # constants as method names without ()
411 'Foo'->bar;
412 ####
413 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
414 # say
415 say 'foo';
416 ####
417 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
418 # state vars
419 state $x = 42;
420 ####
421 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
422 # state var assignment
423 {
424     my $y = (state $x = 42);
425 }
426 ####
427 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
428 # state vars in anonymous subroutines
429 $a = sub {
430     state $x;
431     return $x++;
432 }
433 ;
434 ####
435 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
436 # each @array;
437 each @ARGV;
438 each @$a;
439 ####
440 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
441 # keys @array; values @array
442 keys @$a if keys @ARGV;
443 values @ARGV if values @$a;
444 ####
445 # Anonymous arrays and hashes, and references to them
446 my $a = {};
447 my $b = \{};
448 my $c = [];
449 my $d = \[];
450 ####
451 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
452 # implicit smartmatch in given/when
453 given ('foo') {
454     when ('bar') { continue; }
455     when ($_ ~~ 'quux') { continue; }
456     default { 0; }
457 }
458 ####
459 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
460 if ($a) { x(); }
461 elsif ($b) { x(); }
462 elsif ($a and $b) { x(); }
463 elsif ($a or $b) { x(); }
464 else { x(); }
465 ####
466 # interpolation in regexps
467 my($y, $t);
468 /x${y}z$t/;
469 ####
470 # TODO new undocumented cpan-bug #33708
471 # cpan-bug #33708
472 %{$_ || {}}
473 ####
474 # TODO hash constants not yet fixed
475 # cpan-bug #33708
476 use constant H => { "#" => 1 }; H->{"#"}
477 ####
478 # TODO optimized away 0 not yet fixed
479 # cpan-bug #33708
480 foreach my $i (@_) { 0 }
481 ####
482 # tests with not, not optimized
483 my $c;
484 x() unless $a;
485 x() if not $a and $b;
486 x() if $a and not $b;
487 x() unless not $a and $b;
488 x() unless $a and not $b;
489 x() if not $a or $b;
490 x() if $a or not $b;
491 x() unless not $a or $b;
492 x() unless $a or not $b;
493 x() if $a and not $b and $c;
494 x() if not $a and $b and not $c;
495 x() unless $a and not $b and $c;
496 x() unless not $a and $b and not $c;
497 x() if $a or not $b or $c;
498 x() if not $a or $b or not $c;
499 x() unless $a or not $b or $c;
500 x() unless not $a or $b or not $c;
501 ####
502 # tests with not, optimized
503 my $c;
504 x() if not $a;
505 x() unless not $a;
506 x() if not $a and not $b;
507 x() unless not $a and not $b;
508 x() if not $a or not $b;
509 x() unless not $a or not $b;
510 x() if not $a and not $b and $c;
511 x() unless not $a and not $b and $c;
512 x() if not $a or not $b or $c;
513 x() unless not $a or not $b or $c;
514 x() if not $a and not $b and not $c;
515 x() unless not $a and not $b and not $c;
516 x() if not $a or not $b or not $c;
517 x() unless not $a or not $b or not $c;
518 x() unless not $a or not $b or not $c;
519 >>>>
520 my $c;
521 x() unless $a;
522 x() if $a;
523 x() unless $a or $b;
524 x() if $a or $b;
525 x() unless $a and $b;
526 x() if $a and $b;
527 x() if not $a || $b and $c;
528 x() unless not $a || $b and $c;
529 x() if not $a && $b or $c;
530 x() unless not $a && $b or $c;
531 x() unless $a or $b or $c;
532 x() if $a or $b or $c;
533 x() unless $a and $b and $c;
534 x() if $a and $b and $c;
535 x() unless not $a && $b && $c;
536 ####
537 # tests that should be constant folded
538 x() if 1;
539 x() if GLIPP;
540 x() if !GLIPP;
541 x() if GLIPP && GLIPP;
542 x() if !GLIPP || GLIPP;
543 x() if do { GLIPP };
544 x() if do { no warnings 'void'; 5; GLIPP };
545 x() if do { !GLIPP };
546 if (GLIPP) { x() } else { z() }
547 if (!GLIPP) { x() } else { z() }
548 if (GLIPP) { x() } elsif (GLIPP) { z() }
549 if (!GLIPP) { x() } elsif (GLIPP) { z() }
550 if (GLIPP) { x() } elsif (!GLIPP) { z() }
551 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
552 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
553 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
554 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
555 >>>>
556 x();
557 x();
558 '???';
559 x();
560 x();
561 x();
562 x();
563 do {
564     '???'
565 };
566 do {
567     x()
568 };
569 do {
570     z()
571 };
572 do {
573     x()
574 };
575 do {
576     z()
577 };
578 do {
579     x()
580 };
581 '???';
582 do {
583     t()
584 };
585 '???';
586 !1;
587 ####
588 # TODO constant deparsing has been backed out for 5.12
589 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
590 # tests that shouldn't be constant folded
591 # It might be fundamentally impossible to make this work on ithreads, in which
592 # case the TODO should become a SKIP
593 x() if $a;
594 if ($a == 1) { x() } elsif ($b == 2) { z() }
595 if (do { foo(); GLIPP }) { x() }
596 if (do { $a++; GLIPP }) { x() }
597 >>>>
598 x() if $a;
599 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
600 if (do { foo(); GLIPP }) { x(); }
601 if (do { ++$a; GLIPP }) { x(); }
602 ####
603 # TODO constant deparsing has been backed out for 5.12
604 # tests for deparsing constants
605 warn PI;
606 ####
607 # TODO constant deparsing has been backed out for 5.12
608 # tests for deparsing imported constants
609 warn O_TRUNC;
610 ####
611 # TODO constant deparsing has been backed out for 5.12
612 # tests for deparsing re-exported constants
613 warn O_CREAT;
614 ####
615 # TODO constant deparsing has been backed out for 5.12
616 # tests for deparsing imported constants that got deleted from the original namespace
617 warn O_APPEND;
618 ####
619 # TODO constant deparsing has been backed out for 5.12
620 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
621 # tests for deparsing constants which got turned into full typeglobs
622 # It might be fundamentally impossible to make this work on ithreads, in which
623 # case the TODO should become a SKIP
624 warn O_EXCL;
625 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
626 warn O_EXCL;
627 ####
628 # TODO constant deparsing has been backed out for 5.12
629 # tests for deparsing of blessed constant with overloaded numification
630 warn OVERLOADED_NUMIFICATION;
631 ####
632 # TODO Only strict 'refs' currently supported
633 # strict
634 no strict;
635 $x;
636 ####
637 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
638 # subsets of warnings
639 no warnings 'deprecated';
640 my $x;
641 ####
642 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
643 # CPAN #33708
644 use strict;
645 no warnings;
646
647 foreach (0..3) {
648     my $x = 2;
649     {
650         my $x if 0;
651         print ++$x, "\n";
652     }
653 }
654 ####
655 # no attribute list
656 my $pi = 4;
657 ####
658 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
659 # := treated as an empty attribute list
660 no warnings;
661 my $pi := 4;
662 >>>>
663 no warnings;
664 my $pi = 4;
665 ####
666 # : = empty attribute list
667 my $pi : = 4;
668 >>>>
669 my $pi = 4;
670 ####
671 # in place sort
672 our @a;
673 my @b;
674 @a = sort @a;
675 @b = sort @b;
676 ();
677 ####
678 # in place reverse
679 our @a;
680 my @b;
681 @a = reverse @a;
682 @b = reverse @b;
683 ();
684 ####
685 # #71870 Use of uninitialized value in bitwise and B::Deparse
686 my($r, $s, @a);
687 @a = split(/foo/, $s, 0);
688 $r = qr/foo/;
689 @a = split(/$r/, $s, 0);
690 ();
691 ####
692 # package declaration before label
693 {
694     package Foo;
695     label: print 123;
696 }
697 ####
698 # shift optimisation
699 shift;
700 >>>>
701 shift();
702 ####
703 # shift optimisation
704 shift @_;
705 ####
706 # shift optimisation
707 pop;
708 >>>>
709 pop();
710 ####
711 # shift optimisation
712 pop @_;
713 ####
714 #[perl #20444]
715 "foo" =~ (1 ? /foo/ : /bar/);
716 "foo" =~ (1 ? y/foo// : /bar/);
717 "foo" =~ (1 ? s/foo// : /bar/);
718 >>>>
719 'foo' =~ ($_ =~ /foo/);
720 'foo' =~ ($_ =~ tr/fo//);
721 'foo' =~ ($_ =~ s/foo//);
722 ####
723 # Test @threadsv_names under 5005threads
724 foreach $' (1, 2) {
725     sleep $';
726 }
727 ####
728 # y///r
729 tr/a/b/r;
730 ####
731 # y/uni/code/
732 tr/\x{345}/\x{370}/;
733 ####
734 # [perl #90898]
735 <a,>;
736 ####
737 # [perl #91008]
738 each $@;
739 keys $~;
740 values $!;
741 ####
742 # readpipe with complex expression
743 readpipe $a + $b;
744 ####
745 # aelemfast
746 $b::a[0] = 1;
747 ####
748 # aelemfast for a lexical
749 my @a;
750 $a[0] = 1;
751 ####
752 # feature features without feature
753 BEGIN {
754     delete $^H{'feature_say'};
755     delete $^H{'feature_state'};
756     delete $^H{'feature_switch'};
757 }
758 CORE::state $x;
759 CORE::say $x;
760 CORE::given ($x) {
761     CORE::when (3) {
762         continue;
763     }
764     CORE::default {
765         CORE::break;
766     }
767 }
768 CORE::evalbytes '';
769 ####
770 # $#- $#+ $#{%} etc.
771 my @x;
772 @x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
773 @x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
774 @x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
775 @x = ($#{;}, $#{:});
776 ####
777 # ${#} interpolated (the first line magically disables the warning)
778 () = *#;
779 () = "${#}a";
780 ####
781 # ()[...]
782 my(@a) = ()[()];