4 use Test::More tests => 67;
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 ok( $Foo->has_method('pie'), '... got the method stub pie' );
67 ok( $Foo->has_method('cake'), '... got the constant method stub cake' );
69 my $foo = sub {'Foo::foo'};
71 ok( !UNIVERSAL::isa( $foo, 'Class::MOP::Method' ),
72 '... our method is not yet blessed' );
75 $Foo->add_method( 'foo' => $foo );
77 '... we added the method successfully';
79 my $foo_method = $Foo->get_method('foo');
81 isa_ok( $foo_method, 'Class::MOP::Method' );
83 is( $foo_method->name, 'foo', '... got the right name for the method' );
84 is( $foo_method->package_name, 'Foo',
85 '... got the right package name for the method' );
87 ok( $Foo->has_method('foo'),
88 '... Foo->has_method(foo) (defined with Sub::Name)' );
90 is( $Foo->get_method('foo')->body, $foo,
91 '... Foo->get_method(foo) == \&foo' );
92 is( $Foo->get_method('foo')->execute, 'Foo::foo',
93 '... _method_foo->execute returns "Foo::foo"' );
94 is( Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"' );
96 # now check all our other items ...
98 ok( $Foo->has_method('FOO_CONSTANT'),
99 '... not Foo->has_method(FOO_CONSTANT) (defined w/ use constant)' );
100 ok( !$Foo->has_method('bling'),
101 '... not Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'
104 ok( $Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)' );
105 ok( $Foo->has_method('baz'),
106 '... Foo->has_method(baz) (typeglob aliased within Foo)' );
107 ok( $Foo->has_method('baaz'),
108 '... Foo->has_method(baaz) (typeglob aliased within Foo)' );
109 ok( $Foo->has_method('floob'),
110 '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)'
112 ok( $Foo->has_method('blah'),
113 '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'
115 ok( $Foo->has_method('bang'),
116 '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'
118 ok( $Foo->has_method('evaled_foo'),
119 '... Foo->has_method(evaled_foo) (evaled in main::)' );
121 my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky');
123 ok( $OinkyBoinky->has_method('elk'),
124 "the method 'elk' is defined in OinkyBoinky" );
126 ok( !$OinkyBoinky->has_method('bar'),
127 "the method 'bar' is not defined in OinkyBoinky" );
129 ok( my $bar = $OinkyBoinky->find_method_by_name('bar'),
130 "but if you look in the inheritence chain then 'bar' does exist" );
132 is( reftype( $bar->body ), "CODE", "the returned value is a code ref" );
134 # calling get_method blessed them all
135 for my $method_name (
145 isa_ok( $Foo->get_method($method_name), 'Class::MOP::Method' );
148 is( $Foo->get_method($method_name)->body,
149 \&{ 'Foo::' . $method_name },
150 '... body matches CODE ref in package for ' . $method_name );
154 for my $method_name (
159 is( ref( $Foo->get_package_symbol( '&' . $method_name ) ), 'CODE',
160 '... got the __ANON__ methods' );
163 is( $Foo->get_package_symbol( '&' . $method_name ),
164 \&{ 'Foo::' . $method_name },
165 '... symbol matches CODE ref in package for ' . $method_name );
169 ok( !$Foo->has_method('blessed'),
170 '... !Foo->has_method(blessed) (imported into Foo)' );
171 ok( !$Foo->has_method('boom'),
172 '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)'
175 ok( !$Foo->has_method('not_a_real_method'),
176 '... !Foo->has_method(not_a_real_method) (does not exist)' );
177 is( $Foo->get_method('not_a_real_method'), undef,
178 '... Foo->get_method(not_a_real_method) == undef' );
181 [ sort $Foo->get_method_list ],
182 [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob foo pie)],
183 '... got the right method list for Foo'
187 [ sort { $a->name cmp $b->name } $Foo->get_all_methods() ],
189 map { $Foo->get_method($_) }
204 '... got the right list of applicable methods for Foo'
207 is( $Foo->remove_method('foo')->body, $foo, '... removed the foo method' );
208 ok( !$Foo->has_method('foo'),
209 '... !Foo->has_method(foo) we just removed it' );
210 ok( !$Foo->get_method_map->{foo}, 'foo is not in the method map' );
211 dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there';
214 [ sort $Foo->get_method_list ],
215 [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob pie)],
216 '... got the right method list for Foo'
219 # ... test our class creator
221 my $Bar = Class::MOP::Class->create(
223 superclasses => ['Foo'],
225 foo => sub {'Bar::foo'},
226 bar => sub {'Bar::bar'},
229 isa_ok( $Bar, 'Class::MOP::Class' );
231 ok( $Bar->has_method('foo'), '... Bar->has_method(foo)' );
232 ok( $Bar->has_method('bar'), '... Bar->has_method(bar)' );
234 is( Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo' );
235 is( Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar' );
238 $Bar->add_method( 'foo' => sub {'Bar::foo v2'} );
240 '... overwriting a method is fine';
242 is_deeply( [ Class::MOP::get_code_info( $Bar->get_method('foo')->body ) ],
243 [ "Bar", "foo" ], "subname applied to anonymous method" );
245 ok( $Bar->has_method('foo'), '... Bar-> (still) has_method(foo)' );
246 is( Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"' );
249 [ sort $Bar->get_method_list ],
251 '... got the right method list for Bar'
255 [ sort { $a->name cmp $b->name } $Bar->get_all_methods() ],
257 $Foo->get_method('FOO_CONSTANT'),
258 $Foo->get_method('baaz'),
259 $Foo->get_method('bang'),
260 $Bar->get_method('bar'),
262 map { $Foo->get_method($_) }
271 $Bar->get_method('foo'),
272 $Bar->get_method('meta'),
273 $Foo->get_method('pie'),
275 '... got the right list of applicable methods for Bar'
278 my $method = Class::MOP::Method->wrap(
280 package_name => 'Whatever',
281 body => sub {q{I am an object, and I feel an object's pain}},
284 Bar->meta->add_method( $method->name, $method );
286 my $new_method = Bar->meta->get_method('objecty');
288 isnt( $method, $new_method,
289 'add_method clones method objects as they are added' );
290 is( $new_method->original_method, $method,
291 '... the cloned method has the correct original method' )
292 or diag $new_method->dump;
295 package CustomAccessor;
299 my $meta = Class::MOP::Class->initialize(__PACKAGE__);
301 $meta->add_attribute(
308 no warnings 'redefine', 'once';
311 $self->{custom_store} = $_[0];
315 $meta->add_around_method_modifier(
323 $meta->add_method( 'new', sub { return bless {}, shift } );
327 my $o = CustomAccessor->new;
333 $o->{custom_store}, $str,
334 'Custom glob-assignment-created accessor is still method modifier is added'