6 use Test::More tests => 40;
14 # test no conflicts here
18 sub bar { 'Role::A::bar' }
23 sub xxy { 'Role::B::xxy' }
29 with qw(Role::A Role::B); # no conflict here
32 sub foo { 'Role::C::foo' }
33 sub zot { 'Role::C::zot' }
42 sub zot { 'Class::A::zot' }
45 can_ok( Class::A->new, qw(foo bar xxy zot) );
47 is( Class::A->new->foo, "Role::C::foo", "... got the right foo method" );
48 is( Class::A->new->zot, "Class::A::zot", "... got the right zot method" );
49 is( Class::A->new->bar, "Role::A::bar", "... got the right bar method" );
50 is( Class::A->new->xxy, "Role::B::xxy", "... got the right xxy method" );
53 # check that when a role is added to another role
54 # and they conflict and the method they conflicted
55 # with is then required.
57 package Role::A::Conflict;
62 sub bar { 'Role::A::Conflict::bar' }
64 package Class::A::Conflict;
68 with 'Role::A::Conflict';
69 } qr/requires.*'bar'/, '... did not fufill the requirement of &bar method';
71 package Class::A::Resolved;
75 with 'Role::A::Conflict';
76 } '... did fufill the requirement of &bar method';
78 sub bar { 'Class::A::Resolved::bar' }
81 ok(Role::A::Conflict->meta->requires_method('bar'), '... Role::A::Conflict created the bar requirement');
83 can_ok( Class::A::Resolved->new, qw(bar) );
85 is( Class::A::Resolved->new->bar, 'Class::A::Resolved::bar', "... got the right bar method" );
88 # check that when two roles are composed, they conflict
89 # but the composing role can resolve that conflict
94 sub foo { 'Role::D::foo' }
95 sub bar { 'Role::D::bar' }
100 sub foo { 'Role::E::foo' }
101 sub xxy { 'Role::E::xxy' }
107 with qw(Role::D Role::E); # conflict between 'foo's here
108 } "define role Role::F";
110 sub foo { 'Role::F::foo' }
111 sub zot { 'Role::F::zot' }
118 } "define class Class::B";
120 sub zot { 'Class::B::zot' }
123 can_ok( Class::B->new, qw(foo bar xxy zot) );
125 is( Class::B->new->foo, "Role::F::foo", "... got the &foo method okay" );
126 is( Class::B->new->zot, "Class::B::zot", "... got the &zot method okay" );
127 is( Class::B->new->bar, "Role::D::bar", "... got the &bar method okay" );
128 is( Class::B->new->xxy, "Role::E::xxy", "... got the &xxy method okay" );
130 ok(!Role::F->meta->requires_method('foo'), '... Role::F fufilled the &foo requirement');
133 # check that a conflict can be resolved
134 # by a role, but also new ones can be
135 # created just as easily ...
137 package Role::D::And::E::Conflict;
141 with qw(Role::D Role::E); # conflict between 'foo's here
142 } "... define role Role::D::And::E::Conflict";
144 sub foo { 'Role::D::And::E::Conflict::foo' } # this overrides ...
147 sub xxy { 'Role::D::And::E::Conflict::xxy' }
148 sub bar { 'Role::D::And::E::Conflict::bar' }
152 ok(!Role::D::And::E::Conflict->meta->requires_method('foo'), '... Role::D::And::E::Conflict fufilled the &foo requirement');
153 ok(Role::D::And::E::Conflict->meta->requires_method('xxy'), '... Role::D::And::E::Conflict adds the &xxy requirement');
154 ok(Role::D::And::E::Conflict->meta->requires_method('bar'), '... Role::D::And::E::Conflict adds the &bar requirement');
157 # conflict propagation
162 sub foo { 'Role::H::foo' }
163 sub bar { 'Role::H::bar' }
168 sub foo { 'Role::J::foo' }
169 sub xxy { 'Role::J::xxy' }
175 with qw(Role::J Role::H); # conflict between 'foo's here
176 } "define role Role::I";
178 sub zot { 'Role::I::zot' }
185 } qr/requires.*'foo'/, "defining class Class::C fails";
187 sub zot { 'Class::C::zot' }
194 } "resolved with method";
196 sub foo { 'Class::E::foo' }
197 sub zot { 'Class::E::zot' }
200 can_ok( Class::E->new, qw(foo bar xxy zot) );
202 is( Class::E->new->foo, "Class::E::foo", "... got the right &foo method" );
203 is( Class::E->new->zot, "Class::E::zot", "... got the right &zot method" );
204 is( Class::E->new->bar, "Role::H::bar", "... got the right &bar method" );
205 is( Class::E->new->xxy, "Role::J::xxy", "... got the right &xxy method" );
207 ok(Role::I->meta->requires_method('foo'), '... Role::I still have the &foo requirement');
210 # fix these later ...
212 local $TODO = "add support for attribute methods fufilling reqs";
218 has foo => ( default => __PACKAGE__ . "::foo", is => "rw" );
220 sub zot { 'Class::D::zot' }
224 } "resolved with attr";
226 can_ok( Class::D->new, qw(foo bar xxy zot) );
227 is( eval { Class::D->new->bar }, "Role::H::bar", "bar" );
228 is( eval { Class::D->new->xxy }, "Role::I::xxy", "xxy" );
231 is( eval { Class::D->new->foo }, "Class::D::foo", "foo" );
232 is( eval { Class::D->new->zot }, "Class::D::zot", "zot" );