6 use Test::More tests => 39;
12 # test no conflicts here
16 sub bar { 'Role::A::bar' }
21 sub xxy { 'Role::B::xxy' }
27 with qw(Role::A Role::B); # no conflict here
30 sub foo { 'Role::C::foo' }
31 sub zot { 'Role::C::zot' }
40 sub zot { 'Class::A::zot' }
43 can_ok( Class::A->new, qw(foo bar xxy zot) );
45 is( Class::A->new->foo, "Role::C::foo", "... got the right foo method" );
46 is( Class::A->new->zot, "Class::A::zot", "... got the right zot method" );
47 is( Class::A->new->bar, "Role::A::bar", "... got the right bar method" );
48 is( Class::A->new->xxy, "Role::B::xxy", "... got the right xxy method" );
51 # check that when a role is added to another role
52 # and they conflict and the method they conflicted
53 # with is then required.
55 package Role::A::Conflict;
60 sub bar { 'Role::A::Conflict::bar' }
62 package Class::A::Conflict;
66 with 'Role::A::Conflict';
67 } qr/requires.*'bar'/, '... did not fufill the requirement of &bar method';
69 package Class::A::Resolved;
73 with 'Role::A::Conflict';
74 } '... did fufill the requirement of &bar method';
76 sub bar { 'Class::A::Resolved::bar' }
79 ok(Role::A::Conflict->meta->requires_method('bar'), '... Role::A::Conflict created the bar requirement');
81 can_ok( Class::A::Resolved->new, qw(bar) );
83 is( Class::A::Resolved->new->bar, 'Class::A::Resolved::bar', "... got the right bar method" );
86 # check that when two roles are composed, they conflict
87 # but the composing role can resolve that conflict
92 sub foo { 'Role::D::foo' }
93 sub bar { 'Role::D::bar' }
98 sub foo { 'Role::E::foo' }
99 sub xxy { 'Role::E::xxy' }
105 with qw(Role::D Role::E); # conflict between 'foo's here
106 } "define role Role::F";
108 sub foo { 'Role::F::foo' }
109 sub zot { 'Role::F::zot' }
116 } "define class Class::B";
118 sub zot { 'Class::B::zot' }
121 can_ok( Class::B->new, qw(foo bar xxy zot) );
123 is( Class::B->new->foo, "Role::F::foo", "... got the &foo method okay" );
124 is( Class::B->new->zot, "Class::B::zot", "... got the &zot method okay" );
125 is( Class::B->new->bar, "Role::D::bar", "... got the &bar method okay" );
126 is( Class::B->new->xxy, "Role::E::xxy", "... got the &xxy method okay" );
128 ok(!Role::F->meta->requires_method('foo'), '... Role::F fufilled the &foo requirement');
131 # check that a conflict can be resolved
132 # by a role, but also new ones can be
133 # created just as easily ...
135 package Role::D::And::E::Conflict;
139 with qw(Role::D Role::E); # conflict between 'foo's here
140 } "... define role Role::D::And::E::Conflict";
142 sub foo { 'Role::D::And::E::Conflict::foo' } # this overrides ...
145 sub xxy { 'Role::D::And::E::Conflict::xxy' }
146 sub bar { 'Role::D::And::E::Conflict::bar' }
150 ok(!Role::D::And::E::Conflict->meta->requires_method('foo'), '... Role::D::And::E::Conflict fufilled the &foo requirement');
151 ok(Role::D::And::E::Conflict->meta->requires_method('xxy'), '... Role::D::And::E::Conflict adds the &xxy requirement');
152 ok(Role::D::And::E::Conflict->meta->requires_method('bar'), '... Role::D::And::E::Conflict adds the &bar requirement');
155 # conflict propagation
160 sub foo { 'Role::H::foo' }
161 sub bar { 'Role::H::bar' }
166 sub foo { 'Role::J::foo' }
167 sub xxy { 'Role::J::xxy' }
173 with qw(Role::J Role::H); # conflict between 'foo's here
174 } "define role Role::I";
176 sub zot { 'Role::I::zot' }
183 } qr/requires.*'foo'/, "defining class Class::C fails";
185 sub zot { 'Class::C::zot' }
192 } "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');
208 # fix these later ...
210 local $TODO = "add support for attribute methods fufilling reqs";
216 has foo => ( default => __PACKAGE__ . "::foo", is => "rw" );
218 sub zot { 'Class::D::zot' }
222 } "resolved with attr";
224 can_ok( Class::D->new, qw(foo bar xxy zot) );
225 is( eval { Class::D->new->bar }, "Role::H::bar", "bar" );
226 is( eval { Class::D->new->xxy }, "Role::I::xxy", "xxy" );
229 is( eval { Class::D->new->foo }, "Class::D::foo", "foo" );
230 is( eval { Class::D->new->zot }, "Class::D::zot", "zot" );