Make alias_method simply call add_method.
[gitmo/Class-MOP.git] / t / 003_methods.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 use Scalar::Util qw/reftype/;
10
11 BEGIN {
12     if ( eval 'use Sub::Name (); 1;' ) {
13         plan tests => 65;
14     }
15     else {
16         plan skip_all => 'These tests require Sub::Name';
17     }
18 }
19
20 BEGIN {
21     use_ok('Class::MOP');   
22     use_ok('Class::MOP::Class');        
23 }
24
25 {   # This package tries to test &has_method 
26     # as exhaustively as possible. More corner
27     # cases are welcome :)
28     package Foo;
29     
30     # import a sub
31     use Scalar::Util 'blessed'; 
32     
33     sub pie;
34     sub cake ();
35
36     use constant FOO_CONSTANT => 'Foo-CONSTANT';
37     
38     # define a sub in package
39     sub bar { 'Foo::bar' } 
40     *baz = \&bar;
41     
42     # create something with the typeglob inside the package
43     *baaz = sub { 'Foo::baaz' };    
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
50     # We hateses the "used only once" warnings
51     { 
52         my $temp1 = \&Foo::baz;
53         my $temp2 = \&Foo::baaz;    
54     }
55     
56     package OinkyBoinky;
57     our @ISA = "Foo";
58     
59     sub elk { 'OinkyBoinky::elk' }
60
61     package main;
62     
63     sub Foo::blah { $_[0]->Foo::baz() }
64     
65     {
66         no strict 'refs';
67         *{'Foo::bling'} = sub { '$$Bling$$' };
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' }";           
72     }
73 }
74
75 my $Foo = Class::MOP::Class->initialize('Foo');
76
77 ok(!$Foo->has_method('pie'), '... got the method stub pie');
78 ok(!$Foo->has_method('cake'), '... got the constant method stub cake');
79
80 my $foo = sub { 'Foo::foo' };
81
82 ok(!UNIVERSAL::isa($foo, 'Class::MOP::Method'), '... our method is not yet blessed');
83
84 lives_ok {
85     $Foo->add_method('foo' => $foo);
86 } '... we added the method successfully';
87
88 my $foo_method = $Foo->get_method('foo');
89
90 isa_ok($foo_method, 'Class::MOP::Method');
91
92 is($foo_method->name, 'foo', '... got the right name for the method');
93 is($foo_method->package_name, 'Foo', '... got the right package name for the method');
94
95 ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)');
96
97 is($Foo->get_method('foo')->body, $foo, '... Foo->get_method(foo) == \&foo');
98 is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"');
99
100 # now check all our other items ...
101
102 ok($Foo->has_method('FOO_CONSTANT'), '... not Foo->has_method(FOO_CONSTANT) (defined w/ use constant)');
103 ok(!$Foo->has_method('bling'), '... not Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))');
104
105 ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)');
106 ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)');
107 ok($Foo->has_method('baaz'), '... Foo->has_method(baaz) (typeglob aliased within Foo)');
108 ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)');
109 ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)');
110 ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)');
111 ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)');
112
113 my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky');
114
115 ok($OinkyBoinky->has_method('elk'), "the method 'elk' is defined in OinkyBoinky");
116
117 ok(!$OinkyBoinky->has_method('bar'), "the method 'bar' is not defined in OinkyBoinky");
118
119 ok(my $bar = $OinkyBoinky->find_method_by_name('bar'), "but if you look in the inheritence chain then 'bar' does exist");
120
121 is( reftype($bar->body), "CODE", "the returned value is a code ref" );
122
123
124 # calling get_method blessed them all
125 for my $method_name (qw/baaz
126                         bar
127                         baz
128                         floob
129                         blah
130                         bang
131                         evaled_foo
132                         FOO_CONSTANT/) {
133     isa_ok($Foo->get_method($method_name), 'Class::MOP::Method');
134     {
135         no strict 'refs';
136         is($Foo->get_method($method_name)->body, \&{'Foo::' . $method_name}, '... body matches CODE ref in package for ' . $method_name);
137     }
138 }
139
140 for my $method_name (qw/
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
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
158 ok($Foo->has_method('alias_me'), '... Foo->has_method(alias_me) (aliased from Foo::Aliasing)');
159 ok(defined &Foo::alias_me, '... Foo does have a symbol table slow for alias_me though');
160
161 ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)');
162 ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)');
163
164 ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)');
165 is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef');
166
167 is_deeply(
168     [ sort $Foo->get_method_list ],
169     [ qw(FOO_CONSTANT alias_me baaz bang bar baz blah evaled_foo floob foo) ],
170     '... got the right method list for Foo');
171
172 is_deeply(
173     [ sort { $a->name cmp $b->name } $Foo->get_all_methods() ],
174     [
175         map { $Foo->get_method($_) } qw(
176             FOO_CONSTANT
177             alias_me
178             baaz            
179             bang 
180             bar 
181             baz 
182             blah 
183             evaled_foo 
184             floob 
185             foo
186         )
187     ],
188     '... got the right list of applicable methods for Foo');
189
190 is($Foo->remove_method('foo')->body, $foo, '... removed the foo method');
191 ok(!$Foo->has_method('foo'), '... !Foo->has_method(foo) we just removed it');
192 dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there';
193
194 is_deeply(
195     [ sort $Foo->get_method_list ],
196     [ qw(FOO_CONSTANT alias_me baaz bang bar baz blah evaled_foo floob) ],
197     '... got the right method list for Foo');
198
199
200 # ... test our class creator 
201
202 my $Bar = Class::MOP::Class->create(
203     package      => '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->get_all_methods() ],
232     [
233         $Foo->get_method('FOO_CONSTANT'),
234         $Foo->get_method('alias_me'),
235         $Foo->get_method('baaz'),
236         $Foo->get_method('bang'),
237         $Bar->get_method('bar'),
238         (map { $Foo->get_method($_) } qw(        
239             baz 
240             blah 
241             evaled_foo 
242             floob 
243         )),
244         $Bar->get_method('foo'),
245         $Bar->get_method('meta'),
246     ],
247     '... got the right list of applicable methods for Bar');
248
249