]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blob - t/12-recall.t
Fix some croak backtraces
[perl/modules/Sub-Prototype-Util.git] / t / 12-recall.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 8 + 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 eval { recall 'for' };
26 like($@, qr/^syntax\s+error\s+at\s+\Q$0\E/, 'invalid eval code croaks');
27
28 sub noproto { $_[1], $_[0] }
29 sub mytrunc ($;$) { $_[1], $_[0] }
30 sub mygrep1 (&@) { grep { $_[0]->() } @_[1 .. $#_] }
31 sub mygrep2 (\&@) { grep { $_[0]->() } @_[1 .. $#_] }
32 sub modify ($) { my $old = $_[0]; $_[0] = 5; $old }
33
34 my $t = [ 1, 2, 3, 4 ];
35 my $m = [ sub { $_ + 10 }, 1 .. 5 ];
36 my $g = [ sub { $_ > 2 }, 1 .. 5 ];
37
38 my @tests = (
39  [ 'main::noproto', 'no prototype', $t, $t, [ 2, 1 ] ],
40  [ { 'main::noproto' => undef }, 'no prototype forced', $t, $t, [ 2, 1 ] ],
41  [ 'CORE::push', 'push', [ [ 1, 2 ], 3, 5 ], [ [ 1, 2, 3, 5 ], 3, 5 ], [ 4 ] ],
42  [ { 'CORE::push' => '\@$' }, 'push just one', [ [ 1, 2 ], 3, 5 ], [ [ 1, 2, 3 ], 3, 5 ], [ 3 ] ],
43  [ { 'CORE::map' => '\&@' }, 'map', $m, $m, [ 11 .. 15 ] ],
44  [ 'main::mytrunc', 'truncate 1', [ 1 ], [ 1 ], [ undef, 1 ] ],
45  [ 'main::mytrunc', 'truncate 2', $t, $t, [ 2, 1 ] ],
46  [ 'main::mygrep1', 'grep1', $g, $g, [ 3 .. 5 ] ],
47  [ 'main::mygrep2', 'grep2', $g, $g, [ 3 .. 5 ] ],
48  [ 'main::modify', 'modify arguments', [ 1 ], [ 5 ], [ 1 ] ],
49 );
50
51 sub myit { push @{$_[0]->[2]}, 3; return 4 };
52 if ($^V ge v5.10.0) {
53  set_prototype \&myit, '_';
54  push @tests, [ 'main::myit', '_ with argument',
55                 [ [ 1, 2, [ ] ], 5 ],
56                 [ [ 1, 2, [ 3 ] ], 5 ],
57                 [ 4 ]
58               ];
59  push @tests, [ 'main::myit', '_ with no argument', [ ], [ 3 ], [ 4 ] ];
60 }
61
62 for (@tests) {
63  my $r = [ recall $_->[0], @{$_->[2]} ];
64  is_deeply($r, $_->[4], $_->[1] . ' return value');
65  is_deeply($_->[2], $_->[3], $_->[1] . ' arguments modification');
66 }