6 use Test::More tests => 52;
11 use_ok('Class::MOP::Class');
14 { # This package tries to test &has_method
15 # as exhaustively as possible. More corner
16 # cases are welcome :)
20 use Scalar::Util 'blessed';
22 use constant FOO_CONSTANT => 'Foo-CONSTANT';
24 # define a sub in package
25 sub bar { 'Foo::bar' }
28 { # method named with Sub::Name inside the package scope
30 *{'Foo::floob'} = Sub::Name::subname 'floob' => sub { '!floob!' };
33 # We hateses the "used only once" warnings
34 { my $temp = \&Foo::baz }
38 sub Foo::blah { $_[0]->Foo::baz() }
42 *{'Foo::bling'} = sub { '$$Bling$$' };
43 *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub { '!BANG!' };
44 *{'Foo::boom'} = Sub::Name::subname 'boom' => sub { '!BOOM!' };
46 eval "package Foo; sub evaled_foo { 'Foo::evaled_foo' }";
50 my $Foo = Class::MOP::Class->initialize('Foo');
52 my $foo = sub { 'Foo::foo' };
54 ok(!UNIVERSAL::isa($foo, 'Class::MOP::Method'), '... our method is not yet blessed');
57 $Foo->add_method('foo' => $foo);
58 } '... we added the method successfully';
60 isa_ok($foo, 'Class::MOP::Method');
62 is($foo->name, 'foo', '... got the right name for the method');
63 is($foo->package_name, 'Foo', '... got the right package name for the method');
65 ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)');
67 is($Foo->get_method('foo'), $foo, '... Foo->get_method(foo) == \&foo');
68 is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"');
70 # now check all our other items ...
72 ok($Foo->has_method('FOO_CONSTANT'), '... Foo->has_method(FOO_CONSTANT) (defined w/ use constant)');
73 ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)');
74 ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)');
75 ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)');
76 ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)');
77 ok($Foo->has_method('bling'), '... Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))');
78 ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)');
79 ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)');
81 # calling get_method blessed them all
82 isa_ok($_, 'Class::MOP::Method') for (
94 package Foo::Aliasing;
96 sub alias_me { '...' }
99 $Foo->alias_method('alias_me' => Foo::Aliasing->meta->get_method('alias_me'));
101 ok(!$Foo->has_method('alias_me'), '... !Foo->has_method(alias_me) (aliased from Foo::Aliasing)');
102 ok(defined &Foo::alias_me, '... Foo does have a symbol table slow for alias_me though');
104 ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)');
105 ok(!$Foo->has_method('boom'), '... !Foo->has_method(boom) (defined in main:: using symbol tables and Sub::Name w/out package name)');
107 ok(!$Foo->has_method('not_a_real_method'), '... !Foo->has_method(not_a_real_method) (does not exist)');
108 is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real_method) == undef');
111 [ sort $Foo->get_method_list ],
112 [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob foo) ],
113 '... got the right method list for Foo');
116 [ sort { $a->{name} cmp $b->{name} } $Foo->compute_all_applicable_methods() ],
122 code => $Foo->get_method($_)
136 '... got the right list of applicable methods for Foo');
138 is($Foo->remove_method('foo'), $foo, '... removed the foo method');
139 ok(!$Foo->has_method('foo'), '... !Foo->has_method(foo) we just removed it');
140 dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there';
143 [ sort $Foo->get_method_list ],
144 [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob) ],
145 '... got the right method list for Foo');
147 ok($Foo->remove_method('FOO_CONSTANT'), '... removed the FOO_CONSTANT method');
148 ok(!$Foo->has_method('FOO_CONSTANT'), '... !Foo->has_method(FOO_CONSTANT) we just removed it');
149 dies_ok { Foo->FOO_CONSTANT } '... cannot call Foo->FOO_CONSTANT because it is not there';
152 [ sort $Foo->get_method_list ],
153 [ qw(bang bar baz blah bling evaled_foo floob) ],
154 '... got the right method list for Foo');
156 # ... test our class creator
158 my $Bar = Class::MOP::Class->create(
160 superclasses => [ 'Foo' ],
162 foo => sub { 'Bar::foo' },
163 bar => sub { 'Bar::bar' },
166 isa_ok($Bar, 'Class::MOP::Class');
168 ok($Bar->has_method('foo'), '... Bar->has_method(foo)');
169 ok($Bar->has_method('bar'), '... Bar->has_method(bar)');
171 is(Bar->foo, 'Bar::foo', '... Bar->foo == Bar::foo');
172 is(Bar->bar, 'Bar::bar', '... Bar->bar == Bar::bar');
175 $Bar->add_method('foo' => sub { 'Bar::foo v2' });
176 } '... overwriting a method is fine';
178 ok($Bar->has_method('foo'), '... Bar-> (still) has_method(foo)');
179 is(Bar->foo, 'Bar::foo v2', '... Bar->foo == "Bar::foo v2"');
182 [ sort $Bar->get_method_list ],
183 [ qw(bar foo meta) ],
184 '... got the right method list for Bar');
187 [ sort { $a->{name} cmp $b->{name} } $Bar->compute_all_applicable_methods() ],
192 code => $Foo->get_method('bang')
197 code => $Bar->get_method('bar')
203 code => $Foo->get_method($_)
215 code => $Bar->get_method('foo')
220 code => $Bar->get_method('meta')
223 '... got the right list of applicable methods for Bar');