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