adding the some preliminary junk
[gitmo/Class-C3-XS.git] / t / 30_next_method.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 6;
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 hello { 'Diamond_A::hello' }
32     sub foo { 'Diamond_A::foo' }       
33 }
34 {
35     package Diamond_B;
36     use base 'Diamond_A';
37     use c3;     
38     sub foo { 'Diamond_B::foo => ' . (shift)->next::method() }       
39 }
40 {
41     package Diamond_C;
42     use c3;    
43     use base 'Diamond_A';     
44
45     sub hello { 'Diamond_C::hello => ' . (shift)->next::method() }
46     sub foo { 'Diamond_C::foo => ' . (shift)->next::method() }   
47 }
48 {
49     package Diamond_D;
50     use base ('Diamond_B', 'Diamond_C');
51     use c3; 
52     
53     sub foo { 'Diamond_D::foo => ' . (shift)->next::method() }   
54 }
55
56 Class::C3::initialize();
57
58 is_deeply(
59     [ Class::C3::calculateMRO('Diamond_D') ],
60     [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ],
61     '... got the right MRO for Diamond_D');
62
63 is(Diamond_D->hello, 'Diamond_C::hello => Diamond_A::hello', '... method resolved itself as expected');
64
65 is(Diamond_D->can('hello')->('Diamond_D'), 
66    'Diamond_C::hello => Diamond_A::hello', 
67    '... can(method) resolved itself as expected');
68    
69 is(UNIVERSAL::can("Diamond_D", 'hello')->('Diamond_D'), 
70    'Diamond_C::hello => Diamond_A::hello', 
71    '... can(method) resolved itself as expected');
72
73 is(Diamond_D->foo, 
74     'Diamond_D::foo => Diamond_B::foo => Diamond_C::foo => Diamond_A::foo', 
75     '... method foo resolved itself as expected');