]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blob - t/11-recall.t
Importing Sub-Prototype-Util-0.03.tar.gz
[perl/modules/Sub-Prototype-Util.git] / t / 11-recall.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 3 + 12 + (($^V ge v5.10.0) ? 4 : 0);
7
8 use Scalar::Util qw/set_prototype/;
9 use Sub::Prototype::Util qw/recall/;
10
11 eval { recall undef };
12 like($@, qr/^Wrong\s+subroutine/, 'recall undef croaks');
13 eval { recall '' };
14 like($@, qr/^Wrong\s+subroutine/, 'recall "" croaks');
15 eval { recall 'hlagh' };
16 like($@, qr/^Undefined\s+subroutine/, 'recall <unknown> croaks');
17
18 sub noproto { $_[1], $_[0] }
19 sub mytrunc ($;$) { $_[1], $_[0] };
20 sub mygrep1 (&@) { grep { $_[0]->() } @_[1 .. $#_] };
21 sub mygrep2 (\&@) { grep { $_[0]->() } @_[1 .. $#_] };
22 my $t = [ 1, 2, 3, 4 ];
23 my $g = [ sub { $_ > 2 }, 1 .. 5 ];
24 my @tests = (
25  [ 'main::noproto', 'no prototype', $t, $t, [ 2, 1 ] ],
26  [ 'CORE::push', 'push', [ [ 1, 2 ], 3, 5 ], [ [ 1, 2, 3, 5 ], 3, 5 ], [ 4 ] ],
27  [ 'main::mytrunc', 'truncate 1', [ 1 ], [ 1 ], [ undef, 1 ] ],
28  [ 'main::mytrunc', 'truncate 2', $t, $t, [ 2, 1 ] ],
29  [ 'main::mygrep1', 'grep1', $g, $g, [ 3 .. 5 ] ],
30  [ 'main::mygrep2', 'grep2', $g, $g, [ 3 .. 5 ] ],
31 );
32 sub myit { push @{$_[0]->[2]}, 3; return 4 };
33 if ($^V ge v5.10.0) {
34  set_prototype \&myit, '_';
35  push @tests, [ 'main::myit', '_ with argument',
36                 [ [ 1, 2, [ ] ], 5 ],
37                 [ [ 1, 2, [ 3 ] ], 5 ],
38                 [ 4 ]
39               ];
40  push @tests, [ 'main::myit', '_ with no argument', [ ], [ 3 ], [ 4 ] ];
41 }
42
43 for (@tests) {
44  my $r = [ recall $_->[0], @{$_->[2]} ];
45  is_deeply($r, $_->[4], $_->[1] . ' return value');
46  is_deeply($_->[2], $_->[3], $_->[1] . ' arguments modification');
47 }