6 use Test::More tests => 90;
11 use_ok('Moose::Role');
16 Mutually recursive roles.
26 sub bar { 'Role::Foo::bar' }
33 sub foo { 'Role::Bar::foo' }
41 with 'Role::Foo', 'Role::Bar';
42 } '... our mutually recursive roles combine okay';
48 with 'Role::Bar', 'Role::Foo';
49 } '... our mutually recursive roles combine okay (no matter what order)';
52 my $test1 = My::Test1->new;
53 isa_ok($test1, 'My::Test1');
55 ok($test1->does('Role::Foo'), '... $test1 does Role::Foo');
56 ok($test1->does('Role::Bar'), '... $test1 does Role::Bar');
58 can_ok($test1, 'foo');
59 can_ok($test1, 'bar');
61 is($test1->foo, 'Role::Bar::foo', '... $test1->foo worked');
62 is($test1->bar, 'Role::Foo::bar', '... $test1->bar worked');
64 my $test2 = My::Test2->new;
65 isa_ok($test2, 'My::Test2');
67 ok($test2->does('Role::Foo'), '... $test2 does Role::Foo');
68 ok($test2->does('Role::Bar'), '... $test2 does Role::Bar');
70 can_ok($test2, 'foo');
71 can_ok($test2, 'bar');
73 is($test2->foo, 'Role::Bar::foo', '... $test2->foo worked');
74 is($test2->bar, 'Role::Foo::bar', '... $test2->bar worked');
76 # check some meta-stuff
78 ok(Role::Foo->meta->has_method('bar'), '... it still has the bar method');
79 ok(Role::Foo->meta->requires_method('foo'), '... it still has the required foo method');
81 ok(Role::Bar->meta->has_method('foo'), '... it still has the foo method');
82 ok(Role::Bar->meta->requires_method('bar'), '... it still has the required bar method');
94 sub bling { 'Role::Bling::bling' }
96 package Role::Bling::Bling;
99 sub bling { 'Role::Bling::Bling::bling' }
107 with 'Role::Bling', 'Role::Bling::Bling';
108 } qr/requires the method \'bling\' to be implemented/, '... role methods conflicted and method was required';
115 with 'Role::Bling::Bling';
116 } '... role methods didnt conflict when manually combined';
122 with 'Role::Bling::Bling';
124 } '... role methods didnt conflict when manually combined (in opposite order)';
130 with 'Role::Bling::Bling', 'Role::Bling';
131 } '... role methods didnt conflict when manually resolved';
133 sub bling { 'My::Test6::bling' }
136 ok(!My::Test3->meta->has_method('bling'), '... we didnt get any methods in the conflict');
137 ok(My::Test4->meta->has_method('bling'), '... we did get the method when manually dealt with');
138 ok(My::Test5->meta->has_method('bling'), '... we did get the method when manually dealt with');
139 ok(My::Test6->meta->has_method('bling'), '... we did get the method when manually dealt with');
141 ok(!My::Test3->does('Role::Bling'), '... our class does() the correct roles');
142 ok(!My::Test3->does('Role::Bling::Bling'), '... our class does() the correct roles');
143 ok(My::Test4->does('Role::Bling'), '... our class does() the correct roles');
144 ok(My::Test4->does('Role::Bling::Bling'), '... our class does() the correct roles');
145 ok(My::Test5->does('Role::Bling'), '... our class does() the correct roles');
146 ok(My::Test5->does('Role::Bling::Bling'), '... our class does() the correct roles');
147 ok(My::Test6->does('Role::Bling'), '... our class does() the correct roles');
148 ok(My::Test6->does('Role::Bling::Bling'), '... our class does() the correct roles');
150 is(My::Test4->bling, 'Role::Bling::bling', '... and we got the first method that was added');
151 is(My::Test5->bling, 'Role::Bling::Bling::bling', '... and we got the first method that was added');
152 is(My::Test6->bling, 'My::Test6::bling', '... and we got the local method');
154 # check how this affects role compostion
157 package Role::Bling::Bling::Bling;
160 with 'Role::Bling::Bling';
162 sub bling { 'Role::Bling::Bling::Bling::bling' }
165 ok(Role::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling');
166 ok(Role::Bling::Bling->meta->does_role('Role::Bling::Bling'), '... our role correctly does() the other role');
167 ok(Role::Bling::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling::Bling');
168 is(Role::Bling::Bling::Bling->meta->get_method('bling')->(),
169 'Role::Bling::Bling::Bling::bling',
170 '... still got the bling method in Role::Bling::Bling::Bling');
174 Role attribute conflicts
182 has 'ghost' => (is => 'ro', default => 'Role::Boo::ghost');
184 package Role::Boo::Hoo;
187 has 'ghost' => (is => 'ro', default => 'Role::Boo::Hoo::ghost');
195 with 'Role::Boo', 'Role::Boo::Hoo';
196 } qr/Role \'Role::Boo::Hoo\' has encountered an attribute conflict/,
197 '... role attrs conflicted and method was required';
204 with 'Role::Boo::Hoo';
205 } '... role attrs didnt conflict when manually combined';
211 with 'Role::Boo::Hoo';
213 } '... role attrs didnt conflict when manually combined';
218 has 'ghost' => (is => 'ro', default => 'My::Test10::ghost');
221 with 'Role::Boo', 'Role::Boo::Hoo';
222 } qr/Role \'Role::Boo::Hoo\' has encountered an attribute conflict/,
223 '... role attrs conflicted and cannot be manually disambiguted';
227 ok(!My::Test7->meta->has_attribute('ghost'), '... we didnt get any attributes in the conflict');
228 ok(My::Test8->meta->has_attribute('ghost'), '... we did get an attributes when manually composed');
229 ok(My::Test9->meta->has_attribute('ghost'), '... we did get an attributes when manually composed');
230 ok(My::Test10->meta->has_attribute('ghost'), '... we did still have an attribute ghost (conflict does not mess with class)');
232 ok(!My::Test7->does('Role::Boo'), '... our class does() the correct roles');
233 ok(!My::Test7->does('Role::Boo::Hoo'), '... our class does() the correct roles');
234 ok(My::Test8->does('Role::Boo'), '... our class does() the correct roles');
235 ok(My::Test8->does('Role::Boo::Hoo'), '... our class does() the correct roles');
236 ok(My::Test9->does('Role::Boo'), '... our class does() the correct roles');
237 ok(My::Test9->does('Role::Boo::Hoo'), '... our class does() the correct roles');
238 ok(!My::Test10->does('Role::Boo'), '... our class does() the correct roles');
239 ok(!My::Test10->does('Role::Boo::Hoo'), '... our class does() the correct roles');
241 can_ok('My::Test8', 'ghost');
242 can_ok('My::Test9', 'ghost');
243 can_ok('My::Test10', 'ghost');
245 is(My::Test8->new->ghost, 'Role::Boo::ghost', '... got the expected default attr value');
246 is(My::Test9->new->ghost, 'Role::Boo::Hoo::ghost', '... got the expected default attr value');
247 is(My::Test10->new->ghost, 'My::Test10::ghost', '... got the expected default attr value');
251 Role override method conflicts
259 override 'twist' => sub {
260 super() . ' -> Role::Plot::twist';
266 override 'twist' => sub {
267 super() . ' -> Role::Truth::twist';
272 package My::Test::Base;
275 sub twist { 'My::Test::Base::twist' }
280 extends 'My::Test::Base';
284 } '... composed the role with override okay';
289 extends 'My::Test::Base';
293 } '... composed the role with override okay';
300 } '... cannot compose it because we have no superclass';
305 extends 'My::Test::Base';
308 with 'Role::Plot', 'Role::Truth';
309 } qr/Two \'override\' methods of the same name encountered/,
310 '... cannot compose it because we have no superclass';
313 ok(My::Test11->meta->has_method('twist'), '... the twist method has been added');
314 ok(My::Test12->meta->has_method('twist'), '... the twist method has been added');
315 ok(!My::Test13->meta->has_method('twist'), '... the twist method has not been added');
316 ok(!My::Test14->meta->has_method('twist'), '... the twist method has not been added');
318 ok(!My::Test11->does('Role::Plot'), '... our class does() the correct roles');
319 ok(My::Test11->does('Role::Truth'), '... our class does() the correct roles');
320 ok(!My::Test12->does('Role::Truth'), '... our class does() the correct roles');
321 ok(My::Test12->does('Role::Plot'), '... our class does() the correct roles');
322 ok(!My::Test13->does('Role::Plot'), '... our class does() the correct roles');
323 ok(!My::Test14->does('Role::Truth'), '... our class does() the correct roles');
324 ok(!My::Test14->does('Role::Plot'), '... our class does() the correct roles');
326 is(My::Test11->twist(), 'My::Test::Base::twist -> Role::Truth::twist', '... got the right method return');
327 is(My::Test12->twist(), 'My::Test::Base::twist -> Role::Plot::twist', '... got the right method return');
328 ok(!My::Test13->can('twist'), '... no twist method here at all');
329 is(My::Test14->twist(), 'My::Test::Base::twist', '... got the right method return (from superclass)');
332 package Role::Reality;
337 } qr/A local method of the same name as been found/,
338 '... could not compose roles here, it dies';
341 'Role::Reality::twist';
345 ok(Role::Reality->meta->has_method('twist'), '... the twist method has not been added');
346 ok(!Role::Reality->meta->does_role('Role::Plot'), '... our role does() the correct roles');
347 is(Role::Reality->meta->get_method('twist')->(),
348 'Role::Reality::twist',
349 '... the twist method returns the right value');