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