]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blob - t/12-wrap.t
Better be on irc.perl.org
[perl/modules/Sub-Prototype-Util.git] / t / 12-wrap.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 7 + 6 + 3 + 1 + 6 + 1 + (($^V ge v5.10.0) ? 2 : 0) + 1;
7
8 use Scalar::Util qw/set_prototype/;
9 use Sub::Prototype::Util qw/wrap/;
10
11 eval { wrap undef };
12 like($@, qr/^No\s+subroutine/, 'recall undef croaks');
13 eval { wrap '' };
14 like($@, qr/^No\s+subroutine/, 'recall "" croaks');
15 eval { wrap \1 };
16 like($@, qr/^Unhandled\s+SCALAR/, 'recall scalarref croaks');
17 eval { wrap [ ] };
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');
25
26 my $push_exp = '{ CORE::push(@{$_[0]}, @_[1..$#_]) }';
27 my $push = wrap 'CORE::push';
28 is($push, 'sub ' . $push_exp, 'wrap push as a sub (default)');
29 $push = wrap 'CORE::push', sub => 1;
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', compile => 1;
34 is(ref $push, 'CODE', 'wrap compiled push is a CODE reference');
35 my @a = qw/a b/;
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');
39
40 my $push2 = wrap { 'CORE::push' => '\@;$' }, compile => 1;
41 is(ref $push2, 'CODE', 'wrap compiled truncated push is a CODE reference');
42 @a = qw/x y z/;
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');
46
47 sub cb (\[$@]\[%&]&&);
48 my $cb = wrap 'main::cb', sub => 0, wrong_ref => 'die';
49 my $x = ', sub{&{$c[0]}}, sub{&{$c[1]}}) ';
50 is($cb,
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,
59              q!} else { !,
60               q!die !,
61              q!} !,
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,
68              q!} else { !,
69               q!die !,
70              q!} !,
71             q!} else { !,
72              q!die !,
73             q!} }!),
74     'callbacks');
75
76 sub myref { ref $_[0] };
77
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]};
84   }
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]};
90   }
91  }
92 }
93
94 SKIP: {
95  skip 'perl 5.8.x is needed to test execution of \[$@] prototypes' => 6
96    if $^V lt v5.8.0;
97
98  my $cat = wrap 'main::cat', ref => 'main::myref', wrong_ref => 'die "hlagh"',
99                              sub => 1, compile => 1;
100  my @tests = (
101   [ \'a',        \'b',        [ 'ab' ],        'scalar-scalar' ],
102   [ \'c',        [ qw/d e/ ], [ qw/c d e/ ],   'scalar-array' ],
103   [ [ qw/f g/ ], \'h',        [ qw/f g h/ ],   'array-scalar' ],
104   [ [ qw/i j/ ], [ qw/k l/ ], [ qw/i j k l/ ], 'array-array' ]
105  );
106  for (@tests) {
107   my $res = [ $cat->($_->[0], $_->[1]) ];
108   is_deeply($res, $_->[2], 'cat ' . $_->[3]);
109  }
110  eval { $cat->({ foo => 1 }, [ 2 ] ) };
111  like($@, qr/^hlagh\s+at/, 'wrong reference type 1');
112  eval { $cat->(\1, sub { 2 } ) };
113  like($@, qr/^hlagh\s+at/, 'wrong reference type 2');
114 }
115
116 sub noproto;
117 my $noproto_exp = '{ main::noproto(@_) }';
118 my $noproto = wrap 'main::noproto', sub => 0;
119 is($noproto, $noproto_exp, 'no prototype');
120
121 sub myit { my $ar = shift; push @$ar, @_; };
122 if ($^V ge v5.10.0) {
123  set_prototype \&myit, '\@$_';
124  my $it = wrap 'main::myit', compile => 1;
125  my @a = qw/u v w/;
126  local $_ = 7;
127  $it->(\@a, 3, 4, 5);
128  is_deeply(\@a, [ qw/u v w/, 3, 4 ], '_ with arguments');
129  $it->(\@a, 6);
130  is_deeply(\@a, [ qw/u v w/, 3, 4, 6, 7 ], '_ without arguments');
131 }
132
133 eval { wrap { 'main::dummy' => '\[@%]' }, ref => 'shift', compile => 1 };
134 like($@, qr/to\s+shift\s+must\s+be\s+array/, 'invalid eval code croaks');