6 use Test::More tests => 66;
9 use Scalar::Util qw/reftype/;
13 use_ok('Class::MOP::Class');
16 { # This package tries to test &has_method
17 # as exhaustively as possible. More corner
18 # cases are welcome :)
22 use Scalar::Util 'blessed';
27 use constant FOO_CONSTANT => 'Foo-CONSTANT';
29 # define a sub in package
30 sub bar { 'Foo::bar' }
33 { # method named with Sub::Name inside the package scope
35 *{'Foo::floob'} = Sub::Name::subname 'floob' => sub { '!floob!' };
38 # We hateses the "used only once" warnings
39 { my $temp = \&Foo::baz }
44 sub elk { 'OinkyBoinky::elk' }
48 sub Foo::blah { $_[0]->Foo::baz() }
52 *{'Foo::bling'} = sub { '$$Bling$$' };
53 *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub { '!BANG!' };
54 *{'Foo::boom'} = Sub::Name::subname 'boom' => sub { '!BOOM!' };
56 eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }";
60 my $Foo = Class::MOP::Class->initialize('Foo');
62 ok(!$Foo->has_method('pie'), '... got the method stub pie');
63 ok(!$Foo->has_method('cake'), '... got the constant method stub cake');
65 my $foo = sub { 'Foo::foo' };
67 ok(!UNIVERSAL::isa($foo, 'Class::MOP::Method'), '... our method is not yet blessed');
70 $Foo->add_method('foo' => $foo);
71 } '... we added the method successfully';
73 my $foo_method = $Foo->get_method('foo');
75 isa_ok($foo_method, 'Class::MOP::Method');
77 is($foo_method->name, 'foo', '... got the right name for the method');
78 is($foo_method->package_name, 'Foo', '... got the right package name for the method');
80 ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)');
82 is($Foo->get_method('foo')->body, $foo, '... Foo->get_method(foo) == \&foo');
83 is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"');
85 # now check all our other items ...
87 if (!$Foo->has_method('FOO_CONSTANT')) {
88 pass('... Foo->has_method(FOO_CONSTANT) (defined w/ use constant)');
93 You are using bleadperl or 5.9.5 which handles constants
94 in a differnt way then prior versions of perl. This will
95 cause this test to break, but the test it not critical
96 to the operation of this module, so I am letting pass
97 with a big FIXME note until I have the tuits to install
100 Of course, patches are *always* welcome :) });
101 pass('... FIXME: Foo->has_method(FOO_CONSTANT) (defined w/ use constant)');
103 ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)');
104 ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)');
105 ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)');
106 ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)');
107 ok($Foo->has_method('bling'), '... Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))');
108 ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)');
109 ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)');
111 my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky');
113 ok($OinkyBoinky->has_method('elk'), "the method 'elk' is defined in OinkyBoinky");
115 ok(!$OinkyBoinky->has_method('bar'), "the method 'bar' is not defined in OinkyBoinky");
117 ok(my $bar = $OinkyBoinky->find_method_by_name('bar'), "but if you look in the inheritence chain then 'bar' does exist");
119 is( reftype($bar->body), "CODE", "the returned value is a code ref" );
122 # calling get_method blessed them all
123 for my $method_name (qw/FOO_CONSTANT
131 isa_ok($Foo->get_method($method_name), 'Class::MOP::Method');
134 is($Foo->get_method($method_name)->body, \&{'Foo::' . $method_name}, '... body matches CODE ref in package for ' . $method_name);
139 package Foo::Aliasing;
141 sub alias_me { '...' }
144 $Foo->alias_method('alias_me' => Foo::Aliasing->meta->get_method('alias_me'));
146 ok(!$Foo->has_method('alias_me'), '... !Foo->has_method(alias_me) (aliased from Foo::Aliasing)');
147 ok(defined &Foo::alias_me, '... Foo does have a symbol table slow for alias_me though');
149 ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)');
150 ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)');
152 ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)');
153 is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef');
156 [ sort $Foo->get_method_list ],
157 [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob foo) ],
158 '... got the right method list for Foo');
161 [ sort { $a->{name} cmp $b->{name} } $Foo->compute_all_applicable_methods() ],
167 code => $Foo->get_method($_)
181 '... got the right list of applicable methods for Foo');
183 is($Foo->remove_method('foo')->body, $foo, '... removed the foo method');
184 ok(!$Foo->has_method('foo'), '... !Foo->has_method(foo) we just removed it');
185 dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there';
188 [ sort $Foo->get_method_list ],
189 [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob) ],
190 '... got the right method list for Foo');
192 ok($Foo->remove_method('FOO_CONSTANT'), '... removed the FOO_CONSTANT method');
193 ok(!$Foo->has_method('FOO_CONSTANT'), '... !Foo->has_method(FOO_CONSTANT) we just removed it');
194 dies_ok { Foo->FOO_CONSTANT } '... cannot call Foo->FOO_CONSTANT because it is not there';
197 [ sort $Foo->get_method_list ],
198 [ qw(bang bar baz blah bling evaled_foo floob) ],
199 '... got the right method list for Foo');
201 # ... test our class creator
203 my $Bar = Class::MOP::Class->create(
205 superclasses => [ 'Foo' ],
207 foo => sub { 'Bar::foo' },
208 bar => sub { 'Bar::bar' },
211 isa_ok($Bar, 'Class::MOP::Class');
213 ok($Bar->has_method('foo'), '... Bar->has_method(foo)');
214 ok($Bar->has_method('bar'), '... Bar->has_method(bar)');
216 is(Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo');
217 is(Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar');
220 $Bar->add_method('foo' => sub { 'Bar::foo v2' });
221 } '... overwriting a method is fine';
223 ok($Bar->has_method('foo'), '... Bar-> (still) has_method(foo)');
224 is(Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"');
227 [ sort $Bar->get_method_list ],
228 [ qw(bar foo meta) ],
229 '... got the right method list for Bar');
232 [ sort { $a->{name} cmp $b->{name} } $Bar->compute_all_applicable_methods() ],
237 code => $Foo->get_method('bang')
242 code => $Bar->get_method('bar')
248 code => $Foo->get_method($_)
260 code => $Bar->get_method('foo')
265 code => $Bar->get_method('meta')
268 '... got the right list of applicable methods for Bar');