Commit | Line | Data |
5d5c86d9 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
fa91a1c7 |
6 | use Test::More tests => 11; |
5d5c86d9 |
7 | |
8 | BEGIN { |
9 | use lib 'opt', '../opt', '..'; |
10 | use_ok('c3'); |
11 | # uncomment this line, and re-run the |
12 | # test to see the normal p5 dispatch order |
13 | #$Class::C3::TURN_OFF_C3 = 1; |
14 | } |
15 | |
16 | =pod |
17 | |
18 | This tests the classic diamond inheritence pattern. |
19 | |
20 | <A> |
21 | / \ |
22 | <B> <C> |
23 | \ / |
24 | <D> |
25 | |
26 | =cut |
27 | |
28 | { |
29 | package Diamond_A; |
30 | use c3; |
31 | sub bar { 'Diamond_A::bar' } |
32 | sub baz { 'Diamond_A::baz' } |
33 | } |
34 | { |
35 | package Diamond_B; |
36 | use base 'Diamond_A'; |
37 | use c3; |
38 | sub baz { 'Diamond_B::baz => ' . (shift)->next::method() } |
39 | } |
40 | { |
41 | package Diamond_C; |
42 | use c3; |
43 | use base 'Diamond_A'; |
44 | sub foo { 'Diamond_C::foo' } |
fa91a1c7 |
45 | sub buz { 'Diamond_C::buz' } |
46 | |
47 | sub woz { 'Diamond_C::woz' } |
48 | sub maybe { 'Diamond_C::maybe' } |
5d5c86d9 |
49 | } |
50 | { |
51 | package Diamond_D; |
52 | use base ('Diamond_B', 'Diamond_C'); |
53 | use c3; |
54 | sub foo { 'Diamond_D::foo => ' . (shift)->next::method() } |
55 | sub bar { 'Diamond_D::bar => ' . (shift)->next::method() } |
56 | sub buz { 'Diamond_D::buz => ' . (shift)->baz() } |
fa91a1c7 |
57 | sub fuz { 'Diamond_D::fuz => ' . (shift)->next::method() } |
58 | |
59 | sub woz { 'Diamond_D::woz can => ' . ((shift)->next::can() ? 1 : 0) } |
60 | sub noz { 'Diamond_D::noz can => ' . ((shift)->next::can() ? 1 : 0) } |
61 | |
62 | sub maybe { 'Diamond_D::maybe => ' . ((shift)->maybe::next::method() || 0) } |
63 | sub moybe { 'Diamond_D::moybe => ' . ((shift)->maybe::next::method() || 0) } |
5d5c86d9 |
64 | |
65 | } |
66 | |
2ffffc6d |
67 | Class::C3::initialize(); |
68 | |
5d5c86d9 |
69 | is_deeply( |
70 | [ Class::C3::calculateMRO('Diamond_D') ], |
71 | [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ], |
72 | '... got the right MRO for Diamond_D'); |
73 | |
74 | is(Diamond_D->foo, 'Diamond_D::foo => Diamond_C::foo', '... skipped B and went to C correctly'); |
75 | is(Diamond_D->bar, 'Diamond_D::bar => Diamond_A::bar', '... skipped B & C and went to A correctly'); |
76 | is(Diamond_D->baz, 'Diamond_B::baz => Diamond_A::baz', '... called B method, skipped C and went to A correctly'); |
77 | is(Diamond_D->buz, 'Diamond_D::buz => Diamond_B::baz => Diamond_A::baz', '... called D method dispatched to , different method correctly'); |
78 | eval { Diamond_D->fuz }; |
79 | like($@, qr/^No next::method 'fuz' found for Diamond_D/, '... cannot re-dispatch to a method which is not there'); |
80 | |
322a5920 |
81 | is(Diamond_D->woz, 'Diamond_D::woz can => 1', '... can re-dispatch figured out correctly'); |
82 | is(Diamond_D->noz, 'Diamond_D::noz can => 0', '... cannot re-dispatch figured out correctly'); |
fa91a1c7 |
83 | |
84 | is(Diamond_D->maybe, 'Diamond_D::maybe => Diamond_C::maybe', '... redispatched D to C when it exists'); |
85 | is(Diamond_D->moybe, 'Diamond_D::moybe => 0', '... quietly failed redispatch from D'); |