Fix error message buglet
[gitmo/Moose.git] / t / roles / role_conflict_detection.t
CommitLineData
db1ab48d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
db1ab48d 8
db1ab48d 9=pod
10
11Mutually recursive roles.
12
13=cut
14
15{
16 package Role::Foo;
db1ab48d 17 use Moose::Role;
18
19 requires 'foo';
d03bd989 20
db1ab48d 21 sub bar { 'Role::Foo::bar' }
d03bd989 22
db1ab48d 23 package Role::Bar;
db1ab48d 24 use Moose::Role;
d03bd989 25
db1ab48d 26 requires 'bar';
d03bd989 27
28 sub foo { 'Role::Bar::foo' }
db1ab48d 29}
30
31{
32 package My::Test1;
db1ab48d 33 use Moose;
d03bd989 34
b10dde3a 35 ::is( ::exception {
db1ab48d 36 with 'Role::Foo', 'Role::Bar';
b10dde3a 37 }, undef, '... our mutually recursive roles combine okay' );
d03bd989 38
db1ab48d 39 package My::Test2;
db1ab48d 40 use Moose;
d03bd989 41
b10dde3a 42 ::is( ::exception {
db1ab48d 43 with 'Role::Bar', 'Role::Foo';
b10dde3a 44 }, undef, '... our mutually recursive roles combine okay (no matter what order)' );
db1ab48d 45}
46
47my $test1 = My::Test1->new;
48isa_ok($test1, 'My::Test1');
49
50ok($test1->does('Role::Foo'), '... $test1 does Role::Foo');
51ok($test1->does('Role::Bar'), '... $test1 does Role::Bar');
52
53can_ok($test1, 'foo');
54can_ok($test1, 'bar');
55
56is($test1->foo, 'Role::Bar::foo', '... $test1->foo worked');
57is($test1->bar, 'Role::Foo::bar', '... $test1->bar worked');
58
59my $test2 = My::Test2->new;
60isa_ok($test2, 'My::Test2');
61
62ok($test2->does('Role::Foo'), '... $test2 does Role::Foo');
63ok($test2->does('Role::Bar'), '... $test2 does Role::Bar');
64
65can_ok($test2, 'foo');
66can_ok($test2, 'bar');
67
68is($test2->foo, 'Role::Bar::foo', '... $test2->foo worked');
69is($test2->bar, 'Role::Foo::bar', '... $test2->bar worked');
70
71# check some meta-stuff
72
73ok(Role::Foo->meta->has_method('bar'), '... it still has the bar method');
74ok(Role::Foo->meta->requires_method('foo'), '... it still has the required foo method');
75
76ok(Role::Bar->meta->has_method('foo'), '... it still has the foo method');
77ok(Role::Bar->meta->requires_method('bar'), '... it still has the required bar method');
78
79=pod
80
81Role method conflicts
82
83=cut
84
85{
86 package Role::Bling;
db1ab48d 87 use Moose::Role;
d03bd989 88
db1ab48d 89 sub bling { 'Role::Bling::bling' }
d03bd989 90
db1ab48d 91 package Role::Bling::Bling;
db1ab48d 92 use Moose::Role;
d03bd989 93
94 sub bling { 'Role::Bling::Bling::bling' }
db1ab48d 95}
96
97{
98 package My::Test3;
db1ab48d 99 use Moose;
d03bd989 100
b10dde3a 101 ::like( ::exception {
db1ab48d 102 with 'Role::Bling', 'Role::Bling::Bling';
b10dde3a 103 }, qr/Due to a method name conflict in roles 'Role::Bling' and 'Role::Bling::Bling', the method 'bling' must be implemented or excluded by 'My::Test3'/, '... role methods conflict and method was required' );
d03bd989 104
db1ab48d 105 package My::Test4;
db1ab48d 106 use Moose;
d03bd989 107
b10dde3a 108 ::is( ::exception {
db1ab48d 109 with 'Role::Bling';
f6bee6fe 110 with 'Role::Bling::Bling';
b10dde3a 111 }, undef, '... role methods didnt conflict when manually combined' );
d03bd989 112
db1ab48d 113 package My::Test5;
db1ab48d 114 use Moose;
d03bd989 115
b10dde3a 116 ::is( ::exception {
db1ab48d 117 with 'Role::Bling::Bling';
f6bee6fe 118 with 'Role::Bling';
b10dde3a 119 }, undef, '... role methods didnt conflict when manually combined (in opposite order)' );
d03bd989 120
db1ab48d 121 package My::Test6;
db1ab48d 122 use Moose;
d03bd989 123
b10dde3a 124 ::is( ::exception {
db1ab48d 125 with 'Role::Bling::Bling', 'Role::Bling';
b10dde3a 126 }, undef, '... role methods didnt conflict when manually resolved' );
d03bd989 127
db1ab48d 128 sub bling { 'My::Test6::bling' }
129}
130
131ok(!My::Test3->meta->has_method('bling'), '... we didnt get any methods in the conflict');
132ok(My::Test4->meta->has_method('bling'), '... we did get the method when manually dealt with');
133ok(My::Test5->meta->has_method('bling'), '... we did get the method when manually dealt with');
134ok(My::Test6->meta->has_method('bling'), '... we did get the method when manually dealt with');
135
d79e62fd 136ok(!My::Test3->does('Role::Bling'), '... our class does() the correct roles');
137ok(!My::Test3->does('Role::Bling::Bling'), '... our class does() the correct roles');
138ok(My::Test4->does('Role::Bling'), '... our class does() the correct roles');
139ok(My::Test4->does('Role::Bling::Bling'), '... our class does() the correct roles');
140ok(My::Test5->does('Role::Bling'), '... our class does() the correct roles');
141ok(My::Test5->does('Role::Bling::Bling'), '... our class does() the correct roles');
142ok(My::Test6->does('Role::Bling'), '... our class does() the correct roles');
143ok(My::Test6->does('Role::Bling::Bling'), '... our class does() the correct roles');
144
db1ab48d 145is(My::Test4->bling, 'Role::Bling::bling', '... and we got the first method that was added');
146is(My::Test5->bling, 'Role::Bling::Bling::bling', '... and we got the first method that was added');
147is(My::Test6->bling, 'My::Test6::bling', '... and we got the local method');
148
149# check how this affects role compostion
150
151{
152 package Role::Bling::Bling::Bling;
db1ab48d 153 use Moose::Role;
d03bd989 154
db1ab48d 155 with 'Role::Bling::Bling';
d03bd989 156
157 sub bling { 'Role::Bling::Bling::Bling::bling' }
db1ab48d 158}
159
160ok(Role::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling');
d79e62fd 161ok(Role::Bling::Bling->meta->does_role('Role::Bling::Bling'), '... our role correctly does() the other role');
fb1e11d5 162ok(Role::Bling::Bling::Bling->meta->has_method('bling'), '... dont have the bling method in Role::Bling::Bling::Bling');
d03bd989 163is(Role::Bling::Bling::Bling->meta->get_method('bling')->(),
d05cd563 164 'Role::Bling::Bling::Bling::bling',
165 '... still got the bling method in Role::Bling::Bling::Bling');
db1ab48d 166
fb1e11d5 167
db1ab48d 168=pod
169
170Role attribute conflicts
171
172=cut
173
174{
175 package Role::Boo;
db1ab48d 176 use Moose::Role;
d03bd989 177
db1ab48d 178 has 'ghost' => (is => 'ro', default => 'Role::Boo::ghost');
d03bd989 179
db1ab48d 180 package Role::Boo::Hoo;
db1ab48d 181 use Moose::Role;
d03bd989 182
db1ab48d 183 has 'ghost' => (is => 'ro', default => 'Role::Boo::Hoo::ghost');
184}
185
186{
187 package My::Test7;
db1ab48d 188 use Moose;
d03bd989 189
b10dde3a 190 ::like( ::exception {
db1ab48d 191 with 'Role::Boo', 'Role::Boo::Hoo';
b10dde3a 192 }, qr/We have encountered an attribute conflict.+ghost/ );
db1ab48d 193
194 package My::Test8;
db1ab48d 195 use Moose;
196
b10dde3a 197 ::is( ::exception {
db1ab48d 198 with 'Role::Boo';
199 with 'Role::Boo::Hoo';
b10dde3a 200 }, undef, '... role attrs didnt conflict when manually combined' );
d03bd989 201
db1ab48d 202 package My::Test9;
db1ab48d 203 use Moose;
204
b10dde3a 205 ::is( ::exception {
db1ab48d 206 with 'Role::Boo::Hoo';
207 with 'Role::Boo';
b10dde3a 208 }, undef, '... role attrs didnt conflict when manually combined' );
db1ab48d 209
210 package My::Test10;
db1ab48d 211 use Moose;
d03bd989 212
213 has 'ghost' => (is => 'ro', default => 'My::Test10::ghost');
214
b10dde3a 215 ::like( ::exception {
db1ab48d 216 with 'Role::Boo', 'Role::Boo::Hoo';
b10dde3a 217 }, qr/We have encountered an attribute conflict/, '... role attrs conflict and cannot be manually disambiguted' );
db1ab48d 218
219}
220
221ok(!My::Test7->meta->has_attribute('ghost'), '... we didnt get any attributes in the conflict');
222ok(My::Test8->meta->has_attribute('ghost'), '... we did get an attributes when manually composed');
223ok(My::Test9->meta->has_attribute('ghost'), '... we did get an attributes when manually composed');
224ok(My::Test10->meta->has_attribute('ghost'), '... we did still have an attribute ghost (conflict does not mess with class)');
225
d79e62fd 226ok(!My::Test7->does('Role::Boo'), '... our class does() the correct roles');
227ok(!My::Test7->does('Role::Boo::Hoo'), '... our class does() the correct roles');
228ok(My::Test8->does('Role::Boo'), '... our class does() the correct roles');
229ok(My::Test8->does('Role::Boo::Hoo'), '... our class does() the correct roles');
230ok(My::Test9->does('Role::Boo'), '... our class does() the correct roles');
231ok(My::Test9->does('Role::Boo::Hoo'), '... our class does() the correct roles');
232ok(!My::Test10->does('Role::Boo'), '... our class does() the correct roles');
233ok(!My::Test10->does('Role::Boo::Hoo'), '... our class does() the correct roles');
234
db1ab48d 235can_ok('My::Test8', 'ghost');
236can_ok('My::Test9', 'ghost');
237can_ok('My::Test10', 'ghost');
238
239is(My::Test8->new->ghost, 'Role::Boo::ghost', '... got the expected default attr value');
240is(My::Test9->new->ghost, 'Role::Boo::Hoo::ghost', '... got the expected default attr value');
241is(My::Test10->new->ghost, 'My::Test10::ghost', '... got the expected default attr value');
242
d05cd563 243=pod
244
245Role override method conflicts
246
247=cut
248
0558683c 249{
250 package Role::Plot;
251 use Moose::Role;
d03bd989 252
0558683c 253 override 'twist' => sub {
254 super() . ' -> Role::Plot::twist';
255 };
d03bd989 256
0558683c 257 package Role::Truth;
258 use Moose::Role;
d03bd989 259
0558683c 260 override 'twist' => sub {
261 super() . ' -> Role::Truth::twist';
262 };
263}
264
265{
266 package My::Test::Base;
267 use Moose;
d03bd989 268
0558683c 269 sub twist { 'My::Test::Base::twist' }
d03bd989 270
0558683c 271 package My::Test11;
272 use Moose;
d03bd989 273
0558683c 274 extends 'My::Test::Base';
275
b10dde3a 276 ::is( ::exception {
0558683c 277 with 'Role::Truth';
b10dde3a 278 }, undef, '... composed the role with override okay' );
d03bd989 279
0558683c 280 package My::Test12;
281 use Moose;
282
283 extends 'My::Test::Base';
284
b10dde3a 285 ::is( ::exception {
0558683c 286 with 'Role::Plot';
b10dde3a 287 }, undef, '... composed the role with override okay' );
d03bd989 288
0558683c 289 package My::Test13;
290 use Moose;
291
b10dde3a 292 ::isnt( ::exception {
d03bd989 293 with 'Role::Plot';
b10dde3a 294 }, undef, '... cannot compose it because we have no superclass' );
d03bd989 295
0558683c 296 package My::Test14;
297 use Moose;
298
299 extends 'My::Test::Base';
300
b10dde3a 301 ::like( ::exception {
d03bd989 302 with 'Role::Plot', 'Role::Truth';
b10dde3a 303 }, qr/Two \'override\' methods of the same name encountered/, '... cannot compose it because we have no superclass' );
0558683c 304}
305
306ok(My::Test11->meta->has_method('twist'), '... the twist method has been added');
307ok(My::Test12->meta->has_method('twist'), '... the twist method has been added');
308ok(!My::Test13->meta->has_method('twist'), '... the twist method has not been added');
309ok(!My::Test14->meta->has_method('twist'), '... the twist method has not been added');
310
311ok(!My::Test11->does('Role::Plot'), '... our class does() the correct roles');
312ok(My::Test11->does('Role::Truth'), '... our class does() the correct roles');
313ok(!My::Test12->does('Role::Truth'), '... our class does() the correct roles');
314ok(My::Test12->does('Role::Plot'), '... our class does() the correct roles');
315ok(!My::Test13->does('Role::Plot'), '... our class does() the correct roles');
316ok(!My::Test14->does('Role::Truth'), '... our class does() the correct roles');
317ok(!My::Test14->does('Role::Plot'), '... our class does() the correct roles');
318
319is(My::Test11->twist(), 'My::Test::Base::twist -> Role::Truth::twist', '... got the right method return');
320is(My::Test12->twist(), 'My::Test::Base::twist -> Role::Plot::twist', '... got the right method return');
321ok(!My::Test13->can('twist'), '... no twist method here at all');
322is(My::Test14->twist(), 'My::Test::Base::twist', '... got the right method return (from superclass)');
323
324{
325 package Role::Reality;
326 use Moose::Role;
327
b10dde3a 328 ::like( ::exception {
0558683c 329 with 'Role::Plot';
b10dde3a 330 }, qr/A local method of the same name as been found/, '... could not compose roles here, it dies' );
0558683c 331
332 sub twist {
333 'Role::Reality::twist';
334 }
d03bd989 335}
0558683c 336
337ok(Role::Reality->meta->has_method('twist'), '... the twist method has not been added');
fb1e11d5 338#ok(!Role::Reality->meta->does_role('Role::Plot'), '... our role does() the correct roles');
d03bd989 339is(Role::Reality->meta->get_method('twist')->(),
340 'Role::Reality::twist',
0558683c 341 '... the twist method returns the right value');
3c5fd53a 342
f0916ad8 343# Ovid's test case from rt.cpan.org #44
344{
345 package Role1;
346 use Moose::Role;
347
348 sub foo {}
349}
350{
351 package Role2;
352 use Moose::Role;
353
354 sub foo {}
355}
356{
357 package Conflicts;
358 use Moose;
359
b10dde3a 360 ::like( ::exception {
f0916ad8 361 with qw(Role1 Role2);
b10dde3a 362 }, qr/Due to a method name conflict in roles 'Role1' and 'Role2', the method 'foo' must be implemented or excluded by 'Conflicts'/ );
f0916ad8 363}
364
3c5fd53a 365=pod
366
367Role conflicts between attributes and methods
368
21716c07 369[15:23] <kolibrie> when class defines method and role defines method, class wins
370[15:24] <kolibrie> when class 'has' method and role defines method, class wins
371[15:24] <kolibrie> when class defines method and role 'has' method, role wins
372[15:24] <kolibrie> when class 'has' method and role 'has' method, role wins
6549b0d1 373[15:24] <kolibrie> which means when class 'has' method and two roles 'has' method, no tiebreak is detected
21716c07 374[15:24] <perigrin> this is with role and has declaration in the exact same order in every case?
375[15:25] <kolibrie> yes
376[15:25] <perigrin> interesting
377[15:25] <kolibrie> that's what I thought
378[15:26] <kolibrie> does that sound like something I should write a test for?
379[15:27] <perigrin> stevan, ping?
380[15:27] <perigrin> I'm not sure what the right answer for composition is.
381[15:27] <perigrin> who should win
382[15:27] <perigrin> if I were to guess I'd say the class should always win.
383[15:27] <kolibrie> that would be my guess, but I thought I would ask to make sure
384[15:29] <stevan> kolibrie: please write a test
385[15:29] <stevan> I am not exactly sure who should win either,.. but I suspect it is not working correctly right now
386[15:29] <stevan> I know exactly why it is doing what it is doing though
387
388Now I have to decide actually what happens, and how to fix it.
389- SL
3c5fd53a 390
391{
392 package Role::Method;
393 use Moose::Role;
d03bd989 394
3c5fd53a 395 sub ghost { 'Role::Method::ghost' }
396
397 package Role::Method2;
398 use Moose::Role;
d03bd989 399
3c5fd53a 400 sub ghost { 'Role::Method2::ghost' }
401
402 package Role::Attribute;
403 use Moose::Role;
d03bd989 404
3c5fd53a 405 has 'ghost' => (is => 'ro', default => 'Role::Attribute::ghost');
406
407 package Role::Attribute2;
408 use Moose::Role;
d03bd989 409
3c5fd53a 410 has 'ghost' => (is => 'ro', default => 'Role::Attribute2::ghost');
411}
412
413{
414 package My::Test15;
415 use Moose;
416
53a4d826 417 ::lives_ok {
3c5fd53a 418 with 'Role::Method';
53a4d826 419 } '... composed the method role into the method class';
3c5fd53a 420
421 sub ghost { 'My::Test15::ghost' }
422
423 package My::Test16;
424 use Moose;
425
53a4d826 426 ::lives_ok {
3c5fd53a 427 with 'Role::Method';
53a4d826 428 } '... composed the method role into the attribute class';
3c5fd53a 429
430 has 'ghost' => (is => 'ro', default => 'My::Test16::ghost');
431
432 package My::Test17;
433 use Moose;
434
53a4d826 435 ::lives_ok {
3c5fd53a 436 with 'Role::Attribute';
53a4d826 437 } '... composed the attribute role into the method class';
3c5fd53a 438
439 sub ghost { 'My::Test17::ghost' }
440
441 package My::Test18;
442 use Moose;
443
53a4d826 444 ::lives_ok {
3c5fd53a 445 with 'Role::Attribute';
53a4d826 446 } '... composed the attribute role into the attribute class';
3c5fd53a 447
448 has 'ghost' => (is => 'ro', default => 'My::Test18::ghost');
449
450 package My::Test19;
451 use Moose;
452
53a4d826 453 ::lives_ok {
3c5fd53a 454 with 'Role::Method', 'Role::Method2';
53a4d826 455 } '... composed method roles into class with method tiebreaker';
3c5fd53a 456
457 sub ghost { 'My::Test19::ghost' }
458
459 package My::Test20;
460 use Moose;
461
53a4d826 462 ::lives_ok {
3c5fd53a 463 with 'Role::Method', 'Role::Method2';
53a4d826 464 } '... composed method roles into class with attribute tiebreaker';
3c5fd53a 465
466 has 'ghost' => (is => 'ro', default => 'My::Test20::ghost');
467
468 package My::Test21;
469 use Moose;
470
53a4d826 471 ::lives_ok {
3c5fd53a 472 with 'Role::Attribute', 'Role::Attribute2';
53a4d826 473 } '... composed attribute roles into class with method tiebreaker';
3c5fd53a 474
475 sub ghost { 'My::Test21::ghost' }
476
477 package My::Test22;
478 use Moose;
479
53a4d826 480 ::lives_ok {
3c5fd53a 481 with 'Role::Attribute', 'Role::Attribute2';
53a4d826 482 } '... composed attribute roles into class with attribute tiebreaker';
3c5fd53a 483
484 has 'ghost' => (is => 'ro', default => 'My::Test22::ghost');
485
486 package My::Test23;
487 use Moose;
488
53a4d826 489 ::lives_ok {
3c5fd53a 490 with 'Role::Method', 'Role::Attribute';
53a4d826 491 } '... composed method and attribute role into class with method tiebreaker';
3c5fd53a 492
493 sub ghost { 'My::Test23::ghost' }
494
495 package My::Test24;
496 use Moose;
497
53a4d826 498 ::lives_ok {
3c5fd53a 499 with 'Role::Method', 'Role::Attribute';
53a4d826 500 } '... composed method and attribute role into class with attribute tiebreaker';
3c5fd53a 501
502 has 'ghost' => (is => 'ro', default => 'My::Test24::ghost');
503
504 package My::Test25;
505 use Moose;
506
53a4d826 507 ::lives_ok {
3c5fd53a 508 with 'Role::Attribute', 'Role::Method';
53a4d826 509 } '... composed attribute and method role into class with method tiebreaker';
3c5fd53a 510
511 sub ghost { 'My::Test25::ghost' }
512
513 package My::Test26;
514 use Moose;
515
53a4d826 516 ::lives_ok {
3c5fd53a 517 with 'Role::Attribute', 'Role::Method';
53a4d826 518 } '... composed attribute and method role into class with attribute tiebreaker';
3c5fd53a 519
520 has 'ghost' => (is => 'ro', default => 'My::Test26::ghost');
521}
522
523my $test15 = My::Test15->new;
524isa_ok($test15, 'My::Test15');
525is($test15->ghost, 'My::Test15::ghost', '... we access the method from the class and ignore the role method');
526
527my $test16 = My::Test16->new;
528isa_ok($test16, 'My::Test16');
529is($test16->ghost, 'My::Test16::ghost', '... we access the attribute from the class and ignore the role method');
530
531my $test17 = My::Test17->new;
532isa_ok($test17, 'My::Test17');
533is($test17->ghost, 'My::Test17::ghost', '... we access the method from the class and ignore the role attribute');
534
535my $test18 = My::Test18->new;
536isa_ok($test18, 'My::Test18');
537is($test18->ghost, 'My::Test18::ghost', '... we access the attribute from the class and ignore the role attribute');
538
539my $test19 = My::Test19->new;
540isa_ok($test19, 'My::Test19');
541is($test19->ghost, 'My::Test19::ghost', '... we access the method from the class and ignore the role methods');
542
543my $test20 = My::Test20->new;
544isa_ok($test20, 'My::Test20');
545is($test20->ghost, 'My::Test20::ghost', '... we access the attribute from the class and ignore the role methods');
546
547my $test21 = My::Test21->new;
548isa_ok($test21, 'My::Test21');
549is($test21->ghost, 'My::Test21::ghost', '... we access the method from the class and ignore the role attributes');
550
551my $test22 = My::Test22->new;
552isa_ok($test22, 'My::Test22');
553is($test22->ghost, 'My::Test22::ghost', '... we access the attribute from the class and ignore the role attributes');
554
555my $test23 = My::Test23->new;
556isa_ok($test23, 'My::Test23');
557is($test23->ghost, 'My::Test23::ghost', '... we access the method from the class and ignore the role method and attribute');
558
559my $test24 = My::Test24->new;
560isa_ok($test24, 'My::Test24');
561is($test24->ghost, 'My::Test24::ghost', '... we access the attribute from the class and ignore the role method and attribute');
562
563my $test25 = My::Test25->new;
564isa_ok($test25, 'My::Test25');
565is($test25->ghost, 'My::Test25::ghost', '... we access the method from the class and ignore the role attribute and method');
566
567my $test26 = My::Test26->new;
568isa_ok($test26, 'My::Test26');
569is($test26->ghost, 'My::Test26::ghost', '... we access the attribute from the class and ignore the role attribute and method');
570
21716c07 571=cut
a28e50e4 572
573done_testing;