Commit | Line | Data |
0882828e |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More no_plan => 1; |
7 | use Test::Exception; |
c9b8b7f9 |
8 | use Test::Deep; |
0882828e |
9 | |
10 | BEGIN { |
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 |
51 | my $Foo = Class::MOP::Class->initialize('Foo'); |
0882828e |
52 | |
53 | my $foo = sub { 'Foo::foo' }; |
54 | |
55 | lives_ok { |
56 | $Foo->add_method('foo' => $foo); |
57 | } '... we added the method successfully'; |
58 | |
59 | ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)'); |
bfe4d0fc |
60 | |
61 | is($Foo->get_method('foo'), $foo, '... Foo->get_method(foo) == \&foo'); |
62 | is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"'); |
63 | |
64 | # now check all our other items ... |
65 | |
66 | ok($Foo->has_method('FOO_CONSTANT'), '... Foo->has_method(FOO_CONSTANT) (defined w/ use constant)'); |
0882828e |
67 | ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)'); |
60d90bbc |
68 | ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)'); |
bfe4d0fc |
69 | ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)'); |
60d90bbc |
70 | ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'); |
bfe4d0fc |
71 | ok($Foo->has_method('bling'), '... Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'); |
60d90bbc |
72 | ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'); |
bfe4d0fc |
73 | ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)'); |
0882828e |
74 | |
bfe4d0fc |
75 | ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)'); |
76 | ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)'); |
0882828e |
77 | |
bfe4d0fc |
78 | ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)'); |
79 | is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef'); |
80 | |
c9b8b7f9 |
81 | is_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 | |
86 | is($Foo->remove_method('foo'), $foo, '... removed the foo method'); |
87 | ok(!$Foo->has_method('foo'), '... !Foo->has_method(foo) we just removed it'); |
88 | dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there'; |
89 | |
90 | is_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 | |
95 | ok($Foo->remove_method('FOO_CONSTANT'), '... removed the FOO_CONSTANT method'); |
96 | ok(!$Foo->has_method('FOO_CONSTANT'), '... !Foo->has_method(FOO_CONSTANT) we just removed it'); |
97 | dies_ok { Foo->FOO_CONSTANT } '... cannot call Foo->FOO_CONSTANT because it is not there'; |
98 | |
99 | is_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 | |
106 | my $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 | )); |
114 | isa_ok($Bar, 'Class::MOP::Class'); |
115 | |
116 | ok($Bar->has_method('foo'), '... Bar->has_method(foo)'); |
117 | ok($Bar->has_method('bar'), '... Bar->has_method(bar)'); |
118 | |
119 | is(Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo'); |
120 | is(Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar'); |
121 | |
122 | lives_ok { |
123 | $Bar->add_method('foo' => sub { 'Bar::foo v2' }); |
124 | } '... overwriting a method is fine'; |
125 | |
126 | ok($Bar->has_method('foo'), '... Bar-> (still) has_method(foo)'); |
127 | is(Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"'); |
c9b8b7f9 |
128 | |
129 | is_deeply( |
130 | [ sort $Bar->get_method_list ], |
131 | [ qw(bar foo) ], |
132 | '... got the right method list for Bar'); |