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