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