Commit | Line | Data |
db1ab48d |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6549b0d1 |
6 | use Test::More tests => 87; # it's really 124 with kolibrie's tests; |
db1ab48d |
7 | use Test::Exception; |
8 | |
db1ab48d |
9 | =pod |
10 | |
11 | Mutually recursive roles. |
12 | |
13 | =cut |
14 | |
15 | { |
16 | package Role::Foo; |
db1ab48d |
17 | use Moose::Role; |
18 | |
19 | requires 'foo'; |
20 | |
21 | sub bar { 'Role::Foo::bar' } |
22 | |
23 | package Role::Bar; |
db1ab48d |
24 | use Moose::Role; |
25 | |
26 | requires 'bar'; |
27 | |
28 | sub foo { 'Role::Bar::foo' } |
29 | } |
30 | |
31 | { |
32 | package My::Test1; |
db1ab48d |
33 | use Moose; |
34 | |
35 | ::lives_ok { |
36 | with 'Role::Foo', 'Role::Bar'; |
37 | } '... our mutually recursive roles combine okay'; |
38 | |
39 | package My::Test2; |
db1ab48d |
40 | use Moose; |
41 | |
42 | ::lives_ok { |
43 | with 'Role::Bar', 'Role::Foo'; |
44 | } '... our mutually recursive roles combine okay (no matter what order)'; |
45 | } |
46 | |
47 | my $test1 = My::Test1->new; |
48 | isa_ok($test1, 'My::Test1'); |
49 | |
50 | ok($test1->does('Role::Foo'), '... $test1 does Role::Foo'); |
51 | ok($test1->does('Role::Bar'), '... $test1 does Role::Bar'); |
52 | |
53 | can_ok($test1, 'foo'); |
54 | can_ok($test1, 'bar'); |
55 | |
56 | is($test1->foo, 'Role::Bar::foo', '... $test1->foo worked'); |
57 | is($test1->bar, 'Role::Foo::bar', '... $test1->bar worked'); |
58 | |
59 | my $test2 = My::Test2->new; |
60 | isa_ok($test2, 'My::Test2'); |
61 | |
62 | ok($test2->does('Role::Foo'), '... $test2 does Role::Foo'); |
63 | ok($test2->does('Role::Bar'), '... $test2 does Role::Bar'); |
64 | |
65 | can_ok($test2, 'foo'); |
66 | can_ok($test2, 'bar'); |
67 | |
68 | is($test2->foo, 'Role::Bar::foo', '... $test2->foo worked'); |
69 | is($test2->bar, 'Role::Foo::bar', '... $test2->bar worked'); |
70 | |
71 | # check some meta-stuff |
72 | |
73 | ok(Role::Foo->meta->has_method('bar'), '... it still has the bar method'); |
74 | ok(Role::Foo->meta->requires_method('foo'), '... it still has the required foo method'); |
75 | |
76 | ok(Role::Bar->meta->has_method('foo'), '... it still has the foo method'); |
77 | ok(Role::Bar->meta->requires_method('bar'), '... it still has the required bar method'); |
78 | |
79 | =pod |
80 | |
81 | Role method conflicts |
82 | |
83 | =cut |
84 | |
85 | { |
86 | package Role::Bling; |
db1ab48d |
87 | use Moose::Role; |
88 | |
89 | sub bling { 'Role::Bling::bling' } |
90 | |
91 | package Role::Bling::Bling; |
db1ab48d |
92 | use Moose::Role; |
93 | |
94 | sub bling { 'Role::Bling::Bling::bling' } |
95 | } |
96 | |
97 | { |
98 | package My::Test3; |
db1ab48d |
99 | use Moose; |
100 | |
101 | ::throws_ok { |
102 | with 'Role::Bling', 'Role::Bling::Bling'; |
103 | } qr/requires the method \'bling\' to be implemented/, '... role methods conflicted and method was required'; |
104 | |
105 | package My::Test4; |
db1ab48d |
106 | use Moose; |
107 | |
108 | ::lives_ok { |
109 | with 'Role::Bling'; |
110 | with 'Role::Bling::Bling'; |
111 | } '... role methods didnt conflict when manually combined'; |
112 | |
113 | package My::Test5; |
db1ab48d |
114 | use Moose; |
115 | |
116 | ::lives_ok { |
117 | with 'Role::Bling::Bling'; |
118 | with 'Role::Bling'; |
119 | } '... role methods didnt conflict when manually combined (in opposite order)'; |
120 | |
121 | package My::Test6; |
db1ab48d |
122 | use Moose; |
123 | |
124 | ::lives_ok { |
125 | with 'Role::Bling::Bling', 'Role::Bling'; |
126 | } '... role methods didnt conflict when manually resolved'; |
127 | |
128 | sub bling { 'My::Test6::bling' } |
129 | } |
130 | |
131 | ok(!My::Test3->meta->has_method('bling'), '... we didnt get any methods in the conflict'); |
132 | ok(My::Test4->meta->has_method('bling'), '... we did get the method when manually dealt with'); |
133 | ok(My::Test5->meta->has_method('bling'), '... we did get the method when manually dealt with'); |
134 | ok(My::Test6->meta->has_method('bling'), '... we did get the method when manually dealt with'); |
135 | |
d79e62fd |
136 | ok(!My::Test3->does('Role::Bling'), '... our class does() the correct roles'); |
137 | ok(!My::Test3->does('Role::Bling::Bling'), '... our class does() the correct roles'); |
138 | ok(My::Test4->does('Role::Bling'), '... our class does() the correct roles'); |
139 | ok(My::Test4->does('Role::Bling::Bling'), '... our class does() the correct roles'); |
140 | ok(My::Test5->does('Role::Bling'), '... our class does() the correct roles'); |
141 | ok(My::Test5->does('Role::Bling::Bling'), '... our class does() the correct roles'); |
142 | ok(My::Test6->does('Role::Bling'), '... our class does() the correct roles'); |
143 | ok(My::Test6->does('Role::Bling::Bling'), '... our class does() the correct roles'); |
144 | |
db1ab48d |
145 | is(My::Test4->bling, 'Role::Bling::bling', '... and we got the first method that was added'); |
146 | is(My::Test5->bling, 'Role::Bling::Bling::bling', '... and we got the first method that was added'); |
147 | is(My::Test6->bling, 'My::Test6::bling', '... and we got the local method'); |
148 | |
149 | # check how this affects role compostion |
150 | |
151 | { |
152 | package Role::Bling::Bling::Bling; |
db1ab48d |
153 | use Moose::Role; |
154 | |
155 | with 'Role::Bling::Bling'; |
156 | |
157 | sub bling { 'Role::Bling::Bling::Bling::bling' } |
158 | } |
159 | |
160 | ok(Role::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling'); |
d79e62fd |
161 | ok(Role::Bling::Bling->meta->does_role('Role::Bling::Bling'), '... our role correctly does() the other role'); |
fb1e11d5 |
162 | ok(Role::Bling::Bling::Bling->meta->has_method('bling'), '... dont have the bling method in Role::Bling::Bling::Bling'); |
d05cd563 |
163 | is(Role::Bling::Bling::Bling->meta->get_method('bling')->(), |
164 | 'Role::Bling::Bling::Bling::bling', |
165 | '... still got the bling method in Role::Bling::Bling::Bling'); |
db1ab48d |
166 | |
fb1e11d5 |
167 | |
db1ab48d |
168 | =pod |
169 | |
170 | Role attribute conflicts |
171 | |
172 | =cut |
173 | |
174 | { |
175 | package Role::Boo; |
db1ab48d |
176 | use Moose::Role; |
177 | |
178 | has 'ghost' => (is => 'ro', default => 'Role::Boo::ghost'); |
179 | |
180 | package Role::Boo::Hoo; |
db1ab48d |
181 | use Moose::Role; |
182 | |
183 | has 'ghost' => (is => 'ro', default => 'Role::Boo::Hoo::ghost'); |
184 | } |
185 | |
186 | { |
187 | package My::Test7; |
db1ab48d |
188 | use Moose; |
189 | |
190 | ::throws_ok { |
191 | with 'Role::Boo', 'Role::Boo::Hoo'; |
fb1e11d5 |
192 | } qr/We have encountered an attribute conflict/, |
db1ab48d |
193 | '... role attrs conflicted and method was required'; |
194 | |
195 | package My::Test8; |
db1ab48d |
196 | use Moose; |
197 | |
198 | ::lives_ok { |
199 | with 'Role::Boo'; |
200 | with 'Role::Boo::Hoo'; |
201 | } '... role attrs didnt conflict when manually combined'; |
202 | |
203 | package My::Test9; |
db1ab48d |
204 | use Moose; |
205 | |
206 | ::lives_ok { |
207 | with 'Role::Boo::Hoo'; |
208 | with 'Role::Boo'; |
209 | } '... role attrs didnt conflict when manually combined'; |
210 | |
211 | package My::Test10; |
db1ab48d |
212 | use Moose; |
213 | |
214 | has 'ghost' => (is => 'ro', default => 'My::Test10::ghost'); |
215 | |
216 | ::throws_ok { |
217 | with 'Role::Boo', 'Role::Boo::Hoo'; |
fb1e11d5 |
218 | } qr/We have encountered an attribute conflict/, |
db1ab48d |
219 | '... role attrs conflicted and cannot be manually disambiguted'; |
220 | |
221 | } |
222 | |
223 | ok(!My::Test7->meta->has_attribute('ghost'), '... we didnt get any attributes in the conflict'); |
224 | ok(My::Test8->meta->has_attribute('ghost'), '... we did get an attributes when manually composed'); |
225 | ok(My::Test9->meta->has_attribute('ghost'), '... we did get an attributes when manually composed'); |
226 | ok(My::Test10->meta->has_attribute('ghost'), '... we did still have an attribute ghost (conflict does not mess with class)'); |
227 | |
d79e62fd |
228 | ok(!My::Test7->does('Role::Boo'), '... our class does() the correct roles'); |
229 | ok(!My::Test7->does('Role::Boo::Hoo'), '... our class does() the correct roles'); |
230 | ok(My::Test8->does('Role::Boo'), '... our class does() the correct roles'); |
231 | ok(My::Test8->does('Role::Boo::Hoo'), '... our class does() the correct roles'); |
232 | ok(My::Test9->does('Role::Boo'), '... our class does() the correct roles'); |
233 | ok(My::Test9->does('Role::Boo::Hoo'), '... our class does() the correct roles'); |
234 | ok(!My::Test10->does('Role::Boo'), '... our class does() the correct roles'); |
235 | ok(!My::Test10->does('Role::Boo::Hoo'), '... our class does() the correct roles'); |
236 | |
db1ab48d |
237 | can_ok('My::Test8', 'ghost'); |
238 | can_ok('My::Test9', 'ghost'); |
239 | can_ok('My::Test10', 'ghost'); |
240 | |
241 | is(My::Test8->new->ghost, 'Role::Boo::ghost', '... got the expected default attr value'); |
242 | is(My::Test9->new->ghost, 'Role::Boo::Hoo::ghost', '... got the expected default attr value'); |
243 | is(My::Test10->new->ghost, 'My::Test10::ghost', '... got the expected default attr value'); |
244 | |
d05cd563 |
245 | =pod |
246 | |
247 | Role override method conflicts |
248 | |
249 | =cut |
250 | |
0558683c |
251 | { |
252 | package Role::Plot; |
253 | use Moose::Role; |
254 | |
255 | override 'twist' => sub { |
256 | super() . ' -> Role::Plot::twist'; |
257 | }; |
258 | |
259 | package Role::Truth; |
260 | use Moose::Role; |
261 | |
262 | override 'twist' => sub { |
263 | super() . ' -> Role::Truth::twist'; |
264 | }; |
265 | } |
266 | |
267 | { |
268 | package My::Test::Base; |
269 | use Moose; |
270 | |
271 | sub twist { 'My::Test::Base::twist' } |
272 | |
273 | package My::Test11; |
274 | use Moose; |
275 | |
276 | extends 'My::Test::Base'; |
277 | |
278 | ::lives_ok { |
279 | with 'Role::Truth'; |
280 | } '... composed the role with override okay'; |
281 | |
282 | package My::Test12; |
283 | use Moose; |
284 | |
285 | extends 'My::Test::Base'; |
286 | |
287 | ::lives_ok { |
288 | with 'Role::Plot'; |
289 | } '... composed the role with override okay'; |
290 | |
291 | package My::Test13; |
292 | use Moose; |
293 | |
294 | ::dies_ok { |
295 | with 'Role::Plot'; |
296 | } '... cannot compose it because we have no superclass'; |
297 | |
298 | package My::Test14; |
299 | use Moose; |
300 | |
301 | extends 'My::Test::Base'; |
302 | |
303 | ::throws_ok { |
304 | with 'Role::Plot', 'Role::Truth'; |
305 | } qr/Two \'override\' methods of the same name encountered/, |
306 | '... cannot compose it because we have no superclass'; |
307 | } |
308 | |
309 | ok(My::Test11->meta->has_method('twist'), '... the twist method has been added'); |
310 | ok(My::Test12->meta->has_method('twist'), '... the twist method has been added'); |
311 | ok(!My::Test13->meta->has_method('twist'), '... the twist method has not been added'); |
312 | ok(!My::Test14->meta->has_method('twist'), '... the twist method has not been added'); |
313 | |
314 | ok(!My::Test11->does('Role::Plot'), '... our class does() the correct roles'); |
315 | ok(My::Test11->does('Role::Truth'), '... our class does() the correct roles'); |
316 | ok(!My::Test12->does('Role::Truth'), '... our class does() the correct roles'); |
317 | ok(My::Test12->does('Role::Plot'), '... our class does() the correct roles'); |
318 | ok(!My::Test13->does('Role::Plot'), '... our class does() the correct roles'); |
319 | ok(!My::Test14->does('Role::Truth'), '... our class does() the correct roles'); |
320 | ok(!My::Test14->does('Role::Plot'), '... our class does() the correct roles'); |
321 | |
322 | is(My::Test11->twist(), 'My::Test::Base::twist -> Role::Truth::twist', '... got the right method return'); |
323 | is(My::Test12->twist(), 'My::Test::Base::twist -> Role::Plot::twist', '... got the right method return'); |
324 | ok(!My::Test13->can('twist'), '... no twist method here at all'); |
325 | is(My::Test14->twist(), 'My::Test::Base::twist', '... got the right method return (from superclass)'); |
326 | |
327 | { |
328 | package Role::Reality; |
329 | use Moose::Role; |
330 | |
331 | ::throws_ok { |
332 | with 'Role::Plot'; |
333 | } qr/A local method of the same name as been found/, |
334 | '... could not compose roles here, it dies'; |
335 | |
336 | sub twist { |
337 | 'Role::Reality::twist'; |
338 | } |
339 | } |
340 | |
341 | ok(Role::Reality->meta->has_method('twist'), '... the twist method has not been added'); |
fb1e11d5 |
342 | #ok(!Role::Reality->meta->does_role('Role::Plot'), '... our role does() the correct roles'); |
0558683c |
343 | is(Role::Reality->meta->get_method('twist')->(), |
344 | 'Role::Reality::twist', |
345 | '... the twist method returns the right value'); |
3c5fd53a |
346 | |
347 | =pod |
348 | |
349 | Role conflicts between attributes and methods |
350 | |
21716c07 |
351 | [15:23] <kolibrie> when class defines method and role defines method, class wins |
352 | [15:24] <kolibrie> when class 'has' method and role defines method, class wins |
353 | [15:24] <kolibrie> when class defines method and role 'has' method, role wins |
354 | [15:24] <kolibrie> when class 'has' method and role 'has' method, role wins |
6549b0d1 |
355 | [15:24] <kolibrie> which means when class 'has' method and two roles 'has' method, no tiebreak is detected |
21716c07 |
356 | [15:24] <perigrin> this is with role and has declaration in the exact same order in every case? |
357 | [15:25] <kolibrie> yes |
358 | [15:25] <perigrin> interesting |
359 | [15:25] <kolibrie> that's what I thought |
360 | [15:26] <kolibrie> does that sound like something I should write a test for? |
361 | [15:27] <perigrin> stevan, ping? |
362 | [15:27] <perigrin> I'm not sure what the right answer for composition is. |
363 | [15:27] <perigrin> who should win |
364 | [15:27] <perigrin> if I were to guess I'd say the class should always win. |
365 | [15:27] <kolibrie> that would be my guess, but I thought I would ask to make sure |
366 | [15:29] <stevan> kolibrie: please write a test |
367 | [15:29] <stevan> I am not exactly sure who should win either,.. but I suspect it is not working correctly right now |
368 | [15:29] <stevan> I know exactly why it is doing what it is doing though |
369 | |
370 | Now I have to decide actually what happens, and how to fix it. |
371 | - SL |
3c5fd53a |
372 | |
373 | { |
374 | package Role::Method; |
375 | use Moose::Role; |
376 | |
377 | sub ghost { 'Role::Method::ghost' } |
378 | |
379 | package Role::Method2; |
380 | use Moose::Role; |
381 | |
382 | sub ghost { 'Role::Method2::ghost' } |
383 | |
384 | package Role::Attribute; |
385 | use Moose::Role; |
386 | |
387 | has 'ghost' => (is => 'ro', default => 'Role::Attribute::ghost'); |
388 | |
389 | package Role::Attribute2; |
390 | use Moose::Role; |
391 | |
392 | has 'ghost' => (is => 'ro', default => 'Role::Attribute2::ghost'); |
393 | } |
394 | |
395 | { |
396 | package My::Test15; |
397 | use Moose; |
398 | |
399 | ::lives_ok { |
400 | with 'Role::Method'; |
401 | } '... composed the method role into the method class'; |
402 | |
403 | sub ghost { 'My::Test15::ghost' } |
404 | |
405 | package My::Test16; |
406 | use Moose; |
407 | |
408 | ::lives_ok { |
409 | with 'Role::Method'; |
410 | } '... composed the method role into the attribute class'; |
411 | |
412 | has 'ghost' => (is => 'ro', default => 'My::Test16::ghost'); |
413 | |
414 | package My::Test17; |
415 | use Moose; |
416 | |
417 | ::lives_ok { |
418 | with 'Role::Attribute'; |
419 | } '... composed the attribute role into the method class'; |
420 | |
421 | sub ghost { 'My::Test17::ghost' } |
422 | |
423 | package My::Test18; |
424 | use Moose; |
425 | |
426 | ::lives_ok { |
427 | with 'Role::Attribute'; |
428 | } '... composed the attribute role into the attribute class'; |
429 | |
430 | has 'ghost' => (is => 'ro', default => 'My::Test18::ghost'); |
431 | |
432 | package My::Test19; |
433 | use Moose; |
434 | |
435 | ::lives_ok { |
436 | with 'Role::Method', 'Role::Method2'; |
437 | } '... composed method roles into class with method tiebreaker'; |
438 | |
439 | sub ghost { 'My::Test19::ghost' } |
440 | |
441 | package My::Test20; |
442 | use Moose; |
443 | |
444 | ::lives_ok { |
445 | with 'Role::Method', 'Role::Method2'; |
446 | } '... composed method roles into class with attribute tiebreaker'; |
447 | |
448 | has 'ghost' => (is => 'ro', default => 'My::Test20::ghost'); |
449 | |
450 | package My::Test21; |
451 | use Moose; |
452 | |
453 | ::lives_ok { |
454 | with 'Role::Attribute', 'Role::Attribute2'; |
455 | } '... composed attribute roles into class with method tiebreaker'; |
456 | |
457 | sub ghost { 'My::Test21::ghost' } |
458 | |
459 | package My::Test22; |
460 | use Moose; |
461 | |
462 | ::lives_ok { |
463 | with 'Role::Attribute', 'Role::Attribute2'; |
464 | } '... composed attribute roles into class with attribute tiebreaker'; |
465 | |
466 | has 'ghost' => (is => 'ro', default => 'My::Test22::ghost'); |
467 | |
468 | package My::Test23; |
469 | use Moose; |
470 | |
471 | ::lives_ok { |
472 | with 'Role::Method', 'Role::Attribute'; |
473 | } '... composed method and attribute role into class with method tiebreaker'; |
474 | |
475 | sub ghost { 'My::Test23::ghost' } |
476 | |
477 | package My::Test24; |
478 | use Moose; |
479 | |
480 | ::lives_ok { |
481 | with 'Role::Method', 'Role::Attribute'; |
482 | } '... composed method and attribute role into class with attribute tiebreaker'; |
483 | |
484 | has 'ghost' => (is => 'ro', default => 'My::Test24::ghost'); |
485 | |
486 | package My::Test25; |
487 | use Moose; |
488 | |
489 | ::lives_ok { |
490 | with 'Role::Attribute', 'Role::Method'; |
491 | } '... composed attribute and method role into class with method tiebreaker'; |
492 | |
493 | sub ghost { 'My::Test25::ghost' } |
494 | |
495 | package My::Test26; |
496 | use Moose; |
497 | |
498 | ::lives_ok { |
499 | with 'Role::Attribute', 'Role::Method'; |
500 | } '... composed attribute and method role into class with attribute tiebreaker'; |
501 | |
502 | has 'ghost' => (is => 'ro', default => 'My::Test26::ghost'); |
503 | } |
504 | |
505 | my $test15 = My::Test15->new; |
506 | isa_ok($test15, 'My::Test15'); |
507 | is($test15->ghost, 'My::Test15::ghost', '... we access the method from the class and ignore the role method'); |
508 | |
509 | my $test16 = My::Test16->new; |
510 | isa_ok($test16, 'My::Test16'); |
511 | is($test16->ghost, 'My::Test16::ghost', '... we access the attribute from the class and ignore the role method'); |
512 | |
513 | my $test17 = My::Test17->new; |
514 | isa_ok($test17, 'My::Test17'); |
515 | is($test17->ghost, 'My::Test17::ghost', '... we access the method from the class and ignore the role attribute'); |
516 | |
517 | my $test18 = My::Test18->new; |
518 | isa_ok($test18, 'My::Test18'); |
519 | is($test18->ghost, 'My::Test18::ghost', '... we access the attribute from the class and ignore the role attribute'); |
520 | |
521 | my $test19 = My::Test19->new; |
522 | isa_ok($test19, 'My::Test19'); |
523 | is($test19->ghost, 'My::Test19::ghost', '... we access the method from the class and ignore the role methods'); |
524 | |
525 | my $test20 = My::Test20->new; |
526 | isa_ok($test20, 'My::Test20'); |
527 | is($test20->ghost, 'My::Test20::ghost', '... we access the attribute from the class and ignore the role methods'); |
528 | |
529 | my $test21 = My::Test21->new; |
530 | isa_ok($test21, 'My::Test21'); |
531 | is($test21->ghost, 'My::Test21::ghost', '... we access the method from the class and ignore the role attributes'); |
532 | |
533 | my $test22 = My::Test22->new; |
534 | isa_ok($test22, 'My::Test22'); |
535 | is($test22->ghost, 'My::Test22::ghost', '... we access the attribute from the class and ignore the role attributes'); |
536 | |
537 | my $test23 = My::Test23->new; |
538 | isa_ok($test23, 'My::Test23'); |
539 | is($test23->ghost, 'My::Test23::ghost', '... we access the method from the class and ignore the role method and attribute'); |
540 | |
541 | my $test24 = My::Test24->new; |
542 | isa_ok($test24, 'My::Test24'); |
543 | is($test24->ghost, 'My::Test24::ghost', '... we access the attribute from the class and ignore the role method and attribute'); |
544 | |
545 | my $test25 = My::Test25->new; |
546 | isa_ok($test25, 'My::Test25'); |
547 | is($test25->ghost, 'My::Test25::ghost', '... we access the method from the class and ignore the role attribute and method'); |
548 | |
549 | my $test26 = My::Test26->new; |
550 | isa_ok($test26, 'My::Test26'); |
551 | is($test26->ghost, 'My::Test26::ghost', '... we access the attribute from the class and ignore the role attribute and method'); |
552 | |
21716c07 |
553 | =cut |