]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blob - t/11-recall.t
Better be on irc.perl.org
[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 => 7 + 20 + (($^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/^No\s+subroutine/, 'recall undef croaks');
13 eval { recall '' };
14 like($@, qr/^No\s+subroutine/, 'recall "" croaks');
15 eval { recall \1 };
16 like($@, qr/^Unhandled\s+SCALAR/, 'recall scalarref croaks');
17 eval { recall [ ] };
18 like($@, qr/^Unhandled\s+ARRAY/, 'recall arrayref croaks');
19 eval { recall sub { } };
20 like($@, qr/^Unhandled\s+CODE/, 'recall coderef croaks');
21 eval { recall { 'foo' => undef, 'bar' => undef } };
22 like($@, qr!exactly\s+one\s+key/value\s+pair!, 'recall hashref with 2 pairs croaks');
23 eval { recall 'hlagh' };
24 like($@, qr/^Undefined\s+subroutine/, 'recall <unknown> croaks');
25
26 sub noproto { $_[1], $_[0] }
27 sub mytrunc ($;$) { $_[1], $_[0] }
28 sub mygrep1 (&@) { grep { $_[0]->() } @_[1 .. $#_] }
29 sub mygrep2 (\&@) { grep { $_[0]->() } @_[1 .. $#_] }
30 sub modify ($) { my $old = $_[0]; $_[0] = 5; $old }
31
32 my $t = [ 1, 2, 3, 4 ];
33 my $m = [ sub { $_ + 10 }, 1 .. 5 ];
34 my $g = [ sub { $_ > 2 }, 1 .. 5 ];
35
36 my @tests = (
37  [ 'main::noproto', 'no prototype', $t, $t, [ 2, 1 ] ],
38  [ { 'main::noproto' => undef }, 'no prototype forced', $t, $t, [ 2, 1 ] ],
39  [ 'CORE::push', 'push', [ [ 1, 2 ], 3, 5 ], [ [ 1, 2, 3, 5 ], 3, 5 ], [ 4 ] ],
40  [ { 'CORE::push' => '\@$' }, 'push just one', [ [ 1, 2 ], 3, 5 ], [ [ 1, 2, 3 ], 3, 5 ], [ 3 ] ],
41  [ { 'CORE::map' => '\&@' }, 'map', $m, $m, [ 11 .. 15 ] ],
42  [ 'main::mytrunc', 'truncate 1', [ 1 ], [ 1 ], [ undef, 1 ] ],
43  [ 'main::mytrunc', 'truncate 2', $t, $t, [ 2, 1 ] ],
44  [ 'main::mygrep1', 'grep1', $g, $g, [ 3 .. 5 ] ],
45  [ 'main::mygrep2', 'grep2', $g, $g, [ 3 .. 5 ] ],
46  [ 'main::modify', 'modify arguments', [ 1 ], [ 5 ], [ 1 ] ],
47 );
48
49 sub myit { push @{$_[0]->[2]}, 3; return 4 };
50 if ($^V ge v5.10.0) {
51  set_prototype \&myit, '_';
52  push @tests, [ 'main::myit', '_ with argument',
53                 [ [ 1, 2, [ ] ], 5 ],
54                 [ [ 1, 2, [ 3 ] ], 5 ],
55                 [ 4 ]
56               ];
57  push @tests, [ 'main::myit', '_ with no argument', [ ], [ 3 ], [ 4 ] ];
58 }
59
60 for (@tests) {
61  my $r = [ recall $_->[0], @{$_->[2]} ];
62  is_deeply($r, $_->[4], $_->[1] . ' return value');
63  is_deeply($_->[2], $_->[3], $_->[1] . ' arguments modification');
64 }