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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
#!perl -w
use strict;
use Test::More tests => 55;
use Test::Exception;
use constant HAS_SCOPE_GUARD => eval{ require Scope::Guard };
use Data::Util qw(:all);
sub foo{ @_ }
my @tags;
sub before{ push @tags, 'before'; }
sub around{ push @tags, 'around'; my $next = shift; $next->(@_) }
sub after { push @tags, 'after'; }
ok is_code_ref(modify_subroutine(\&foo)), 'modify_subroutine()';
my $w = modify_subroutine \&foo,
before => [\&before],
around => [\&around],
after => [\&after];
lives_ok{
ok subroutine_modifier($w);
ok !subroutine_modifier(\&foo);
};
is_deeply [subroutine_modifier $w, 'before'], [\&before], 'getter:before';
is_deeply [subroutine_modifier $w, 'around'], [\&around], 'getter:around';
is_deeply [subroutine_modifier $w, 'after'], [\&after], 'getter:after';
is_deeply [scalar $w->(1 .. 10)], [10], 'call with scalar context';
is_deeply \@tags, [qw(before around after)];
@tags = ();
is_deeply [$w->(1 .. 10)], [1 .. 10], 'call with list context';
is_deeply \@tags, [qw(before around after)];
$w = modify_subroutine \&foo;
subroutine_modifier $w, before => \&before;
@tags = ();
is_deeply [$w->(1 .. 10)], [1 .. 10];
is_deeply \@tags, [qw(before)], 'add :before modifiers';
$w = modify_subroutine \&foo;
subroutine_modifier $w, around => \&around;
@tags = ();
is_deeply [$w->(1 .. 10)], [1 .. 10];
is_deeply \@tags, [qw(around)], 'add :around modifiers';
$w = modify_subroutine \&foo;
subroutine_modifier $w, after => \&after;
@tags = ();
is_deeply [$w->(1 .. 10)], [1 .. 10];
is_deeply \@tags, [qw(after)], 'add :after modifiers';
$w = modify_subroutine \&foo, before => [(\&before) x 10], around => [(\&around) x 10], after => [(\&after) x 10];
@tags = ();
is_deeply [$w->(42)], [42];
is_deeply \@tags, [('before') x 10, ('around') x 10, ('after') x 10], 'with multiple modifiers';
subroutine_modifier $w, before => \&before, \&before;
subroutine_modifier $w, around => \&around, \&around;
subroutine_modifier $w, after => \&after, \&after;
@tags = ();
is_deeply [$w->(1 .. 10)], [1 .. 10];
is_deeply \@tags, [('before') x 12, ('around') x 12, ('after') x 12], 'add modifiers';
# calling order and copying
sub f1{
push @tags, 'f1';
my $next = shift;
$next->(@_);
}
sub f2{
push @tags, 'f2';
my $next = shift;
$next->(@_);
}
sub f3{
push @tags, 'f3';
my $next = shift;
$next->(@_);
}
sub before2{ push @tags, 'before2' }
sub before3{ push @tags, 'before3' }
sub after2 { push @tags, 'after2' }
sub after3 { push @tags, 'after3' }
# the order of around modifier
$w = modify_subroutine \&foo, around => [ \&f1, \&f2, \&f3 ];
@tags = ();
$w->();
is_deeply \@tags, [qw(f1 f2 f3)], ":around order (modify_subroutine)(@tags)";
$w = modify_subroutine \&foo;
subroutine_modifier $w, around => \&f3, \&f2, \&f1;
@tags = ();
$w->();
is_deeply \@tags, [qw(f3 f2 f1)], ":around order (subroutine_modifier) (@tags)";
$w = modify_subroutine \&foo;
subroutine_modifier $w, around => $_ for \&f1, \&f2, \&f3;
@tags = ();
$w->();
is_deeply \@tags, [qw(f3 f2 f1)], ":around order (subroutine_modifier) (@tags)";
# the order of before modifier
$w = modify_subroutine \&foo, before => [\&before, \&before2, \&before3];
@tags = ();
$w->();
is_deeply \@tags, [qw(before before2 before3)], ':before order (modify_subroutine)';
$w = modify_subroutine \&foo;
subroutine_modifier $w, before => \&before, \&before2, \&before3;
@tags = ();
$w->();
is_deeply \@tags, [qw(before3 before2 before)], ':before order (subroutine_modifier)';
$w = modify_subroutine \&foo;
subroutine_modifier $w, before => $_ for \&before, \&before2, \&before3;
@tags = ();
$w->();
is_deeply \@tags, [qw(before3 before2 before)], ":before order (subroutine_modifier) (@tags)";
# the order of after modifier
$w = modify_subroutine \&foo, after => [\&after, \&after2, \&after3];
@tags = ();
$w->();
is_deeply \@tags, [qw(after after2 after3)], ':after order (modify_subroutine)';
$w = modify_subroutine \&foo;
subroutine_modifier $w, after => \&after, \&after2, \&after3;
@tags = ();
$w->();
is_deeply \@tags, [qw(after after2 after3)], ':after order (subroutine_modifier)';
$w = modify_subroutine \&foo;
subroutine_modifier $w, after => $_ for \&after, \&after2, \&after3;
@tags = ();
$w->();
is_deeply \@tags, [qw(after after2 after3)], ":after order (subroutine_modifier) (@tags)";
# Moose compatibility
$w = modify_subroutine \&foo;
subroutine_modifier $w, before => $_ for \&before1, \&before2, \&before3;
subroutine_modifier $w, around => $_ for \&around1, \&around2, \&around3;
subroutine_modifier $w, after => $_ for \&after1, \&after2, \&after3;
is_deeply [subroutine_modifier $w, 'before'], [\&before3, \&before2, \&before1], 'get before modifiers';
is_deeply [subroutine_modifier $w, 'around'], [\&around3, \&around2, \&around1], 'get around modifiers';
is_deeply [subroutine_modifier $w, 'after' ], [\&after1, \&after2, \&after3 ], 'get after modifiers';
# Copying possilbility
$w = modify_subroutine \&foo,
before => [subroutine_modifier $w, 'before'],
around => [subroutine_modifier $w, 'around'],
after => [subroutine_modifier $w, 'after' ];
is_deeply [subroutine_modifier $w, 'before'], [\&before3, \&before2, \&before1], 'copy before modifiers';
is_deeply [subroutine_modifier $w, 'around'], [\&around3, \&around2, \&around1], 'copy around modifiers';
is_deeply [subroutine_modifier $w, 'after' ], [\&after1, \&after2, \&after3 ], 'copy after modifiers';
# Contexts
sub get_context{
push @tags, wantarray ? 'list'
: defined(wantarray) ? 'scalar'
: 'void';
}
$w = modify_subroutine(\&foo, around => [\&get_context]);
@tags = ();
() = $w->();
is_deeply \@tags, [qw(list)], 'list context in around';
@tags = ();
scalar $w->();
is_deeply \@tags, [qw(scalar)], 'scalar context in around';
@tags = ();
$w->();
is_deeply \@tags, [qw(void)], 'void context in around';
# Modifier's args
sub mutator{
$_[0]++;
}
$w = modify_subroutine(\&foo, before => [\&mutator]);
my $n = 42;
is_deeply [ $w->($n) ], [43]; # $n++
is $n, 43;
# GC
SKIP:{
skip 'requires Scope::Gurard for testing GC', 3 unless HAS_SCOPE_GUARD;
@tags = ();
for(1 .. 10){
my $gbefore = Scope::Guard->new(sub{ push @tags, 'before' });
my $garound = Scope::Guard->new(sub{ push @tags, 'around' });
my $gafter = Scope::Guard->new(sub{ push @tags, 'after' });
my $w = modify_subroutine \&foo,
before => [sub{ $gbefore }], # encloses guard objects
around => [sub{ $gafter }],
after => [sub{ $gafter }];
}
is_deeply [sort @tags], [sort((qw(after around before)) x 10)], 'closed values are released';
@tags = ();
my $i = 0;
for(1 .. 10){
my $gbefore = Scope::Guard->new(sub{ push @tags, 'before' });
my $garound = Scope::Guard->new(sub{ push @tags, 'around' });
my $gafter = Scope::Guard->new(sub{ push @tags, 'after' });
my $w = modify_subroutine \&foo,
before => [sub{ $gbefore }], # encloses guard objects
around => [sub{ $gafter }],
after => [sub{ $gafter }];
$w->(Scope::Guard->new( sub{ $i++ } ));
}
is_deeply [sort @tags], [sort((qw(after around before)) x 10)], '... called and released';
is $i, 10, '... and the argument is also released';
}
# FATAL
dies_ok{
modify_subroutine(undef);
};
dies_ok{
modify_subroutine(\&foo, []);
};
dies_ok{
modify_subroutine(\&foo, before => [1]);
};
dies_ok{
modify_subroutine(\&foo, around => [1]);
};
dies_ok{
modify_subroutine(\&foo, after => [1]);
};
$w = modify_subroutine(\&foo);
throws_ok{
subroutine_modifier($w, 'foo');
} qr/Validation failed:.* a modifier property/;
throws_ok{
subroutine_modifier($w, undef);
} qr/Validation failed:.* a modifier property/;
throws_ok{
subroutine_modifier($w, before => 'foo');
} qr/Validation failed:.* a CODE reference/;
throws_ok{
subroutine_modifier($w, foo => sub{});
} qr/Validation failed:.* a modifier property/;
throws_ok{
subroutine_modifier(\&foo, 'before');
} qr/Validation failed:.* a modified subroutine/;
throws_ok{
subroutine_modifier(\&foo, before => sub{});
} qr/Validation failed:.* a modified subroutine/;
|