0.02 release
[gitmo/Class-C3.git] / t / 01_MRO.t
CommitLineData
95bebf8c 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
d0e2efe5 6use Test::More tests => 11;
95bebf8c 7
8BEGIN {
9 use_ok('Class::C3');
d401eda1 10 # uncomment this line, and re-run the
11 # test to see the normal p5 dispatch order
12 #$Class::C3::TURN_OFF_C3 = 1;
95bebf8c 13}
14
d401eda1 15=pod
16
17This tests the classic diamond inheritence pattern.
18
19 <A>
20 / \
21<B> <C>
22 \ /
23 <D>
24
25=cut
26
95bebf8c 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
50is_deeply(
51 [ Class::C3::calculateMRO('Diamond_D') ],
52 [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ],
53 '... got the right MRO for Diamond_D');
54
55is(Diamond_D->hello, 'Diamond_C::hello', '... method resolved itself as expected');
d0e2efe5 56
95bebf8c 57is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected');
d0e2efe5 58is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected');
59
60# now undo the C3
61Class::C3::uninitialize();
62
63is(Diamond_D->hello, 'Diamond_A::hello', '... old method resolution has been restored');
64
65is(Diamond_D->can('hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored');
66is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored');
67
68Class::C3::initialize();
69
70is(Diamond_D->hello, 'Diamond_C::hello', '... C3 method restored itself as expected');
95bebf8c 71
d0e2efe5 72is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected');
73is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected');