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