0.02 release
[gitmo/Class-C3.git] / t / 20_reinitialize.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 5;
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 Start with this:
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     sub hello { 'Diamond_C::hello' }
42 }
43 {
44     package Diamond_D;
45     use base ('Diamond_B', 'Diamond_C');
46     use Class::C3;    
47 }
48
49 is_deeply(
50     [ Class::C3::calculateMRO('Diamond_D') ],
51     [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ],
52     '... got the right MRO for Diamond_D');
53
54 =pod
55
56 Then change it to this:
57
58 <E>   <A>
59   \  /   \
60    <B>   <C>
61      \   /
62       <D>
63
64 =cut
65
66 {
67     package Diamond_E;
68     use Class::C3;      
69     sub hello { 'Diamond_E::hello' }      
70 }
71
72 {
73     no strict 'refs';
74     unshift @{"Diamond_B::ISA"} => 'Diamond_E';
75 }
76
77 is_deeply(
78     [ Class::C3::calculateMRO('Diamond_D') ],
79     [ qw(Diamond_D Diamond_B Diamond_E Diamond_C Diamond_A) ],
80     '... got the new MRO for Diamond_D');
81
82 is(Diamond_D->hello, 'Diamond_C::hello', '... method still resolves with old MRO');
83
84 Class::C3::reinitialize();
85
86 is(Diamond_D->hello, 'Diamond_E::hello', '... method resolves with reinitialized MRO');