From: Stevan Little Date: Sun, 18 May 2008 17:18:03 +0000 (+0000) Subject: fixing the get_method_map stuff X-Git-Tag: 0_64~63 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=46b23b447c9ab2e7681b7377d505f2ad6fbc39b3;p=gitmo%2FClass-MOP.git fixing the get_method_map stuff --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 7128954..47a65f1 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -28,6 +28,10 @@ BEGIN { # get it from MRO::Compat now ... *check_package_cache_flag = \&mro::get_pkg_gen; + + # UNCOMMENT ME TO TEST WITHOUT XS + #no warnings 'prototype', 'redefine'; + #*check_package_cache_flag = \&MRO::Compat::__get_pkg_gen_pp } { diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 304f9dc..cb5c463 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -321,8 +321,11 @@ sub get_method_map { $map->{$symbol}->body == $code; my ($pkg, $name) = Class::MOP::get_code_info($code); - next if ($pkg || '') ne $class_name && - ($name || '') ne '__ANON__'; + + next if ($pkg || '') ne $class_name || + (($name || '') ne '__ANON__' && ($pkg || '') ne $class_name); + + #warn "Checking $pkg against $class_name && $name against __ANON__"; $map->{$symbol} = $method_metaclass->wrap($code); } diff --git a/t/003_methods.t b/t/003_methods.t index 9dfcb9e..0946d49 100644 --- a/t/003_methods.t +++ b/t/003_methods.t @@ -29,6 +29,9 @@ BEGIN { # define a sub in package sub bar { 'Foo::bar' } *baz = \&bar; + + # create something with the typeglob inside the package + *baaz = sub { 'Foo::baaz' }; { # method named with Sub::Name inside the package scope no strict 'refs'; @@ -36,7 +39,10 @@ BEGIN { } # We hateses the "used only once" warnings - { my $temp = \&Foo::baz } + { + my $temp1 = \&Foo::baz; + my $temp2 = \&Foo::baaz; + } package OinkyBoinky; our @ISA = "Foo"; @@ -84,12 +90,14 @@ is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"'); # now check all our other items ... -ok($Foo->has_method('FOO_CONSTANT'), '... Foo->has_method(FOO_CONSTANT) (defined w/ use constant)'); +ok(!$Foo->has_method('FOO_CONSTANT'), '... Foo->has_method(FOO_CONSTANT) (defined w/ use constant)'); +ok(!$Foo->has_method('bling'), '... Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'); + ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)'); ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)'); +ok($Foo->has_method('baaz'), '... Foo->has_method(baaz) (typeglob aliased within Foo)'); ok($Foo->has_method('floob'), '... Foo->has_method(floob) (defined in Foo:: using symbol tables and Sub::Name w/out package name)'); ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'); -ok($Foo->has_method('bling'), '... Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'); ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'); ok($Foo->has_method('evaled_foo'), '... Foo->has_method(evaled_foo) (evaled in main::)'); @@ -105,13 +113,12 @@ is( reftype($bar->body), "CODE", "the returned value is a code ref" ); # calling get_method blessed them all -for my $method_name (qw/FOO_CONSTANT - bar +for my $method_name (qw/baaz + bar baz floob - blah - bling - bang + blah + bang evaled_foo/) { isa_ok($Foo->get_method($method_name), 'Class::MOP::Method'); { @@ -120,6 +127,17 @@ for my $method_name (qw/FOO_CONSTANT } } +for my $method_name (qw/ + FOO_CONSTANT + bling + /) { + is(ref($Foo->get_package_symbol('&' . $method_name)), 'CODE', '... got the __ANON__ methods'); + { + no strict 'refs'; + is($Foo->get_package_symbol('&' . $method_name), \&{'Foo::' . $method_name}, '... symbol matches CODE ref in package for ' . $method_name); + } +} + { package Foo::Aliasing; use metaclass; @@ -139,7 +157,7 @@ is($Foo->get_method('not_a_real_method'), undef, '... Foo->get_method(not_a_real is_deeply( [ sort $Foo->get_method_list ], - [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob foo) ], + [ qw(baaz bang bar baz blah evaled_foo floob foo) ], '... got the right method list for Foo'); is_deeply( @@ -152,12 +170,11 @@ is_deeply( code => $Foo->get_method($_) } } qw( - FOO_CONSTANT + baaz bang bar baz blah - bling evaled_foo floob foo @@ -171,16 +188,12 @@ dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there'; is_deeply( [ sort $Foo->get_method_list ], - [ qw(FOO_CONSTANT bang bar baz blah bling evaled_foo floob) ], + [ qw(baaz bang bar baz blah evaled_foo floob) ], '... got the right method list for Foo'); -ok($Foo->remove_method('FOO_CONSTANT'), '... removed the FOO_CONSTANT method'); -ok(!$Foo->has_method('FOO_CONSTANT'), '... !Foo->has_method(FOO_CONSTANT) we just removed it'); -dies_ok { Foo->FOO_CONSTANT } '... cannot call Foo->FOO_CONSTANT because it is not there'; - is_deeply( [ sort $Foo->get_method_list ], - [ qw(bang bar baz blah bling evaled_foo floob) ], + [ qw(baaz bang bar baz blah evaled_foo floob) ], '... got the right method list for Foo'); # ... test our class creator @@ -217,6 +230,11 @@ is_deeply( [ sort { $a->{name} cmp $b->{name} } $Bar->compute_all_applicable_methods() ], [ { + name => 'baaz', + class => 'Foo', + code => $Foo->get_method('baaz') + }, + { name => 'bang', class => 'Foo', code => $Foo->get_method('bang') @@ -235,7 +253,6 @@ is_deeply( } qw( baz blah - bling evaled_foo floob )), diff --git a/t/073_make_mutable.t b/t/073_make_mutable.t index 66b432a..1c29981 100644 --- a/t/073_make_mutable.t +++ b/t/073_make_mutable.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 108; +use Test::More tests => 112; use Test::Exception; use Scalar::Util; @@ -52,12 +52,14 @@ BEGIN { ok($meta->is_immutable, '... our class is now immutable'); ok(!$meta->make_immutable, '... make immutable now returns nothing'); ok($meta->get_method_map->{new}, '... inlined constructor created'); + ok($meta->has_method('new'), '... inlined constructor created for sure'); lives_ok { $meta->make_mutable; } '... changed Baz to be mutable'; ok($meta->is_mutable, '... our class is mutable'); ok(!$meta->is_immutable, '... our class is not immutable'); ok(!$meta->make_mutable, '... make mutable now returns nothing'); ok(!$meta->get_method_map->{new}, '... inlined constructor removed'); + ok(!$meta->has_method('new'), '... inlined constructor removed for sure'); my @new_keys = sort keys %$meta; is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys'); @@ -66,7 +68,10 @@ BEGIN { ok( $meta->add_method('xyz', sub{'xxx'}), '... added method'); is( Baz->xyz, 'xxx', '... method xyz works'); + + ok(! $meta->has_method('zxy') ,'... we dont have the aliased method yet'); ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method'); + ok(! $meta->has_method('zxy') ,'... the aliased method does not register (correctly)'); is( Baz->zxy, 'xxx', '... method zxy works'); ok( $meta->remove_method('xyz'), '... removed method'); ok(! $meta->remove_method('zxy'), '... removed aliased method');