]> git.vpit.fr Git - perl/modules/B-RecDeparse.git/blob - t/14-refs.t
21c4607fb0ac0c898f5c43ca75db9671fca1e0b4
[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 sub dummy { }
11 sub add { $_[0] + $_[1] }
12 sub call ($$$) { my $x = \&dummy; $_[0]->($_[1], $_[2]) }
13 sub foo { call(\&add, $_[0], 1); }
14 sub bar { my $y = \&call; $y->(\&add, $_[0], 1); }
15
16 sub which {
17  my ($brd, $coderef, $yfunc, $yref, $nfunc, $nref, $l) = @_;
18  my $code = $brd->coderef2text($coderef);
19  for (@$yfunc) {
20   like($code, qr/\b(?<!\\&)$_\b/, "expansion at level $l contains the function $_");
21  }
22  for (@$yref) {
23   like($code, qr/\b(?<=\\&)$_\b/, "expansion at level $l contains the ref $_");
24  }
25  for (@$nfunc) {
26   unlike($code, qr/\b(?<!\\&)$_\b/, "expansion at level $l does not contain the function $_");
27  }
28  for (@$nref) {
29   unlike($code, qr/\b(?<=\\&)$_\b/, "expansion at level $l does not contain the ref $_");
30  }
31  $code = eval 'sub ' . $code;
32  is($@, '', "result compiles at level $l");
33  is_deeply( [ defined $code, ref $code ], [ 1, 'CODE' ], "result compiles to a code reference at level $l");
34  is($code->(2), $coderef->(2), "result compiles to the good thing at level $l");
35 }
36
37 my $br_args = '-sCi0v1';
38
39 my $brd = B::RecDeparse->new(deparse => $br_args, level => -1);
40 which $brd, \&foo, [ ], [ qw/add dummy/ ], [ qw/add call/ ], [ ], -1;
41 which $brd, \&bar, [ ], [ qw/add call/ ], [ qw/add call/ ], [ ], -1;
42
43 $brd = B::RecDeparse->new(deparse => $br_args, level => 0);
44 which $brd, \&foo, [ qw/call/ ], [ qw/add/ ], [ qw/add/ ], [ qw/dummy/ ], 0;
45 which $brd, \&bar, [ ], [ qw/add call/ ], [ qw/add/ ], [ qw/dummy/ ], 0;
46
47 $brd = B::RecDeparse->new(deparse => $br_args, level => 1);
48 which $brd, \&foo, [ ], [ qw/add dummy/ ], [ qw/add call/ ], [ ], 1;
49 which $brd, \&bar, [ ], [ qw/add call/ ], [ qw/add call/ ], [ ], 1;
50
51 $brd = B::RecDeparse->new(deparse => $br_args, level => 2);
52 which $brd, \&foo, [ ], [ qw/add dummy/ ], [ qw/add call/ ], [ ], 2;
53 which $brd, \&bar, [ ], [ qw/add call/ ], [ qw/add call/ ], [ ], 2;