0.58
[gitmo/Class-MOP.git] / t / 003_methods.t
CommitLineData
0882828e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
d7d3f3cb 6use Test::More tests => 65;
0882828e 7use Test::Exception;
8
16e960bd 9use Scalar::Util qw/reftype/;
87f3c133 10use Sub::Name ();
16e960bd 11
0882828e 12BEGIN {
13 use_ok('Class::MOP');
14 use_ok('Class::MOP::Class');
15}
16
bfe4d0fc 17{ # This package tries to test &has_method
18 # as exhaustively as possible. More corner
19 # cases are welcome :)
0882828e 20 package Foo;
21
22 # import a sub
23 use Scalar::Util 'blessed';
24
823a5d31 25 sub pie;
26 sub cake ();
27
bfe4d0fc 28 use constant FOO_CONSTANT => 'Foo-CONSTANT';
29
0882828e 30 # define a sub in package
31 sub bar { 'Foo::bar' }
60d90bbc 32 *baz = \&bar;
46b23b44 33
34 # create something with the typeglob inside the package
35 *baaz = sub { 'Foo::baaz' };
bfe4d0fc 36
37 { # method named with Sub::Name inside the package scope
38 no strict 'refs';
39 *{'Foo::floob'} = Sub::Name::subname 'floob' => sub { '!floob!' };
40 }
41
60d90bbc 42 # We hateses the "used only once" warnings
46b23b44 43 {
44 my $temp1 = \&Foo::baz;
45 my $temp2 = \&Foo::baaz;
46 }
16e960bd 47
48 package OinkyBoinky;
49 our @ISA = "Foo";
50
51 sub elk { 'OinkyBoinky::elk' }
60d90bbc 52
53 package main;
54
55 sub Foo::blah { $_[0]->Foo::baz() }
56
57 {
58 no strict 'refs';
59 *{'Foo::bling'} = sub { '$$Bling$$' };
bfe4d0fc 60 *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub { '!BANG!' };
61 *{'Foo::boom'} = Sub::Name::subname 'boom' => sub { '!BOOM!' };
62
63 eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }";
60d90bbc 64 }
0882828e 65}
66
bfe4d0fc 67my $Foo = Class::MOP::Class->initialize('Foo');
0882828e 68
823a5d31 69ok(!$Foo->has_method('pie'), '... got the method stub pie');
70ok(!$Foo->has_method('cake'), '... got the constant method stub cake');
71
0882828e 72my $foo = sub { 'Foo::foo' };
73
de19f115 74ok(!UNIVERSAL::isa($foo, 'Class::MOP::Method'), '... our method is not yet blessed');
75
0882828e 76lives_ok {
77 $Foo->add_method('foo' => $foo);
78} '... we added the method successfully';
79
7855ddba 80my $foo_method = $Foo->get_method('foo');
de19f115 81
7855ddba 82isa_ok($foo_method, 'Class::MOP::Method');
83
84is($foo_method->name, 'foo', '... got the right name for the method');
85is($foo_method->package_name, 'Foo', '... got the right package name for the method');
de19f115 86
0882828e 87ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)');
bfe4d0fc 88
7855ddba 89is($Foo->get_method('foo')->body, $foo, '... Foo->get_method(foo) == \&foo');
bfe4d0fc 90is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"');
91
92# now check all our other items ...
93
d7d3f3cb 94ok($Foo->has_method('FOO_CONSTANT'), '... not Foo->has_method(FOO_CONSTANT) (defined w/ use constant)');
95ok(!$Foo->has_method('bling'), '... not Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))');
46b23b44 96
0882828e 97ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)');
60d90bbc 98ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)');
46b23b44 99ok($Foo->has_method('baaz'), '... Foo->has_method(baaz) (typeglob aliased within Foo)');
bfe4d0fc 100ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)');
60d90bbc 101ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)');
60d90bbc 102ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)');
bfe4d0fc 103ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)');
0882828e 104
16e960bd 105my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky');
106
107ok($OinkyBoinky->has_method('elk'), "the method 'elk' is defined in OinkyBoinky");
108
109ok(!$OinkyBoinky->has_method('bar'), "the method 'bar' is not defined in OinkyBoinky");
110
111ok(my $bar = $OinkyBoinky->find_method_by_name('bar'), "but if you look in the inheritence chain then 'bar' does exist");
112
b9575695 113is( reftype($bar->body), "CODE", "the returned value is a code ref" );
16e960bd 114
115
de19f115 116# calling get_method blessed them all
46b23b44 117for my $method_name (qw/baaz
118 bar
7855ddba 119 baz
120 floob
46b23b44 121 blah
122 bang
d7d3f3cb 123 evaled_foo
124 FOO_CONSTANT/) {
7855ddba 125 isa_ok($Foo->get_method($method_name), 'Class::MOP::Method');
126 {
127 no strict 'refs';
187c832e 128 is($Foo->get_method($method_name)->body, \&{'Foo::' . $method_name}, '... body matches CODE ref in package for ' . $method_name);
7855ddba 129 }
130}
de19f115 131
46b23b44 132for my $method_name (qw/
46b23b44 133 bling
134 /) {
135 is(ref($Foo->get_package_symbol('&' . $method_name)), 'CODE', '... got the __ANON__ methods');
136 {
137 no strict 'refs';
138 is($Foo->get_package_symbol('&' . $method_name), \&{'Foo::' . $method_name}, '... symbol matches CODE ref in package for ' . $method_name);
139 }
140}
141
663f8198 142{
143 package Foo::Aliasing;
144 use metaclass;
145 sub alias_me { '...' }
146}
147
148$Foo->alias_method('alias_me' => Foo::Aliasing->meta->get_method('alias_me'));
149
150ok(!$Foo->has_method('alias_me'), '... !Foo->has_method(alias_me) (aliased from Foo::Aliasing)');
151ok(defined &Foo::alias_me, '... Foo does have a symbol table slow for alias_me though');
152
bfe4d0fc 153ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)');
154ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)');
0882828e 155
bfe4d0fc 156ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)');
157is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef');
158
c9b8b7f9 159is_deeply(
160 [ sort $Foo->get_method_list ],
d7d3f3cb 161 [ qw(FOO_CONSTANT baaz bang bar baz blah evaled_foo floob foo) ],
c9b8b7f9 162 '... got the right method list for Foo');
163
a5eca695 164is_deeply(
165 [ sort { $a->{name} cmp $b->{name} } $Foo->compute_all_applicable_methods() ],
166 [
167 map {
168 {
169 name => $_,
170 class => 'Foo',
7855ddba 171 code => $Foo->get_method($_)
a5eca695 172 }
173 } qw(
d7d3f3cb 174 FOO_CONSTANT
46b23b44 175 baaz
a5eca695 176 bang
177 bar
178 baz
179 blah
a5eca695 180 evaled_foo
181 floob
182 foo
183 )
184 ],
185 '... got the right list of applicable methods for Foo');
186
7855ddba 187is($Foo->remove_method('foo')->body, $foo, '... removed the foo method');
c9b8b7f9 188ok(!$Foo->has_method('foo'), '... !Foo->has_method(foo) we just removed it');
189dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there';
190
191is_deeply(
192 [ sort $Foo->get_method_list ],
d7d3f3cb 193 [ qw(FOO_CONSTANT baaz bang bar baz blah evaled_foo floob) ],
c9b8b7f9 194 '... got the right method list for Foo');
195
c9b8b7f9 196
bfe4d0fc 197# ... test our class creator
198
199my $Bar = Class::MOP::Class->create(
88dd563c 200 'Bar' => (
c9b8b7f9 201 superclasses => [ 'Foo' ],
bfe4d0fc 202 methods => {
203 foo => sub { 'Bar::foo' },
204 bar => sub { 'Bar::bar' },
205 }
206 ));
207isa_ok($Bar, 'Class::MOP::Class');
208
209ok($Bar->has_method('foo'), '... Bar->has_method(foo)');
210ok($Bar->has_method('bar'), '... Bar->has_method(bar)');
211
212is(Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo');
213is(Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar');
214
215lives_ok {
216 $Bar->add_method('foo' => sub { 'Bar::foo v2' });
217} '... overwriting a method is fine';
218
219ok($Bar->has_method('foo'), '... Bar-> (still) has_method(foo)');
220is(Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"');
c9b8b7f9 221
222is_deeply(
223 [ sort $Bar->get_method_list ],
aa448b16 224 [ qw(bar foo meta) ],
a5eca695 225 '... got the right method list for Bar');
226
227is_deeply(
228 [ sort { $a->{name} cmp $b->{name} } $Bar->compute_all_applicable_methods() ],
229 [
230 {
d7d3f3cb 231 name => 'FOO_CONSTANT',
232 class => 'Foo',
233 code => $Foo->get_method('FOO_CONSTANT')
234 },
235 {
46b23b44 236 name => 'baaz',
237 class => 'Foo',
238 code => $Foo->get_method('baaz')
239 },
240 {
a5eca695 241 name => 'bang',
242 class => 'Foo',
7855ddba 243 code => $Foo->get_method('bang')
a5eca695 244 },
245 {
246 name => 'bar',
247 class => 'Bar',
7855ddba 248 code => $Bar->get_method('bar')
a5eca695 249 },
250 (map {
251 {
252 name => $_,
253 class => 'Foo',
7855ddba 254 code => $Foo->get_method($_)
a5eca695 255 }
256 } qw(
257 baz
258 blah
a5eca695 259 evaled_foo
260 floob
261 )),
262 {
263 name => 'foo',
264 class => 'Bar',
7855ddba 265 code => $Bar->get_method('foo')
a5eca695 266 },
aa448b16 267 {
268 name => 'meta',
269 class => 'Bar',
7855ddba 270 code => $Bar->get_method('meta')
aa448b16 271 }
a5eca695 272 ],
273 '... got the right list of applicable methods for Bar');
274
275