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