X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F03_MRO.t;fp=t%2F03_MRO.t;h=a13294fa9289ef6dbd6c98bd0ea614621b462ca1;hb=8995e8271e0f7f7b9c0942a4425e8a44099bf608;hp=0000000000000000000000000000000000000000;hpb=7cf94227b3d924007404bc5968c7362135d299f8;p=gitmo%2FClass-C3-XS.git diff --git a/t/03_MRO.t b/t/03_MRO.t new file mode 100644 index 0000000..a13294f --- /dev/null +++ b/t/03_MRO.t @@ -0,0 +1,117 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 8; + +BEGIN { + use_ok('Class::C3'); + # uncomment this line, and re-run the + # test to see the normal p5 dispatch order + #$Class::C3::TURN_OFF_C3 = 1; +} + +=pod + + +This example is take from: http://www.python.org/2.3/mro.html + +"My second example" +class O: pass +class F(O): pass +class E(O): pass +class D(O): pass +class C(D,F): pass +class B(E,D): pass +class A(B,C): pass + + 6 + --- +Level 3 | O | + / --- \ + / | \ + / | \ + / | \ + --- --- --- +Level 2 2 | E | 4 | D | | F | 5 + --- --- --- + \ / \ / + \ / \ / + \ / \ / + --- --- +Level 1 1 | B | | C | 3 + --- --- + \ / + \ / + --- +Level 0 0 | A | + --- + +>>> A.mro() +(, , , +, , , +) + +=cut + +{ + package Test::O; + use Class::C3; + + sub O_or_D { 'Test::O' } + sub O_or_F { 'Test::O' } + + package Test::F; + use base 'Test::O'; + use Class::C3; + + sub O_or_F { 'Test::F' } + + package Test::E; + use base 'Test::O'; + use Class::C3; + + package Test::D; + use base 'Test::O'; + use Class::C3; + + sub O_or_D { 'Test::D' } + sub C_or_D { 'Test::D' } + + package Test::C; + use base ('Test::D', 'Test::F'); + use Class::C3; + + sub C_or_D { 'Test::C' } + + package Test::B; + use base ('Test::E', 'Test::D'); + use Class::C3; + + package Test::A; + use base ('Test::B', 'Test::C'); + use Class::C3; +} + +Class::C3::initialize(); + +is_deeply( + [ Class::C3::calculateMRO('Test::A') ], + [ qw(Test::A Test::B Test::E Test::C Test::D Test::F Test::O) ], + '... got the right MRO for Test::A'); + +is(Test::A->O_or_D, 'Test::D', '... got the right method dispatch'); +is(Test::A->O_or_F, 'Test::F', '... got the right method dispatch'); + +# NOTE: +# this test is particularly interesting because the p5 dispatch +# would actually call Test::D before Test::C and Test::D is a +# subclass of Test::C +is(Test::A->C_or_D, 'Test::C', '... got the right method dispatch'); + +Class::C3::uninitialize(); + +is(Test::A->O_or_D, 'Test::O', '... old dispatch order is restored'); +is(Test::A->O_or_F, 'Test::O', '... old dispatch order is restored'); +is(Test::A->C_or_D, 'Test::D', '... old dispatch order is restored');