]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blob - t/11-recall.t
3e3c7a28180f404ac5e997b68d5081a52b49aa92
[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) ? 2 : 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 @{$_->[2]}, 1; return 2 };
33 if ($^V ge v5.10.0) {
34  set_prototype \&myit, '_';
35  push @tests, [ 'main::myit', '_ prototype', [ ], [ 1 ], [ 2 ] ];
36 }
37
38 for (@tests) {
39  my $r = [ recall $_->[0], @{$_->[2]} ];
40  is_deeply($r, $_->[4], $_->[1] . ' return value');
41  is_deeply($_->[2], $_->[3], $_->[1] . ' arguments modification');
42 }