X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F030_roles%2F005_role_conflict_detection.t;h=d33b1c035ae54c04d4e08e29eab32b1619af4e95;hb=a28e50e44945358d15eb19e4688573741a319fe0;hp=5f432a3bab0ca9ebd31a1b641e4998990b1ff84a;hpb=e59a5c292a333cac504b65ebd4bba20b5e98d796;p=gitmo%2FMoose.git diff --git a/t/030_roles/005_role_conflict_detection.t b/t/030_roles/005_role_conflict_detection.t index 5f432a3..d33b1c0 100644 --- a/t/030_roles/005_role_conflict_detection.t +++ b/t/030_roles/005_role_conflict_detection.t @@ -3,14 +3,9 @@ use strict; use warnings; -use Test::More tests => 90; # it's really 126 with kolibre's tests; +use Test::More; use Test::Exception; -BEGIN { - use_ok('Moose'); - use_ok('Moose::Role'); -} - =pod Mutually recursive roles. @@ -22,31 +17,31 @@ Mutually recursive roles. use Moose::Role; requires 'foo'; - + sub bar { 'Role::Foo::bar' } - + package Role::Bar; use Moose::Role; - + requires 'bar'; - - sub foo { 'Role::Bar::foo' } + + sub foo { 'Role::Bar::foo' } } { package My::Test1; use Moose; - + ::lives_ok { with 'Role::Foo', 'Role::Bar'; } '... our mutually recursive roles combine okay'; - + package My::Test2; use Moose; - + ::lives_ok { with 'Role::Bar', 'Role::Foo'; - } '... our mutually recursive roles combine okay (no matter what order)'; + } '... our mutually recursive roles combine okay (no matter what order)'; } my $test1 = My::Test1->new; @@ -90,46 +85,46 @@ Role method conflicts { package Role::Bling; use Moose::Role; - + sub bling { 'Role::Bling::bling' } - + package Role::Bling::Bling; use Moose::Role; - - sub bling { 'Role::Bling::Bling::bling' } + + sub bling { 'Role::Bling::Bling::bling' } } { package My::Test3; use Moose; - + ::throws_ok { with 'Role::Bling', 'Role::Bling::Bling'; - } qr/requires the method \'bling\' to be implemented/, '... role methods conflicted and method was required'; - + } 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'; + package My::Test4; use Moose; - + ::lives_ok { with 'Role::Bling'; with 'Role::Bling::Bling'; - } '... role methods didnt conflict when manually combined'; - + } '... role methods didnt conflict when manually combined'; + package My::Test5; use Moose; - + ::lives_ok { with 'Role::Bling::Bling'; with 'Role::Bling'; - } '... role methods didnt conflict when manually combined (in opposite order)'; - + } '... role methods didnt conflict when manually combined (in opposite order)'; + package My::Test6; use Moose; - + ::lives_ok { with 'Role::Bling::Bling', 'Role::Bling'; - } '... role methods didnt conflict when manually resolved'; - + } '... role methods didnt conflict when manually resolved'; + sub bling { 'My::Test6::bling' } } @@ -156,19 +151,20 @@ is(My::Test6->bling, 'My::Test6::bling', '... and we got the local method'); { package Role::Bling::Bling::Bling; use Moose::Role; - + with 'Role::Bling::Bling'; - - sub bling { 'Role::Bling::Bling::Bling::bling' } + + sub bling { 'Role::Bling::Bling::Bling::bling' } } ok(Role::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling'); ok(Role::Bling::Bling->meta->does_role('Role::Bling::Bling'), '... our role correctly does() the other role'); -ok(Role::Bling::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling::Bling'); -is(Role::Bling::Bling::Bling->meta->get_method('bling')->(), +ok(Role::Bling::Bling::Bling->meta->has_method('bling'), '... dont have the bling method in Role::Bling::Bling::Bling'); +is(Role::Bling::Bling::Bling->meta->get_method('bling')->(), 'Role::Bling::Bling::Bling::bling', '... still got the bling method in Role::Bling::Bling::Bling'); + =pod Role attribute conflicts @@ -178,23 +174,23 @@ Role attribute conflicts { package Role::Boo; use Moose::Role; - + has 'ghost' => (is => 'ro', default => 'Role::Boo::ghost'); - + package Role::Boo::Hoo; use Moose::Role; - + has 'ghost' => (is => 'ro', default => 'Role::Boo::Hoo::ghost'); } { package My::Test7; use Moose; - + ::throws_ok { with 'Role::Boo', 'Role::Boo::Hoo'; - } qr/Role \'Role::Boo::Hoo\' has encountered an attribute conflict/, - '... role attrs conflicted and method was required'; + } qr/We have encountered an attribute conflict/, + '... role attrs conflict and method was required'; package My::Test8; use Moose; @@ -203,24 +199,24 @@ Role attribute conflicts with 'Role::Boo'; with 'Role::Boo::Hoo'; } '... role attrs didnt conflict when manually combined'; - + package My::Test9; use Moose; ::lives_ok { with 'Role::Boo::Hoo'; with 'Role::Boo'; - } '... role attrs didnt conflict when manually combined'; + } '... role attrs didnt conflict when manually combined'; package My::Test10; use Moose; - - has 'ghost' => (is => 'ro', default => 'My::Test10::ghost'); - + + has 'ghost' => (is => 'ro', default => 'My::Test10::ghost'); + ::throws_ok { with 'Role::Boo', 'Role::Boo::Hoo'; - } qr/Role \'Role::Boo::Hoo\' has encountered an attribute conflict/, - '... role attrs conflicted and cannot be manually disambiguted'; + } qr/We have encountered an attribute conflict/, + '... role attrs conflict and cannot be manually disambiguted'; } @@ -255,14 +251,14 @@ Role override method conflicts { package Role::Plot; use Moose::Role; - + override 'twist' => sub { super() . ' -> Role::Plot::twist'; }; - + package Role::Truth; use Moose::Role; - + override 'twist' => sub { super() . ' -> Role::Truth::twist'; }; @@ -271,43 +267,43 @@ Role override method conflicts { package My::Test::Base; use Moose; - + sub twist { 'My::Test::Base::twist' } - + package My::Test11; use Moose; - + extends 'My::Test::Base'; ::lives_ok { with 'Role::Truth'; } '... composed the role with override okay'; - + package My::Test12; use Moose; extends 'My::Test::Base'; - ::lives_ok { + ::lives_ok { with 'Role::Plot'; } '... composed the role with override okay'; - + package My::Test13; use Moose; ::dies_ok { - with 'Role::Plot'; + with 'Role::Plot'; } '... cannot compose it because we have no superclass'; - + package My::Test14; use Moose; extends 'My::Test::Base'; ::throws_ok { - with 'Role::Plot', 'Role::Truth'; - } qr/Two \'override\' methods of the same name encountered/, - '... cannot compose it because we have no superclass'; + with 'Role::Plot', 'Role::Truth'; + } qr/Two \'override\' methods of the same name encountered/, + '... cannot compose it because we have no superclass'; } ok(My::Test11->meta->has_method('twist'), '... the twist method has been added'); @@ -332,22 +328,44 @@ is(My::Test14->twist(), 'My::Test::Base::twist', '... got the right method retur package Role::Reality; use Moose::Role; - ::throws_ok { + ::throws_ok { with 'Role::Plot'; - } qr/A local method of the same name as been found/, + } qr/A local method of the same name as been found/, '... could not compose roles here, it dies'; sub twist { 'Role::Reality::twist'; } -} +} ok(Role::Reality->meta->has_method('twist'), '... the twist method has not been added'); -ok(!Role::Reality->meta->does_role('Role::Plot'), '... our role does() the correct roles'); -is(Role::Reality->meta->get_method('twist')->(), - 'Role::Reality::twist', +#ok(!Role::Reality->meta->does_role('Role::Plot'), '... our role does() the correct roles'); +is(Role::Reality->meta->get_method('twist')->(), + 'Role::Reality::twist', '... the twist method returns the right value'); +# Ovid's test case from rt.cpan.org #44 +{ + package Role1; + use Moose::Role; + + sub foo {} +} +{ + package Role2; + use Moose::Role; + + sub foo {} +} +{ + package Conflicts; + use Moose; + + ::throws_ok { + with qw(Role1 Role2); + } qr/Due to a method name conflict in roles 'Role1' and 'Role2', the method 'foo' must be implemented or excluded by 'Conflicts'/; +} + =pod Role conflicts between attributes and methods @@ -356,8 +374,7 @@ Role conflicts between attributes and methods [15:24] when class 'has' method and role defines method, class wins [15:24] when class defines method and role 'has' method, role wins [15:24] when class 'has' method and role 'has' method, role wins -[15:24] which means when class 'has' method and two roles 'has' method, no tiebreak is d -[15:24] etected +[15:24] which means when class 'has' method and two roles 'has' method, no tiebreak is detected [15:24] this is with role and has declaration in the exact same order in every case? [15:25] yes [15:25] interesting @@ -378,22 +395,22 @@ Now I have to decide actually what happens, and how to fix it. { package Role::Method; use Moose::Role; - + sub ghost { 'Role::Method::ghost' } package Role::Method2; use Moose::Role; - + sub ghost { 'Role::Method2::ghost' } package Role::Attribute; use Moose::Role; - + has 'ghost' => (is => 'ro', default => 'Role::Attribute::ghost'); package Role::Attribute2; use Moose::Role; - + has 'ghost' => (is => 'ro', default => 'Role::Attribute2::ghost'); } @@ -401,7 +418,7 @@ Now I have to decide actually what happens, and how to fix it. package My::Test15; use Moose; - ::lives_ok { + ::lives_ok { with 'Role::Method'; } '... composed the method role into the method class'; @@ -556,3 +573,5 @@ isa_ok($test26, 'My::Test26'); is($test26->ghost, 'My::Test26::ghost', '... we access the attribute from the class and ignore the role attribute and method'); =cut + +done_testing;