Redo conversion to Test::Fatal
[gitmo/Class-MOP.git] / t / 004_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     [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo')->get_all_methods() ],
73     [
74         Class::MOP::Class->initialize('Foo')->get_method('BUILD') ,
75         Class::MOP::Class->initialize('Foo')->get_method('foo'),
76     ],
77     '... got the right list of applicable methods for Foo');
78
79 is_deeply(
80     [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Bar')->get_all_methods() ],
81     [
82         Class::MOP::Class->initialize('Bar')->get_method('BUILD'),
83         Class::MOP::Class->initialize('Bar')->get_method('bar'),
84         Class::MOP::Class->initialize('Foo')->get_method('foo'),
85     ],
86     '... got the right list of applicable methods for Bar');
87
88
89 is_deeply(
90     [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Baz')->get_all_methods() ],
91     [
92         Class::MOP::Class->initialize('Bar')->get_method('BUILD'),
93         Class::MOP::Class->initialize('Bar')->get_method('bar'),
94         Class::MOP::Class->initialize('Baz')->get_method('baz'),
95         Class::MOP::Class->initialize('Baz')->get_method('foo'),
96     ],
97     '... got the right list of applicable methods for Baz');
98
99 is_deeply(
100     [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo::Bar')->get_all_methods() ],
101     [
102         Class::MOP::Class->initialize('Foo::Bar')->get_method('BUILD'),
103         Class::MOP::Class->initialize('Bar')->get_method('bar'),
104         Class::MOP::Class->initialize('Foo')->get_method('foo'),
105         Class::MOP::Class->initialize('Foo::Bar')->get_method('foobar'),
106     ],
107     '... got the right list of applicable methods for Foo::Bar');
108
109 ## find_all_methods_by_name
110
111 is_deeply(
112     [ Class::MOP::Class->initialize('Foo::Bar')->find_all_methods_by_name('BUILD') ],
113     [
114         {
115             name  => 'BUILD',
116             class => 'Foo::Bar',
117             code  => Class::MOP::Class->initialize('Foo::Bar')->get_method('BUILD')
118         },
119         {
120             name  => 'BUILD',
121             class => 'Foo',
122             code  => Class::MOP::Class->initialize('Foo')->get_method('BUILD')
123         },
124         {
125             name  => 'BUILD',
126             class => 'Bar',
127             code  => Class::MOP::Class->initialize('Bar')->get_method('BUILD')
128         }
129     ],
130     '... got the right list of BUILD methods for Foo::Bar');
131
132 is_deeply(
133     [ Class::MOP::Class->initialize('Foo::Bar::Baz')->find_all_methods_by_name('BUILD') ],
134     [
135         {
136             name  => 'BUILD',
137             class => 'Foo::Bar::Baz',
138             code  => Class::MOP::Class->initialize('Foo::Bar::Baz')->get_method('BUILD')
139         },
140         {
141             name  => 'BUILD',
142             class => 'Foo',
143             code  => Class::MOP::Class->initialize('Foo')->get_method('BUILD')
144         },
145         {
146             name  => 'BUILD',
147             class => 'Bar',
148             code  => Class::MOP::Class->initialize('Bar')->get_method('BUILD')
149         },
150     ],
151     '... got the right list of BUILD methods for Foo::Bar::Baz');
152
153 done_testing;