6 use Test::More tests => 8 + 20 + (($^V ge v5.10.0) ? 4 : 0);
8 use Scalar::Util qw/set_prototype/;
9 use Sub::Prototype::Util qw/recall/;
14 return qr/^$msg.*?at\s+\Q$0\E\s+line\s+\d+/;
17 eval { recall undef };
18 like $@, exception('No subroutine'), 'recall undef croaks';
20 like $@, exception('No subroutine'), 'recall "" croaks';
22 like $@, exception('Unhandled SCALAR'), 'recall scalarref croaks';
24 like $@, exception('Unhandled ARRAY'), 'recall arrayref croaks';
25 eval { recall sub { } };
26 like $@, exception('Unhandled CODE'), 'recall coderef croaks';
27 eval { recall { 'foo' => undef, 'bar' => undef } };
28 like $@, qr!exactly\s+one\s+key/value\s+pair!,
29 'recall hashref with 2 pairs croaks';
30 eval { recall 'hlagh' };
31 like $@, qr/^Undefined\s+subroutine/, 'recall <unknown> croaks';
32 eval { recall 'for' };
33 like $@, exception('syntax error'), 'invalid eval code croaks';
35 sub noproto { $_[1], $_[0] }
36 sub mytrunc ($;$) { $_[1], $_[0] }
37 sub mygrep1 (&@) { grep { $_[0]->() } @_[1 .. $#_] }
38 sub mygrep2 (\&@) { grep { $_[0]->() } @_[1 .. $#_] }
39 sub modify ($) { my $old = $_[0]; $_[0] = 5; $old }
41 my $t = [ 1, 2, 3, 4 ];
42 my $m = [ sub { $_ + 10 }, 1 .. 5 ];
43 my $g = [ sub { $_ > 2 }, 1 .. 5 ];
46 [ 'main::noproto', 'no prototype', $t, $t, [ 2, 1 ] ],
47 [ { 'main::noproto' => undef }, 'no prototype forced', $t, $t, [ 2, 1 ] ],
48 [ 'CORE::push', 'push', [ [ 1, 2 ], 3, 5 ], [ [ 1, 2, 3, 5 ], 3, 5 ], [ 4 ] ],
49 [ { 'CORE::push' => '\@$' }, 'push just one', [ [ 1, 2 ], 3, 5 ], [ [ 1, 2, 3 ], 3, 5 ], [ 3 ] ],
50 [ { 'CORE::map' => '\&@' }, 'map', $m, $m, [ 11 .. 15 ] ],
51 [ 'main::mytrunc', 'truncate 1', [ 1 ], [ 1 ], [ undef, 1 ] ],
52 [ 'main::mytrunc', 'truncate 2', $t, $t, [ 2, 1 ] ],
53 [ 'main::mygrep1', 'grep1', $g, $g, [ 3 .. 5 ] ],
54 [ 'main::mygrep2', 'grep2', $g, $g, [ 3 .. 5 ] ],
55 [ 'main::modify', 'modify arguments', [ 1 ], [ 5 ], [ 1 ] ],
58 sub myit { push @{$_[0]->[2]}, 3; return 4 };
60 set_prototype \&myit, '_';
61 push @tests, [ 'main::myit', '_ with argument',
63 [ [ 1, 2, [ 3 ] ], 5 ],
66 push @tests, [ 'main::myit', '_ with no argument', [ ], [ 3 ], [ 4 ] ];
70 my $r = [ recall $_->[0], @{$_->[2]} ];
71 is_deeply($r, $_->[4], $_->[1] . ' return value');
72 is_deeply($_->[2], $_->[3], $_->[1] . ' arguments modification');