adding the some preliminary junk
[gitmo/Class-C3-XS.git] / t / 01_MRO.t
CommitLineData
8995e827 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 11;
7
8BEGIN {
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
17This tests the classic diamond inheritence pattern.
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
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
50Class::C3::initialize();
51
52
53is_deeply(
54 [ Class::C3::calculateMRO('Diamond_D') ],
55 [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ],
56 '... got the right MRO for Diamond_D');
57
58is(Diamond_D->hello, 'Diamond_C::hello', '... method resolved itself as expected');
59
60is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected');
61is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected');
62
63# now undo the C3
64Class::C3::uninitialize();
65
66is(Diamond_D->hello, 'Diamond_A::hello', '... old method resolution has been restored');
67
68is(Diamond_D->can('hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored');
69is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored');
70
71Class::C3::initialize();
72
73is(Diamond_D->hello, 'Diamond_C::hello', '... C3 method restored itself as expected');
74
75is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected');
76is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected');