6 use Test::More tests => 7 + 18 + (($^V ge v5.10.0) ? 4 : 0);
8 use Scalar::Util qw/set_prototype/;
9 use Sub::Prototype::Util qw/recall/;
11 eval { recall undef };
12 like($@, qr/^No\s+subroutine/, 'recall undef croaks');
14 like($@, qr/^No\s+subroutine/, 'recall "" croaks');
16 like($@, qr/^Unhandled\s+SCALAR/, 'recall scalarref croaks');
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');
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 }
32 my $t = [ 1, 2, 3, 4 ];
33 my $m = [ sub { $_ + 10 }, 1 .. 5 ];
34 my $g = [ sub { $_ > 2 }, 1 .. 5 ];
37 [ 'main::noproto', 'no prototype', $t, $t, [ 2, 1 ] ],
38 [ { 'CORE::push' => undef }, 'push', [ [ 1, 2 ], 3, 5 ], [ [ 1, 2, 3, 5 ], 3, 5 ], [ 4 ] ],
39 [ { 'CORE::push' => '\@$' }, 'push just one', [ [ 1, 2 ], 3, 5 ], [ [ 1, 2, 3 ], 3, 5 ], [ 3 ] ],
40 [ { 'CORE::map' => '\&@' }, 'map', $m, $m, [ 11 .. 15 ] ],
41 [ 'main::mytrunc', 'truncate 1', [ 1 ], [ 1 ], [ undef, 1 ] ],
42 [ 'main::mytrunc', 'truncate 2', $t, $t, [ 2, 1 ] ],
43 [ 'main::mygrep1', 'grep1', $g, $g, [ 3 .. 5 ] ],
44 [ 'main::mygrep2', 'grep2', $g, $g, [ 3 .. 5 ] ],
45 [ 'main::modify', 'modify arguments', [ 1 ], [ 5 ], [ 1 ] ],
48 sub myit { push @{$_[0]->[2]}, 3; return 4 };
50 set_prototype \&myit, '_';
51 push @tests, [ 'main::myit', '_ with argument',
53 [ [ 1, 2, [ 3 ] ], 5 ],
56 push @tests, [ 'main::myit', '_ with no argument', [ ], [ 3 ], [ 4 ] ];
60 my $r = [ recall $_->[0], @{$_->[2]} ];
61 is_deeply($r, $_->[4], $_->[1] . ' return value');
62 is_deeply($_->[2], $_->[3], $_->[1] . ' arguments modification');