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 | |
---|
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 | |
---|
35 | my $words = shift; |
---|
36 | my $word = shift; |
---|
37 | my $word_point = shift; |
---|
38 | |
---|
39 | my $word_start = shift; |
---|
40 | my $word_end = shift; |
---|
41 | |
---|
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."); |
---|
46 | is($ctx->word_start, $word_start, "Correct start of word"); |
---|
47 | is($ctx->word_end, $word_end, "Correct end of word"); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
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 | |
---|
65 | |
---|
66 | isa_ok(BarnOwl::Completion::Context->new('Hello, W', 'orld'), 'BarnOwl::Completion::Context'); |
---|
67 | |
---|
68 | no warnings 'qw'; |
---|
69 | test_tokenize('Hello, W', 'orld', |
---|
70 | [qw(Hello, World)], 1, 1, 7, 12); |
---|
71 | |
---|
72 | test_tokenize('Hello, World', '', |
---|
73 | [qw(Hello, World)], 1, 5, 7, 12); |
---|
74 | |
---|
75 | test_tokenize('', '', |
---|
76 | [qw()], 0, 0, 0, 0); |
---|
77 | |
---|
78 | test_tokenize('Hello', 'World', |
---|
79 | [qw(HelloWorld)], 0, 5, 0, 10); |
---|
80 | |
---|
81 | test_tokenize('lorem ipsum dolor ', 'sit amet', |
---|
82 | [qw(lorem ipsum dolor sit amet)], |
---|
83 | 3, 0, 18, 21); |
---|
84 | |
---|
85 | test_tokenize(q{error "ls -l failed"}, q{}, |
---|
86 | ['error', 'ls -l failed'], |
---|
87 | 1, 12, 6, 20); |
---|
88 | |
---|
89 | test_tokenize(q{"a long"' word'}, q{}, |
---|
90 | ['a long word']); |
---|
91 | |
---|
92 | test_tokenize(q{"'"}, q{}, [q{'}], 0, 1, 0, 3); |
---|
93 | |
---|
94 | test_tokenize(q{"Hello, }, q{World"}, |
---|
95 | [q{Hello, World}], |
---|
96 | 0, 7, 0, 14); |
---|
97 | |
---|
98 | test_tokenize(q{But 'Hello, }, q{World'}, |
---|
99 | ['But', q{Hello, World}], |
---|
100 | 1, 7, 4, 18); |
---|
101 | |
---|
102 | test_tokenize(q{But "Hello, }, q{World"''''''""}, |
---|
103 | ['But', q{Hello, World}], |
---|
104 | 1, 7, 4, 26); |
---|
105 | |
---|
106 | test_tokenize(q{}, q{''Hello}, |
---|
107 | ['Hello'], |
---|
108 | 0, 0, 0, 7); |
---|
109 | |
---|
110 | test_tokenize(q{"Hello, }, q{World}, |
---|
111 | [q{Hello, World}], |
---|
112 | 0, 7, 0, 13); |
---|
113 | |
---|
114 | test_tokenize(q{Hello }, q{World}, |
---|
115 | [qw{Hello World}], |
---|
116 | 1, 0, 9, 14); |
---|
117 | |
---|
118 | test_tokenize(q{Hello '' ""}, q{ World}, |
---|
119 | ["Hello", '', '', 'World'], |
---|
120 | 2, 0, 9, 11); |
---|
121 | |
---|
122 | test_tokenize(q{zwrite -c }, q{}, |
---|
123 | [qw(zwrite -c), ''], |
---|
124 | 2, 0, 10, 10); |
---|
125 | |
---|
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}, |
---|
129 | [qw(Hello World)], |
---|
130 | 1, -1, 7, 12); |
---|
131 | |
---|
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); |
---|
142 | |
---|
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 | } |
---|
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 | |
---|
165 | is(BarnOwl::Completion::common_prefix('a', ''), ''); |
---|
166 | |
---|
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; |
---|
189 | my $complete = shift || \&complete_zwrite; |
---|
190 | |
---|
191 | my $ctx = BarnOwl::Completion::Context->new($before, $after); |
---|
192 | |
---|
193 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
---|
194 | |
---|
195 | my @got = $complete->($ctx); |
---|
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 | |
---|
211 | test_complete('zwrite -c nelhage -- ', '', |
---|
212 | [qw(nelhage asedeno geofft)]); |
---|
213 | |
---|
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 | |
---|
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 | |
---|
246 | 1; |
---|
247 | |
---|