-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Result object and refactor internals to reflect that
- Loading branch information
1 parent
b83a1f1
commit 50c73f6
Showing
7 changed files
with
206 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use strict; | ||
use warnings; | ||
|
||
package Path::IsDev::Result; | ||
|
||
# ABSTRACT: Result container | ||
|
||
use Class::Tiny 'path', 'result', { | ||
reasons => sub { [] } | ||
}; | ||
sub _path { require Path::Tiny; goto &Path::Tiny::path } | ||
|
||
sub BUILD { | ||
my ( $self, $args ) = @_; | ||
if ( not $self->path ) { | ||
die "<path> is a mandatory parameter"; | ||
} | ||
if ( not ref $self->path ) { | ||
$self->path( _path( $self->path ) ); | ||
} | ||
if ( not -e $self->path ) { | ||
die "<path> parameter must exist for heuristics to be performed"; | ||
} | ||
} | ||
my %type_map = ( | ||
'Path::IsDev::Heuristic' => 'positive heuristic', | ||
'Path::IsDev::NegativeHeuristic' => 'negative heuristic', | ||
); | ||
|
||
sub add_reason { | ||
my ( $self, $heuristic_name, $heuristic_result, $context ) = @_; | ||
$context ||= {}; | ||
$context->{heuristic} = $heuristic_name; | ||
$context->{result} = $heuristic_result; | ||
|
||
for my $type ( sort keys %type_map ) { | ||
if ( $heuristic_name->isa($type) ) { | ||
$context->{type} = $type_map{$type}; | ||
} | ||
} | ||
push @{ $self->reasons }, $context; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use Path::Tiny qw(path); | ||
use Test::Fatal qw( exception ); | ||
use FindBin; | ||
|
||
sub nofatal { | ||
my ( $message, $sub ) = @_; | ||
my $e = exception { $sub->() }; | ||
return is( $e, undef, "no exceptions: $message" ); | ||
} | ||
|
||
my $corpus_dir = | ||
path($FindBin::Bin)->parent->parent->child('corpus')->child('Changelog'); | ||
|
||
subtest 'corpus/Changelog' => sub { | ||
return unless nofatal 'require Path::IsDev::Object' => sub { | ||
require Path::IsDev::Object; | ||
}; | ||
my $instance; | ||
return unless nofatal 'instance = Path::IsDev::Object->new()' => sub { | ||
$instance = Path::IsDev::Object->new(); | ||
}; | ||
return unless nofatal 'instance->set()' => sub { | ||
is( $instance->set(), 'Basic', 'instance->set() == Basic' ); | ||
}; | ||
return unless nofatal 'instance->set_prefix()' => sub { | ||
is( $instance->set_prefix, 'Path::IsDev::HeuristicSet', 'instance->set_prefix() == Path::IsDev::HeuristicSet' ); | ||
}; | ||
return unless nofatal 'instance->set_module()' => sub { | ||
is( $instance->set_module, 'Path::IsDev::HeuristicSet::Basic', 'instance->set_module() == Path::IsDev::HeuristicSet::Basic' ); | ||
}; | ||
return unless nofatal 'instance->loaded_set_module()' => sub { | ||
is( | ||
$instance->set_module, | ||
'Path::IsDev::HeuristicSet::Basic', | ||
'instance->loaded_set_module() == Path::IsDev::HeuristicSet::Basic' | ||
); | ||
}; | ||
return unless nofatal 'instance->_matches($path_isdev_source)' => sub { | ||
my $computed_root = path($FindBin::Bin)->parent->parent; | ||
my $result = $instance->_matches($computed_root); | ||
ok( defined $result->result, 'instance->_matches($path_isdev_source)->result is defined' ); | ||
subtest "result_object" => sub { | ||
return unless nofatal 'result->path' => sub { | ||
my $path = $result->path; | ||
ok( defined $path, '->path is defined' ); | ||
ok( ref $path, '->path is a ref' ); | ||
}; | ||
return unless nofatal 'result->result' => sub { | ||
my $result = $result->result; | ||
ok( defined $result, '->result is defined' ); | ||
}; | ||
return unless nofatal 'result->reasons' => sub { | ||
my $reasons = $result->reasons; | ||
ok( defined $reasons, '->reasons is defined' ); | ||
ok( ref $reasons, '->reasons is a ref' ); | ||
is( ref $reasons, 'ARRAY', '->reasons is ARRAY' ); | ||
}; | ||
}; | ||
}; | ||
return unless nofatal 'instance->matches($corpus_Changes_dir)' => sub { | ||
my $result = $instance->matches($corpus_dir); | ||
ok( defined $result, 'instance->matches($corpus_Changes_dir) is defined' ); | ||
}; | ||
return unless nofatal 'instance->matches($corpus_Changes_dir/../)' => sub { | ||
my $result = $instance->matches( $corpus_dir->parent ); | ||
ok( !defined $result, 'instance->matches($corpus_Changes_dir/../) is not defined' ); | ||
}; | ||
return unless nofatal 'instance->_instance_id' => sub { | ||
my $result = $instance->_instance_id; | ||
ok( defined $result, 'instance->_instance_id is defined' ); | ||
}; | ||
return unless nofatal 'instance->_debug(testing)' => sub { | ||
my $result = $instance->_debug('testing'); | ||
pass("_debug(testing) OK "); | ||
}; | ||
}; | ||
|
||
done_testing; |