7 use Scalar::Util qw/reftype/;
11 use Class::MOP::Class;
12 use Class::MOP::Method;
15 # This package tries to test &has_method as exhaustively as
16 # possible. More corner cases are welcome :)
20 use Scalar::Util 'blessed';
25 use constant FOO_CONSTANT => 'Foo-CONSTANT';
27 # define a sub in package
31 # create something with the typeglob inside the package
32 *baaz = sub {'Foo::baaz'};
34 { # method named with Sub::Name inside the package scope
36 *{'Foo::floob'} = Sub::Name::subname 'floob' => sub {'!floob!'};
39 # We hateses the "used only once" warnings
41 my $temp1 = \&Foo::baz;
42 my $temp2 = \&Foo::baaz;
48 sub elk {'OinkyBoinky::elk'}
52 sub Foo::blah { $_[0]->Foo::baz() }
56 *{'Foo::bling'} = sub {'$$Bling$$'};
57 *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub {'!BANG!'};
58 *{'Foo::boom'} = Sub::Name::subname 'boom' => sub {'!BOOM!'};
60 eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }";
64 my $Foo = Class::MOP::Class->initialize('Foo');
66 is join(' ', sort $Foo->get_method_list),
67 'FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob pie';
69 ok( $Foo->has_method('pie'), '... got the method stub pie' );
70 ok( $Foo->has_method('cake'), '... got the constant method stub cake' );
72 my $foo = sub {'Foo::foo'};
74 ok( !UNIVERSAL::isa( $foo, 'Class::MOP::Method' ),
75 '... our method is not yet blessed' );
78 $Foo->add_method( 'foo' => $foo );
79 }, undef, '... we added the method successfully' );
81 my $foo_method = $Foo->get_method('foo');
83 isa_ok( $foo_method, 'Class::MOP::Method' );
85 is( $foo_method->name, 'foo', '... got the right name for the method' );
86 is( $foo_method->package_name, 'Foo',
87 '... got the right package name for the method' );
89 ok( $Foo->has_method('foo'),
90 '... Foo->has_method(foo) (defined with Sub::Name)' );
92 is( $Foo->get_method('foo')->body, $foo,
93 '... Foo->get_method(foo) == \&foo' );
94 is( $Foo->get_method('foo')->execute, 'Foo::foo',
95 '... _method_foo->execute returns "Foo::foo"' );
96 is( Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"' );
98 # now check all our other items ...
100 ok( $Foo->has_method('FOO_CONSTANT'),
101 '... not Foo->has_method(FOO_CONSTANT) (defined w/ use constant)' );
102 ok( !$Foo->has_method('bling'),
103 '... not Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'
106 ok( $Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)' );
107 ok( $Foo->has_method('baz'),
108 '... Foo->has_method(baz) (typeglob aliased within Foo)' );
109 ok( $Foo->has_method('baaz'),
110 '... Foo->has_method(baaz) (typeglob aliased within Foo)' );
111 ok( $Foo->has_method('floob'),
112 '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)'
114 ok( $Foo->has_method('blah'),
115 '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'
117 ok( $Foo->has_method('bang'),
118 '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'
120 ok( $Foo->has_method('evaled_foo'),
121 '... Foo->has_method(evaled_foo) (evaled in main::)' );
123 my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky');
125 ok( $OinkyBoinky->has_method('elk'),
126 "the method 'elk' is defined in OinkyBoinky" );
128 ok( !$OinkyBoinky->has_method('bar'),
129 "the method 'bar' is not defined in OinkyBoinky" );
131 ok( my $bar = $OinkyBoinky->find_method_by_name('bar'),
132 "but if you look in the inheritence chain then 'bar' does exist" );
134 is( reftype( $bar->body ), "CODE", "the returned value is a code ref" );
136 # calling get_method blessed them all
137 for my $method_name (
147 isa_ok( $Foo->get_method($method_name), 'Class::MOP::Method' );
150 is( $Foo->get_method($method_name)->body,
151 \&{ 'Foo::' . $method_name },
152 '... body matches CODE ref in package for ' . $method_name );
156 for my $method_name (
161 is( ref( $Foo->get_package_symbol( '&' . $method_name ) ), 'CODE',
162 '... got the __ANON__ methods' );
165 is( $Foo->get_package_symbol( '&' . $method_name ),
166 \&{ 'Foo::' . $method_name },
167 '... symbol matches CODE ref in package for ' . $method_name );
171 ok( !$Foo->has_method('blessed'),
172 '... !Foo->has_method(blessed) (imported into Foo)' );
173 ok( !$Foo->has_method('boom'),
174 '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)'
177 ok( !$Foo->has_method('not_a_real_method'),
178 '... !Foo->has_method(not_a_real_method) (does not exist)' );
179 is( $Foo->get_method('not_a_real_method'), undef,
180 '... Foo->get_method(not_a_real_method) == undef' );
183 [ sort $Foo->get_method_list ],
184 [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob foo pie)],
185 '... got the right method list for Foo'
188 my @universal_methods = qw/isa can VERSION/;
189 push @universal_methods, 'DOES' if $] >= 5.010;
193 map { $_->name => $_ }
194 sort { $a->name cmp $b->name } $Foo->get_all_methods()
197 map { $_->name => $_ }
198 map { $Foo->find_method_by_name($_) }
214 '... got the right list of applicable methods for Foo'
217 is( $Foo->remove_method('foo')->body, $foo, '... removed the foo method' );
218 ok( !$Foo->has_method('foo'),
219 '... !Foo->has_method(foo) we just removed it' );
220 isnt( exception { Foo->foo }, undef, '... cannot call Foo->foo because it is not there' );
223 [ sort $Foo->get_method_list ],
224 [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob pie)],
225 '... got the right method list for Foo'
228 # ... test our class creator
230 my $Bar = Class::MOP::Class->create(
232 superclasses => ['Foo'],
234 foo => sub {'Bar::foo'},
235 bar => sub {'Bar::bar'},
238 isa_ok( $Bar, 'Class::MOP::Class' );
240 ok( $Bar->has_method('foo'), '... Bar->has_method(foo)' );
241 ok( $Bar->has_method('bar'), '... Bar->has_method(bar)' );
243 is( Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo' );
244 is( Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar' );
247 $Bar->add_method( 'foo' => sub {'Bar::foo v2'} );
248 }, undef, '... overwriting a method is fine' );
250 is_deeply( [ Class::MOP::get_code_info( $Bar->get_method('foo')->body ) ],
251 [ "Bar", "foo" ], "subname applied to anonymous method" );
253 ok( $Bar->has_method('foo'), '... Bar-> (still) has_method(foo)' );
254 is( Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"' );
257 [ sort $Bar->get_method_list ],
259 '... got the right method list for Bar'
264 map { $_->name => $_ }
265 sort { $a->name cmp $b->name } $Bar->get_all_methods()
268 map { $_->name => $_ }
269 sort { $a->name cmp $b->name } (
270 $Foo->get_method('FOO_CONSTANT'),
271 $Foo->get_method('baaz'),
272 $Foo->get_method('bang'),
273 $Bar->get_method('bar'),
275 map { $Foo->get_method($_) }
284 $Bar->get_method('foo'),
285 $Bar->get_method('meta'),
286 $Foo->get_method('pie'),
287 ( map { $Bar->find_next_method_by_name($_) } @universal_methods )
290 '... got the right list of applicable methods for Bar'
293 my $method = Class::MOP::Method->wrap(
295 package_name => 'Whatever',
296 body => sub {q{I am an object, and I feel an object's pain}},
299 Bar->meta->add_method( $method->name, $method );
301 my $new_method = Bar->meta->get_method('objecty');
303 isnt( $method, $new_method,
304 'add_method clones method objects as they are added' );
305 is( $new_method->original_method, $method,
306 '... the cloned method has the correct original method' )
307 or diag $new_method->dump;
310 package CustomAccessor;
314 my $meta = Class::MOP::Class->initialize(__PACKAGE__);
316 $meta->add_attribute(
323 no warnings 'redefine', 'once';
326 $self->{custom_store} = $_[0];
330 $meta->add_around_method_modifier(
339 return bless {}, shift;
344 my $o = CustomAccessor->new;
350 $o->{custom_store}, $str,
351 'Custom glob-assignment-created accessor still has method modifier'
356 # Since the sub reference below is not a closure, Perl caches it and uses
357 # the same reference each time through the loop. See RT #48985 for the
359 foreach my $ns ( qw( Foo2 Bar2 Baz2 ) ) {
360 my $meta = Class::MOP::Class->create($ns);
364 $meta->add_method( 'foo', $sub );
366 my $method = $meta->get_method('foo');
367 ok( $method, 'Got the foo method back' );
372 package HasConstants;
374 use constant FOO => 1;
375 use constant BAR => [];
376 use constant BAZ => {};
377 use constant UNDEF => undef;
383 my $HC = Class::MOP::Class->initialize('HasConstants');
386 [ sort $HC->get_method_list ],
387 [qw( BAR BAZ FOO UNDEF quux thing )],
388 'get_method_list handles constants properly'
392 [ sort map { $_->name } $HC->_get_local_methods ],
393 [qw( BAR BAZ FOO UNDEF quux thing )],
394 '_get_local_methods handles constants properly'
398 package DeleteFromMe;
403 my $DFMmeta = Class::MOP::Class->initialize('DeleteFromMe');
404 ok($DFMmeta->get_method('foo'));
406 delete $DeleteFromMe::{foo};
408 ok(!$DFMmeta->get_method('foo'));
409 ok(!DeleteFromMe->can('foo'));
413 my $baz_meta = Class::MOP::Class->initialize('Baz');
414 $baz_meta->add_method(foo => sub { });
415 my $stash = Package::Stash->new('Baz');
416 $stash->remove_package_symbol('&foo');
417 is_deeply([$baz_meta->get_method_list], [], "method is deleted");
418 ok(!Baz->can('foo'), "Baz can't foo");