6 use Test::More tests => 7 + 6 + 3 + 1 + 6 + 1 + (($^V ge v5.10.0) ? 2 : 0) + 1;
8 use Scalar::Util qw/set_prototype/;
9 use Sub::Prototype::Util qw/wrap/;
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 { wrap sub { } };
20 like($@, qr/^Unhandled\s+CODE/, 'recall coderef croaks');
21 eval { wrap { 'foo' => undef, 'bar' => undef } };
22 like($@, qr!exactly\s+one\s+key/value\s+pair!, 'recall hashref with 2 pairs croaks');
23 eval { wrap 'hlagh', qw/a b c/ };
24 like($@, qr/^Optional\s+arguments/, 'recall takes options in a key => value list');
26 my $push_exp = '{ CORE::push(@{$_[0]}, @_[1..$#_]) }';
27 my $push = wrap 'CORE::push', compile => 0;
28 is($push, 'sub ' . $push_exp, 'wrap push as a sub (default)');
29 $push = wrap 'CORE::push', sub => 1, compile => 0;
30 is($push, 'sub ' . $push_exp, 'wrap push as a sub');
31 $push = wrap 'CORE::push', sub => 0;
32 is($push, $push_exp, 'wrap push as a raw string');
33 $push = wrap 'CORE::push';
34 is(ref $push, 'CODE', 'wrap compiled push is a CODE reference');
36 my $ret = $push->(\@a, 7 .. 12);
37 is_deeply(\@a, [ qw/a b/, 7 .. 12 ], 'wrap compiled push works');
38 is($ret, 8, 'wrap compiled push returns the correct number of elements');
40 my $push2 = wrap { 'CORE::push' => '\@;$' };
41 is(ref $push2, 'CODE', 'wrap compiled truncated push is a CODE reference');
43 $ret = $push2->(\@a, 3 .. 5);
44 is_deeply(\@a, [ qw/x y z/, 3 ], 'wrap compiled truncated push works');
45 is($ret, 4, 'wrap compiled truncated push returns the correct number of elements');
47 sub cb (\[$@]\[%&]&&);
48 my $cb = wrap 'main::cb', sub => 0, wrong_ref => 'die';
49 my $x = ', sub{&{$c[0]}}, sub{&{$c[1]}}) ';
51 join('', q!{ my @c; push @c, $_[2]; push @c, $_[3]; !,
52 q!my $r = ref($_[0]); !,
53 q!if ($r eq 'SCALAR') { !,
54 q!my $r = ref($_[1]); !,
55 q!if ($r eq 'HASH') { !,
56 q!main::cb(${$_[0]}, %{$_[1]}! . $x,
57 q!} elsif ($r eq 'CODE') { !,
58 q!main::cb(${$_[0]}, &{$_[1]}! . $x,
62 q!} elsif ($r eq 'ARRAY') { !,
63 q!my $r = ref($_[1]); !,
64 q!if ($r eq 'HASH') { !,
65 q!main::cb(@{$_[0]}, %{$_[1]}! . $x,
66 q!} elsif ($r eq 'CODE') { !,
67 q!main::cb(@{$_[0]}, &{$_[1]}! . $x,
76 sub myref { ref $_[0] };
78 sub cat (\[$@]\[$@]) {
79 if (ref $_[0] eq 'SCALAR') {
80 if (ref $_[1] eq 'SCALAR') {
81 return ${$_[0]} . ${$_[1]};
82 } elsif (ref $_[1] eq 'ARRAY') {
83 return ${$_[0]}, @{$_[1]};
85 } elsif (ref $_[0] eq 'ARRAY') {
86 if (ref $_[1] eq 'SCALAR') {
87 return @{$_[0]}, ${$_[1]};
88 } elsif (ref $_[1] eq 'ARRAY') {
89 return @{$_[0]}, @{$_[1]};
95 skip 'perl 5.8.x is needed to test execution of \[$@] prototypes' => 6
98 my $cat = wrap 'main::cat', ref => 'main::myref',
100 wrong_ref => 'die "hlagh"',
102 [ \'a', \'b', [ 'ab' ], 'scalar-scalar' ],
103 [ \'c', [ qw/d e/ ], [ qw/c d e/ ], 'scalar-array' ],
104 [ [ qw/f g/ ], \'h', [ qw/f g h/ ], 'array-scalar' ],
105 [ [ qw/i j/ ], [ qw/k l/ ], [ qw/i j k l/ ], 'array-array' ]
108 my $res = [ $cat->($_->[0], $_->[1]) ];
109 is_deeply($res, $_->[2], 'cat ' . $_->[3]);
111 eval { $cat->({ foo => 1 }, [ 2 ] ) };
112 like($@, qr/^hlagh\s+at/, 'wrong reference type 1');
113 eval { $cat->(\1, sub { 2 } ) };
114 like($@, qr/^hlagh\s+at/, 'wrong reference type 2');
118 my $noproto_exp = '{ main::noproto(@_) }';
119 my $noproto = wrap 'main::noproto', sub => 0;
120 is($noproto, $noproto_exp, 'no prototype');
122 sub myit { my $ar = shift; push @$ar, @_; };
123 if ($^V ge v5.10.0) {
124 set_prototype \&myit, '\@$_';
125 my $it = wrap 'main::myit';
129 is_deeply(\@a, [ qw/u v w/, 3, 4 ], '_ with arguments');
131 is_deeply(\@a, [ qw/u v w/, 3, 4, 6, 7 ], '_ without arguments');
134 eval { wrap { 'main::dummy' => '\[@%]' }, ref => 'shift' };
135 like($@, qr/to\s+shift\s+must\s+be\s+array +\([\w ]+\) +at\s+\Q$0\E/, 'invalid eval code croaks');