de128e46d2e4da2e9a6fecb1111fe6f444d0dbd0
[gitmo/Class-MOP.git] / t / 003_methods.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Fatal;
6
7 use Scalar::Util qw/reftype/;
8 use Sub::Name;
9
10 use Class::MOP;
11 use Class::MOP::Class;
12 use Class::MOP::Method;
13
14 {
15     # This package tries to test &has_method as exhaustively as
16     # possible. More corner cases are welcome :)
17     package Foo;
18
19     # import a sub
20     use Scalar::Util 'blessed';
21
22     sub pie;
23     sub cake ();
24
25     use constant FOO_CONSTANT => 'Foo-CONSTANT';
26
27     # define a sub in package
28     sub bar {'Foo::bar'}
29     *baz = \&bar;
30
31     # create something with the typeglob inside the package
32     *baaz = sub {'Foo::baaz'};
33
34     {    # method named with Sub::Name inside the package scope
35         no strict 'refs';
36         *{'Foo::floob'} = Sub::Name::subname 'floob' => sub {'!floob!'};
37     }
38
39     # We hateses the "used only once" warnings
40     {
41         my $temp1 = \&Foo::baz;
42         my $temp2 = \&Foo::baaz;
43     }
44
45     package OinkyBoinky;
46     our @ISA = "Foo";
47
48     sub elk {'OinkyBoinky::elk'}
49
50     package main;
51
52     sub Foo::blah { $_[0]->Foo::baz() }
53
54     {
55         no strict 'refs';
56         *{'Foo::bling'} = sub {'$$Bling$$'};
57         *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub {'!BANG!'};
58         *{'Foo::boom'} = Sub::Name::subname 'boom'      => sub {'!BOOM!'};
59
60         eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }";
61     }
62 }
63
64 my $Foo = Class::MOP::Class->initialize('Foo');
65
66 is join(' ', sort $Foo->get_method_list),
67     'FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob pie';
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' ),
75     '... our method is not yet blessed' );
76
77 ok ! exception {
78     $Foo->add_method( 'foo' => $foo );
79 },
80 '... we added the method successfully';
81
82 my $foo_method = $Foo->get_method('foo');
83
84 isa_ok( $foo_method, 'Class::MOP::Method' );
85
86 is( $foo_method->name, 'foo', '... got the right name for the method' );
87 is( $foo_method->package_name, 'Foo',
88     '... got the right package name for the method' );
89
90 ok( $Foo->has_method('foo'),
91     '... Foo->has_method(foo) (defined with Sub::Name)' );
92
93 is( $Foo->get_method('foo')->body, $foo,
94     '... Foo->get_method(foo) == \&foo' );
95 is( $Foo->get_method('foo')->execute, 'Foo::foo',
96     '... _method_foo->execute returns "Foo::foo"' );
97 is( Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"' );
98
99 # now check all our other items ...
100
101 ok( $Foo->has_method('FOO_CONSTANT'),
102     '... not Foo->has_method(FOO_CONSTANT) (defined w/ use constant)' );
103 ok( !$Foo->has_method('bling'),
104     '... not Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'
105 );
106
107 ok( $Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)' );
108 ok( $Foo->has_method('baz'),
109     '... Foo->has_method(baz) (typeglob aliased within Foo)' );
110 ok( $Foo->has_method('baaz'),
111     '... Foo->has_method(baaz) (typeglob aliased within Foo)' );
112 ok( $Foo->has_method('floob'),
113     '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)'
114 );
115 ok( $Foo->has_method('blah'),
116     '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'
117 );
118 ok( $Foo->has_method('bang'),
119     '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'
120 );
121 ok( $Foo->has_method('evaled_foo'),
122     '... Foo->has_method(evaled_foo) (evaled in main::)' );
123
124 my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky');
125
126 ok( $OinkyBoinky->has_method('elk'),
127     "the method 'elk' is defined in OinkyBoinky" );
128
129 ok( !$OinkyBoinky->has_method('bar'),
130     "the method 'bar' is not defined in OinkyBoinky" );
131
132 ok( my $bar = $OinkyBoinky->find_method_by_name('bar'),
133     "but if you look in the inheritence chain then 'bar' does exist" );
134
135 is( reftype( $bar->body ), "CODE", "the returned value is a code ref" );
136
137 # calling get_method blessed them all
138 for my $method_name (
139     qw/baaz
140     bar
141     baz
142     floob
143     blah
144     bang
145     evaled_foo
146     FOO_CONSTANT/
147     ) {
148     isa_ok( $Foo->get_method($method_name), 'Class::MOP::Method' );
149     {
150         no strict 'refs';
151         is( $Foo->get_method($method_name)->body,
152             \&{ 'Foo::' . $method_name },
153             '... body matches CODE ref in package for ' . $method_name );
154     }
155 }
156
157 for my $method_name (
158     qw/
159     bling
160     /
161     ) {
162     is( ref( $Foo->get_package_symbol( '&' . $method_name ) ), 'CODE',
163         '... got the __ANON__ methods' );
164     {
165         no strict 'refs';
166         is( $Foo->get_package_symbol( '&' . $method_name ),
167             \&{ 'Foo::' . $method_name },
168             '... symbol matches CODE ref in package for ' . $method_name );
169     }
170 }
171
172 ok( !$Foo->has_method('blessed'),
173     '... !Foo->has_method(blessed) (imported into Foo)' );
174 ok( !$Foo->has_method('boom'),
175     '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)'
176 );
177
178 ok( !$Foo->has_method('not_a_real_method'),
179     '... !Foo->has_method(not_a_real_method) (does not exist)' );
180 is( $Foo->get_method('not_a_real_method'), undef,
181     '... Foo->get_method(not_a_real_method) == undef' );
182
183 is_deeply(
184     [ sort $Foo->get_method_list ],
185     [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob foo pie)],
186     '... got the right method list for Foo'
187 );
188
189 is_deeply(
190     [ sort { $a->name cmp $b->name } $Foo->get_all_methods() ],
191     [
192         map { $Foo->get_method($_) }
193             qw(
194             FOO_CONSTANT
195             baaz
196             bang
197             bar
198             baz
199             blah
200             cake
201             evaled_foo
202             floob
203             foo
204             pie
205             )
206     ],
207     '... got the right list of applicable methods for Foo'
208 );
209
210 is( $Foo->remove_method('foo')->body, $foo, '... removed the foo method' );
211 ok( !$Foo->has_method('foo'),
212     '... !Foo->has_method(foo) we just removed it' );
213 ok exception { Foo->foo }, '... cannot call Foo->foo because it is not there';
214
215 is_deeply(
216     [ sort $Foo->get_method_list ],
217     [qw(FOO_CONSTANT baaz bang bar baz blah cake evaled_foo floob pie)],
218     '... got the right method list for Foo'
219 );
220
221 # ... test our class creator
222
223 my $Bar = Class::MOP::Class->create(
224     package      => 'Bar',
225     superclasses => ['Foo'],
226     methods      => {
227         foo => sub {'Bar::foo'},
228         bar => sub {'Bar::bar'},
229     }
230 );
231 isa_ok( $Bar, 'Class::MOP::Class' );
232
233 ok( $Bar->has_method('foo'), '... Bar->has_method(foo)' );
234 ok( $Bar->has_method('bar'), '... Bar->has_method(bar)' );
235
236 is( Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo' );
237 is( Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar' );
238
239 ok ! exception {
240     $Bar->add_method( 'foo' => sub {'Bar::foo v2'} );
241 },
242 '... overwriting a method is fine';
243
244 is_deeply( [ Class::MOP::get_code_info( $Bar->get_method('foo')->body ) ],
245     [ "Bar", "foo" ], "subname applied to anonymous method" );
246
247 ok( $Bar->has_method('foo'), '... Bar-> (still) has_method(foo)' );
248 is( Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"' );
249
250 is_deeply(
251     [ sort $Bar->get_method_list ],
252     [qw(bar foo meta)],
253     '... got the right method list for Bar'
254 );
255
256 is_deeply(
257     [ sort { $a->name cmp $b->name } $Bar->get_all_methods() ],
258     [
259         $Foo->get_method('FOO_CONSTANT'),
260         $Foo->get_method('baaz'),
261         $Foo->get_method('bang'),
262         $Bar->get_method('bar'),
263         (
264             map { $Foo->get_method($_) }
265                 qw(
266                 baz
267                 blah
268                 cake
269                 evaled_foo
270                 floob
271                 )
272         ),
273         $Bar->get_method('foo'),
274         $Bar->get_method('meta'),
275         $Foo->get_method('pie'),
276     ],
277     '... got the right list of applicable methods for Bar'
278 );
279
280 my $method = Class::MOP::Method->wrap(
281     name         => 'objecty',
282     package_name => 'Whatever',
283     body         => sub {q{I am an object, and I feel an object's pain}},
284 );
285
286 Bar->meta->add_method( $method->name, $method );
287
288 my $new_method = Bar->meta->get_method('objecty');
289
290 isnt( $method, $new_method,
291     'add_method clones method objects as they are added' );
292 is( $new_method->original_method, $method,
293     '... the cloned method has the correct original method' )
294         or diag $new_method->dump;
295
296 {
297     package CustomAccessor;
298
299     use Class::MOP;
300
301     my $meta = Class::MOP::Class->initialize(__PACKAGE__);
302
303     $meta->add_attribute(
304         foo => (
305             accessor => 'foo',
306         )
307     );
308
309     {
310         no warnings 'redefine', 'once';
311         *foo = sub {
312             my $self = shift;
313             $self->{custom_store} = $_[0];
314         };
315     }
316
317     $meta->add_around_method_modifier(
318         'foo',
319         sub {
320             my $orig = shift;
321             $orig->(@_);
322         }
323     );
324
325     sub new {
326         return bless {}, shift;
327     }
328 }
329
330 {
331     my $o   = CustomAccessor->new;
332     my $str = 'string';
333
334     $o->foo($str);
335
336     is(
337         $o->{custom_store}, $str,
338         'Custom glob-assignment-created accessor still has method modifier'
339     );
340 }
341
342 {
343     # Since the sub reference below is not a closure, Perl caches it and uses
344     # the same reference each time through the loop. See RT #48985 for the
345     # bug.
346     foreach my $ns ( qw( Foo2 Bar2 Baz2 ) ) {
347         my $meta = Class::MOP::Class->create($ns);
348
349         my $sub = sub { };
350
351         $meta->add_method( 'foo', $sub );
352
353         my $method = $meta->get_method('foo');
354         ok( $method, 'Got the foo method back' );
355     }
356 }
357
358 {
359     package HasConstants;
360
361     use constant FOO   => 1;
362     use constant BAR   => [];
363     use constant BAZ   => {};
364     use constant UNDEF => undef;
365
366     sub quux  {1}
367     sub thing {1}
368 }
369
370 my $HC = Class::MOP::Class->initialize('HasConstants');
371
372 is_deeply(
373     [ sort $HC->get_method_list ],
374     [qw( BAR BAZ FOO UNDEF quux thing )],
375     'get_method_list handles constants properly'
376 );
377
378 is_deeply(
379     [ sort map { $_->name } $HC->_get_local_methods ],
380     [qw( BAR BAZ FOO UNDEF quux thing )],
381     '_get_local_methods handles constants properly'
382 );
383
384
385 done_testing;