Commit | Line | Data |
---|---|---|
e1a479c5 BB |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
c94dd5be | 6 | require q(./test.pl); plan(tests => 10); |
e1a479c5 BB |
7 | |
8 | =pod | |
9 | ||
93f09d7b | 10 | This tests the classic diamond inheritance pattern. |
e1a479c5 BB |
11 | |
12 | <A> | |
13 | / \ | |
14 | <B> <C> | |
15 | \ / | |
16 | <D> | |
17 | ||
18 | =cut | |
19 | ||
20 | { | |
21 | package Diamond_A; | |
22 | use mro 'c3'; | |
23 | sub bar { 'Diamond_A::bar' } | |
24 | sub baz { 'Diamond_A::baz' } | |
25 | } | |
26 | { | |
27 | package Diamond_B; | |
28 | use base 'Diamond_A'; | |
29 | use mro 'c3'; | |
30 | sub baz { 'Diamond_B::baz => ' . (shift)->next::method() } | |
31 | } | |
32 | { | |
33 | package Diamond_C; | |
34 | use mro 'c3'; | |
35 | use base 'Diamond_A'; | |
36 | sub foo { 'Diamond_C::foo' } | |
37 | sub buz { 'Diamond_C::buz' } | |
38 | ||
39 | sub woz { 'Diamond_C::woz' } | |
40 | sub maybe { 'Diamond_C::maybe' } | |
41 | } | |
42 | { | |
43 | package Diamond_D; | |
44 | use base ('Diamond_B', 'Diamond_C'); | |
45 | use mro 'c3'; | |
46 | sub foo { 'Diamond_D::foo => ' . (shift)->next::method() } | |
47 | sub bar { 'Diamond_D::bar => ' . (shift)->next::method() } | |
48 | sub buz { 'Diamond_D::buz => ' . (shift)->baz() } | |
49 | sub fuz { 'Diamond_D::fuz => ' . (shift)->next::method() } | |
50 | ||
51 | sub woz { 'Diamond_D::woz can => ' . ((shift)->next::can() ? 1 : 0) } | |
52 | sub noz { 'Diamond_D::noz can => ' . ((shift)->next::can() ? 1 : 0) } | |
53 | ||
54 | sub maybe { 'Diamond_D::maybe => ' . ((shift)->maybe::next::method() || 0) } | |
55 | sub moybe { 'Diamond_D::moybe => ' . ((shift)->maybe::next::method() || 0) } | |
56 | ||
57 | } | |
58 | ||
c94dd5be | 59 | ok(eq_array( |
e1a479c5 | 60 | mro::get_linear_isa('Diamond_D'), |
c94dd5be RGS |
61 | [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ] |
62 | ), '... got the right MRO for Diamond_D'); | |
e1a479c5 BB |
63 | |
64 | is(Diamond_D->foo, 'Diamond_D::foo => Diamond_C::foo', '... skipped B and went to C correctly'); | |
65 | is(Diamond_D->bar, 'Diamond_D::bar => Diamond_A::bar', '... skipped B & C and went to A correctly'); | |
66 | is(Diamond_D->baz, 'Diamond_B::baz => Diamond_A::baz', '... called B method, skipped C and went to A correctly'); | |
67 | is(Diamond_D->buz, 'Diamond_D::buz => Diamond_B::baz => Diamond_A::baz', '... called D method dispatched to , different method correctly'); | |
68 | eval { Diamond_D->fuz }; | |
69 | like($@, qr/^No next::method 'fuz' found for Diamond_D/, '... cannot re-dispatch to a method which is not there'); | |
70 | ||
71 | is(Diamond_D->woz, 'Diamond_D::woz can => 1', '... can re-dispatch figured out correctly'); | |
72 | is(Diamond_D->noz, 'Diamond_D::noz can => 0', '... cannot re-dispatch figured out correctly'); | |
73 | ||
74 | is(Diamond_D->maybe, 'Diamond_D::maybe => Diamond_C::maybe', '... redispatched D to C when it exists'); | |
75 | is(Diamond_D->moybe, 'Diamond_D::moybe => 0', '... quietly failed redispatch from D'); |