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
|
#!perl -w
use strict;
use warnings;
use Test::More tests => 25;
my @seen;
my @expected = ("before 4",
"before 3",
"around 4 before",
"around 3 before",
"before 2",
"before 1",
"around 2 before",
"around 1 before",
"orig",
"around 1 after",
"around 2 after",
"after 1",
"after 2",
"around 3 after",
"around 4 after",
"after 3",
"after 4",
);
my $child = Grandchild->new; $child->orig;
is_deeply(\@seen, \@expected, "multiple afters called in the right order");
BEGIN {
package Parent;
use Mouse;
sub orig {
push @seen, "orig";
}
}
BEGIN {
package Child;
use Mouse;
extends 'Parent';
before orig => sub {
push @seen, "before 1";
};
before orig => sub {
push @seen, "before 2";
};
around orig => sub {
my $orig = shift;
push @seen, "around 1 before";
$orig->();
push @seen, "around 1 after";
};
around orig => sub {
my $orig = shift;
push @seen, "around 2 before";
$orig->();
push @seen, "around 2 after";
};
after orig => sub {
push @seen, "after 1";
};
after orig => sub {
push @seen, "after 2";
};
}
BEGIN {
package Grandchild;
use Mouse;
extends 'Child';
before orig => sub {
push @seen, "before 3";
};
before orig => sub {
push @seen, "before 4";
};
around orig => sub {
my $orig = shift;
push @seen, "around 3 before";
$orig->();
push @seen, "around 3 after";
};
around orig => sub {
my $orig = shift;
push @seen, "around 4 before";
$orig->();
push @seen, "around 4 after";
};
after orig => sub {
push @seen, "after 3";
};
after orig => sub {
push @seen, "after 4";
};
}
# from Class::Method::Modifers' t/020-multiple-inheritance.t
# inheritance tree looks like:
#
# SuperL SuperR
# \ /
# MiddleL MiddleR
# \ /
# -Child-
# the Child and MiddleR modules use modifiers
# Child will modify a method in SuperL (sl_c)
# Child will modify a method in SuperR (sr_c)
# Child will modify a method in SuperR already modified by MiddleR (sr_m_c)
# SuperL and MiddleR will both have a method of the same name, doing different
# things (called 'conflict' and 'cnf_mod')
# every method and modifier will just return <Class:Method:STUFF>
BEGIN
{
{
package SuperL;
use Mouse;
sub superl { "<SuperL:superl>" }
sub conflict { "<SuperL:conflict>" }
sub cnf_mod { "<SuperL:cnf_mod>" }
sub sl_c { "<SuperL:sl_c>" }
}
{
package SuperR;
use Mouse;
sub superr { "<SuperR:superr>" }
sub sr_c { "<SuperR:sr_c>" }
sub sr_m_c { "<SuperR:sr_m_c>" }
}
{
package MiddleL;
use Mouse;
extends 'SuperL';
sub middlel { "<MiddleL:middlel>" }
}
{
package MiddleR;
use Mouse;
extends 'SuperR';
sub middler { "<MiddleR:middler>" }
sub conflict { "<MiddleR:conflict>" }
sub cnf_mod { "<MiddleR:cnf_mod>" }
around sr_m_c => sub {
my $orig = shift;
return "<MiddleR:sr_m_c:".$orig->(@_).">"
};
}
{
package Child;
use Mouse;
extends qw(MiddleL MiddleR);
sub child { "<Child:child>" }
around cnf_mod => sub { "<Child:cnf_mod:".shift->(@_).">" };
around sl_c => sub { "<Child:sl_c:".shift->(@_).">" };
around sr_c => sub { "<Child:sr_c:".shift->(@_).">" };
around sr_m_c => sub {
my $orig = shift;
return "<Child:sr_m_c:".$orig->(@_).">"
};
}
}
my $SuperL = SuperL->new();
my $SuperR = SuperR->new();
my $MiddleL = MiddleL->new();
my $MiddleR = MiddleR->new();
my $Child = Child->new();
is($SuperL->superl, "<SuperL:superl>", "SuperL loaded correctly");
is($SuperR->superr, "<SuperR:superr>", "SuperR loaded correctly");
is($MiddleL->middlel, "<MiddleL:middlel>", "MiddleL loaded correctly");
is($MiddleR->middler, "<MiddleR:middler>", "MiddleR loaded correctly");
is($Child->child, "<Child:child>", "Child loaded correctly");
is($SuperL->sl_c, "<SuperL:sl_c>", "SuperL->sl_c on SuperL");
is($Child->sl_c, "<Child:sl_c:<SuperL:sl_c>>", "SuperL->sl_c wrapped by Child's around");
is($SuperR->sr_c, "<SuperR:sr_c>", "SuperR->sr_c on SuperR");
is($Child->sr_c, "<Child:sr_c:<SuperR:sr_c>>", "SuperR->sr_c wrapped by Child's around");
is($SuperR->sr_m_c, "<SuperR:sr_m_c>", "SuperR->sr_m_c on SuperR");
is($MiddleR->sr_m_c, "<MiddleR:sr_m_c:<SuperR:sr_m_c>>", "SuperR->sr_m_c wrapped by MiddleR's around");
is($Child->sr_m_c, "<Child:sr_m_c:<MiddleR:sr_m_c:<SuperR:sr_m_c>>>", "MiddleR->sr_m_c's wrapping wrapped by Child's around");
is($SuperL->conflict, "<SuperL:conflict>", "SuperL->conflict on SuperL");
is($MiddleR->conflict, "<MiddleR:conflict>", "MiddleR->conflict on MiddleR");
is($Child->conflict, "<SuperL:conflict>", "SuperL->conflict on Child");
is($SuperL->cnf_mod, "<SuperL:cnf_mod>", "SuperL->cnf_mod on SuperL");
is($MiddleR->cnf_mod, "<MiddleR:cnf_mod>", "MiddleR->cnf_mod on MiddleR");
is($Child->cnf_mod, "<Child:cnf_mod:<SuperL:cnf_mod>>", "SuperL->cnf_mod wrapped by Child's around");
# taken from Class::Method::Modifiers' t/051-undef-list-ctxt.t
my($orig_called, $after_called);
BEGIN
{
package ParentX;
use Mouse;
sub orig
{
my $self = shift;
$orig_called = 1;
return;
}
package ChildX;
use Mouse;
extends 'ParentX';
after 'orig' => sub
{
$after_called = 1;
};
}
{
($after_called, $orig_called) = (0, 0);
my $child = ChildX->new();
my @results = $child->orig();
ok($orig_called, "original method called");
ok($after_called, "after-modifier called");
is(@results, 0, "list context with after doesn't screw up 'return'");
($after_called, $orig_called) = (0, 0);
my $result = $child->orig();
ok($orig_called, "original method called");
ok($after_called, "after-modifier called");
is($result, undef, "scalar context with after doesn't screw up 'return'");
}
|