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 );
80 '... we added the method successfully';
82 my $foo_method = $Foo->get_method('foo');
84 isa_ok( $foo_method, 'Class::MOP::Method' );
86 is( $foo_method->name, 'foo', '... got the right name for the method' );
87 is( $foo_method->package_name, 'Foo',
88 '... got the right package name for the method' );
90 ok( $Foo->has_method('foo'),
91 '... Foo->has_method(foo) (defined with Sub::Name)' );
93 is( $Foo->get_method('foo')->body, $foo,
94 '... Foo->get_method(foo) == \&foo' );
95 is( $Foo->get_method('foo')->execute, 'Foo::foo',
96 '... _method_foo->execute returns "Foo::foo"' );
97 is( Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"' );
99 # now check all our other items ...
101 ok( $Foo->has_method('FOO_CONSTANT'),
102 '... not Foo->has_method(FOO_CONSTANT) (defined w/ use constant)' );
103 ok( !$Foo->has_method('bling'),
104 '... not Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'
107 ok( $Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)' );
108 ok( $Foo->has_method('baz'),
109 '... Foo->has_method(baz) (typeglob aliased within Foo)' );
110 ok( $Foo->has_method('baaz'),
111 '... Foo->has_method(baaz) (typeglob aliased within Foo)' );
112 ok( $Foo->has_method('floob'),
113 '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)'
115 ok( $Foo->has_method('blah'),
116 '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'
118 ok( $Foo->has_method('bang'),
119 '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'
121 ok( $Foo->has_method('evaled_foo'),
122 '... Foo->has_method(evaled_foo) (evaled in main::)' );
124 my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky');
126 ok( $OinkyBoinky->has_method('elk'),
127 "the method 'elk' is defined in OinkyBoinky" );
129 ok( !$OinkyBoinky->has_method('bar'),
130 "the method 'bar' is not defined in OinkyBoinky" );
132 ok( my $bar = $OinkyBoinky->find_method_by_name('bar'),
133 "but if you look in the inheritence chain then 'bar' does exist" );
135 is( reftype( $bar->body ), "CODE", "the returned value is a code ref" );
137 # calling get_method blessed them all
138 for my $method_name (
148 isa_ok( $Foo->get_method($method_name), 'Class::MOP::Method' );
151 is( $Foo->get_method($method_name)->body,
152 \&{ 'Foo::' . $method_name },
153 '... body matches CODE ref in package for ' . $method_name );
157 for my $method_name (
162 is( ref( $Foo->get_package_symbol( '&' . $method_name ) ), 'CODE',
163 '... got the __ANON__ methods' );
166 is( $Foo->get_package_symbol( '&' . $method_name ),
167 \&{ 'Foo::' . $method_name },
168 '... symbol matches CODE ref in package for ' . $method_name );
172 ok( !$Foo->has_method('blessed'),
173 '... !Foo->has_method(blessed) (imported into Foo)' );
174 ok( !$Foo->has_method('boom'),
175 '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)'
178 ok( !$Foo->has_method('not_a_real_method'),
179 '... !Foo->has_method(not_a_real_method) (does not exist)' );
180 is( $Foo->get_method('not_a_real_method'), undef,
181 '... Foo->get_method(not_a_real_method) == undef' );
184 [ sort $Foo->get_method_list ],
185 [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob foo pie)],
186 '... got the right method list for Foo'
190 [ sort { $a->name cmp $b->name } $Foo->get_all_methods() ],
192 map { $Foo->get_method($_) }
207 '... got the right list of applicable methods for Foo'
210 is( $Foo->remove_method('foo')->body, $foo, '... removed the foo method' );
211 ok( !$Foo->has_method('foo'),
212 '... !Foo->has_method(foo) we just removed it' );
213 ok exception { Foo->foo }, '... cannot call Foo->foo because it is not there';
216 [ sort $Foo->get_method_list ],
217 [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob pie)],
218 '... got the right method list for Foo'
221 # ... test our class creator
223 my $Bar = Class::MOP::Class->create(
225 superclasses => ['Foo'],
227 foo => sub {'Bar::foo'},
228 bar => sub {'Bar::bar'},
231 isa_ok( $Bar, 'Class::MOP::Class' );
233 ok( $Bar->has_method('foo'), '... Bar->has_method(foo)' );
234 ok( $Bar->has_method('bar'), '... Bar->has_method(bar)' );
236 is( Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo' );
237 is( Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar' );
240 $Bar->add_method( 'foo' => sub {'Bar::foo v2'} );
242 '... overwriting a method is fine';
244 is_deeply( [ Class::MOP::get_code_info( $Bar->get_method('foo')->body ) ],
245 [ "Bar", "foo" ], "subname applied to anonymous method" );
247 ok( $Bar->has_method('foo'), '... Bar-> (still) has_method(foo)' );
248 is( Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"' );
251 [ sort $Bar->get_method_list ],
253 '... got the right method list for Bar'
257 [ sort { $a->name cmp $b->name } $Bar->get_all_methods() ],
259 $Foo->get_method('FOO_CONSTANT'),
260 $Foo->get_method('baaz'),
261 $Foo->get_method('bang'),
262 $Bar->get_method('bar'),
264 map { $Foo->get_method($_) }
273 $Bar->get_method('foo'),
274 $Bar->get_method('meta'),
275 $Foo->get_method('pie'),
277 '... got the right list of applicable methods for Bar'
280 my $method = Class::MOP::Method->wrap(
282 package_name => 'Whatever',
283 body => sub {q{I am an object, and I feel an object's pain}},
286 Bar->meta->add_method( $method->name, $method );
288 my $new_method = Bar->meta->get_method('objecty');
290 isnt( $method, $new_method,
291 'add_method clones method objects as they are added' );
292 is( $new_method->original_method, $method,
293 '... the cloned method has the correct original method' )
294 or diag $new_method->dump;
297 package CustomAccessor;
301 my $meta = Class::MOP::Class->initialize(__PACKAGE__);
303 $meta->add_attribute(
310 no warnings 'redefine', 'once';
313 $self->{custom_store} = $_[0];
317 $meta->add_around_method_modifier(
326 return bless {}, shift;
331 my $o = CustomAccessor->new;
337 $o->{custom_store}, $str,
338 'Custom glob-assignment-created accessor still has method modifier'
343 # Since the sub reference below is not a closure, Perl caches it and uses
344 # the same reference each time through the loop. See RT #48985 for the
346 foreach my $ns ( qw( Foo2 Bar2 Baz2 ) ) {
347 my $meta = Class::MOP::Class->create($ns);
351 $meta->add_method( 'foo', $sub );
353 my $method = $meta->get_method('foo');
354 ok( $method, 'Got the foo method back' );
359 package HasConstants;
361 use constant FOO => 1;
362 use constant BAR => [];
363 use constant BAZ => {};
364 use constant UNDEF => undef;
370 my $HC = Class::MOP::Class->initialize('HasConstants');
373 [ sort $HC->get_method_list ],
374 [qw( BAR BAZ FOO UNDEF quux thing )],
375 'get_method_list handles constants properly'
379 [ sort map { $_->name } $HC->_get_local_methods ],
380 [qw( BAR BAZ FOO UNDEF quux thing )],
381 '_get_local_methods handles constants properly'