This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #46947] Parse method-BLOCK arguments as a term
[perl5.git] / ext / B / t / showlex.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     require 'test.pl';
11 }
12
13 $| = 1;
14 use warnings;
15 use strict;
16 use Config;
17 use B::Showlex ();
18
19 plan tests => 15;
20
21 my $verbose = @ARGV; # set if ANY ARGS
22
23 my $a;
24 my $Is_VMS = $^O eq 'VMS';
25
26 my $path = join " ", map { qq["-I$_"] } @INC;
27 $path = '"-I../lib" "-Iperl_root:[lib]"' if $Is_VMS;   # gets too long otherwise
28 my $is_thread = $Config{use5005threads} && $Config{use5005threads} eq 'define';
29
30 if ($is_thread) {
31     ok "# use5005threads: test skipped\n";
32 } else {
33     $a = `$^X $path "-MO=Showlex" -e "my \@one" 2>&1`;
34     like ($a, qr/sv_undef.*PVNV.*\@one.*Nullsv.*AV/s,
35           "canonical usage works");
36 }
37
38 # v1.01 tests
39
40 my ($na,$nb,$nc);       # holds regex-strs
41 my ($out, $newlex);     # output, option-flag
42
43 sub padrep {
44     my ($varname,$newlex) = @_;
45     return ($newlex)
46         ? 'PVNV \(0x[0-9a-fA-F]+\) "\\'.$varname.'" = '
47         : "PVNV \\\(0x[0-9a-fA-F]+\\\) \\$varname\n";
48 }
49
50 for $newlex ('', '-newlex') {
51
52     $out = runperl ( switches => ["-MO=Showlex,$newlex"],
53                      prog => 'my ($a,$b)', stderr => 1 );
54     $na = padrep('$a',$newlex);
55     $nb = padrep('$b',$newlex);
56     like ($out, qr/1: $na/ms, 'found $a in "my ($a,$b)"');
57     like ($out, qr/2: $nb/ms, 'found $b in "my ($a,$b)"');
58
59     print $out if $verbose;
60
61 SKIP: {
62     skip "no perlio in this build", 5
63     unless $Config::Config{useperlio};
64
65     our $buf = 'arb startval';
66     my $ak = B::Showlex::walk_output (\$buf);
67
68     my $walker = B::Showlex::compile( $newlex, sub{my($foo,$bar)} );
69     $walker->();
70     $na = padrep('$foo',$newlex);
71     $nb = padrep('$bar',$newlex);
72     like ($buf, qr/1: $na/ms, 'found $foo in "sub { my ($foo,$bar) }"');
73     like ($buf, qr/2: $nb/ms, 'found $bar in "sub { my ($foo,$bar) }"');
74
75     print $buf if $verbose;
76
77     $ak = B::Showlex::walk_output (\$buf);
78
79     my $src = 'sub { my ($scalar,@arr,%hash) }';
80     my $sub = eval $src;
81     $walker = B::Showlex::compile($sub);
82     $walker->();
83     $na = padrep('$scalar',$newlex);
84     $nb = padrep('@arr',$newlex);
85     $nc = padrep('%hash',$newlex);
86     like ($buf, qr/1: $na/ms, 'found $scalar in "'. $src .'"');
87     like ($buf, qr/2: $nb/ms, 'found @arr    in "'. $src .'"');
88     like ($buf, qr/3: $nc/ms, 'found %hash   in "'. $src .'"');
89
90     print $buf if $verbose;
91
92     # fibonacci function under test
93     my $asub = sub {
94         my ($self,%props)=@_;
95         my $total;
96         { # inner block vars
97             my (@fib)=(1,2);
98             for (my $i=2; $i<10; $i++) {
99                 $fib[$i] = $fib[$i-2] + $fib[$i-1];
100             }
101             for my $i(0..10) {
102                 $total += $i;
103             }
104         }
105     };
106     $walker = B::Showlex::compile($asub, $newlex, -nosp);
107     $walker->();
108     print $buf if $verbose;
109
110     $walker = B::Concise::compile($asub, '-exec');
111     $walker->();
112
113 }
114 }