-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path141-prototype.t
More file actions
108 lines (88 loc) · 2.2 KB
/
141-prototype.t
File metadata and controls
108 lines (88 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use strict;
use warnings;
use Test::More 0.88;
use Class::Method::Modifiers;
{
sub foo ($) { scalar @_ }
my $after;
after foo => sub { $after = @_ };
is eval q{ foo( @{[10, 20]} ) }, 1,
'after wrapped sub maintains prototype';
is $after, 1,
'after modifier applied';
}
{
my $bar;
my $guff;
sub bar ($) :lvalue { $guff = @_; $bar }
my $after;
after bar => sub { $after = @_ };
eval q{ bar( @{[10, 20]} ) = 5 };
is $guff, 1,
'after wrapped lvalue sub maintains prototype';
is $bar, 5,
'after wrapped lvalue sub maintains lvalue';
is $after, 1,
'after modifier applied';
}
{
my $around;
sub bog ($) { scalar @_ }
my $wrap_bog = sub ($$) {
my $orig = shift;
$around = @_;
$orig->(@_);
};
my $warn_line;
my @warn;
{
local $SIG{__WARN__} = sub { push @warn, @_ };
$warn_line = __LINE__ + 1;
around bog => $wrap_bog;
}
is eval q{ bog( @{[5, 6]}, @{[10, 11]} ) }, 2,
'around wrapped lvalue sub takes modifier prototype';
is $around, 2,
'around modifier applied';
my $warn = join '', @warn;
like $warn, qr/Prototype mismatch/,
'changing prototype throws warning';
like $warn, qr/\Q${\__FILE__}\E line $warn_line\b/,
'warning is reported from correct location';
}
{
sub brog ($) { scalar @_ }
my $wrap_brog = sub ($$) {
my $orig = shift;
$orig->(@_);
};
my @warn;
{
local $SIG{__WARN__} = sub { push @warn, @_ };
no warnings;
around brog => $wrap_brog;
}
is 0+@warn, 0,
'warnings controllable via warning pragma';
}
{
my $around;
require List::Util;
List::Util->import('sum');
my $wrap_sum = sub :prototype(@) {
my $orig = shift;
$around = @_;
return 2 * $orig->(@_);
};
my @warn;
{
local $SIG{__WARN__} = sub { push @warn, @_ };
around sum => $wrap_sum;
}
is eval q{sum(11, 1, 4, 5, 9)}, 60,
'result from around xs function';
is $around, 5, 'prototype @ wrapped';
is 0+@warn, 0, 'no warnings';
}
done_testing;
# vim: set ts=8 sts=4 sw=4 tw=115 et :