Add another MOOSE_TEST_MD option, MooseX
[gitmo/Moose.git] / t / cmop / advanced_methods.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Fatal;
6
7 use Class::MOP;
8 use Class::MOP::Class;
9
10 =pod
11
12 The following class hierarhcy is very contrived
13 and totally horrid (it won't work under C3 even),
14 but it tests a number of aspect of this module.
15
16 A more real-world example would be a nice addition :)
17
18 =cut
19
20 {
21     package Foo;
22
23     sub BUILD { 'Foo::BUILD' }
24     sub foo { 'Foo::foo' }
25
26     package Bar;
27     our @ISA = ('Foo');
28
29     sub BUILD { 'Bar::BUILD' }
30     sub bar { 'Bar::bar' }
31
32     package Baz;
33     our @ISA = ('Bar');
34
35     sub baz { 'Baz::baz' }
36     sub foo { 'Baz::foo' }
37
38     package Foo::Bar;
39     our @ISA = ('Foo', 'Bar');
40
41     sub BUILD { 'Foo::Bar::BUILD' }
42     sub foobar { 'Foo::Bar::foobar' }
43
44     package Foo::Bar::Baz;
45     our @ISA = ('Foo', 'Bar', 'Baz');
46
47     sub BUILD { 'Foo::Bar::Baz::BUILD' }
48     sub bar { 'Foo::Bar::Baz::bar' }
49     sub foobarbaz { 'Foo::Bar::Baz::foobarbaz' }
50 }
51
52 ok(!defined(Class::MOP::Class->initialize('Foo')->find_next_method_by_name('BUILD')),
53    '... Foo::BUILD has not next method');
54
55 is(Class::MOP::Class->initialize('Bar')->find_next_method_by_name('BUILD'),
56    Class::MOP::Class->initialize('Foo')->get_method('BUILD'),
57    '... Bar::BUILD does have a next method');
58
59 is(Class::MOP::Class->initialize('Baz')->find_next_method_by_name('BUILD'),
60    Class::MOP::Class->initialize('Bar')->get_method('BUILD'),
61    '... Baz->BUILD does have a next method');
62
63 is(Class::MOP::Class->initialize('Foo::Bar')->find_next_method_by_name('BUILD'),
64    Class::MOP::Class->initialize('Foo')->get_method('BUILD'),
65    '... Foo::Bar->BUILD does have a next method');
66
67 is(Class::MOP::Class->initialize('Foo::Bar::Baz')->find_next_method_by_name('BUILD'),
68    Class::MOP::Class->initialize('Foo')->get_method('BUILD'),
69    '... Foo::Bar::Baz->BUILD does have a next method');
70
71 is_deeply(
72     [
73         sort { $a->name cmp $b->name }
74             grep { $_->package_name ne 'UNIVERSAL' }
75             Class::MOP::Class->initialize('Foo')->get_all_methods()
76     ],
77     [
78         Class::MOP::Class->initialize('Foo')->get_method('BUILD') ,
79         Class::MOP::Class->initialize('Foo')->get_method('foo'),
80     ],
81     '... got the right list of applicable methods for Foo');
82
83 is_deeply(
84     [
85         sort { $a->name cmp $b->name }
86             grep { $_->package_name ne 'UNIVERSAL' }
87             Class::MOP::Class->initialize('Bar')->get_all_methods()
88     ],
89     [
90         Class::MOP::Class->initialize('Bar')->get_method('BUILD'),
91         Class::MOP::Class->initialize('Bar')->get_method('bar'),
92         Class::MOP::Class->initialize('Foo')->get_method('foo'),
93     ],
94     '... got the right list of applicable methods for Bar');
95
96
97 is_deeply(
98     [
99         sort { $a->name cmp $b->name }
100             grep { $_->package_name ne 'UNIVERSAL' }
101             Class::MOP::Class->initialize('Baz')->get_all_methods()
102     ],
103     [
104         Class::MOP::Class->initialize('Bar')->get_method('BUILD'),
105         Class::MOP::Class->initialize('Bar')->get_method('bar'),
106         Class::MOP::Class->initialize('Baz')->get_method('baz'),
107         Class::MOP::Class->initialize('Baz')->get_method('foo'),
108     ],
109     '... got the right list of applicable methods for Baz');
110
111 is_deeply(
112     [
113         sort { $a->name cmp $b->name }
114             grep { $_->package_name ne 'UNIVERSAL' }
115             Class::MOP::Class->initialize('Foo::Bar')->get_all_methods()
116     ],
117     [
118         Class::MOP::Class->initialize('Foo::Bar')->get_method('BUILD'),
119         Class::MOP::Class->initialize('Bar')->get_method('bar'),
120         Class::MOP::Class->initialize('Foo')->get_method('foo'),
121         Class::MOP::Class->initialize('Foo::Bar')->get_method('foobar'),
122     ],
123     '... got the right list of applicable methods for Foo::Bar');
124
125 ## find_all_methods_by_name
126
127 is_deeply(
128     [ Class::MOP::Class->initialize('Foo::Bar')->find_all_methods_by_name('BUILD') ],
129     [
130         {
131             name  => 'BUILD',
132             class => 'Foo::Bar',
133             code  => Class::MOP::Class->initialize('Foo::Bar')->get_method('BUILD')
134         },
135         {
136             name  => 'BUILD',
137             class => 'Foo',
138             code  => Class::MOP::Class->initialize('Foo')->get_method('BUILD')
139         },
140         {
141             name  => 'BUILD',
142             class => 'Bar',
143             code  => Class::MOP::Class->initialize('Bar')->get_method('BUILD')
144         }
145     ],
146     '... got the right list of BUILD methods for Foo::Bar');
147
148 is_deeply(
149     [ Class::MOP::Class->initialize('Foo::Bar::Baz')->find_all_methods_by_name('BUILD') ],
150     [
151         {
152             name  => 'BUILD',
153             class => 'Foo::Bar::Baz',
154             code  => Class::MOP::Class->initialize('Foo::Bar::Baz')->get_method('BUILD')
155         },
156         {
157             name  => 'BUILD',
158             class => 'Foo',
159             code  => Class::MOP::Class->initialize('Foo')->get_method('BUILD')
160         },
161         {
162             name  => 'BUILD',
163             class => 'Bar',
164             code  => Class::MOP::Class->initialize('Bar')->get_method('BUILD')
165         },
166     ],
167     '... got the right list of BUILD methods for Foo::Bar::Baz');
168
169 done_testing;