6 use Test::More tests => 90;
11 use_ok('Moose::Role');
16 Mutually recursive roles.
28 sub bar { 'Role::Foo::bar' }
37 sub foo { 'Role::Bar::foo' }
47 with 'Role::Foo', 'Role::Bar';
48 } '... our mutually recursive roles combine okay';
56 with 'Role::Bar', 'Role::Foo';
57 } '... our mutually recursive roles combine okay (no matter what order)';
60 my $test1 = My::Test1->new;
61 isa_ok($test1, 'My::Test1');
63 ok($test1->does('Role::Foo'), '... $test1 does Role::Foo');
64 ok($test1->does('Role::Bar'), '... $test1 does Role::Bar');
66 can_ok($test1, 'foo');
67 can_ok($test1, 'bar');
69 is($test1->foo, 'Role::Bar::foo', '... $test1->foo worked');
70 is($test1->bar, 'Role::Foo::bar', '... $test1->bar worked');
72 my $test2 = My::Test2->new;
73 isa_ok($test2, 'My::Test2');
75 ok($test2->does('Role::Foo'), '... $test2 does Role::Foo');
76 ok($test2->does('Role::Bar'), '... $test2 does Role::Bar');
78 can_ok($test2, 'foo');
79 can_ok($test2, 'bar');
81 is($test2->foo, 'Role::Bar::foo', '... $test2->foo worked');
82 is($test2->bar, 'Role::Foo::bar', '... $test2->bar worked');
84 # check some meta-stuff
86 ok(Role::Foo->meta->has_method('bar'), '... it still has the bar method');
87 ok(Role::Foo->meta->requires_method('foo'), '... it still has the required foo method');
89 ok(Role::Bar->meta->has_method('foo'), '... it still has the foo method');
90 ok(Role::Bar->meta->requires_method('bar'), '... it still has the required bar method');
104 sub bling { 'Role::Bling::bling' }
106 package Role::Bling::Bling;
111 sub bling { 'Role::Bling::Bling::bling' }
121 with 'Role::Bling', 'Role::Bling::Bling';
122 } qr/requires the method \'bling\' to be implemented/, '... role methods conflicted and method was required';
131 with 'Role::Bling::Bling';
132 } '... role methods didnt conflict when manually combined';
140 with 'Role::Bling::Bling';
142 } '... role methods didnt conflict when manually combined (in opposite order)';
150 with 'Role::Bling::Bling', 'Role::Bling';
151 } '... role methods didnt conflict when manually resolved';
153 sub bling { 'My::Test6::bling' }
156 ok(!My::Test3->meta->has_method('bling'), '... we didnt get any methods in the conflict');
157 ok(My::Test4->meta->has_method('bling'), '... we did get the method when manually dealt with');
158 ok(My::Test5->meta->has_method('bling'), '... we did get the method when manually dealt with');
159 ok(My::Test6->meta->has_method('bling'), '... we did get the method when manually dealt with');
161 ok(!My::Test3->does('Role::Bling'), '... our class does() the correct roles');
162 ok(!My::Test3->does('Role::Bling::Bling'), '... our class does() the correct roles');
163 ok(My::Test4->does('Role::Bling'), '... our class does() the correct roles');
164 ok(My::Test4->does('Role::Bling::Bling'), '... our class does() the correct roles');
165 ok(My::Test5->does('Role::Bling'), '... our class does() the correct roles');
166 ok(My::Test5->does('Role::Bling::Bling'), '... our class does() the correct roles');
167 ok(My::Test6->does('Role::Bling'), '... our class does() the correct roles');
168 ok(My::Test6->does('Role::Bling::Bling'), '... our class does() the correct roles');
170 is(My::Test4->bling, 'Role::Bling::bling', '... and we got the first method that was added');
171 is(My::Test5->bling, 'Role::Bling::Bling::bling', '... and we got the first method that was added');
172 is(My::Test6->bling, 'My::Test6::bling', '... and we got the local method');
174 # check how this affects role compostion
177 package Role::Bling::Bling::Bling;
182 with 'Role::Bling::Bling';
184 sub bling { 'Role::Bling::Bling::Bling::bling' }
187 ok(Role::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling');
188 ok(Role::Bling::Bling->meta->does_role('Role::Bling::Bling'), '... our role correctly does() the other role');
189 ok(Role::Bling::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling::Bling');
190 is(Role::Bling::Bling::Bling->meta->get_method('bling')->(),
191 'Role::Bling::Bling::Bling::bling',
192 '... still got the bling method in Role::Bling::Bling::Bling');
196 Role attribute conflicts
206 has 'ghost' => (is => 'ro', default => 'Role::Boo::ghost');
208 package Role::Boo::Hoo;
213 has 'ghost' => (is => 'ro', default => 'Role::Boo::Hoo::ghost');
223 with 'Role::Boo', 'Role::Boo::Hoo';
224 } qr/Role \'Role::Boo::Hoo\' has encountered an attribute conflict/,
225 '... role attrs conflicted and method was required';
234 with 'Role::Boo::Hoo';
235 } '... role attrs didnt conflict when manually combined';
243 with 'Role::Boo::Hoo';
245 } '... role attrs didnt conflict when manually combined';
252 has 'ghost' => (is => 'ro', default => 'My::Test10::ghost');
255 with 'Role::Boo', 'Role::Boo::Hoo';
256 } qr/Role \'Role::Boo::Hoo\' has encountered an attribute conflict/,
257 '... role attrs conflicted and cannot be manually disambiguted';
261 ok(!My::Test7->meta->has_attribute('ghost'), '... we didnt get any attributes in the conflict');
262 ok(My::Test8->meta->has_attribute('ghost'), '... we did get an attributes when manually composed');
263 ok(My::Test9->meta->has_attribute('ghost'), '... we did get an attributes when manually composed');
264 ok(My::Test10->meta->has_attribute('ghost'), '... we did still have an attribute ghost (conflict does not mess with class)');
266 ok(!My::Test7->does('Role::Boo'), '... our class does() the correct roles');
267 ok(!My::Test7->does('Role::Boo::Hoo'), '... our class does() the correct roles');
268 ok(My::Test8->does('Role::Boo'), '... our class does() the correct roles');
269 ok(My::Test8->does('Role::Boo::Hoo'), '... our class does() the correct roles');
270 ok(My::Test9->does('Role::Boo'), '... our class does() the correct roles');
271 ok(My::Test9->does('Role::Boo::Hoo'), '... our class does() the correct roles');
272 ok(!My::Test10->does('Role::Boo'), '... our class does() the correct roles');
273 ok(!My::Test10->does('Role::Boo::Hoo'), '... our class does() the correct roles');
275 can_ok('My::Test8', 'ghost');
276 can_ok('My::Test9', 'ghost');
277 can_ok('My::Test10', 'ghost');
279 is(My::Test8->new->ghost, 'Role::Boo::ghost', '... got the expected default attr value');
280 is(My::Test9->new->ghost, 'Role::Boo::Hoo::ghost', '... got the expected default attr value');
281 is(My::Test10->new->ghost, 'My::Test10::ghost', '... got the expected default attr value');
285 Role override method conflicts
295 override 'twist' => sub {
296 super() . ' -> Role::Plot::twist';
304 override 'twist' => sub {
305 super() . ' -> Role::Truth::twist';
310 package My::Test::Base;
315 sub twist { 'My::Test::Base::twist' }
322 extends 'My::Test::Base';
326 } '... composed the role with override okay';
333 extends 'My::Test::Base';
337 } '... composed the role with override okay';
346 } '... cannot compose it because we have no superclass';
353 extends 'My::Test::Base';
356 with 'Role::Plot', 'Role::Truth';
357 } qr/Two \'override\' methods of the same name encountered/,
358 '... cannot compose it because we have no superclass';
361 ok(My::Test11->meta->has_method('twist'), '... the twist method has been added');
362 ok(My::Test12->meta->has_method('twist'), '... the twist method has been added');
363 ok(!My::Test13->meta->has_method('twist'), '... the twist method has not been added');
364 ok(!My::Test14->meta->has_method('twist'), '... the twist method has not been added');
366 ok(!My::Test11->does('Role::Plot'), '... our class does() the correct roles');
367 ok(My::Test11->does('Role::Truth'), '... our class does() the correct roles');
368 ok(!My::Test12->does('Role::Truth'), '... our class does() the correct roles');
369 ok(My::Test12->does('Role::Plot'), '... our class does() the correct roles');
370 ok(!My::Test13->does('Role::Plot'), '... our class does() the correct roles');
371 ok(!My::Test14->does('Role::Truth'), '... our class does() the correct roles');
372 ok(!My::Test14->does('Role::Plot'), '... our class does() the correct roles');
374 is(My::Test11->twist(), 'My::Test::Base::twist -> Role::Truth::twist', '... got the right method return');
375 is(My::Test12->twist(), 'My::Test::Base::twist -> Role::Plot::twist', '... got the right method return');
376 ok(!My::Test13->can('twist'), '... no twist method here at all');
377 is(My::Test14->twist(), 'My::Test::Base::twist', '... got the right method return (from superclass)');
380 package Role::Reality;
387 } qr/A local method of the same name as been found/,
388 '... could not compose roles here, it dies';
391 'Role::Reality::twist';
395 ok(Role::Reality->meta->has_method('twist'), '... the twist method has not been added');
396 ok(!Role::Reality->meta->does_role('Role::Plot'), '... our role does() the correct roles');
397 is(Role::Reality->meta->get_method('twist')->(),
398 'Role::Reality::twist',
399 '... the twist method returns the right value');