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