]> git.vpit.fr Git - perl/modules/B-RecDeparse.git/blob - t/14-refs.t
Make t/14-refs.t pass on perl 5.21
[perl/modules/B-RecDeparse.git] / t / 14-refs.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 2 * (4 + 3) * 4;
7
8 use B::RecDeparse;
9
10 {
11  BEGIN {
12   strict->unimport('vars') if "$]" >= 5.021;
13  }
14  sub dummy { }
15  sub add { $_[0] + $_[1] }
16  sub call ($$$) { my $x = \&dummy; $_[0]->($_[1], $_[2]) }
17  sub foo { call(\&add, $_[0], 1); }
18  sub bar { my $y = \&call; $y->(\&add, $_[0], 1); }
19 }
20
21 sub which {
22  my ($brd, $coderef, $yfunc, $yref, $nfunc, $nref, $l) = @_;
23  my $code = $brd->coderef2text($coderef);
24  for (@$yfunc) {
25   like($code, qr/\b(?<!\\&)$_\b/, "expansion at level $l contains the function $_");
26  }
27  for (@$yref) {
28   like($code, qr/\b(?<=\\&)$_\b/, "expansion at level $l contains the ref $_");
29  }
30  for (@$nfunc) {
31   unlike($code, qr/\b(?<!\\&)$_\b/, "expansion at level $l does not contain the function $_");
32  }
33  for (@$nref) {
34   unlike($code, qr/\b(?<=\\&)$_\b/, "expansion at level $l does not contain the ref $_");
35  }
36  $code = eval 'sub ' . $code;
37  is($@, '', "result compiles at level $l");
38  is_deeply( [ defined $code, ref $code ], [ 1, 'CODE' ], "result compiles to a code reference at level $l");
39  is($code->(2), $coderef->(2), "result compiles to the good thing at level $l");
40 }
41
42 my $bd_args = '-sCi0v1';
43
44 my $brd = B::RecDeparse->new(deparse => $bd_args, level => -1);
45 which $brd, \&foo, [ ], [ qw<add dummy> ], [ qw<add call> ], [ ], -1;
46 which $brd, \&bar, [ ], [ qw<add call> ], [ qw<add call> ], [ ], -1;
47
48 $brd = B::RecDeparse->new(deparse => $bd_args, level => 0);
49 which $brd, \&foo, [ qw<call> ], [ qw<add> ], [ qw<add> ], [ qw<dummy> ], 0;
50 which $brd, \&bar, [ ], [ qw<add call> ], [ qw<add> ], [ qw<dummy> ], 0;
51
52 $brd = B::RecDeparse->new(deparse => $bd_args, level => 1);
53 which $brd, \&foo, [ ], [ qw<add dummy> ], [ qw<add call> ], [ ], 1;
54 which $brd, \&bar, [ ], [ qw<add call> ], [ qw<add call> ], [ ], 1;
55
56 $brd = B::RecDeparse->new(deparse => $bd_args, level => 2);
57 which $brd, \&foo, [ ], [ qw<add dummy> ], [ qw<add call> ], [ ], 2;
58 which $brd, \&bar, [ ], [ qw<add call> ], [ qw<add call> ], [ ], 2;