11 # test no conflicts here
15 sub bar { 'Role::A::bar' }
20 sub xxy { 'Role::B::xxy' }
26 with qw(Role::A Role::B); # no conflict here
27 }, undef, "define role C" );
29 sub foo { 'Role::C::foo' }
30 sub zot { 'Role::C::zot' }
37 }, undef, "define class A" );
39 sub zot { 'Class::A::zot' }
42 can_ok( Class::A->new, qw(foo bar xxy zot) );
44 is( Class::A->new->foo, "Role::C::foo", "... got the right foo method" );
45 is( Class::A->new->zot, "Class::A::zot", "... got the right zot method" );
46 is( Class::A->new->bar, "Role::A::bar", "... got the right bar method" );
47 is( Class::A->new->xxy, "Role::B::xxy", "... got the right xxy method" );
50 # check that when a role is added to another role
51 # and they conflict and the method they conflict
52 # with is then required.
54 package Role::A::Conflict;
59 sub bar { 'Role::A::Conflict::bar' }
61 package Class::A::Conflict;
65 with 'Role::A::Conflict';
66 }, qr/Due to a method name conflict in roles 'Role::A' and 'Role::A::Conflict', the method 'bar' must be implemented or excluded by 'Class::A::Conflict'/, '... did not fufill the requirement of &bar method' );
68 package Class::A::Resolved;
72 with 'Role::A::Conflict';
73 }, undef, '... did fufill the requirement of &bar method' );
75 sub bar { 'Class::A::Resolved::bar' }
78 ok(Role::A::Conflict->meta->requires_method('bar'), '... Role::A::Conflict created the bar requirement');
80 can_ok( Class::A::Resolved->new, qw(bar) );
82 is( Class::A::Resolved->new->bar, 'Class::A::Resolved::bar', "... got the right bar method" );
85 # check that when two roles are composed, they conflict
86 # but the composing role can resolve that conflict
91 sub foo { 'Role::D::foo' }
92 sub bar { 'Role::D::bar' }
97 sub foo { 'Role::E::foo' }
98 sub xxy { 'Role::E::xxy' }
104 with qw(Role::D Role::E); # conflict between 'foo's here
105 }, undef, "define role Role::F" );
107 sub foo { 'Role::F::foo' }
108 sub zot { 'Role::F::zot' }
115 }, undef, "define class Class::B" );
117 sub zot { 'Class::B::zot' }
120 can_ok( Class::B->new, qw(foo bar xxy zot) );
122 is( Class::B->new->foo, "Role::F::foo", "... got the &foo method okay" );
123 is( Class::B->new->zot, "Class::B::zot", "... got the &zot method okay" );
124 is( Class::B->new->bar, "Role::D::bar", "... got the &bar method okay" );
125 is( Class::B->new->xxy, "Role::E::xxy", "... got the &xxy method okay" );
127 ok(!Role::F->meta->requires_method('foo'), '... Role::F fufilled the &foo requirement');
130 # check that a conflict can be resolved
131 # by a role, but also new ones can be
132 # created just as easily ...
134 package Role::D::And::E::Conflict;
138 with qw(Role::D Role::E); # conflict between 'foo's here
139 }, undef, "... define role Role::D::And::E::Conflict" );
141 sub foo { 'Role::D::And::E::Conflict::foo' } # this overrides ...
144 sub xxy { 'Role::D::And::E::Conflict::xxy' }
145 sub bar { 'Role::D::And::E::Conflict::bar' }
149 ok(!Role::D::And::E::Conflict->meta->requires_method('foo'), '... Role::D::And::E::Conflict fufilled the &foo requirement');
150 ok(Role::D::And::E::Conflict->meta->requires_method('xxy'), '... Role::D::And::E::Conflict adds the &xxy requirement');
151 ok(Role::D::And::E::Conflict->meta->requires_method('bar'), '... Role::D::And::E::Conflict adds the &bar requirement');
154 # conflict propagation
159 sub foo { 'Role::H::foo' }
160 sub bar { 'Role::H::bar' }
165 sub foo { 'Role::J::foo' }
166 sub xxy { 'Role::J::xxy' }
172 with qw(Role::J Role::H); # conflict between 'foo's here
173 }, undef, "define role Role::I" );
175 sub zot { 'Role::I::zot' }
176 sub zzy { 'Role::I::zzy' }
181 ::like( ::exception {
183 }, qr/Due to a method name conflict in roles 'Role::H' and 'Role::J', the method 'foo' must be implemented or excluded by 'Class::C'/, "defining class Class::C fails" );
185 sub zot { 'Class::C::zot' }
192 }, undef, "resolved with method" );
194 sub foo { 'Class::E::foo' }
195 sub zot { 'Class::E::zot' }
198 can_ok( Class::E->new, qw(foo bar xxy zot) );
200 is( Class::E->new->foo, "Class::E::foo", "... got the right &foo method" );
201 is( Class::E->new->zot, "Class::E::zot", "... got the right &zot method" );
202 is( Class::E->new->bar, "Role::H::bar", "... got the right &bar method" );
203 is( Class::E->new->xxy, "Role::J::xxy", "... got the right &xxy method" );
205 ok(Role::I->meta->requires_method('foo'), '... Role::I still have the &foo requirement');
212 has foo => ( default => __PACKAGE__ . "::foo", is => "rw" );
214 sub zot { 'Class::D::zot' }
218 }, undef, "resolved with attr" );
220 can_ok( Class::D->new, qw(foo bar xxy zot) );
221 is( eval { Class::D->new->bar }, "Role::H::bar", "bar" );
222 is( eval { Class::D->new->zzy }, "Role::I::zzy", "zzy" );
224 is( eval { Class::D->new->foo }, "Class::D::foo", "foo" );
225 is( eval { Class::D->new->zot }, "Class::D::zot", "zot" );