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
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
{
package Foo;
use Moose;
sub foo { 'Foo::foo(' . (inner() || '') . ')' }
sub bar { 'Foo::bar(' . (inner() || '') . ')' }
sub baz { 'Foo::baz(' . (inner() || '') . ')' }
package Bar;
use Moose;
extends 'Foo';
augment foo => sub { 'Bar::foo(' . (inner() || '') . ')' };
augment bar => sub { 'Bar::bar' };
no Moose; # ensure inner() still works after unimport
package Baz;
use Moose;
extends 'Bar';
augment foo => sub { 'Baz::foo' };
augment baz => sub { 'Baz::baz' };
# this will actually never run,
# because Bar::bar does not call inner()
augment bar => sub { 'Baz::bar' };
}
my $baz = Baz->new();
isa_ok($baz, 'Baz');
isa_ok($baz, 'Bar');
isa_ok($baz, 'Foo');
is($baz->foo(), 'Foo::foo(Bar::foo(Baz::foo))', '... got the right value from &foo');
is($baz->bar(), 'Foo::bar(Bar::bar)', '... got the right value from &bar');
is($baz->baz(), 'Foo::baz(Baz::baz)', '... got the right value from &baz');
my $bar = Bar->new();
isa_ok($bar, 'Bar');
isa_ok($bar, 'Foo');
is($bar->foo(), 'Foo::foo(Bar::foo())', '... got the right value from &foo');
is($bar->bar(), 'Foo::bar(Bar::bar)', '... got the right value from &bar');
is($bar->baz(), 'Foo::baz()', '... got the right value from &baz');
my $foo = Foo->new();
isa_ok($foo, 'Foo');
is($foo->foo(), 'Foo::foo()', '... got the right value from &foo');
is($foo->bar(), 'Foo::bar()', '... got the right value from &bar');
is($foo->baz(), 'Foo::baz()', '... got the right value from &baz');
# test saved state when crossing objects
{
package X;
use Moose;
has name => (is => 'rw');
sub run {
"$_[0]->{name}.X", inner()
}
package Y;
use Moose;
extends 'X';
augment 'run' => sub {
"$_[0]->{name}.Y", ($_[1] ? $_[1]->() : ()), inner();
};
package Z;
use Moose;
extends 'Y';
augment 'run' => sub {
"$_[0]->{name}.Z"
}
}
is('a.X a.Y b.X b.Y b.Z a.Z',
do {
my $a = Z->new(name => 'a');
my $b = Z->new(name => 'b');
join(' ', $a->run(sub { $b->run }))
},
'State is saved when cross-calling augmented methods on different objects');
# some error cases
{
package Bling;
use Moose;
sub bling { 'Bling::bling' }
package Bling::Bling;
use Moose;
extends 'Bling';
sub bling { 'Bling::bling' }
::isnt( ::exception {
augment 'bling' => sub {};
}, undef, '... cannot augment a method which has a local equivalent' );
}
done_testing;
|