This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
mg_free frees data but leaves it accessible
[perl5.git] / ext / B / t / deparse.t
CommitLineData
87a42246
MS
1#!./perl
2
3BEGIN {
5638aaac
SM
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
NC
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
MS
20}
21
87a42246
MS
22use warnings;
23use strict;
e9c69003
NC
24BEGIN {
25 # BEGIN block is acutally a subroutine :-)
26 return unless $] > 5.009;
27 require feature;
28 feature->import(':5.10');
29}
7ddd1a01 30use Test::More tests => 54;
87a42246
MS
31
32use B::Deparse;
09d856fb
CK
33my $deparse = B::Deparse->new();
34ok($deparse);
87a42246
MS
35
36# Tell B::Deparse about our ambient pragmas
0ced6c29
RGS
37{ my ($hint_bits, $warning_bits, $hinthash);
38 BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
87a42246
MS
39 $deparse->ambient_pragmas (
40 hint_bits => $hint_bits,
41 warning_bits => $warning_bits,
0ced6c29
RGS
42 '$[' => 0 + $[,
43 '%^H' => $hinthash,
87a42246
MS
44 );
45}
46
ad46c0be
RH
47$/ = "\n####\n";
48while (<DATA>) {
49 chomp;
e9c69003
NC
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
RGS
64 s/#\s*(.*)$//mg;
65 my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;
e9c69003
NC
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
RH
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
RGS
84 diag("$num deparsed: $@");
85 ok(0, $testname);
ad46c0be
RH
86 }
87 else {
88 my $deparsed = $deparse->coderef2text( $coderef );
31c6271a
RD
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
MS
95}
96
87a42246 97use constant 'c', 'stuff';
09d856fb 98is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff');
87a42246 99
09d856fb
CK
100my $a = 0;
101is("{\n (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a }));
87a42246 102
d989cdac
SM
103use constant cr => ['hello'];
104my $string = "sub " . $deparse->coderef2text(\&cr);
105my $val = (eval $string)->();
09d856fb 106ok( ref($val) eq 'ARRAY' && $val->[0] eq 'hello');
87a42246 107
87a42246
MS
108my $Is_VMS = $^O eq 'VMS';
109my $Is_MacOS = $^O eq 'MacOS';
110
111my $path = join " ", map { qq["-I$_"] } @INC;
be708cc0 112$path .= " -MMac::err=unix" if $Is_MacOS;
87a42246
MS
113my $redir = $Is_MacOS ? "" : "2>&1";
114
d2bc402e 115$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 $redir`;
e69a2255 116$a =~ s/-e syntax OK\n//g;
d2bc402e 117$a =~ s/.*possible typo.*\n//; # Remove warning line
87a42246
MS
118$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
119$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
120$b = <<'EOF';
d2bc402e
RGS
121BEGIN { $^I = ".bak"; }
122BEGIN { $^W = 1; }
123BEGIN { $/ = "\n"; $\ = "\n"; }
87a42246
MS
124LINE: while (defined($_ = <ARGV>)) {
125 chomp $_;
f86ea535 126 our(@F) = split(' ', $_, 0);
87a42246
MS
127 '???';
128}
87a42246 129EOF
e69a2255
JH
130$b =~ s/(LINE:)/sub BEGIN {
131 'MacPerl'->bootstrap;
132 'OSA'->bootstrap;
133 'XL'->bootstrap;
134}
135$1/ if $Is_MacOS;
09d856fb 136is($a, $b);
87a42246 137
579a54dc 138#Re: perlbug #35857, patch #24505
b3980c39
YO
139#handle warnings::register-ed packages properly.
140package B::Deparse::Wrapper;
141use strict;
142use warnings;
143use warnings::register;
144sub getcode {
579a54dc 145 my $deparser = B::Deparse->new();
b3980c39
YO
146 return $deparser->coderef2text(shift);
147}
148
149package main;
150use strict;
151use warnings;
152sub test {
579a54dc
RGS
153 my $val = shift;
154 my $res = B::Deparse::Wrapper::getcode($val);
09d856fb 155 like( $res, qr/use warnings/);
b3980c39
YO
156}
157my ($q,$p);
158my $x=sub { ++$q,++$p };
159test($x);
160eval <<EOFCODE and test($x);
161 package bar;
162 use strict;
163 use warnings;
164 use warnings::register;
165 package main;
166 1
167EOFCODE
168
ad46c0be 169__DATA__
14a55f98 170# 2
ad46c0be
RH
1711;
172####
14a55f98 173# 3
ad46c0be
RH
174{
175 no warnings;
176 '???';
177 2;
178}
179####
14a55f98 180# 4
ad46c0be
RH
181my $test;
182++$test and $test /= 2;
183>>>>
184my $test;
185$test /= 2 if ++$test;
186####
14a55f98 187# 5
ad46c0be
RH
188-((1, 2) x 2);
189####
14a55f98 190# 6
ad46c0be
RH
191{
192 my $test = sub : lvalue {
193 my $x;
194 }
195 ;
196}
197####
14a55f98 198# 7
ad46c0be
RH
199{
200 my $test = sub : method {
201 my $x;
202 }
203 ;
204}
205####
14a55f98 206# 8
ad46c0be
RH
207{
208 my $test = sub : locked method {
209 my $x;
210 }
211 ;
212}
213####
14a55f98 214# 9
87a42246 215{
ad46c0be 216 234;
f99a63a2 217}
ad46c0be
RH
218continue {
219 123;
87a42246 220}
ce4e655d 221####
14a55f98 222# 10
ce4e655d
RH
223my $x;
224print $main::x;
225####
14a55f98 226# 11
ce4e655d
RH
227my @x;
228print $main::x[1];
14a55f98
RH
229####
230# 12
231my %x;
232$x{warn()};
ad8caead
RGS
233####
234# 13
235my $foo;
236$_ .= <ARGV> . <$foo>;
cef22867
JH
237####
238# 14
239my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
4ae52e81
RGS
240####
241# 15
242s/x/'y';/e;
241416b8
DM
243####
244# 16 - various lypes of loop
245{ my $x; }
246####
247# 17
248while (1) { my $k; }
249####
250# 18
251my ($x,@a);
252$x=1 for @a;
253>>>>
254my($x, @a);
0bb5f065 255$x = 1 foreach (@a);
241416b8
DM
256####
257# 19
258for (my $i = 0; $i < 2;) {
259 my $z = 1;
260}
261####
262# 20
263for (my $i = 0; $i < 2; ++$i) {
264 my $z = 1;
265}
266####
267# 21
268for (my $i = 0; $i < 2; ++$i) {
269 my $z = 1;
270}
271####
272# 22
273my $i;
274while ($i) { my $z = 1; } continue { $i = 99; }
275####
276# 23
09d856fb 277foreach my $i (1, 2) {
241416b8
DM
278 my $z = 1;
279}
280####
281# 24
282my $i;
283foreach $i (1, 2) {
284 my $z = 1;
285}
286####
287# 25
288my $i;
289foreach my $i (1, 2) {
290 my $z = 1;
291}
292####
293# 26
294foreach my $i (1, 2) {
295 my $z = 1;
296}
297####
298# 27
299foreach our $i (1, 2) {
300 my $z = 1;
301}
302####
303# 28
304my $i;
305foreach our $i (1, 2) {
306 my $z = 1;
307}
3ac6e0f9
RGS
308####
309# 29
310my @x;
311print reverse sort(@x);
312####
313# 30
314my @x;
315print((sort {$b cmp $a} @x));
316####
317# 31
318my @x;
319print((reverse sort {$b <=> $a} @x));
36d57d93
RGS
320####
321# 32
322our @a;
323print $_ foreach (reverse @a);
aae53c41 324####
579a54dc 325# 33
aae53c41
RGS
326our @a;
327print $_ foreach (reverse 1, 2..5);
f86ea535
SM
328####
329# 34 (bug #38684)
330our @ary;
331@ary = split(' ', 'foo', 0);
31c6271a
RD
332####
333# 35 (bug #40055)
334do { () };
335####
336# 36 (ibid.)
337do { my $x = 1; $x };
d9002312
SM
338####
339# 37 <20061012113037.GJ25805@c4.convolution.nl>
340my $f = sub {
341 +{[]};
342} ;
8b2d6640
FC
343####
344# 38 (bug #43010)
345'!@$%'->();
346####
347# 39 (ibid.)
348::();
349####
350# 40 (ibid.)
351'::::'->();
352####
353# 41 (ibid.)
354&::::;
09d856fb
CK
355####
356# 42
357my $bar;
358'Foo'->$bar('orz');
359####
360# 43
361'Foo'->bar('orz');
362####
363# 44
364'Foo'->bar;
0ced6c29 365####
e9c69003 366# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
7ddd1a01
NC
367# 45 say
368say 'foo';
369####
e9c69003 370# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 371# 46 state vars
0ced6c29
RGS
372state $x = 42;
373####
e9c69003 374# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01
NC
375# 47 state var assignment
376{
377 my $y = (state $x = 42);
378}
379####
e9c69003 380# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01
NC
381# 48 state vars in anoymous subroutines
382$a = sub {
383 state $x;
384 return $x++;
385}
386;