blob: 98a4337780fe715c2842faf0727b35c38e58eb17 [file] [log] [blame]
Bram Moolenaarae3150e2016-06-11 23:22:36 +02001" Tests for editing the command line.
2
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01003set belloff=all
4
Bram Moolenaarae3150e2016-06-11 23:22:36 +02005func Test_complete_tab()
6 call writefile(['testfile'], 'Xtestfile')
7 call feedkeys(":e Xtest\t\r", "tx")
8 call assert_equal('testfile', getline(1))
9 call delete('Xtestfile')
10endfunc
11
12func Test_complete_list()
13 " We can't see the output, but at least we check the code runs properly.
14 call feedkeys(":e test\<C-D>\r", "tx")
15 call assert_equal('test', expand('%:t'))
16endfunc
17
18func Test_complete_wildmenu()
19 call writefile(['testfile1'], 'Xtestfile1')
20 call writefile(['testfile2'], 'Xtestfile2')
21 set wildmenu
22 call feedkeys(":e Xtest\t\t\r", "tx")
23 call assert_equal('testfile2', getline(1))
24
25 call delete('Xtestfile1')
26 call delete('Xtestfile2')
27 set nowildmenu
28endfunc
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020029
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +010030func Test_map_completion()
31 if !has('cmdline_compl')
32 return
33 endif
34 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
35 call assert_equal('"map <unique> <silent>', getreg(':'))
36 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
37 call assert_equal('"map <script> <unique>', getreg(':'))
38 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
39 call assert_equal('"map <expr> <script>', getreg(':'))
40 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
41 call assert_equal('"map <buffer> <expr>', getreg(':'))
42 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
43 call assert_equal('"map <nowait> <buffer>', getreg(':'))
44 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
45 call assert_equal('"map <special> <nowait>', getreg(':'))
46 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
47 call assert_equal('"map <silent> <special>', getreg(':'))
48endfunc
49
Bram Moolenaar15eedf12017-01-22 19:25:33 +010050func Test_match_completion()
51 if !has('cmdline_compl')
52 return
53 endif
54 hi Aardig ctermfg=green
55 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
56 call assert_equal('"match Aardig', getreg(':'))
57 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
58 call assert_equal('"match none', getreg(':'))
59endfunc
60
61func Test_highlight_completion()
62 if !has('cmdline_compl')
63 return
64 endif
65 hi Aardig ctermfg=green
66 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
67 call assert_equal('"hi Aardig', getreg(':'))
68 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
69 call assert_equal('"hi link', getreg(':'))
70 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
71 call assert_equal('"hi default', getreg(':'))
72 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
73 call assert_equal('"hi clear', getreg(':'))
74endfunc
75
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010076func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +010077 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010078 return
79 endif
80 for cmd in [
81 \ 'let a = ',
82 \ 'if',
83 \ 'elseif',
84 \ 'while',
85 \ 'for',
86 \ 'echo',
87 \ 'echon',
88 \ 'execute',
89 \ 'echomsg',
90 \ 'echoerr',
91 \ 'call',
92 \ 'return',
93 \ 'cexpr',
94 \ 'caddexpr',
95 \ 'cgetexpr',
96 \ 'lexpr',
97 \ 'laddexpr',
98 \ 'lgetexpr']
99 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
100 call assert_equal('"' . cmd . ' getline(', getreg(':'))
101 endfor
102endfunc
103
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200104func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200105 if !has('cmdline_compl')
106 return
107 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200108 let groupcount = len(getcompletion('', 'event'))
109 call assert_true(groupcount > 0)
110 let matchcount = len(getcompletion('File', 'event'))
111 call assert_true(matchcount > 0)
112 call assert_true(groupcount > matchcount)
113
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200114 if has('menu')
115 source $VIMRUNTIME/menu.vim
116 let matchcount = len(getcompletion('', 'menu'))
117 call assert_true(matchcount > 0)
118 call assert_equal(['File.'], getcompletion('File', 'menu'))
119 call assert_true(matchcount > 0)
120 let matchcount = len(getcompletion('File.', 'menu'))
121 call assert_true(matchcount > 0)
122 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200123
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200124 let l = getcompletion('v:n', 'var')
125 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200126 let l = getcompletion('v:notexists', 'var')
127 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200128
129 let l = getcompletion('', 'augroup')
130 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200131 let l = getcompletion('blahblah', 'augroup')
132 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200133
134 let l = getcompletion('', 'behave')
135 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200136 let l = getcompletion('not', 'behave')
137 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200138
139 let l = getcompletion('', 'color')
140 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200141 let l = getcompletion('dirty', 'color')
142 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200143
144 let l = getcompletion('', 'command')
145 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200146 let l = getcompletion('awake', 'command')
147 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200148
149 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200150 call assert_true(index(l, 'samples/') >= 0)
151 let l = getcompletion('NoMatch', 'dir')
152 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200153
154 let l = getcompletion('exe', 'expression')
155 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200156 let l = getcompletion('kill', 'expression')
157 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200158
159 let l = getcompletion('tag', 'function')
160 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200161 let l = getcompletion('paint', 'function')
162 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200163
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200164 let Flambda = {-> 'hello'}
165 let l = getcompletion('', 'function')
166 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200167 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200168
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200169 let l = getcompletion('run', 'file')
170 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200171 let l = getcompletion('walk', 'file')
172 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200173 set wildignore=*.vim
174 let l = getcompletion('run', 'file', 1)
175 call assert_true(index(l, 'runtest.vim') < 0)
176 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200177
178 let l = getcompletion('ha', 'filetype')
179 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200180 let l = getcompletion('horse', 'filetype')
181 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200182
183 let l = getcompletion('z', 'syntax')
184 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200185 let l = getcompletion('emacs', 'syntax')
186 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200187
188 let l = getcompletion('jikes', 'compiler')
189 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200190 let l = getcompletion('break', 'compiler')
191 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200192
193 let l = getcompletion('last', 'help')
194 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200195 let l = getcompletion('giveup', 'help')
196 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200197
198 let l = getcompletion('time', 'option')
199 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200200 let l = getcompletion('space', 'option')
201 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200202
203 let l = getcompletion('er', 'highlight')
204 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200205 let l = getcompletion('dark', 'highlight')
206 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200207
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200208 let l = getcompletion('', 'messages')
209 call assert_true(index(l, 'clear') >= 0)
210 let l = getcompletion('not', 'messages')
211 call assert_equal([], l)
212
Bram Moolenaarb650b982016-08-05 20:35:13 +0200213 if has('cscope')
214 let l = getcompletion('', 'cscope')
215 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
216 call assert_equal(cmds, l)
217 " using cmdline completion must not change the result
218 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
219 let l = getcompletion('', 'cscope')
220 call assert_equal(cmds, l)
221 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
222 let l = getcompletion('find ', 'cscope')
223 call assert_equal(keys, l)
224 endif
225
Bram Moolenaar7522f692016-08-06 14:12:50 +0200226 if has('signs')
227 sign define Testing linehl=Comment
228 let l = getcompletion('', 'sign')
229 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
230 call assert_equal(cmds, l)
231 " using cmdline completion must not change the result
232 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
233 let l = getcompletion('', 'sign')
234 call assert_equal(cmds, l)
235 let l = getcompletion('list ', 'sign')
236 call assert_equal(['Testing'], l)
237 endif
238
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200239 " For others test if the name is recognized.
240 let names = ['buffer', 'environment', 'file_in_path',
241 \ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200242 if has('cmdline_hist')
243 call add(names, 'history')
244 endif
245 if has('gettext')
246 call add(names, 'locale')
247 endif
248 if has('profile')
249 call add(names, 'syntime')
250 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200251
252 set tags=Xtags
253 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
254
255 for name in names
256 let matchcount = len(getcompletion('', name))
257 call assert_true(matchcount >= 0, 'No matches for ' . name)
258 endfor
259
260 call delete('Xtags')
261
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200262 call assert_fails('call getcompletion("", "burp")', 'E475:')
263endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200264
265func Test_expand_star_star()
266 call mkdir('a/b', 'p')
267 call writefile(['asdfasdf'], 'a/b/fileXname')
268 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
269 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200270 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200271 call delete('a', 'rf')
272endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100273
274func Test_paste_in_cmdline()
275 let @a = "def"
276 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
277 call assert_equal('"abc def ghi', @:)
278
279 new
280 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
281
282 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
283 call assert_equal('"aaa asdf bbb', @:)
284
285 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
286 call assert_equal('"aaa /tmp/some bbb', @:)
287
288 set incsearch
289 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
290 call assert_equal('"aaa verylongword bbb', @:)
291
292 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
293 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100294
295 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
296 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100297 bwipe!
298endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100299
300func Test_remove_char_in_cmdline()
301 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
302 call assert_equal('"abc ef', @:)
303
304 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
305 call assert_equal('"abcdef', @:)
306
307 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
308 call assert_equal('"abc ghi', @:)
309
310 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
311 call assert_equal('"def', @:)
312endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100313
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100314func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100315 new
316 2;'(
317 2;')
318 quit
319endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100320
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100321func Test_illegal_address2()
322 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
323 new
324 source Xtest.vim
325 " Trigger calling validate_cursor()
326 diffsp Xtest.vim
327 quit!
328 bwipe!
329 call delete('Xtest.vim')
330endfunc
331
Bram Moolenaarba47b512017-01-24 21:18:19 +0100332func Test_cmdline_complete_wildoptions()
333 help
334 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
335 let a = join(sort(split(@:)),' ')
336 set wildoptions=tagfile
337 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
338 let b = join(sort(split(@:)),' ')
339 call assert_equal(a, b)
340 bw!
341endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100342
343" using a leading backslash here
344set cpo+=C
345
346func Test_cmdline_search_range()
347 new
348 call setline(1, ['a', 'b', 'c', 'd'])
349 /d
350 1,\/s/b/B/
351 call assert_equal('B', getline(2))
352
353 /a
354 $
355 \?,4s/c/C/
356 call assert_equal('C', getline(3))
357
358 call setline(1, ['a', 'b', 'c', 'd'])
359 %s/c/c/
360 1,\&s/b/B/
361 call assert_equal('B', getline(2))
362
363 bwipe!
364endfunc
365
Bram Moolenaar65189a12017-02-06 22:22:17 +0100366" Tests for getcmdline(), getcmdpos() and getcmdtype()
367func Check_cmdline(cmdtype)
368 call assert_equal('MyCmd a', getcmdline())
369 call assert_equal(8, getcmdpos())
370 call assert_equal(a:cmdtype, getcmdtype())
371 return ''
372endfunc
373
374func Test_getcmdtype()
375 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
376
377 let cmdtype = ''
378 debuggreedy
379 call feedkeys(":debug echo 'test'\<CR>", "t")
380 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
381 call feedkeys("cont\<CR>", "xt")
382 0debuggreedy
383 call assert_equal('>', cmdtype)
384
385 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
386 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
387
388 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100389 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100390
391 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
392
393 cnoremap <expr> <F6> Check_cmdline('=')
394 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
395 cunmap <F6>
396endfunc
397
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100398set cpo&