Changeset e6cec01 for t


Ignore:
Timestamp:
Dec 28, 2009, 12:03:29 AM (14 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.10, release-1.6, release-1.7, release-1.8, release-1.9
Children:
dc8f6e0
Parents:
880311d
git-author:
Nelson Elhage <nelhage@mit.edu> (12/27/09 23:48:30)
git-committer:
Nelson Elhage <nelhage@mit.edu> (12/28/09 00:03:29)
Message:
Completion: Add a helper function to complete paths.

Add tests as well.

[Thanks to davidben@mit.edu for bug reports and helping to clarify the
 $dir vs $pfx distinction in the code]
File:
1 edited

Legend:

Unmodified
Added
Removed
  • t/completion.t

    r1167bf1 re6cec01  
    289289              \&complete_filter_expr);
    290290
     291# Test complete_files
     292use BarnOwl::Completion::Util qw(complete_file);
     293use File::Temp;
     294use File::Path qw(mkpath);
     295
     296my $tmpdir = File::Temp::tempdir(CLEANUP => 1);
     297
     298# Make sure $tmpdir does not have a trailing /
     299$tmpdir =~ s{/$}{};
     300$ENV{HOME} = $tmpdir;
     301
     302sub touch {
     303    my $path = shift;
     304    system("touch", "$path");
     305}
     306
     307mkpath("$tmpdir/.owl/",
     308       "$tmpdir/.owl/modules/",
     309       "$tmpdir/Public/",
     310       "$tmpdir/Private/",
     311       "$tmpdir/.ours",
     312       "$tmpdir/www");
     313touch("$tmpdir/.zephyr.subs");
     314touch("$tmpdir/wheee");
     315touch("$tmpdir/.owl/startup");
     316
     317sub completion_value {
     318    my $c = shift;
     319    return $c unless ref($c) eq 'ARRAY';
     320    return $c->[1];
     321}
     322
     323sub test_file {
     324    my $spec  = shift;
     325    my $pfx   = shift;
     326    my $dirs  = shift;
     327    my $files = shift;
     328
     329    my $expect = [ sort {$a->[1] cmp $b->[1]}
     330        ((map {["$_/", defined($pfx)?"$pfx/$_/":"$_/", 0]} @$dirs),
     331         (map {["$_",  defined($pfx)?"$pfx/$_" :$_   , 1]} @$files))
     332       ];
     333
     334    local $Test::Builder::Level = $Test::Builder::Level + 1;
     335
     336    my @got = complete_file($spec);
     337
     338    @got = grep {completion_value($_) =~ m{^\Q$spec\E}} @got;
     339    @got = sort {completion_value($a) cmp completion_value($b)} @got;
     340
     341    use Data::Dumper;
     342    is_deeply(\@got, $expect);
     343}
     344
     345is_deeply([complete_file("~")], [["~/", "~/", 0]]);
     346
     347chdir($tmpdir);
     348test_file("$tmpdir/", $tmpdir,
     349          [qw(Public Private www)],
     350          [qw(wheee)]);
     351
     352test_file("./", ".",
     353          [qw(Public Private www)],
     354          [qw(wheee)]);
     355
     356test_file("", undef, [qw(Public Private www)], [qw(wheee)]);
     357
     358test_file("./.owl/", "./.owl",
     359          [qw(modules)],
     360          [qw(startup)]);
     361
     362test_file("~/", "~",
     363          [qw(Public Private www)],
     364          [qw(wheee)]);
     365
     366test_file("P", undef, [qw(Public Private)], []);
     367
     368test_file("$tmpdir/.", $tmpdir,
     369          [qw(. .. .owl .ours)],
     370          [qw(.zephyr.subs)]);
    2913711;
    292372
Note: See TracChangeset for help on using the changeset viewer.