]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blob - t/12-wrap.t
Importing Sub-Prototype-Util-0.06.tar.gz
[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);
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 my $cat = wrap 'main::cat', ref => 'main::myref', wrong_ref => 'die "hlagh"',
95                             sub => 1, compile => 1,
96 my @tests = (
97  [ \'a',        \'b',        [ 'ab' ],        'scalar-scalar' ],
98  [ \'c',        [ qw/d e/ ], [ qw/c d e/ ],   'scalar-array' ],
99  [ [ qw/f g/ ], \'h',        [ qw/f g h/ ],   'array-scalar' ],
100  [ [ qw/i j/ ], [ qw/k l/ ], [ qw/i j k l/ ], 'array-array' ]
101 );
102 for (@tests) {
103  my $res = [ $cat->($_->[0], $_->[1]) ];
104  is_deeply($res, $_->[2], 'cat ' . $_->[3]);
105 }
106 eval { $cat->({ foo => 1 }, [ 2 ] ) };
107 like($@, qr/^hlagh\s+at/, 'wrong reference type 1');
108 eval { $cat->(\1, sub { 2 } ) };
109 like($@, qr/^hlagh\s+at/, 'wrong reference type 2');
110
111 sub noproto;
112 my $noproto_exp = '{ main::noproto(@_) }';
113 my $noproto = wrap 'main::noproto', sub => 0;
114 is($noproto, $noproto_exp, 'no prototype');
115
116 sub myit { my $ar = shift; push @$ar, @_; };
117 if ($^V ge v5.10.0) {
118  set_prototype \&myit, '\@$_';
119  my $it = wrap 'main::myit', compile => 1;
120  my @a = qw/u v w/;
121  local $_ = 7;
122  $it->(\@a, 3, 4, 5);
123  is_deeply(\@a, [ qw/u v w/, 3, 4 ], '_ with arguments');
124  $it->(\@a, 6);
125  is_deeply(\@a, [ qw/u v w/, 3, 4, 6, 7 ], '_ without arguments');
126 }