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
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
{
# NOTE:
# this tests that repeated role
# composition will not cause
# a conflict between two methods
# which are actually the same anyway
{
package RootA;
use Moose::Role;
sub foo { "RootA::foo" }
package SubAA;
use Moose::Role;
with "RootA";
sub bar { "SubAA::bar" }
package SubAB;
use Moose;
::is( ::exception {
with "SubAA", "RootA";
}, undef, '... role was composed as expected' );
}
ok( SubAB->does("SubAA"), "does SubAA");
ok( SubAB->does("RootA"), "does RootA");
isa_ok( my $i = SubAB->new, "SubAB" );
can_ok( $i, "bar" );
is( $i->bar, "SubAA::bar", "... got thr right bar rv" );
can_ok( $i, "foo" );
my $foo_rv;
is( exception {
$foo_rv = $i->foo;
}, undef, '... called foo successfully' );
is($foo_rv, "RootA::foo", "... got the right foo rv");
}
{
# NOTE:
# this edge cases shows the application of
# an after modifier over a method which
# was added during role composotion.
# The way this will work is as follows:
# role SubBA will consume RootB and
# get a local copy of RootB::foo, it
# will also store a deferred after modifier
# to be applied to whatever class SubBA is
# composed into.
# When class SubBB comsumed role SubBA, the
# RootB::foo method is added to SubBB, then
# the deferred after modifier from SubBA is
# applied to it.
# It is important to note that the application
# of the after modifier does not happen until
# role SubBA is composed into SubAA.
{
package RootB;
use Moose::Role;
sub foo { "RootB::foo" }
package SubBA;
use Moose::Role;
with "RootB";
has counter => (
isa => "Num",
is => "rw",
default => 0,
);
after foo => sub {
$_[0]->counter( $_[0]->counter + 1 );
};
package SubBB;
use Moose;
::is( ::exception {
with "SubBA";
}, undef, '... composed the role successfully' );
}
ok( SubBB->does("SubBA"), "BB does SubBA" );
ok( SubBB->does("RootB"), "BB does RootB" );
isa_ok( my $i = SubBB->new, "SubBB" );
can_ok( $i, "foo" );
my $foo_rv;
is( exception {
$foo_rv = $i->foo
}, undef, '... called foo successfully' );
is( $foo_rv, "RootB::foo", "foo rv" );
is( $i->counter, 1, "after hook called" );
is( exception { $i->foo }, undef, '... called foo successfully (again)' );
is( $i->counter, 2, "after hook called (again)" );
ok(SubBA->meta->has_method('foo'), '... this has the foo method');
#my $subba_foo_rv;
#lives_ok {
# $subba_foo_rv = SubBA::foo();
#} '... called the sub as a function correctly';
#is($subba_foo_rv, 'RootB::foo', '... the SubBA->foo is still the RootB version');
}
{
# NOTE:
# this checks that an override method
# does not try to trample over a locally
# composed in method. In this case the
# RootC::foo, which is composed into
# SubCA cannot be trampled with an
# override of 'foo'
{
package RootC;
use Moose::Role;
sub foo { "RootC::foo" }
package SubCA;
use Moose::Role;
with "RootC";
::isnt( ::exception {
override foo => sub { "overridden" };
}, undef, '... cannot compose an override over a local method' );
}
}
# NOTE:
# need to talk to Yuval about the motivation behind
# this test, I am not sure we are testing anything
# useful here (although more tests cant hurt)
{
use List::Util qw/shuffle/;
{
package Abstract;
use Moose::Role;
requires "method";
requires "other";
sub another { "abstract" }
package ConcreteA;
use Moose::Role;
with "Abstract";
sub other { "concrete a" }
package ConcreteB;
use Moose::Role;
with "Abstract";
sub method { "concrete b" }
package ConcreteC;
use Moose::Role;
with "ConcreteA";
# NOTE:
# this was originally override, but
# that wont work (see above set of tests)
# so I switched it to around.
# However, this may not be testing the
# same thing that was originally intended
around other => sub {
return ( (shift)->() . " + c" );
};
package SimpleClassWithSome;
use Moose;
eval { with ::shuffle qw/ConcreteA ConcreteB/ };
::ok( !$@, "simple composition without abstract" ) || ::diag $@;
package SimpleClassWithAll;
use Moose;
eval { with ::shuffle qw/ConcreteA ConcreteB Abstract/ };
::ok( !$@, "simple composition with abstract" ) || ::diag $@;
}
foreach my $class (qw/SimpleClassWithSome SimpleClassWithAll/) {
foreach my $role (qw/Abstract ConcreteA ConcreteB/) {
ok( $class->does($role), "$class does $role");
}
foreach my $method (qw/method other another/) {
can_ok( $class, $method );
}
is( eval { $class->another }, "abstract", "provided by abstract" );
is( eval { $class->other }, "concrete a", "provided by concrete a" );
is( eval { $class->method }, "concrete b", "provided by concrete b" );
}
{
package ClassWithSome;
use Moose;
eval { with ::shuffle qw/ConcreteC ConcreteB/ };
::ok( !$@, "composition without abstract" ) || ::diag $@;
package ClassWithAll;
use Moose;
eval { with ::shuffle qw/ConcreteC Abstract ConcreteB/ };
::ok( !$@, "composition with abstract" ) || ::diag $@;
package ClassWithEverything;
use Moose;
eval { with ::shuffle qw/ConcreteC Abstract ConcreteA ConcreteB/ }; # this should clash
::ok( !$@, "can compose ConcreteA and ConcreteC together" );
}
foreach my $class (qw/ClassWithSome ClassWithAll ClassWithEverything/) {
foreach my $role (qw/Abstract ConcreteA ConcreteB ConcreteC/) {
ok( $class->does($role), "$class does $role");
}
foreach my $method (qw/method other another/) {
can_ok( $class, $method );
}
is( eval { $class->another }, "abstract", "provided by abstract" );
is( eval { $class->other }, "concrete a + c", "provided by concrete c + a" );
is( eval { $class->method }, "concrete b", "provided by concrete b" );
}
}
done_testing;
|