Merge branch 'stable'
[gitmo/Class-MOP.git] / t / 003_methods.t
CommitLineData
0882828e 1use strict;
2use warnings;
3
86a4d873 4use Test::More;
871e9eb5 5use Test::Fatal;
0882828e 6
16e960bd 7use Scalar::Util qw/reftype/;
2e13df84 8use Sub::Name;
16e960bd 9
7d682ca2 10use Class::MOP;
11use Class::MOP::Class;
12use Class::MOP::Method;
0882828e 13
49ca2e97 14{
15 # This package tries to test &has_method as exhaustively as
16 # possible. More corner cases are welcome :)
0882828e 17 package Foo;
49ca2e97 18
0882828e 19 # import a sub
49ca2e97 20 use Scalar::Util 'blessed';
21
823a5d31 22 sub pie;
23 sub cake ();
24
bfe4d0fc 25 use constant FOO_CONSTANT => 'Foo-CONSTANT';
49ca2e97 26
0882828e 27 # define a sub in package
49ca2e97 28 sub bar {'Foo::bar'}
60d90bbc 29 *baz = \&bar;
49ca2e97 30
46b23b44 31 # create something with the typeglob inside the package
49ca2e97 32 *baaz = sub {'Foo::baaz'};
bfe4d0fc 33
49ca2e97 34 { # method named with Sub::Name inside the package scope
bfe4d0fc 35 no strict 'refs';
49ca2e97 36 *{'Foo::floob'} = Sub::Name::subname 'floob' => sub {'!floob!'};
bfe4d0fc 37 }
38
60d90bbc 39 # We hateses the "used only once" warnings
49ca2e97 40 {
46b23b44 41 my $temp1 = \&Foo::baz;
49ca2e97 42 my $temp2 = \&Foo::baaz;
46b23b44 43 }
49ca2e97 44
16e960bd 45 package OinkyBoinky;
46 our @ISA = "Foo";
49ca2e97 47
48 sub elk {'OinkyBoinky::elk'}
60d90bbc 49
50 package main;
49ca2e97 51
60d90bbc 52 sub Foo::blah { $_[0]->Foo::baz() }
49ca2e97 53
60d90bbc 54 {
55 no strict 'refs';
49ca2e97 56 *{'Foo::bling'} = sub {'$$Bling$$'};
57 *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub {'!BANG!'};
58 *{'Foo::boom'} = Sub::Name::subname 'boom' => sub {'!BOOM!'};
59
60 eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }";
60d90bbc 61 }
0882828e 62}
63
bfe4d0fc 64my $Foo = Class::MOP::Class->initialize('Foo');
0882828e 65
130b7aff 66is join(' ', sort $Foo->get_method_list),
67 'FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob pie';
68
49ca2e97 69ok( $Foo->has_method('pie'), '... got the method stub pie' );
70ok( $Foo->has_method('cake'), '... got the constant method stub cake' );
823a5d31 71
49ca2e97 72my $foo = sub {'Foo::foo'};
0882828e 73
49ca2e97 74ok( !UNIVERSAL::isa( $foo, 'Class::MOP::Method' ),
75 '... our method is not yet blessed' );
de19f115 76
871e9eb5 77is( exception {
49ca2e97 78 $Foo->add_method( 'foo' => $foo );
871e9eb5 79}, undef, '... we added the method successfully' );
0882828e 80
7855ddba 81my $foo_method = $Foo->get_method('foo');
de19f115 82
49ca2e97 83isa_ok( $foo_method, 'Class::MOP::Method' );
7855ddba 84
49ca2e97 85is( $foo_method->name, 'foo', '... got the right name for the method' );
86is( $foo_method->package_name, 'Foo',
87 '... got the right package name for the method' );
de19f115 88
49ca2e97 89ok( $Foo->has_method('foo'),
90 '... Foo->has_method(foo) (defined with Sub::Name)' );
bfe4d0fc 91
49ca2e97 92is( $Foo->get_method('foo')->body, $foo,
93 '... Foo->get_method(foo) == \&foo' );
94is( $Foo->get_method('foo')->execute, 'Foo::foo',
95 '... _method_foo->execute returns "Foo::foo"' );
96is( Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"' );
bfe4d0fc 97
98# now check all our other items ...
99
49ca2e97 100ok( $Foo->has_method('FOO_CONSTANT'),
101 '... not Foo->has_method(FOO_CONSTANT) (defined w/ use constant)' );
102ok( !$Foo->has_method('bling'),
103 '... not Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'
104);
46b23b44 105
49ca2e97 106ok( $Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)' );
107ok( $Foo->has_method('baz'),
108 '... Foo->has_method(baz) (typeglob aliased within Foo)' );
109ok( $Foo->has_method('baaz'),
110 '... Foo->has_method(baaz) (typeglob aliased within Foo)' );
111ok( $Foo->has_method('floob'),
112 '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)'
113);
114ok( $Foo->has_method('blah'),
115 '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'
116);
117ok( $Foo->has_method('bang'),
118 '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'
119);
120ok( $Foo->has_method('evaled_foo'),
121 '... Foo->has_method(evaled_foo) (evaled in main::)' );
0882828e 122
16e960bd 123my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky');
124
49ca2e97 125ok( $OinkyBoinky->has_method('elk'),
126 "the method 'elk' is defined in OinkyBoinky" );
16e960bd 127
49ca2e97 128ok( !$OinkyBoinky->has_method('bar'),
129 "the method 'bar' is not defined in OinkyBoinky" );
16e960bd 130
49ca2e97 131ok( my $bar = $OinkyBoinky->find_method_by_name('bar'),
132 "but if you look in the inheritence chain then 'bar' does exist" );
16e960bd 133
49ca2e97 134is( reftype( $bar->body ), "CODE", "the returned value is a code ref" );
16e960bd 135
de19f115 136# calling get_method blessed them all
49ca2e97 137for my $method_name (
138 qw/baaz
139 bar
140 baz
141 floob
142 blah
143 bang
144 evaled_foo
145 FOO_CONSTANT/
146 ) {
147 isa_ok( $Foo->get_method($method_name), 'Class::MOP::Method' );
7855ddba 148 {
149 no strict 'refs';
49ca2e97 150 is( $Foo->get_method($method_name)->body,
151 \&{ 'Foo::' . $method_name },
152 '... body matches CODE ref in package for ' . $method_name );
7855ddba 153 }
154}
de19f115 155
49ca2e97 156for my $method_name (
157 qw/
158 bling
159 /
160 ) {
161 is( ref( $Foo->get_package_symbol( '&' . $method_name ) ), 'CODE',
162 '... got the __ANON__ methods' );
46b23b44 163 {
164 no strict 'refs';
49ca2e97 165 is( $Foo->get_package_symbol( '&' . $method_name ),
166 \&{ 'Foo::' . $method_name },
167 '... symbol matches CODE ref in package for ' . $method_name );
46b23b44 168 }
169}
170
49ca2e97 171ok( !$Foo->has_method('blessed'),
172 '... !Foo->has_method(blessed) (imported into Foo)' );
173ok( !$Foo->has_method('boom'),
174 '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)'
175);
0882828e 176
49ca2e97 177ok( !$Foo->has_method('not_a_real_method'),
178 '... !Foo->has_method(not_a_real_method) (does not exist)' );
179is( $Foo->get_method('not_a_real_method'), undef,
180 '... Foo->get_method(not_a_real_method) == undef' );
bfe4d0fc 181
c9b8b7f9 182is_deeply(
183 [ sort $Foo->get_method_list ],
49ca2e97 184 [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob foo pie)],
185 '... got the right method list for Foo'
186);
c9b8b7f9 187
a5eca695 188is_deeply(
77ff94df 189 [ sort { $a->name cmp $b->name } $Foo->get_all_methods() ],
a5eca695 190 [
49ca2e97 191 map { $Foo->get_method($_) }
192 qw(
d7d3f3cb 193 FOO_CONSTANT
49ca2e97 194 baaz
195 bang
196 bar
197 baz
198 blah
c3b8d5ad 199 cake
49ca2e97 200 evaled_foo
201 floob
a5eca695 202 foo
c3b8d5ad 203 pie
49ca2e97 204 )
a5eca695 205 ],
49ca2e97 206 '... got the right list of applicable methods for Foo'
207);
a5eca695 208
49ca2e97 209is( $Foo->remove_method('foo')->body, $foo, '... removed the foo method' );
210ok( !$Foo->has_method('foo'),
211 '... !Foo->has_method(foo) we just removed it' );
871e9eb5 212isnt( exception { Foo->foo }, undef, '... cannot call Foo->foo because it is not there' );
c9b8b7f9 213
214is_deeply(
215 [ sort $Foo->get_method_list ],
49ca2e97 216 [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob pie)],
217 '... got the right method list for Foo'
218);
c9b8b7f9 219
49ca2e97 220# ... test our class creator
bfe4d0fc 221
222my $Bar = Class::MOP::Class->create(
99b84658 223 package => 'Bar',
49ca2e97 224 superclasses => ['Foo'],
3976fb78 225 methods => {
49ca2e97 226 foo => sub {'Bar::foo'},
227 bar => sub {'Bar::bar'},
3976fb78 228 }
229);
49ca2e97 230isa_ok( $Bar, 'Class::MOP::Class' );
bfe4d0fc 231
49ca2e97 232ok( $Bar->has_method('foo'), '... Bar->has_method(foo)' );
233ok( $Bar->has_method('bar'), '... Bar->has_method(bar)' );
bfe4d0fc 234
49ca2e97 235is( Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo' );
236is( Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar' );
bfe4d0fc 237
871e9eb5 238is( exception {
49ca2e97 239 $Bar->add_method( 'foo' => sub {'Bar::foo v2'} );
871e9eb5 240}, undef, '... overwriting a method is fine' );
bfe4d0fc 241
49ca2e97 242is_deeply( [ Class::MOP::get_code_info( $Bar->get_method('foo')->body ) ],
243 [ "Bar", "foo" ], "subname applied to anonymous method" );
bfa0d9f8 244
49ca2e97 245ok( $Bar->has_method('foo'), '... Bar-> (still) has_method(foo)' );
246is( Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"' );
c9b8b7f9 247
248is_deeply(
249 [ sort $Bar->get_method_list ],
49ca2e97 250 [qw(bar foo meta)],
251 '... got the right method list for Bar'
252);
253
a5eca695 254is_deeply(
77ff94df 255 [ sort { $a->name cmp $b->name } $Bar->get_all_methods() ],
a5eca695 256 [
77ff94df 257 $Foo->get_method('FOO_CONSTANT'),
258 $Foo->get_method('baaz'),
259 $Foo->get_method('bang'),
260 $Bar->get_method('bar'),
49ca2e97 261 (
262 map { $Foo->get_method($_) }
263 qw(
264 baz
265 blah
266 cake
267 evaled_foo
268 floob
269 )
270 ),
77ff94df 271 $Bar->get_method('foo'),
272 $Bar->get_method('meta'),
c3b8d5ad 273 $Foo->get_method('pie'),
a5eca695 274 ],
49ca2e97 275 '... got the right list of applicable methods for Bar'
276);
a5eca695 277
7d682ca2 278my $method = Class::MOP::Method->wrap(
279 name => 'objecty',
280 package_name => 'Whatever',
281 body => sub {q{I am an object, and I feel an object's pain}},
282);
283
284Bar->meta->add_method( $method->name, $method );
285
286my $new_method = Bar->meta->get_method('objecty');
a5eca695 287
49ca2e97 288isnt( $method, $new_method,
289 'add_method clones method objects as they are added' );
290is( $new_method->original_method, $method,
55039f82 291 '... the cloned method has the correct original method' )
292 or diag $new_method->dump;
c41df5a9 293
294{
c41df5a9 295 package CustomAccessor;
296
297 use Class::MOP;
298
299 my $meta = Class::MOP::Class->initialize(__PACKAGE__);
300
301 $meta->add_attribute(
302 foo => (
303 accessor => 'foo',
304 )
305 );
306
307 {
f675e129 308 no warnings 'redefine', 'once';
c41df5a9 309 *foo = sub {
310 my $self = shift;
311 $self->{custom_store} = $_[0];
312 };
313 }
314
315 $meta->add_around_method_modifier(
316 'foo',
317 sub {
318 my $orig = shift;
319 $orig->(@_);
320 }
321 );
322
5327fc78 323 sub new {
324 return bless {}, shift;
325 }
c41df5a9 326}
327
328{
329 my $o = CustomAccessor->new;
330 my $str = 'string';
331
332 $o->foo($str);
333
49ca2e97 334 is(
335 $o->{custom_store}, $str,
5327fc78 336 'Custom glob-assignment-created accessor still has method modifier'
49ca2e97 337 );
c41df5a9 338}
5327fc78 339
340{
341 # Since the sub reference below is not a closure, Perl caches it and uses
342 # the same reference each time through the loop. See RT #48985 for the
343 # bug.
344 foreach my $ns ( qw( Foo2 Bar2 Baz2 ) ) {
345 my $meta = Class::MOP::Class->create($ns);
346
347 my $sub = sub { };
348
349 $meta->add_method( 'foo', $sub );
350
351 my $method = $meta->get_method('foo');
352 ok( $method, 'Got the foo method back' );
353 }
354}
86a4d873 355
7f9ef61e 356{
357 package HasConstants;
358
adb2de7d 359 use constant FOO => 1;
360 use constant BAR => [];
361 use constant BAZ => {};
362 use constant UNDEF => undef;
7f9ef61e 363
364 sub quux {1}
365 sub thing {1}
366}
367
368my $HC = Class::MOP::Class->initialize('HasConstants');
369
370is_deeply(
371 [ sort $HC->get_method_list ],
adb2de7d 372 [qw( BAR BAZ FOO UNDEF quux thing )],
7f9ef61e 373 'get_method_list handles constants properly'
374);
375
376is_deeply(
377 [ sort map { $_->name } $HC->_get_local_methods ],
adb2de7d 378 [qw( BAR BAZ FOO UNDEF quux thing )],
7f9ef61e 379 '_get_local_methods handles constants properly'
380);
381
e4679e2c 382{
383 package DeleteFromMe;
384 sub foo { 1 }
385}
386
387{
388 my $DFMmeta = Class::MOP::Class->initialize('DeleteFromMe');
389 ok($DFMmeta->get_method('foo'));
390
391 delete $DeleteFromMe::{foo};
392
393 ok(!$DFMmeta->get_method('foo'));
394 ok(!DeleteFromMe->can('foo'));
395}
396
7f9ef61e 397
86a4d873 398done_testing;