Class::MOP - closer
[gitmo/Class-MOP.git] / t / 003_methods.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More no_plan => 1;
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 lives_ok {
55     $Foo->add_method('foo' => $foo);
56 } '... we added the method successfully';
57
58 ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)');
59
60 is($Foo->get_method('foo'), $foo, '... Foo->get_method(foo) == \&foo');
61 is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"');
62
63 # now check all our other items ...
64
65 ok($Foo->has_method('FOO_CONSTANT'), '... Foo->has_method(FOO_CONSTANT) (defined w/ use constant)');
66 ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)');
67 ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)');
68 ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)');
69 ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)');
70 ok($Foo->has_method('bling'), '... Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))');
71 ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)');
72 ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)');
73
74 ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)');
75 ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)');
76
77 ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)');
78 is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef');
79
80 # ... test our class creator 
81
82 my $Bar = Class::MOP::Class->create(
83             'Bar' => '0.10' => (
84                 methods => {
85                     foo => sub { 'Bar::foo' },
86                     bar => sub { 'Bar::bar' },                    
87                 }
88             ));
89 isa_ok($Bar, 'Class::MOP::Class');
90
91 ok($Bar->has_method('foo'), '... Bar->has_method(foo)');
92 ok($Bar->has_method('bar'), '... Bar->has_method(bar)');
93
94 is(Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo');
95 is(Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar');
96
97 lives_ok {
98     $Bar->add_method('foo' => sub { 'Bar::foo v2' });
99 } '... overwriting a method is fine';
100
101 ok($Bar->has_method('foo'), '... Bar-> (still) has_method(foo)');
102 is(Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"');