Commit | Line | Data |
0882828e |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
e896822d |
6 | use Test::More; |
0882828e |
7 | use Test::Exception; |
8 | |
16e960bd |
9 | use Scalar::Util qw/reftype/; |
e896822d |
10 | |
11 | BEGIN { |
2621d59f |
12 | if ( eval 'use Sub::Name (); 1;' ) { |
e896822d |
13 | plan tests => 65; |
14 | } |
15 | else { |
16 | plan skip_all => 'These tests require Sub::Name'; |
17 | } |
18 | } |
16e960bd |
19 | |
0882828e |
20 | BEGIN { |
21 | use_ok('Class::MOP'); |
22 | use_ok('Class::MOP::Class'); |
23 | } |
24 | |
bfe4d0fc |
25 | { # This package tries to test &has_method |
26 | # as exhaustively as possible. More corner |
27 | # cases are welcome :) |
0882828e |
28 | package Foo; |
29 | |
30 | # import a sub |
31 | use Scalar::Util 'blessed'; |
32 | |
823a5d31 |
33 | sub pie; |
34 | sub cake (); |
35 | |
bfe4d0fc |
36 | use constant FOO_CONSTANT => 'Foo-CONSTANT'; |
37 | |
0882828e |
38 | # define a sub in package |
39 | sub bar { 'Foo::bar' } |
60d90bbc |
40 | *baz = \&bar; |
46b23b44 |
41 | |
42 | # create something with the typeglob inside the package |
43 | *baaz = sub { 'Foo::baaz' }; |
bfe4d0fc |
44 | |
45 | { # method named with Sub::Name inside the package scope |
46 | no strict 'refs'; |
47 | *{'Foo::floob'} = Sub::Name::subname 'floob' => sub { '!floob!' }; |
48 | } |
49 | |
60d90bbc |
50 | # We hateses the "used only once" warnings |
46b23b44 |
51 | { |
52 | my $temp1 = \&Foo::baz; |
53 | my $temp2 = \&Foo::baaz; |
54 | } |
16e960bd |
55 | |
56 | package OinkyBoinky; |
57 | our @ISA = "Foo"; |
58 | |
59 | sub elk { 'OinkyBoinky::elk' } |
60d90bbc |
60 | |
61 | package main; |
62 | |
63 | sub Foo::blah { $_[0]->Foo::baz() } |
64 | |
65 | { |
66 | no strict 'refs'; |
67 | *{'Foo::bling'} = sub { '$$Bling$$' }; |
bfe4d0fc |
68 | *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub { '!BANG!' }; |
69 | *{'Foo::boom'} = Sub::Name::subname 'boom' => sub { '!BOOM!' }; |
70 | |
71 | eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }"; |
60d90bbc |
72 | } |
0882828e |
73 | } |
74 | |
bfe4d0fc |
75 | my $Foo = Class::MOP::Class->initialize('Foo'); |
0882828e |
76 | |
823a5d31 |
77 | ok(!$Foo->has_method('pie'), '... got the method stub pie'); |
78 | ok(!$Foo->has_method('cake'), '... got the constant method stub cake'); |
79 | |
0882828e |
80 | my $foo = sub { 'Foo::foo' }; |
81 | |
de19f115 |
82 | ok(!UNIVERSAL::isa($foo, 'Class::MOP::Method'), '... our method is not yet blessed'); |
83 | |
0882828e |
84 | lives_ok { |
85 | $Foo->add_method('foo' => $foo); |
86 | } '... we added the method successfully'; |
87 | |
7855ddba |
88 | my $foo_method = $Foo->get_method('foo'); |
de19f115 |
89 | |
7855ddba |
90 | isa_ok($foo_method, 'Class::MOP::Method'); |
91 | |
92 | is($foo_method->name, 'foo', '... got the right name for the method'); |
93 | is($foo_method->package_name, 'Foo', '... got the right package name for the method'); |
de19f115 |
94 | |
0882828e |
95 | ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)'); |
bfe4d0fc |
96 | |
7855ddba |
97 | is($Foo->get_method('foo')->body, $foo, '... Foo->get_method(foo) == \&foo'); |
bfe4d0fc |
98 | is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"'); |
99 | |
100 | # now check all our other items ... |
101 | |
d7d3f3cb |
102 | ok($Foo->has_method('FOO_CONSTANT'), '... not Foo->has_method(FOO_CONSTANT) (defined w/ use constant)'); |
103 | ok(!$Foo->has_method('bling'), '... not Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'); |
46b23b44 |
104 | |
0882828e |
105 | ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)'); |
60d90bbc |
106 | ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)'); |
46b23b44 |
107 | ok($Foo->has_method('baaz'), '... Foo->has_method(baaz) (typeglob aliased within Foo)'); |
bfe4d0fc |
108 | ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)'); |
60d90bbc |
109 | ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'); |
60d90bbc |
110 | ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'); |
bfe4d0fc |
111 | ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)'); |
0882828e |
112 | |
16e960bd |
113 | my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky'); |
114 | |
115 | ok($OinkyBoinky->has_method('elk'), "the method 'elk' is defined in OinkyBoinky"); |
116 | |
117 | ok(!$OinkyBoinky->has_method('bar'), "the method 'bar' is not defined in OinkyBoinky"); |
118 | |
119 | ok(my $bar = $OinkyBoinky->find_method_by_name('bar'), "but if you look in the inheritence chain then 'bar' does exist"); |
120 | |
b9575695 |
121 | is( reftype($bar->body), "CODE", "the returned value is a code ref" ); |
16e960bd |
122 | |
123 | |
de19f115 |
124 | # calling get_method blessed them all |
46b23b44 |
125 | for my $method_name (qw/baaz |
126 | bar |
7855ddba |
127 | baz |
128 | floob |
46b23b44 |
129 | blah |
130 | bang |
d7d3f3cb |
131 | evaled_foo |
132 | FOO_CONSTANT/) { |
7855ddba |
133 | isa_ok($Foo->get_method($method_name), 'Class::MOP::Method'); |
134 | { |
135 | no strict 'refs'; |
187c832e |
136 | is($Foo->get_method($method_name)->body, \&{'Foo::' . $method_name}, '... body matches CODE ref in package for ' . $method_name); |
7855ddba |
137 | } |
138 | } |
de19f115 |
139 | |
46b23b44 |
140 | for my $method_name (qw/ |
46b23b44 |
141 | bling |
142 | /) { |
143 | is(ref($Foo->get_package_symbol('&' . $method_name)), 'CODE', '... got the __ANON__ methods'); |
144 | { |
145 | no strict 'refs'; |
146 | is($Foo->get_package_symbol('&' . $method_name), \&{'Foo::' . $method_name}, '... symbol matches CODE ref in package for ' . $method_name); |
147 | } |
148 | } |
149 | |
663f8198 |
150 | { |
151 | package Foo::Aliasing; |
152 | use metaclass; |
153 | sub alias_me { '...' } |
154 | } |
155 | |
156 | $Foo->alias_method('alias_me' => Foo::Aliasing->meta->get_method('alias_me')); |
157 | |
158 | ok(!$Foo->has_method('alias_me'), '... !Foo->has_method(alias_me) (aliased from Foo::Aliasing)'); |
159 | ok(defined &Foo::alias_me, '... Foo does have a symbol table slow for alias_me though'); |
160 | |
bfe4d0fc |
161 | ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)'); |
162 | ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)'); |
0882828e |
163 | |
bfe4d0fc |
164 | ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)'); |
165 | is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef'); |
166 | |
c9b8b7f9 |
167 | is_deeply( |
168 | [ sort $Foo->get_method_list ], |
d7d3f3cb |
169 | [ qw(FOO_CONSTANT baaz bang bar baz blah evaled_foo floob foo) ], |
c9b8b7f9 |
170 | '... got the right method list for Foo'); |
171 | |
a5eca695 |
172 | is_deeply( |
77ff94df |
173 | [ sort { $a->name cmp $b->name } $Foo->get_all_methods() ], |
a5eca695 |
174 | [ |
77ff94df |
175 | map { $Foo->get_method($_) } qw( |
d7d3f3cb |
176 | FOO_CONSTANT |
46b23b44 |
177 | baaz |
a5eca695 |
178 | bang |
179 | bar |
180 | baz |
181 | blah |
a5eca695 |
182 | evaled_foo |
183 | floob |
184 | foo |
185 | ) |
186 | ], |
187 | '... got the right list of applicable methods for Foo'); |
188 | |
7855ddba |
189 | is($Foo->remove_method('foo')->body, $foo, '... removed the foo method'); |
c9b8b7f9 |
190 | ok(!$Foo->has_method('foo'), '... !Foo->has_method(foo) we just removed it'); |
191 | dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there'; |
192 | |
193 | is_deeply( |
194 | [ sort $Foo->get_method_list ], |
d7d3f3cb |
195 | [ qw(FOO_CONSTANT baaz bang bar baz blah evaled_foo floob) ], |
c9b8b7f9 |
196 | '... got the right method list for Foo'); |
197 | |
c9b8b7f9 |
198 | |
bfe4d0fc |
199 | # ... test our class creator |
200 | |
201 | my $Bar = Class::MOP::Class->create( |
99b84658 |
202 | package => 'Bar', |
3976fb78 |
203 | superclasses => [ 'Foo' ], |
204 | methods => { |
205 | foo => sub { 'Bar::foo' }, |
206 | bar => sub { 'Bar::bar' }, |
207 | } |
208 | ); |
bfe4d0fc |
209 | isa_ok($Bar, 'Class::MOP::Class'); |
210 | |
211 | ok($Bar->has_method('foo'), '... Bar->has_method(foo)'); |
212 | ok($Bar->has_method('bar'), '... Bar->has_method(bar)'); |
213 | |
214 | is(Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo'); |
215 | is(Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar'); |
216 | |
217 | lives_ok { |
218 | $Bar->add_method('foo' => sub { 'Bar::foo v2' }); |
219 | } '... overwriting a method is fine'; |
220 | |
221 | ok($Bar->has_method('foo'), '... Bar-> (still) has_method(foo)'); |
222 | is(Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"'); |
c9b8b7f9 |
223 | |
224 | is_deeply( |
225 | [ sort $Bar->get_method_list ], |
aa448b16 |
226 | [ qw(bar foo meta) ], |
a5eca695 |
227 | '... got the right method list for Bar'); |
228 | |
229 | is_deeply( |
77ff94df |
230 | [ sort { $a->name cmp $b->name } $Bar->get_all_methods() ], |
a5eca695 |
231 | [ |
77ff94df |
232 | $Foo->get_method('FOO_CONSTANT'), |
233 | $Foo->get_method('baaz'), |
234 | $Foo->get_method('bang'), |
235 | $Bar->get_method('bar'), |
236 | (map { $Foo->get_method($_) } qw( |
a5eca695 |
237 | baz |
238 | blah |
a5eca695 |
239 | evaled_foo |
240 | floob |
241 | )), |
77ff94df |
242 | $Bar->get_method('foo'), |
243 | $Bar->get_method('meta'), |
a5eca695 |
244 | ], |
245 | '... got the right list of applicable methods for Bar'); |
246 | |
247 | |