Skip to content

Commit

Permalink
Merge pull request #253 from carloslima/methods-ignore-functions
Browse files Browse the repository at this point in the history
Make methods() ignore subroutines declared as functions for #222
  • Loading branch information
schwern committed Jul 23, 2014
2 parents a83c588 + 06798b1 commit 66f5f23
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/perl5i/2/Meta.pm
Expand Up @@ -10,6 +10,7 @@ require mro;

require perl5i::2::Meta::Instance;
require perl5i::2::Meta::Class;
use perl5i::2::autobox;

sub UNIVERSAL::mo {
# Be careful to pass through an alias, not a copy
Expand Down Expand Up @@ -70,7 +71,9 @@ sub methods {
for my $name (keys %$sym_table) {
my $glob = $sym_table->{$name};
next unless ref \$glob eq "GLOB";
next unless *{$glob}{CODE};
next unless my $code = *{$glob}{CODE};
my $sig = $code->signature;
next if $sig and !$sig->is_method;
$all_methods{$name} = $class;
}
}
Expand Down
65 changes: 64 additions & 1 deletion t/Meta/methods.t
Expand Up @@ -21,7 +21,6 @@ use Test::More;
sub parent2 {}
}


note "methods of a class with no parent"; {
my $class = "My::Parent";

Expand Down Expand Up @@ -108,5 +107,69 @@ SKIP: {
can_ok "Fcntl", @methods;
}

{
package My::MixedDefs;
use perl5i::latest;

sub as_sub {}
func as_func {}
method as_method {}
}
{
package My::MixedDefs::Child;
our @ISA = qw(My::MixedDefs);
use perl5i::latest;

sub as_sub2 {}
func as_func2 {}
method as_method2 {}
}


note "func gets filtered out of methods list"; {
my( @methods, @expected, @not_expected );

my $class = "My::MixedDefs";

@methods = $class->mc->methods;
@expected = qw( as_method as_sub );
@not_expected = qw( as_func inexistant );
is_deeply
scalar @methods->intersect( [ @expected, @not_expected ] )->sort,
scalar @expected->sort,
'on a class';

my $obj = bless {}, $class;

@methods = $obj->mo->methods;
@expected = qw( as_method as_sub );
@not_expected = qw( as_func inexistant );
is_deeply
scalar @methods->intersect( [ @expected, @not_expected ] )->sort,
scalar @expected->sort,
'on an object';

$class = "My::MixedDefs::Child";

@methods = $class->mc->methods;
@expected = qw( as_method as_sub as_method2 as_sub2 );
@not_expected = qw( as_func as_func2 inexistant );
is_deeply
scalar @methods->intersect( [ @expected, @not_expected ] )->sort,
scalar @expected->sort,
'on a child class';

$obj = bless {}, $class;

@methods = $obj->mo->methods;
@expected = qw( as_method as_sub as_method2 as_sub2 );
@not_expected = qw( as_func as_func2 inexistant );
is_deeply
scalar @methods->intersect( [ @expected, @not_expected ] )->sort,
scalar @expected->sort,
'on a child object';

can_ok( $class, 'as_func'); # sanity check
}

done_testing;

0 comments on commit 66f5f23

Please sign in to comment.