Commit | Line | Data |
95bebf8c |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
d0e2efe5 |
6 | use Test::More tests => 11; |
95bebf8c |
7 | |
8 | BEGIN { |
9 | use_ok('Class::C3'); |
d401eda1 |
10 | # uncomment this line, and re-run the |
11 | # test to see the normal p5 dispatch order |
12 | #$Class::C3::TURN_OFF_C3 = 1; |
95bebf8c |
13 | } |
14 | |
d401eda1 |
15 | =pod |
16 | |
17 | This tests the classic diamond inheritence pattern. |
18 | |
19 | <A> |
20 | / \ |
21 | <B> <C> |
22 | \ / |
23 | <D> |
24 | |
25 | =cut |
26 | |
95bebf8c |
27 | { |
28 | package Diamond_A; |
29 | use Class::C3; |
30 | sub hello { 'Diamond_A::hello' } |
31 | } |
32 | { |
33 | package Diamond_B; |
34 | use base 'Diamond_A'; |
35 | use Class::C3; |
36 | } |
37 | { |
38 | package Diamond_C; |
39 | use Class::C3; |
40 | use base 'Diamond_A'; |
41 | |
42 | sub hello { 'Diamond_C::hello' } |
43 | } |
44 | { |
45 | package Diamond_D; |
46 | use base ('Diamond_B', 'Diamond_C'); |
47 | use Class::C3; |
48 | } |
49 | |
50 | is_deeply( |
51 | [ Class::C3::calculateMRO('Diamond_D') ], |
52 | [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ], |
53 | '... got the right MRO for Diamond_D'); |
54 | |
55 | is(Diamond_D->hello, 'Diamond_C::hello', '... method resolved itself as expected'); |
d0e2efe5 |
56 | |
95bebf8c |
57 | is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected'); |
d0e2efe5 |
58 | is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected'); |
59 | |
60 | # now undo the C3 |
61 | Class::C3::uninitialize(); |
62 | |
63 | is(Diamond_D->hello, 'Diamond_A::hello', '... old method resolution has been restored'); |
64 | |
65 | is(Diamond_D->can('hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored'); |
66 | is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored'); |
67 | |
68 | Class::C3::initialize(); |
69 | |
70 | is(Diamond_D->hello, 'Diamond_C::hello', '... C3 method restored itself as expected'); |
95bebf8c |
71 | |
d0e2efe5 |
72 | is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected'); |
73 | is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected'); |