[ patch ] add line-number to embed.pl warnings
[p5sagit/p5-mst-13.2.git] / ext / B / t / deparse.t
CommitLineData
87a42246 1#!./perl
2
3BEGIN {
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 22use warnings;
23use strict;
0ced6c29 24use feature ":5.10";
25use Test::More tests => 52;
87a42246 26
27use B::Deparse;
09d856fb 28my $deparse = B::Deparse->new();
29ok($deparse);
87a42246 30
31# Tell B::Deparse about our ambient pragmas
0ced6c29 32{ my ($hint_bits, $warning_bits, $hinthash);
33 BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
87a42246 34 $deparse->ambient_pragmas (
35 hint_bits => $hint_bits,
36 warning_bits => $warning_bits,
0ced6c29 37 '$[' => 0 + $[,
38 '%^H' => $hinthash,
87a42246 39 );
40}
41
ad46c0be 42$/ = "\n####\n";
43while (<DATA>) {
44 chomp;
ec59cdf2 45 s/#\s*(.*)$//mg;
46 my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;
ad46c0be 47 my ($input, $expected);
48 if (/(.*)\n>>>>\n(.*)/s) {
49 ($input, $expected) = ($1, $2);
50 }
51 else {
52 ($input, $expected) = ($_, $_);
53 }
87a42246 54
ad46c0be 55 my $coderef = eval "sub {$input}";
87a42246 56
ad46c0be 57 if ($@) {
ec59cdf2 58 diag("$num deparsed: $@");
59 ok(0, $testname);
ad46c0be 60 }
61 else {
62 my $deparsed = $deparse->coderef2text( $coderef );
31c6271a 63 my $regex = $expected;
64 $regex =~ s/(\S+)/\Q$1/g;
65 $regex =~ s/\s+/\\s+/g;
66 $regex = '^\{\s*' . $regex . '\s*\}$';
ec59cdf2 67 like($deparsed, qr/$regex/, $testname);
87a42246 68 }
87a42246 69}
70
87a42246 71use constant 'c', 'stuff';
09d856fb 72is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff');
87a42246 73
09d856fb 74my $a = 0;
75is("{\n (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a }));
87a42246 76
d989cdac 77use constant cr => ['hello'];
78my $string = "sub " . $deparse->coderef2text(\&cr);
79my $val = (eval $string)->();
09d856fb 80ok( ref($val) eq 'ARRAY' && $val->[0] eq 'hello');
87a42246 81
87a42246 82my $Is_VMS = $^O eq 'VMS';
83my $Is_MacOS = $^O eq 'MacOS';
84
85my $path = join " ", map { qq["-I$_"] } @INC;
be708cc0 86$path .= " -MMac::err=unix" if $Is_MacOS;
87a42246 87my $redir = $Is_MacOS ? "" : "2>&1";
88
d2bc402e 89$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 $redir`;
e69a2255 90$a =~ s/-e syntax OK\n//g;
d2bc402e 91$a =~ s/.*possible typo.*\n//; # Remove warning line
87a42246 92$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
93$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
94$b = <<'EOF';
d2bc402e 95BEGIN { $^I = ".bak"; }
96BEGIN { $^W = 1; }
97BEGIN { $/ = "\n"; $\ = "\n"; }
87a42246 98LINE: while (defined($_ = <ARGV>)) {
99 chomp $_;
f86ea535 100 our(@F) = split(' ', $_, 0);
87a42246 101 '???';
102}
87a42246 103EOF
e69a2255 104$b =~ s/(LINE:)/sub BEGIN {
105 'MacPerl'->bootstrap;
106 'OSA'->bootstrap;
107 'XL'->bootstrap;
108}
109$1/ if $Is_MacOS;
09d856fb 110is($a, $b);
87a42246 111
579a54dc 112#Re: perlbug #35857, patch #24505
b3980c39 113#handle warnings::register-ed packages properly.
114package B::Deparse::Wrapper;
115use strict;
116use warnings;
117use warnings::register;
118sub getcode {
579a54dc 119 my $deparser = B::Deparse->new();
b3980c39 120 return $deparser->coderef2text(shift);
121}
122
123package main;
124use strict;
125use warnings;
126sub test {
579a54dc 127 my $val = shift;
128 my $res = B::Deparse::Wrapper::getcode($val);
09d856fb 129 like( $res, qr/use warnings/);
b3980c39 130}
131my ($q,$p);
132my $x=sub { ++$q,++$p };
133test($x);
134eval <<EOFCODE and test($x);
135 package bar;
136 use strict;
137 use warnings;
138 use warnings::register;
139 package main;
140 1
141EOFCODE
142
ad46c0be 143__DATA__
14a55f98 144# 2
ad46c0be 1451;
146####
14a55f98 147# 3
ad46c0be 148{
149 no warnings;
150 '???';
151 2;
152}
153####
14a55f98 154# 4
ad46c0be 155my $test;
156++$test and $test /= 2;
157>>>>
158my $test;
159$test /= 2 if ++$test;
160####
14a55f98 161# 5
ad46c0be 162-((1, 2) x 2);
163####
14a55f98 164# 6
ad46c0be 165{
166 my $test = sub : lvalue {
167 my $x;
168 }
169 ;
170}
171####
14a55f98 172# 7
ad46c0be 173{
174 my $test = sub : method {
175 my $x;
176 }
177 ;
178}
179####
14a55f98 180# 8
ad46c0be 181{
182 my $test = sub : locked method {
183 my $x;
184 }
185 ;
186}
187####
14a55f98 188# 9
87a42246 189{
ad46c0be 190 234;
f99a63a2 191}
ad46c0be 192continue {
193 123;
87a42246 194}
ce4e655d 195####
14a55f98 196# 10
ce4e655d 197my $x;
198print $main::x;
199####
14a55f98 200# 11
ce4e655d 201my @x;
202print $main::x[1];
14a55f98 203####
204# 12
205my %x;
206$x{warn()};
ad8caead 207####
208# 13
209my $foo;
210$_ .= <ARGV> . <$foo>;
cef22867 211####
212# 14
213my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
4ae52e81 214####
215# 15
216s/x/'y';/e;
241416b8 217####
218# 16 - various lypes of loop
219{ my $x; }
220####
221# 17
222while (1) { my $k; }
223####
224# 18
225my ($x,@a);
226$x=1 for @a;
227>>>>
228my($x, @a);
0bb5f065 229$x = 1 foreach (@a);
241416b8 230####
231# 19
232for (my $i = 0; $i < 2;) {
233 my $z = 1;
234}
235####
236# 20
237for (my $i = 0; $i < 2; ++$i) {
238 my $z = 1;
239}
240####
241# 21
242for (my $i = 0; $i < 2; ++$i) {
243 my $z = 1;
244}
245####
246# 22
247my $i;
248while ($i) { my $z = 1; } continue { $i = 99; }
249####
250# 23
09d856fb 251foreach my $i (1, 2) {
241416b8 252 my $z = 1;
253}
254####
255# 24
256my $i;
257foreach $i (1, 2) {
258 my $z = 1;
259}
260####
261# 25
262my $i;
263foreach my $i (1, 2) {
264 my $z = 1;
265}
266####
267# 26
268foreach my $i (1, 2) {
269 my $z = 1;
270}
271####
272# 27
273foreach our $i (1, 2) {
274 my $z = 1;
275}
276####
277# 28
278my $i;
279foreach our $i (1, 2) {
280 my $z = 1;
281}
3ac6e0f9 282####
283# 29
284my @x;
285print reverse sort(@x);
286####
287# 30
288my @x;
289print((sort {$b cmp $a} @x));
290####
291# 31
292my @x;
293print((reverse sort {$b <=> $a} @x));
36d57d93 294####
295# 32
296our @a;
297print $_ foreach (reverse @a);
aae53c41 298####
579a54dc 299# 33
aae53c41 300our @a;
301print $_ foreach (reverse 1, 2..5);
f86ea535 302####
303# 34 (bug #38684)
304our @ary;
305@ary = split(' ', 'foo', 0);
31c6271a 306####
307# 35 (bug #40055)
308do { () };
309####
310# 36 (ibid.)
311do { my $x = 1; $x };
d9002312 312####
313# 37 <20061012113037.GJ25805@c4.convolution.nl>
314my $f = sub {
315 +{[]};
316} ;
8b2d6640 317####
318# 38 (bug #43010)
319'!@$%'->();
320####
321# 39 (ibid.)
322::();
323####
324# 40 (ibid.)
325'::::'->();
326####
327# 41 (ibid.)
328&::::;
09d856fb 329####
330# 42
331my $bar;
332'Foo'->$bar('orz');
333####
334# 43
335'Foo'->bar('orz');
336####
337# 44
338'Foo'->bar;
0ced6c29 339####
340# 45 state vars
341state $x = 42;
342####
343# 46 say
344say 'foo';