From: Brandon L Black Date: Fri, 28 Jul 2006 15:19:45 +0000 (+0000) Subject: refactor fetcher and add a test for 100% coverage X-Git-Tag: 0.02~5^2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aeed4a60598c2db61f0751ac1905e8db1072cace;p=gitmo%2FAlgorithm-C3.git refactor fetcher and add a test for 100% coverage --- diff --git a/lib/Algorithm/C3.pm b/lib/Algorithm/C3.pm index 064e935..b0a9a61 100644 --- a/lib/Algorithm/C3.pm +++ b/lib/Algorithm/C3.pm @@ -8,17 +8,6 @@ use Carp 'confess'; our $VERSION = '0.01'; -sub _fetcher { - my ($root, $_parent_fetcher) = @_; - - my $parent_fetcher = $_parent_fetcher; - unless (ref($parent_fetcher) && ref($parent_fetcher) eq 'CODE') { - $parent_fetcher = $root->can($_parent_fetcher) - || confess "Could not find method $_parent_fetcher in $root"; - } - [ $parent_fetcher->($root) ]; -} - sub merge { my ($root, $parent_fetcher) = @_; @@ -26,8 +15,14 @@ sub merge { my %fcache; # cache of _fetcher results my %mcache; # cache of merge do-block results + my $pfetcher_is_coderef = ref($parent_fetcher) eq 'CODE'; + + unless ($pfetcher_is_coderef or $root->can($parent_fetcher)) { + confess "Could not find method $parent_fetcher in $root"; + } + my $current_root = $root; - my $current_parents = $fcache{$root} ||= _fetcher($root, $parent_fetcher); + my $current_parents = [ $root->$parent_fetcher ]; my $recurse_mergeout = []; my $i = 0; @@ -35,6 +30,10 @@ sub merge { if($i < @$current_parents) { my $new_root = $current_parents->[$i++]; + unless ($pfetcher_is_coderef or $new_root->can($parent_fetcher)) { + confess "Could not find method $parent_fetcher in $new_root"; + } + push(@STACK, [ $current_root, $current_parents, @@ -43,8 +42,7 @@ sub merge { ]); $current_root = $new_root; - $current_parents = $fcache{$current_root} - ||= _fetcher($current_root, $parent_fetcher); + $current_parents = $fcache{$current_root} ||= [ $current_root->$parent_fetcher ]; $recurse_mergeout = []; $i = 0; next; diff --git a/t/001_merge.t b/t/001_merge.t index e18f85d..68a8537 100644 --- a/t/001_merge.t +++ b/t/001_merge.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 4; +use Test::More tests => 5; BEGIN { use_ok('Algorithm::C3'); @@ -47,7 +47,8 @@ BEGIN { package My::G; our @ISA = ('My::E'); package My::H; - our @ISA = ('My::G', 'My::F'); + our @ISA = ('My::G', 'My::F'); + sub method_exists_only_in_H { @ISA } } { @@ -67,4 +68,10 @@ eval { }; ok($@, '... this died as we expected'); - +eval { + Algorithm::C3::merge( + 'My::H', + 'method_exists_only_in_H' + ); +}; +ok($@, '... this died as we expected');