Commit | Line | Data |
4c15a12d |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
f6bee6fe |
6 | use Test::More tests => 39; |
4c15a12d |
7 | use Test::Exception; |
8 | |
7ff56534 |
9 | |
4c15a12d |
10 | |
d03bd989 |
11 | { |
c4538447 |
12 | # test no conflicts here |
13 | package Role::A; |
14 | use Moose::Role; |
4c15a12d |
15 | |
c4538447 |
16 | sub bar { 'Role::A::bar' } |
4c15a12d |
17 | |
c4538447 |
18 | package Role::B; |
19 | use Moose::Role; |
4c15a12d |
20 | |
c4538447 |
21 | sub xxy { 'Role::B::xxy' } |
4c15a12d |
22 | |
c4538447 |
23 | package Role::C; |
24 | use Moose::Role; |
d03bd989 |
25 | |
c4538447 |
26 | ::lives_ok { |
27 | with qw(Role::A Role::B); # no conflict here |
4c15a12d |
28 | } "define role C"; |
29 | |
c4538447 |
30 | sub foo { 'Role::C::foo' } |
31 | sub zot { 'Role::C::zot' } |
4c15a12d |
32 | |
c4538447 |
33 | package Class::A; |
34 | use Moose; |
4c15a12d |
35 | |
c4538447 |
36 | ::lives_ok { |
f6bee6fe |
37 | with qw(Role::C); |
4c15a12d |
38 | } "define class A"; |
d03bd989 |
39 | |
c4538447 |
40 | sub zot { 'Class::A::zot' } |
41 | } |
4c15a12d |
42 | |
c4538447 |
43 | can_ok( Class::A->new, qw(foo bar xxy zot) ); |
4c15a12d |
44 | |
c4538447 |
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" ); |
4c15a12d |
49 | |
50 | { |
c4538447 |
51 | # check that when a role is added to another role |
bb153262 |
52 | # and they conflict and the method they conflict |
d03bd989 |
53 | # with is then required. |
54 | |
c4538447 |
55 | package Role::A::Conflict; |
56 | use Moose::Role; |
d03bd989 |
57 | |
c4538447 |
58 | with 'Role::A'; |
d03bd989 |
59 | |
c4538447 |
60 | sub bar { 'Role::A::Conflict::bar' } |
d03bd989 |
61 | |
c4538447 |
62 | package Class::A::Conflict; |
63 | use Moose; |
d03bd989 |
64 | |
c4538447 |
65 | ::throws_ok { |
66 | with 'Role::A::Conflict'; |
353d4b3b |
67 | } 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'; |
d03bd989 |
68 | |
c4538447 |
69 | package Class::A::Resolved; |
70 | use Moose; |
d03bd989 |
71 | |
c4538447 |
72 | ::lives_ok { |
f6bee6fe |
73 | with 'Role::A::Conflict'; |
d03bd989 |
74 | } '... did fufill the requirement of &bar method'; |
75 | |
c4538447 |
76 | sub bar { 'Class::A::Resolved::bar' } |
77 | } |
4c15a12d |
78 | |
c4538447 |
79 | ok(Role::A::Conflict->meta->requires_method('bar'), '... Role::A::Conflict created the bar requirement'); |
4c15a12d |
80 | |
c4538447 |
81 | can_ok( Class::A::Resolved->new, qw(bar) ); |
4c15a12d |
82 | |
c4538447 |
83 | is( Class::A::Resolved->new->bar, 'Class::A::Resolved::bar', "... got the right bar method" ); |
4c15a12d |
84 | |
c4538447 |
85 | { |
86 | # check that when two roles are composed, they conflict |
87 | # but the composing role can resolve that conflict |
d03bd989 |
88 | |
c4538447 |
89 | package Role::D; |
90 | use Moose::Role; |
4c15a12d |
91 | |
c4538447 |
92 | sub foo { 'Role::D::foo' } |
d03bd989 |
93 | sub bar { 'Role::D::bar' } |
4c15a12d |
94 | |
c4538447 |
95 | package Role::E; |
96 | use Moose::Role; |
4c15a12d |
97 | |
c4538447 |
98 | sub foo { 'Role::E::foo' } |
99 | sub xxy { 'Role::E::xxy' } |
4c15a12d |
100 | |
c4538447 |
101 | package Role::F; |
102 | use Moose::Role; |
4c15a12d |
103 | |
c4538447 |
104 | ::lives_ok { |
105 | with qw(Role::D Role::E); # conflict between 'foo's here |
106 | } "define role Role::F"; |
d03bd989 |
107 | |
c4538447 |
108 | sub foo { 'Role::F::foo' } |
d03bd989 |
109 | sub zot { 'Role::F::zot' } |
110 | |
c4538447 |
111 | package Class::B; |
112 | use Moose; |
d03bd989 |
113 | |
c4538447 |
114 | ::lives_ok { |
f6bee6fe |
115 | with qw(Role::F); |
4c15a12d |
116 | } "define class Class::B"; |
d03bd989 |
117 | |
c4538447 |
118 | sub zot { 'Class::B::zot' } |
119 | } |
4c15a12d |
120 | |
c4538447 |
121 | can_ok( Class::B->new, qw(foo bar xxy zot) ); |
4c15a12d |
122 | |
c4538447 |
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" ); |
127 | |
128 | ok(!Role::F->meta->requires_method('foo'), '... Role::F fufilled the &foo requirement'); |
129 | |
130 | { |
131 | # check that a conflict can be resolved |
d03bd989 |
132 | # by a role, but also new ones can be |
c4538447 |
133 | # created just as easily ... |
d03bd989 |
134 | |
c4538447 |
135 | package Role::D::And::E::Conflict; |
136 | use Moose::Role; |
137 | |
138 | ::lives_ok { |
139 | with qw(Role::D Role::E); # conflict between 'foo's here |
140 | } "... define role Role::D::And::E::Conflict"; |
d03bd989 |
141 | |
c4538447 |
142 | sub foo { 'Role::D::And::E::Conflict::foo' } # this overrides ... |
d03bd989 |
143 | |
144 | # but these conflict |
145 | sub xxy { 'Role::D::And::E::Conflict::xxy' } |
146 | sub bar { 'Role::D::And::E::Conflict::bar' } |
4c15a12d |
147 | |
148 | } |
149 | |
c4538447 |
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'); |
153 | |
4c15a12d |
154 | { |
155 | # conflict propagation |
d03bd989 |
156 | |
c4538447 |
157 | package Role::H; |
158 | use Moose::Role; |
4c15a12d |
159 | |
c4538447 |
160 | sub foo { 'Role::H::foo' } |
d03bd989 |
161 | sub bar { 'Role::H::bar' } |
4c15a12d |
162 | |
c4538447 |
163 | package Role::J; |
164 | use Moose::Role; |
4c15a12d |
165 | |
c4538447 |
166 | sub foo { 'Role::J::foo' } |
167 | sub xxy { 'Role::J::xxy' } |
4c15a12d |
168 | |
c4538447 |
169 | package Role::I; |
170 | use Moose::Role; |
4c15a12d |
171 | |
c4538447 |
172 | ::lives_ok { |
4c15a12d |
173 | with qw(Role::J Role::H); # conflict between 'foo's here |
4c15a12d |
174 | } "define role Role::I"; |
d03bd989 |
175 | |
c4538447 |
176 | sub zot { 'Role::I::zot' } |
f3e76c8f |
177 | sub zzy { 'Role::I::zzy' } |
4c15a12d |
178 | |
c4538447 |
179 | package Class::C; |
180 | use Moose; |
d03bd989 |
181 | |
c4538447 |
182 | ::throws_ok { |
4c15a12d |
183 | with qw(Role::I); |
353d4b3b |
184 | } 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"; |
4c15a12d |
185 | |
c4538447 |
186 | sub zot { 'Class::C::zot' } |
4c15a12d |
187 | |
c4538447 |
188 | package Class::E; |
189 | use Moose; |
190 | |
191 | ::lives_ok { |
f6bee6fe |
192 | with qw(Role::I); |
d03bd989 |
193 | } "resolved with method"; |
c4538447 |
194 | |
195 | sub foo { 'Class::E::foo' } |
d03bd989 |
196 | sub zot { 'Class::E::zot' } |
c4538447 |
197 | } |
198 | |
199 | can_ok( Class::E->new, qw(foo bar xxy zot) ); |
4c15a12d |
200 | |
c4538447 |
201 | is( Class::E->new->foo, "Class::E::foo", "... got the right &foo method" ); |
202 | is( Class::E->new->zot, "Class::E::zot", "... got the right &zot method" ); |
203 | is( Class::E->new->bar, "Role::H::bar", "... got the right &bar method" ); |
204 | is( Class::E->new->xxy, "Role::J::xxy", "... got the right &xxy method" ); |
4c15a12d |
205 | |
c4538447 |
206 | ok(Role::I->meta->requires_method('foo'), '... Role::I still have the &foo requirement'); |
207 | |
208 | { |
f3e76c8f |
209 | lives_ok { |
210 | package Class::D; |
211 | use Moose; |
fb1e11d5 |
212 | |
f3e76c8f |
213 | has foo => ( default => __PACKAGE__ . "::foo", is => "rw" ); |
fb1e11d5 |
214 | |
f3e76c8f |
215 | sub zot { 'Class::D::zot' } |
fb1e11d5 |
216 | |
f6bee6fe |
217 | with qw(Role::I); |
fb1e11d5 |
218 | |
f3e76c8f |
219 | } "resolved with attr"; |
fb1e11d5 |
220 | |
f3e76c8f |
221 | can_ok( Class::D->new, qw(foo bar xxy zot) ); |
222 | is( eval { Class::D->new->bar }, "Role::H::bar", "bar" ); |
223 | is( eval { Class::D->new->zzy }, "Role::I::zzy", "zzy" ); |
4c15a12d |
224 | |
225 | is( eval { Class::D->new->foo }, "Class::D::foo", "foo" ); |
226 | is( eval { Class::D->new->zot }, "Class::D::zot", "zot" ); |
4c15a12d |
227 | |
4c15a12d |
228 | } |
229 | |