forked from moose/Class-Method-Modifiers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path141-prototype.t
More file actions
96 lines (83 loc) · 2.22 KB
/
141-prototype.t
File metadata and controls
96 lines (83 loc) · 2.22 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
use strict;
use warnings;
use Test::More 0.88;
use Test::Warnings ($ENV{AUTHOR_TESTING} ? () : ':no_end_test'), 'warnings';
use Test::Fatal;
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';
}
{
sub bog ($) { scalar @_ }
my $min_lineno = __LINE__;
my $around;
my ($warn) = warnings {
around bog => sub ($$) {
my $orig = shift;
$around = @_;
$orig->(@_);
};
};
my $max_lineno = __LINE__;
is eval q{ bog( @{[5, 6]}, @{[10, 11]} ) }, 2,
'around wrapped lvalue sub takes modifier prototype';
is $around, 2,
'around modifier applied';
like $warn, qr/Prototype mismatch/,
'changing prototype throws warning';
like $warn, qr/\Q${\__FILE__}\E/,
'warning is reported from correct location';
my ($lineno) = ($warn =~ qr/\Q${\__FILE__}\E line ([0-9]+)/);
is !!($min_lineno < $lineno && $lineno < $max_lineno), 1,
'line no is within range';
}
{
sub brog ($) { scalar @_ }
no warnings;
my @warn = warnings {
around brog => sub ($$) {
my $orig = shift;
$orig->(@_);
};
};
is 0+@warn, 0,
'warnings controllable via warning pragma';
}
{
require List::Util;
List::Util->import('sum');
my $around;
my @warn = warnings {
around sum => sub :prototype(@) {
my $orig = shift;
$around = @_;
return 2 * $orig->(@_);
};
};
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 :