getting closer with the method thing
[gitmo/Class-MOP.git] / t / 003_methods.t
CommitLineData
0882828e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More no_plan => 1;
7use Test::Exception;
c9b8b7f9 8use Test::Deep;
0882828e 9
10BEGIN {
11 use_ok('Class::MOP');
12 use_ok('Class::MOP::Class');
13}
14
bfe4d0fc 15{ # This package tries to test &has_method
16 # as exhaustively as possible. More corner
17 # cases are welcome :)
0882828e 18 package Foo;
19
20 # import a sub
21 use Scalar::Util 'blessed';
22
bfe4d0fc 23 use constant FOO_CONSTANT => 'Foo-CONSTANT';
24
0882828e 25 # define a sub in package
26 sub bar { 'Foo::bar' }
60d90bbc 27 *baz = \&bar;
bfe4d0fc 28
29 { # method named with Sub::Name inside the package scope
30 no strict 'refs';
31 *{'Foo::floob'} = Sub::Name::subname 'floob' => sub { '!floob!' };
32 }
33
60d90bbc 34 # We hateses the "used only once" warnings
35 { my $temp = \&Foo::baz }
36
37 package main;
38
39 sub Foo::blah { $_[0]->Foo::baz() }
40
41 {
42 no strict 'refs';
43 *{'Foo::bling'} = sub { '$$Bling$$' };
bfe4d0fc 44 *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub { '!BANG!' };
45 *{'Foo::boom'} = Sub::Name::subname 'boom' => sub { '!BOOM!' };
46
47 eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }";
60d90bbc 48 }
0882828e 49}
50
bfe4d0fc 51my $Foo = Class::MOP::Class->initialize('Foo');
0882828e 52
53my $foo = sub { 'Foo::foo' };
54
55lives_ok {
56 $Foo->add_method('foo' => $foo);
57} '... we added the method successfully';
58
59ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)');
bfe4d0fc 60
61is($Foo->get_method('foo'), $foo, '... Foo->get_method(foo) == \&foo');
62is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"');
63
64# now check all our other items ...
65
66ok($Foo->has_method('FOO_CONSTANT'), '... Foo->has_method(FOO_CONSTANT) (defined w/ use constant)');
0882828e 67ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)');
60d90bbc 68ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)');
bfe4d0fc 69ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)');
60d90bbc 70ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)');
bfe4d0fc 71ok($Foo->has_method('bling'), '... Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))');
60d90bbc 72ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)');
bfe4d0fc 73ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)');
0882828e 74
bfe4d0fc 75ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)');
76ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)');
0882828e 77
bfe4d0fc 78ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)');
79is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef');
80
c9b8b7f9 81is_deeply(
82 [ sort $Foo->get_method_list ],
83 [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob foo) ],
84 '... got the right method list for Foo');
85
86is($Foo->remove_method('foo'), $foo, '... removed the foo method');
87ok(!$Foo->has_method('foo'), '... !Foo->has_method(foo) we just removed it');
88dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there';
89
90is_deeply(
91 [ sort $Foo->get_method_list ],
92 [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob) ],
93 '... got the right method list for Foo');
94
95ok($Foo->remove_method('FOO_CONSTANT'), '... removed the FOO_CONSTANT method');
96ok(!$Foo->has_method('FOO_CONSTANT'), '... !Foo->has_method(FOO_CONSTANT) we just removed it');
97dies_ok { Foo->FOO_CONSTANT } '... cannot call Foo->FOO_CONSTANT because it is not there';
98
99is_deeply(
100 [ sort $Foo->get_method_list ],
101 [ qw(bang bar baz blah bling evaled_foo floob) ],
102 '... got the right method list for Foo');
103
bfe4d0fc 104# ... test our class creator
105
106my $Bar = Class::MOP::Class->create(
107 'Bar' => '0.10' => (
c9b8b7f9 108 superclasses => [ 'Foo' ],
bfe4d0fc 109 methods => {
110 foo => sub { 'Bar::foo' },
111 bar => sub { 'Bar::bar' },
112 }
113 ));
114isa_ok($Bar, 'Class::MOP::Class');
115
116ok($Bar->has_method('foo'), '... Bar->has_method(foo)');
117ok($Bar->has_method('bar'), '... Bar->has_method(bar)');
118
119is(Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo');
120is(Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar');
121
122lives_ok {
123 $Bar->add_method('foo' => sub { 'Bar::foo v2' });
124} '... overwriting a method is fine';
125
126ok($Bar->has_method('foo'), '... Bar-> (still) has_method(foo)');
127is(Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"');
c9b8b7f9 128
129is_deeply(
130 [ sort $Bar->get_method_list ],
131 [ qw(bar foo) ],
132 '... got the right method list for Bar');