6 use Test::More tests => 56;
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';
24 use constant FOO_CONSTANT => 'Foo-CONSTANT';
26 # define a sub in package
27 sub bar { 'Foo::bar' }
30 { # method named with Sub::Name inside the package scope
32 *{'Foo::floob'} = Sub::Name::subname 'floob' => sub { '!floob!' };
35 # We hateses the "used only once" warnings
36 { my $temp = \&Foo::baz }
41 sub elk { 'OinkyBoinky::elk' }
45 sub Foo::blah { $_[0]->Foo::baz() }
49 *{'Foo::bling'} = sub { '$$Bling$$' };
50 *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub { '!BANG!' };
51 *{'Foo::boom'} = Sub::Name::subname 'boom' => sub { '!BOOM!' };
53 eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }";
57 my $Foo = Class::MOP::Class->initialize('Foo');
59 my $foo = sub { 'Foo::foo' };
61 ok(!UNIVERSAL::isa($foo, 'Class::MOP::Method'), '... our method is not yet blessed');
64 $Foo->add_method('foo' => $foo);
65 } '... we added the method successfully';
67 isa_ok($foo, 'Class::MOP::Method');
69 is($foo->name, 'foo', '... got the right name for the method');
70 is($foo->package_name, 'Foo', '... got the right package name for the method');
72 ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)');
74 is($Foo->get_method('foo'), $foo, '... Foo->get_method(foo) == \&foo');
75 is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"');
77 # now check all our other items ...
79 ok($Foo->has_method('FOO_CONSTANT'), '... Foo->has_method(FOO_CONSTANT) (defined w/ use constant)');
80 ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)');
81 ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)');
82 ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)');
83 ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)');
84 ok($Foo->has_method('bling'), '... Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))');
85 ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)');
86 ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)');
88 my $OinkyBoinky = Class::MOP::Class->initialize('OinkyBoinky');
90 ok($OinkyBoinky->has_method('elk'), "the method 'elk' is defined in OinkyBoinky");
92 ok(!$OinkyBoinky->has_method('bar'), "the method 'bar' is not defined in OinkyBoinky");
94 ok(my $bar = $OinkyBoinky->find_method_by_name('bar'), "but if you look in the inheritence chain then 'bar' does exist");
96 is( reftype($bar), "CODE", "the returned value is a code ref" );
99 # calling get_method blessed them all
100 isa_ok($_, 'Class::MOP::Method') for (
112 package Foo::Aliasing;
114 sub alias_me { '...' }
117 $Foo->alias_method('alias_me' => Foo::Aliasing->meta->get_method('alias_me'));
119 ok(!$Foo->has_method('alias_me'), '... !Foo->has_method(alias_me) (aliased from Foo::Aliasing)');
120 ok(defined &Foo::alias_me, '... Foo does have a symbol table slow for alias_me though');
122 ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)');
123 ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)');
125 ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)');
126 is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef');
129 [ sort $Foo->get_method_list ],
130 [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob foo) ],
131 '... got the right method list for Foo');
134 [ sort { $a->{name} cmp $b->{name} } $Foo->compute_all_applicable_methods() ],
140 code => $Foo->get_method($_)
154 '... got the right list of applicable methods for Foo');
156 is($Foo->remove_method('foo'), $foo, '... removed the foo method');
157 ok(!$Foo->has_method('foo'), '... !Foo->has_method(foo) we just removed it');
158 dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there';
161 [ sort $Foo->get_method_list ],
162 [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob) ],
163 '... got the right method list for Foo');
165 ok($Foo->remove_method('FOO_CONSTANT'), '... removed the FOO_CONSTANT method');
166 ok(!$Foo->has_method('FOO_CONSTANT'), '... !Foo->has_method(FOO_CONSTANT) we just removed it');
167 dies_ok { Foo->FOO_CONSTANT } '... cannot call Foo->FOO_CONSTANT because it is not there';
170 [ sort $Foo->get_method_list ],
171 [ qw(bang bar baz blah bling evaled_foo floob) ],
172 '... got the right method list for Foo');
174 # ... test our class creator
176 my $Bar = Class::MOP::Class->create(
178 superclasses => [ 'Foo' ],
180 foo => sub { 'Bar::foo' },
181 bar => sub { 'Bar::bar' },
184 isa_ok($Bar, 'Class::MOP::Class');
186 ok($Bar->has_method('foo'), '... Bar->has_method(foo)');
187 ok($Bar->has_method('bar'), '... Bar->has_method(bar)');
189 is(Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo');
190 is(Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar');
193 $Bar->add_method('foo' => sub { 'Bar::foo v2' });
194 } '... overwriting a method is fine';
196 ok($Bar->has_method('foo'), '... Bar-> (still) has_method(foo)');
197 is(Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"');
200 [ sort $Bar->get_method_list ],
201 [ qw(bar foo meta) ],
202 '... got the right method list for Bar');
205 [ sort { $a->{name} cmp $b->{name} } $Bar->compute_all_applicable_methods() ],
210 code => $Foo->get_method('bang')
215 code => $Bar->get_method('bar')
221 code => $Foo->get_method($_)
233 code => $Bar->get_method('foo')
238 code => $Bar->get_method('meta')
241 '... got the right list of applicable methods for Bar');