5865612808ccb608f68a8319a92a7dcd30be20cf
[gitmo/Class-C3-XS.git] / t / 01_MRO.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 11;
7
8 BEGIN {
9     use_ok('Class::C3');
10     # uncomment this line, and re-run the
11     # test to see the normal p5 dispatch order
12     #$Class::C3::TURN_OFF_C3 = 1;    
13 }
14
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
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 Class::C3::initialize();
51
52
53 is_deeply(
54     [ Class::C3::calculateMRO('Diamond_D') ],
55     [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ],
56     '... got the right MRO for Diamond_D');
57
58 is(Diamond_D->hello, 'Diamond_C::hello', '... method resolved itself as expected');
59
60 is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected');
61 is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected');
62
63 # now undo the C3
64 Class::C3::uninitialize();
65
66 is(Diamond_D->hello, 'Diamond_A::hello', '... old method resolution has been restored');
67
68 is(Diamond_D->can('hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored');
69 is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored');
70
71 Class::C3::initialize();
72
73 is(Diamond_D->hello, 'Diamond_C::hello', '... C3 method restored itself as expected');
74
75 is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected');
76 is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected');