get_all_methods and get_all_attributes
[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 {
2621d59f 12 if ( eval 'use Sub::Name (); 1;' ) {
e896822d 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(
77ff94df 173 [ sort { $a->name cmp $b->name } $Foo->get_all_methods() ],
a5eca695 174 [
77ff94df 175 map { $Foo->get_method($_) } qw(
d7d3f3cb 176 FOO_CONSTANT
46b23b44 177 baaz
a5eca695 178 bang
179 bar
180 baz
181 blah
a5eca695 182 evaled_foo
183 floob
184 foo
185 )
186 ],
187 '... got the right list of applicable methods for Foo');
188
7855ddba 189is($Foo->remove_method('foo')->body, $foo, '... removed the foo method');
c9b8b7f9 190ok(!$Foo->has_method('foo'), '... !Foo->has_method(foo) we just removed it');
191dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there';
192
193is_deeply(
194 [ sort $Foo->get_method_list ],
d7d3f3cb 195 [ qw(FOO_CONSTANT baaz bang bar baz blah evaled_foo floob) ],
c9b8b7f9 196 '... got the right method list for Foo');
197
c9b8b7f9 198
bfe4d0fc 199# ... test our class creator
200
201my $Bar = Class::MOP::Class->create(
99b84658 202 package => 'Bar',
3976fb78 203 superclasses => [ 'Foo' ],
204 methods => {
205 foo => sub { 'Bar::foo' },
206 bar => sub { 'Bar::bar' },
207 }
208);
bfe4d0fc 209isa_ok($Bar, 'Class::MOP::Class');
210
211ok($Bar->has_method('foo'), '... Bar->has_method(foo)');
212ok($Bar->has_method('bar'), '... Bar->has_method(bar)');
213
214is(Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo');
215is(Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar');
216
217lives_ok {
218 $Bar->add_method('foo' => sub { 'Bar::foo v2' });
219} '... overwriting a method is fine';
220
221ok($Bar->has_method('foo'), '... Bar-> (still) has_method(foo)');
222is(Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"');
c9b8b7f9 223
224is_deeply(
225 [ sort $Bar->get_method_list ],
aa448b16 226 [ qw(bar foo meta) ],
a5eca695 227 '... got the right method list for Bar');
228
229is_deeply(
77ff94df 230 [ sort { $a->name cmp $b->name } $Bar->get_all_methods() ],
a5eca695 231 [
77ff94df 232 $Foo->get_method('FOO_CONSTANT'),
233 $Foo->get_method('baaz'),
234 $Foo->get_method('bang'),
235 $Bar->get_method('bar'),
236 (map { $Foo->get_method($_) } qw(
a5eca695 237 baz
238 blah
a5eca695 239 evaled_foo
240 floob
241 )),
77ff94df 242 $Bar->get_method('foo'),
243 $Bar->get_method('meta'),
a5eca695 244 ],
245 '... got the right list of applicable methods for Bar');
246
247