[8eac1a5] | 1 | #!/usr/bin/env perl |
---|
| 2 | use strict; |
---|
| 3 | use warnings; |
---|
| 4 | |
---|
| 5 | use Test::More qw(no_plan); |
---|
| 6 | |
---|
| 7 | use File::Basename; |
---|
| 8 | BEGIN {require (dirname($0) . "/mock.pl");}; |
---|
| 9 | |
---|
| 10 | =head1 DESCRIPTION |
---|
| 11 | |
---|
| 12 | Basic tests for tab-completion functionality. |
---|
| 13 | |
---|
| 14 | =cut |
---|
| 15 | |
---|
| 16 | sub test_tokenize { |
---|
| 17 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
---|
| 18 | |
---|
| 19 | my $before_point = shift; |
---|
| 20 | my $after_point = shift; |
---|
| 21 | |
---|
[e97c5d05] | 22 | my $ctx = BarnOwl::Completion::Context->new($before_point, |
---|
| 23 | $after_point); |
---|
| 24 | is($ctx->line, $before_point . $after_point); |
---|
| 25 | is($ctx->point, length $before_point); |
---|
| 26 | |
---|
| 27 | test_ctx($ctx, @_); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | sub test_ctx { |
---|
| 31 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
---|
| 32 | |
---|
| 33 | my $ctx = shift; |
---|
| 34 | |
---|
[8eac1a5] | 35 | my $words = shift; |
---|
| 36 | my $word = shift; |
---|
| 37 | my $word_point = shift; |
---|
| 38 | |
---|
[13614e7] | 39 | my $word_start = shift; |
---|
| 40 | my $word_end = shift; |
---|
| 41 | |
---|
[8eac1a5] | 42 | is_deeply($ctx->words, $words); |
---|
| 43 | if (defined($word)) { |
---|
| 44 | is($ctx->word, $word, "Correct current word."); |
---|
| 45 | is($ctx->word_point, $word_point, "Correct point within word."); |
---|
[13614e7] | 46 | is($ctx->word_start, $word_start, "Correct start of word"); |
---|
| 47 | is($ctx->word_end, $word_end, "Correct end of word"); |
---|
[8eac1a5] | 48 | } |
---|
| 49 | } |
---|
| 50 | |
---|
[e97c5d05] | 51 | sub test_shift { |
---|
| 52 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
---|
| 53 | |
---|
| 54 | my $before_point = shift; |
---|
| 55 | my $after_point = shift; |
---|
| 56 | my $shift = shift; |
---|
| 57 | |
---|
| 58 | my $ctx = BarnOwl::Completion::Context->new($before_point, |
---|
| 59 | $after_point); |
---|
| 60 | $ctx = $ctx->shift_words($shift); |
---|
| 61 | |
---|
| 62 | test_ctx($ctx, @_); |
---|
| 63 | } |
---|
| 64 | |
---|
[8eac1a5] | 65 | |
---|
[14acbbd] | 66 | isa_ok(BarnOwl::Completion::Context->new('Hello, W', 'orld'), 'BarnOwl::Completion::Context'); |
---|
[8eac1a5] | 67 | |
---|
| 68 | no warnings 'qw'; |
---|
| 69 | test_tokenize('Hello, W', 'orld', |
---|
[13614e7] | 70 | [qw(Hello, World)], 1, 1, 7, 12); |
---|
[8eac1a5] | 71 | |
---|
| 72 | test_tokenize('Hello, World', '', |
---|
[13614e7] | 73 | [qw(Hello, World)], 1, 5, 7, 12); |
---|
[8eac1a5] | 74 | |
---|
| 75 | test_tokenize('', '', |
---|
[13614e7] | 76 | [qw()], 0, 0, 0, 0); |
---|
[8eac1a5] | 77 | |
---|
| 78 | test_tokenize('Hello', 'World', |
---|
[13614e7] | 79 | [qw(HelloWorld)], 0, 5, 0, 10); |
---|
[8eac1a5] | 80 | |
---|
| 81 | test_tokenize('lorem ipsum dolor ', 'sit amet', |
---|
[13614e7] | 82 | [qw(lorem ipsum dolor sit amet)], |
---|
| 83 | 3, 0, 18, 21); |
---|
[8eac1a5] | 84 | |
---|
| 85 | test_tokenize(q{error "ls -l failed"}, q{}, |
---|
[13614e7] | 86 | ['error', 'ls -l failed'], |
---|
| 87 | 1, 12, 6, 20); |
---|
[8eac1a5] | 88 | |
---|
| 89 | test_tokenize(q{"a long"' word'}, q{}, |
---|
| 90 | ['a long word']); |
---|
| 91 | |
---|
[13614e7] | 92 | test_tokenize(q{"'"}, q{}, [q{'}], 0, 1, 0, 3); |
---|
[8eac1a5] | 93 | |
---|
| 94 | test_tokenize(q{"Hello, }, q{World"}, |
---|
[13614e7] | 95 | [q{Hello, World}], |
---|
| 96 | 0, 7, 0, 14); |
---|
[8eac1a5] | 97 | |
---|
| 98 | test_tokenize(q{But 'Hello, }, q{World'}, |
---|
[13614e7] | 99 | ['But', q{Hello, World}], |
---|
| 100 | 1, 7, 4, 18); |
---|
[8eac1a5] | 101 | |
---|
| 102 | test_tokenize(q{But "Hello, }, q{World"''''''""}, |
---|
[13614e7] | 103 | ['But', q{Hello, World}], |
---|
| 104 | 1, 7, 4, 26); |
---|
[8eac1a5] | 105 | |
---|
| 106 | test_tokenize(q{}, q{''Hello}, |
---|
[13614e7] | 107 | ['Hello'], |
---|
| 108 | 0, 0, 0, 7); |
---|
[8eac1a5] | 109 | |
---|
| 110 | test_tokenize(q{"Hello, }, q{World}, |
---|
[13614e7] | 111 | [q{Hello, World}], |
---|
| 112 | 0, 7, 0, 13); |
---|
[8eac1a5] | 113 | |
---|
| 114 | test_tokenize(q{Hello }, q{World}, |
---|
[13614e7] | 115 | [qw{Hello World}], |
---|
| 116 | 1, 0, 9, 14); |
---|
[8eac1a5] | 117 | |
---|
| 118 | test_tokenize(q{Hello '' ""}, q{ World}, |
---|
[13614e7] | 119 | ["Hello", '', '', 'World'], |
---|
| 120 | 2, 0, 9, 11); |
---|
[8eac1a5] | 121 | |
---|
[7be5d8b] | 122 | test_tokenize(q{zwrite -c }, q{}, |
---|
| 123 | [qw(zwrite -c), ''], |
---|
| 124 | 2, 0, 10, 10); |
---|
| 125 | |
---|
[8eac1a5] | 126 | # It's not entirely clear what we should do here. Make a test for the |
---|
| 127 | # current behavior, so we'll notice if it changes. |
---|
| 128 | test_tokenize(q{Hello }, q{ World}, |
---|
[13614e7] | 129 | [qw(Hello World)], |
---|
| 130 | 1, -1, 7, 12); |
---|
[8eac1a5] | 131 | |
---|
[e97c5d05] | 132 | ## Test Context::shift |
---|
| 133 | SKIP: { |
---|
| 134 | skip "Can't yet test code that depends on perlglue.xs", 4; |
---|
| 135 | test_shift('lorem ipsum dolor ', 'sit amet', 0, |
---|
| 136 | [qw(lorem ipsum dolor sit amet)], |
---|
| 137 | 3, 0, 18, 21); |
---|
| 138 | |
---|
| 139 | test_shift('lorem ipsum dolor ', 'sit amet', 1, |
---|
| 140 | [qw(lorem ipsum dolor sit amet)], |
---|
| 141 | 2, 0, 12, 15); |
---|
[6e48560] | 142 | |
---|
[e97c5d05] | 143 | test_shift('lorem ipsum dolor ', 'sit amet', 2, |
---|
| 144 | [qw(lorem ipsum dolor sit amet)], |
---|
| 145 | 1, 0, 6, 9); |
---|
| 146 | |
---|
| 147 | test_shift('lorem ipsum dolor ', 'sit amet', 3, |
---|
| 148 | [qw(lorem ipsum dolor sit amet)], |
---|
| 149 | 0, 0, 0, 3); |
---|
| 150 | |
---|
| 151 | } |
---|
[6e48560] | 152 | ## Test common_prefix |
---|
| 153 | |
---|
| 154 | is(BarnOwl::Completion::common_prefix(qw(a b)), ''); |
---|
| 155 | is(BarnOwl::Completion::common_prefix(qw(a aa)), 'a'); |
---|
| 156 | |
---|
| 157 | is(BarnOwl::Completion::common_prefix(qw(aa)), 'aa'); |
---|
| 158 | |
---|
| 159 | is(BarnOwl::Completion::common_prefix(qw(a ab abc)), 'a'); |
---|
| 160 | |
---|
| 161 | is(BarnOwl::Completion::common_prefix(qw(abc abcd)), 'abc'); |
---|
| 162 | |
---|
| 163 | is(BarnOwl::Completion::common_prefix(qw(abc abc)), 'abc'); |
---|
| 164 | |
---|
[c4efb46] | 165 | is(BarnOwl::Completion::common_prefix('a', ''), ''); |
---|
| 166 | |
---|
[82a6e8b] | 167 | ## Test complete_flags |
---|
| 168 | |
---|
| 169 | use BarnOwl::Completion::Util qw(complete_flags); |
---|
| 170 | |
---|
| 171 | # dummy complete_zwrite |
---|
| 172 | sub complete_zwrite { |
---|
| 173 | my $ctx = shift; |
---|
| 174 | return complete_flags($ctx, |
---|
| 175 | [qw(-n -C -m)], |
---|
| 176 | { |
---|
| 177 | "-c" => sub {qw(nelhage nethack sipb help)}, |
---|
| 178 | "-i" => sub {qw()}, |
---|
| 179 | "-r" => sub {qw(ATHENA.MIT.EDU ZONE.MIT.EDU ANDREW.CMU.EDU)}, |
---|
| 180 | "-O" => sub {qw()}, |
---|
| 181 | }, |
---|
| 182 | sub {qw(nelhage asedeno geofft)}); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | sub test_complete { |
---|
| 186 | my $before = shift; |
---|
| 187 | my $after = shift; |
---|
| 188 | my $words = shift; |
---|
[4fbc7f5] | 189 | my $complete = shift || \&complete_zwrite; |
---|
[82a6e8b] | 190 | |
---|
| 191 | my $ctx = BarnOwl::Completion::Context->new($before, $after); |
---|
| 192 | |
---|
| 193 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
---|
| 194 | |
---|
[4fbc7f5] | 195 | my @got = $complete->($ctx); |
---|
[82a6e8b] | 196 | is_deeply([sort @got], [sort @$words]); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | test_complete('zwrite -c ', '', [qw(nelhage nethack sipb help)]); |
---|
| 200 | |
---|
| 201 | test_complete('zwrite -c nelhage', '', [qw(nelhage nethack sipb help)]); |
---|
| 202 | |
---|
| 203 | test_complete('zwrite -c nelhage -i ', '', [qw()]); |
---|
| 204 | |
---|
| 205 | test_complete('zwrite -c nelhage ', '', |
---|
| 206 | [qw(-n -C -m -c -i -r -O nelhage asedeno geofft)]); |
---|
| 207 | |
---|
| 208 | test_complete('zwrite -c nelhage ', '-', |
---|
| 209 | [qw(-n -C -m -c -i -r -O nelhage asedeno geofft)]); |
---|
| 210 | |
---|
[e711ca7] | 211 | test_complete('zwrite -c nelhage -- ', '', |
---|
| 212 | [qw(nelhage asedeno geofft)]); |
---|
| 213 | |
---|
[4fbc7f5] | 214 | sub complete_word { |
---|
| 215 | my $ctx = shift; |
---|
| 216 | return complete_flags($ctx, |
---|
| 217 | [qw(-a -b -c)], |
---|
| 218 | { |
---|
| 219 | "-d" => sub {qw(some words for completing)}, |
---|
| 220 | }, |
---|
| 221 | sub {$_[1]}); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | test_complete('cmd -a -d foo -c hello ','', |
---|
| 225 | [qw(-a -b -c -d 1)], \&complete_word); |
---|
| 226 | |
---|
| 227 | test_complete('cmd -a -d foo -c ','', |
---|
| 228 | [qw(-a -b -c -d 0)], \&complete_word); |
---|
| 229 | |
---|
[b017b03] | 230 | # Test that words after -- are counted properly. |
---|
| 231 | test_complete('cmd -- hi there ','', |
---|
| 232 | [qw(2)], \&complete_word); |
---|
| 233 | |
---|
| 234 | test_complete('cmd --','', |
---|
| 235 | [qw(-a -b -c -d 0)], \&complete_word); |
---|
| 236 | |
---|
| 237 | test_complete('cmd -- ','', |
---|
| 238 | [qw(0)], \&complete_word); |
---|
| 239 | |
---|
| 240 | test_complete('cmd foo -- ','', |
---|
| 241 | [qw(1)], \&complete_word); |
---|
| 242 | |
---|
| 243 | test_complete('cmd foo -- bar ','', |
---|
| 244 | [qw(2)], \&complete_word); |
---|
| 245 | |
---|
[8eac1a5] | 246 | 1; |
---|
[82a6e8b] | 247 | |
---|