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 # that the consumer's method shadows just like for classes.
53 package Role::A::Shadow;
58 sub bar { 'Role::A::Shadow::bar' }
60 package Class::A::Shadow;
64 with 'Role::A::Shadow';
65 }, undef, '... did fufill the requirement of &bar method' );
68 can_ok( Class::A::Shadow->new, qw(bar) );
70 is( Class::A::Shadow->new->bar, 'Role::A::Shadow::bar', "... got the right bar method" );
73 # check that when two roles are composed, they conflict
74 # but the composing role can resolve that conflict
79 sub foo { 'Role::D::foo' }
80 sub bar { 'Role::D::bar' }
85 sub foo { 'Role::E::foo' }
86 sub xxy { 'Role::E::xxy' }
92 with qw(Role::D Role::E); # conflict between 'foo's here
93 }, undef, "define role Role::F" );
95 sub foo { 'Role::F::foo' }
96 sub zot { 'Role::F::zot' }
103 }, undef, "define class Class::B" );
105 sub zot { 'Class::B::zot' }
108 can_ok( Class::B->new, qw(foo bar xxy zot) );
110 is( Class::B->new->foo, "Role::F::foo", "... got the &foo method okay" );
111 is( Class::B->new->zot, "Class::B::zot", "... got the &zot method okay" );
112 is( Class::B->new->bar, "Role::D::bar", "... got the &bar method okay" );
113 is( Class::B->new->xxy, "Role::E::xxy", "... got the &xxy method okay" );
115 ok(!Role::F->meta->requires_method('foo'), '... Role::F fufilled the &foo requirement');
118 # check that a conflict can be resolved
119 # by a role, but also new ones can be
120 # created just as easily ...
122 package Role::D::And::E::NoConflict;
126 with qw(Role::D Role::E); # conflict between 'foo's here
127 }, undef, "... define role Role::D::And::E::NoConflict" );
129 sub foo { 'Role::D::And::E::NoConflict::foo' } # this overrides ...
131 sub xxy { 'Role::D::And::E::NoConflict::xxy' } # and so do these ...
132 sub bar { 'Role::D::And::E::NoConflict::bar' }
136 ok(!Role::D::And::E::NoConflict->meta->requires_method('foo'), '... Role::D::And::E::NoConflict fufilled the &foo requirement');
137 ok(!Role::D::And::E::NoConflict->meta->requires_method('xxy'), '... Role::D::And::E::NoConflict fulfilled the &xxy requirement');
138 ok(!Role::D::And::E::NoConflict->meta->requires_method('bar'), '... Role::D::And::E::NoConflict fulfilled the &bar requirement');
141 # conflict propagation
146 sub foo { 'Role::H::foo' }
147 sub bar { 'Role::H::bar' }
152 sub foo { 'Role::J::foo' }
153 sub xxy { 'Role::J::xxy' }
159 with qw(Role::J Role::H); # conflict between 'foo's here
160 }, undef, "define role Role::I" );
162 sub zot { 'Role::I::zot' }
163 sub zzy { 'Role::I::zzy' }
168 ::like( ::exception {
170 }, 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" );
172 sub zot { 'Class::C::zot' }
179 }, undef, "resolved with method" );
181 sub foo { 'Class::E::foo' }
182 sub zot { 'Class::E::zot' }
185 can_ok( Class::E->new, qw(foo bar xxy zot) );
187 is( Class::E->new->foo, "Class::E::foo", "... got the right &foo method" );
188 is( Class::E->new->zot, "Class::E::zot", "... got the right &zot method" );
189 is( Class::E->new->bar, "Role::H::bar", "... got the right &bar method" );
190 is( Class::E->new->xxy, "Role::J::xxy", "... got the right &xxy method" );
192 ok(Role::I->meta->requires_method('foo'), '... Role::I still have the &foo requirement');
199 has foo => ( default => __PACKAGE__ . "::foo", is => "rw" );
201 sub zot { 'Class::D::zot' }
205 }, undef, "resolved with attr" );
207 can_ok( Class::D->new, qw(foo bar xxy zot) );
208 is( eval { Class::D->new->bar }, "Role::H::bar", "bar" );
209 is( eval { Class::D->new->zzy }, "Role::I::zzy", "zzy" );
211 is( eval { Class::D->new->foo }, "Class::D::foo", "foo" );
212 is( eval { Class::D->new->zot }, "Class::D::zot", "zot" );