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