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