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 | |
7d682ca2 |
20 | use Class::MOP; |
21 | use Class::MOP::Class; |
22 | use Class::MOP::Method; |
0882828e |
23 | |
bfe4d0fc |
24 | { # This package tries to test &has_method |
25 | # as exhaustively as possible. More corner |
26 | # cases are welcome :) |
0882828e |
27 | package Foo; |
28 | |
29 | # import a sub |
30 | use Scalar::Util 'blessed'; |
31 | |
823a5d31 |
32 | sub pie; |
33 | sub cake (); |
34 | |
bfe4d0fc |
35 | use constant FOO_CONSTANT => 'Foo-CONSTANT'; |
36 | |
0882828e |
37 | # define a sub in package |
38 | sub bar { 'Foo::bar' } |
60d90bbc |
39 | *baz = \&bar; |
46b23b44 |
40 | |
41 | # create something with the typeglob inside the package |
42 | *baaz = sub { 'Foo::baaz' }; |
bfe4d0fc |
43 | |
44 | { # method named with Sub::Name inside the package scope |
45 | no strict 'refs'; |
46 | *{'Foo::floob'} = Sub::Name::subname 'floob' => sub { '!floob!' }; |
47 | } |
48 | |
60d90bbc |
49 | # We hateses the "used only once" warnings |
46b23b44 |
50 | { |
51 | my $temp1 = \&Foo::baz; |
52 | my $temp2 = \&Foo::baaz; |
53 | } |
16e960bd |
54 | |
55 | package OinkyBoinky; |
56 | our @ISA = "Foo"; |
57 | |
58 | sub elk { 'OinkyBoinky::elk' } |
60d90bbc |
59 | |
60 | package main; |
61 | |
62 | sub Foo::blah { $_[0]->Foo::baz() } |
63 | |
64 | { |
65 | no strict 'refs'; |
66 | *{'Foo::bling'} = sub { '$$Bling$$' }; |
bfe4d0fc |
67 | *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub { '!BANG!' }; |
68 | *{'Foo::boom'} = Sub::Name::subname 'boom' => sub { '!BOOM!' }; |
69 | |
70 | eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }"; |
60d90bbc |
71 | } |
0882828e |
72 | } |
73 | |
bfe4d0fc |
74 | my $Foo = Class::MOP::Class->initialize('Foo'); |
0882828e |
75 | |
823a5d31 |
76 | ok(!$Foo->has_method('pie'), '... got the method stub pie'); |
77 | ok(!$Foo->has_method('cake'), '... got the constant method stub cake'); |
78 | |
0882828e |
79 | my $foo = sub { 'Foo::foo' }; |
80 | |
de19f115 |
81 | ok(!UNIVERSAL::isa($foo, 'Class::MOP::Method'), '... our method is not yet blessed'); |
82 | |
0882828e |
83 | lives_ok { |
84 | $Foo->add_method('foo' => $foo); |
85 | } '... we added the method successfully'; |
86 | |
7855ddba |
87 | my $foo_method = $Foo->get_method('foo'); |
de19f115 |
88 | |
7855ddba |
89 | isa_ok($foo_method, 'Class::MOP::Method'); |
90 | |
91 | is($foo_method->name, 'foo', '... got the right name for the method'); |
92 | is($foo_method->package_name, 'Foo', '... got the right package name for the method'); |
de19f115 |
93 | |
0882828e |
94 | ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)'); |
bfe4d0fc |
95 | |
7855ddba |
96 | is($Foo->get_method('foo')->body, $foo, '... Foo->get_method(foo) == \&foo'); |
bfe4d0fc |
97 | is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"'); |
98 | |
99 | # now check all our other items ... |
100 | |
d7d3f3cb |
101 | ok($Foo->has_method('FOO_CONSTANT'), '... not Foo->has_method(FOO_CONSTANT) (defined w/ use constant)'); |
102 | ok(!$Foo->has_method('bling'), '... not Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'); |
46b23b44 |
103 | |
0882828e |
104 | ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)'); |
60d90bbc |
105 | ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)'); |
46b23b44 |
106 | ok($Foo->has_method('baaz'), '... Foo->has_method(baaz) (typeglob aliased within Foo)'); |
bfe4d0fc |
107 | ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)'); |
60d90bbc |
108 | ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'); |
60d90bbc |
109 | ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'); |
bfe4d0fc |
110 | ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)'); |
0882828e |
111 | |
16e960bd |
112 | my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky'); |
113 | |
114 | ok($OinkyBoinky->has_method('elk'), "the method 'elk' is defined in OinkyBoinky"); |
115 | |
116 | ok(!$OinkyBoinky->has_method('bar'), "the method 'bar' is not defined in OinkyBoinky"); |
117 | |
118 | ok(my $bar = $OinkyBoinky->find_method_by_name('bar'), "but if you look in the inheritence chain then 'bar' does exist"); |
119 | |
b9575695 |
120 | is( reftype($bar->body), "CODE", "the returned value is a code ref" ); |
16e960bd |
121 | |
122 | |
de19f115 |
123 | # calling get_method blessed them all |
46b23b44 |
124 | for my $method_name (qw/baaz |
125 | bar |
7855ddba |
126 | baz |
127 | floob |
46b23b44 |
128 | blah |
129 | bang |
d7d3f3cb |
130 | evaled_foo |
131 | FOO_CONSTANT/) { |
7855ddba |
132 | isa_ok($Foo->get_method($method_name), 'Class::MOP::Method'); |
133 | { |
134 | no strict 'refs'; |
187c832e |
135 | is($Foo->get_method($method_name)->body, \&{'Foo::' . $method_name}, '... body matches CODE ref in package for ' . $method_name); |
7855ddba |
136 | } |
137 | } |
de19f115 |
138 | |
46b23b44 |
139 | for my $method_name (qw/ |
46b23b44 |
140 | bling |
141 | /) { |
142 | is(ref($Foo->get_package_symbol('&' . $method_name)), 'CODE', '... got the __ANON__ methods'); |
143 | { |
144 | no strict 'refs'; |
145 | is($Foo->get_package_symbol('&' . $method_name), \&{'Foo::' . $method_name}, '... symbol matches CODE ref in package for ' . $method_name); |
146 | } |
147 | } |
148 | |
663f8198 |
149 | { |
150 | package Foo::Aliasing; |
151 | use metaclass; |
152 | sub alias_me { '...' } |
153 | } |
154 | |
155 | $Foo->alias_method('alias_me' => Foo::Aliasing->meta->get_method('alias_me')); |
156 | |
91b73829 |
157 | ok($Foo->has_method('alias_me'), '... Foo->has_method(alias_me) (aliased from Foo::Aliasing)'); |
663f8198 |
158 | ok(defined &Foo::alias_me, '... Foo does have a symbol table slow for alias_me though'); |
159 | |
bfe4d0fc |
160 | ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)'); |
161 | ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)'); |
0882828e |
162 | |
bfe4d0fc |
163 | ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)'); |
164 | is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef'); |
165 | |
c9b8b7f9 |
166 | is_deeply( |
167 | [ sort $Foo->get_method_list ], |
91b73829 |
168 | [ qw(FOO_CONSTANT alias_me baaz bang bar baz blah evaled_foo floob foo) ], |
c9b8b7f9 |
169 | '... got the right method list for Foo'); |
170 | |
a5eca695 |
171 | is_deeply( |
77ff94df |
172 | [ sort { $a->name cmp $b->name } $Foo->get_all_methods() ], |
a5eca695 |
173 | [ |
77ff94df |
174 | map { $Foo->get_method($_) } qw( |
d7d3f3cb |
175 | FOO_CONSTANT |
91b73829 |
176 | alias_me |
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 ], |
91b73829 |
195 | [ qw(FOO_CONSTANT alias_me 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'), |
91b73829 |
233 | $Foo->get_method('alias_me'), |
77ff94df |
234 | $Foo->get_method('baaz'), |
235 | $Foo->get_method('bang'), |
236 | $Bar->get_method('bar'), |
237 | (map { $Foo->get_method($_) } qw( |
a5eca695 |
238 | baz |
239 | blah |
a5eca695 |
240 | evaled_foo |
241 | floob |
242 | )), |
77ff94df |
243 | $Bar->get_method('foo'), |
244 | $Bar->get_method('meta'), |
a5eca695 |
245 | ], |
246 | '... got the right list of applicable methods for Bar'); |
247 | |
7d682ca2 |
248 | my $method = Class::MOP::Method->wrap( |
249 | name => 'objecty', |
250 | package_name => 'Whatever', |
251 | body => sub {q{I am an object, and I feel an object's pain}}, |
252 | ); |
253 | |
254 | Bar->meta->add_method( $method->name, $method ); |
255 | |
256 | my $new_method = Bar->meta->get_method('objecty'); |
a5eca695 |
257 | |
7d682ca2 |
258 | isnt( $method, $new_method, 'add_method clones method objects as they are added' ); |
259 | is( $new_method->original_method, $method, '... the cloned method has the correct original method' ); |