Skip to content

Commit

Permalink
rewrite find_similar_commands, with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gugod committed Aug 4, 2022
1 parent c4b014e commit a24f6de
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
15 changes: 2 additions & 13 deletions lib/App/perlbrew.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use JSON::PP qw( decode_json );
use File::Copy qw( copy );
use Capture::Tiny ();

use App::Perlbrew::Util qw( editdist files_are_the_same uniq );
use App::Perlbrew::Util qw( editdist files_are_the_same uniq find_similar_tokens );
use App::Perlbrew::Path ();
use App::Perlbrew::Path::Root ();
use App::Perlbrew::HTTP qw( http_download http_get );
Expand Down Expand Up @@ -403,21 +403,10 @@ sub commands {

sub find_similar_commands {
my ( $self, $command ) = @_;
my $SIMILAR_DISTANCE = 6;

$command =~ s/_/-/g;

my @commands = sort { $a->[1] <=> $b->[1] } map {
my $d = editdist( $_, $command );
( ( $d < $SIMILAR_DISTANCE ) ? [$_, $d] : () )
} $self->commands;

if (@commands) {
my $best = $commands[0][1];
@commands = map { $_->[0] } grep { $_->[1] == $best } @commands;
}

return @commands;
return @{ find_similar_tokens($command, [ sort $self->commands ]) };
}

# This method is called in the 'run' loop
Expand Down
34 changes: 34 additions & 0 deletions t/19.find_similar_commands.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!perl
use strict;
use warnings;
use Test::More;

use App::perlbrew;
use File::Temp qw( tempdir );

$ENV{PERLBREW_ROOT} = $App::perlbrew::PERLBREW_ROOT = tempdir( CLEANUP => 1 );
$App::perlbrew::PERLBREW_HOME = tempdir( CLEANUP => 1 );

my $app = App::perlbrew->new;
diag join ", ", sort $app->commands;

subtest "exact result", sub {
my @commands = $app->find_similar_commands( "install" );
is 0+@commands, 1;
is $commands[0], "install";
};

subtest "one result", sub {
my @commands = $app->find_similar_commands( "instali" );
is 0+@commands, 1;
is $commands[0], "install";
};

subtest "two result", sub {
my @commands = $app->find_similar_commands( "install-cpam" );
is 0+@commands, 2;
is $commands[0], "install-cpanm";
is $commands[1], "install-cpm";
};

done_testing;

0 comments on commit a24f6de

Please sign in to comment.