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