blob: ff190821cd4c106605bc8b12af024d8276c1e516 [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 +01003
Bram Moolenaarae3150e2016-06-11 23:22:36 +02004func Test_complete_tab()
5 call writefile(['testfile'], 'Xtestfile')
6 call feedkeys(":e Xtest\t\r", "tx")
7 call assert_equal('testfile', getline(1))
8 call delete('Xtestfile')
9endfunc
10
11func Test_complete_list()
12 " We can't see the output, but at least we check the code runs properly.
13 call feedkeys(":e test\<C-D>\r", "tx")
14 call assert_equal('test', expand('%:t'))
15endfunc
16
17func Test_complete_wildmenu()
18 call writefile(['testfile1'], 'Xtestfile1')
19 call writefile(['testfile2'], 'Xtestfile2')
20 set wildmenu
21 call feedkeys(":e Xtest\t\t\r", "tx")
22 call assert_equal('testfile2', getline(1))
23
24 call delete('Xtestfile1')
25 call delete('Xtestfile2')
26 set nowildmenu
27endfunc
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020028
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +010029func Test_map_completion()
30 if !has('cmdline_compl')
31 return
32 endif
33 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
34 call assert_equal('"map <unique> <silent>', getreg(':'))
35 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
36 call assert_equal('"map <script> <unique>', getreg(':'))
37 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
38 call assert_equal('"map <expr> <script>', getreg(':'))
39 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
40 call assert_equal('"map <buffer> <expr>', getreg(':'))
41 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
42 call assert_equal('"map <nowait> <buffer>', getreg(':'))
43 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
44 call assert_equal('"map <special> <nowait>', getreg(':'))
45 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
46 call assert_equal('"map <silent> <special>', getreg(':'))
47endfunc
48
Bram Moolenaar15eedf12017-01-22 19:25:33 +010049func Test_match_completion()
50 if !has('cmdline_compl')
51 return
52 endif
53 hi Aardig ctermfg=green
54 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
55 call assert_equal('"match Aardig', getreg(':'))
56 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
57 call assert_equal('"match none', getreg(':'))
58endfunc
59
60func Test_highlight_completion()
61 if !has('cmdline_compl')
62 return
63 endif
64 hi Aardig ctermfg=green
65 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
66 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +020067 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
68 call assert_equal('"hi default Aardig', getreg(':'))
69 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
70 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +010071 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
72 call assert_equal('"hi link', getreg(':'))
73 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
74 call assert_equal('"hi default', getreg(':'))
75 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
76 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +020077
78 " A cleared group does not show up in completions.
79 hi Anders ctermfg=green
80 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
81 hi clear Aardig
82 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
83 hi clear Anders
84 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +010085endfunc
86
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010087func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +010088 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010089 return
90 endif
91 for cmd in [
92 \ 'let a = ',
93 \ 'if',
94 \ 'elseif',
95 \ 'while',
96 \ 'for',
97 \ 'echo',
98 \ 'echon',
99 \ 'execute',
100 \ 'echomsg',
101 \ 'echoerr',
102 \ 'call',
103 \ 'return',
104 \ 'cexpr',
105 \ 'caddexpr',
106 \ 'cgetexpr',
107 \ 'lexpr',
108 \ 'laddexpr',
109 \ 'lgetexpr']
110 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
111 call assert_equal('"' . cmd . ' getline(', getreg(':'))
112 endfor
113endfunc
114
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200115func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200116 if !has('cmdline_compl')
117 return
118 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200119 let groupcount = len(getcompletion('', 'event'))
120 call assert_true(groupcount > 0)
121 let matchcount = len(getcompletion('File', 'event'))
122 call assert_true(matchcount > 0)
123 call assert_true(groupcount > matchcount)
124
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200125 if has('menu')
126 source $VIMRUNTIME/menu.vim
127 let matchcount = len(getcompletion('', 'menu'))
128 call assert_true(matchcount > 0)
129 call assert_equal(['File.'], getcompletion('File', 'menu'))
130 call assert_true(matchcount > 0)
131 let matchcount = len(getcompletion('File.', 'menu'))
132 call assert_true(matchcount > 0)
133 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200134
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200135 let l = getcompletion('v:n', 'var')
136 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200137 let l = getcompletion('v:notexists', 'var')
138 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200139
140 let l = getcompletion('', 'augroup')
141 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200142 let l = getcompletion('blahblah', 'augroup')
143 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200144
145 let l = getcompletion('', 'behave')
146 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200147 let l = getcompletion('not', 'behave')
148 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200149
150 let l = getcompletion('', 'color')
151 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200152 let l = getcompletion('dirty', 'color')
153 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200154
155 let l = getcompletion('', 'command')
156 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200157 let l = getcompletion('awake', 'command')
158 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200159
160 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200161 call assert_true(index(l, 'samples/') >= 0)
162 let l = getcompletion('NoMatch', 'dir')
163 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200164
165 let l = getcompletion('exe', 'expression')
166 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200167 let l = getcompletion('kill', 'expression')
168 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200169
170 let l = getcompletion('tag', 'function')
171 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200172 let l = getcompletion('paint', 'function')
173 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200174
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200175 let Flambda = {-> 'hello'}
176 let l = getcompletion('', 'function')
177 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200178 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200179
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200180 let l = getcompletion('run', 'file')
181 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200182 let l = getcompletion('walk', 'file')
183 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200184 set wildignore=*.vim
185 let l = getcompletion('run', 'file', 1)
186 call assert_true(index(l, 'runtest.vim') < 0)
187 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200188
189 let l = getcompletion('ha', 'filetype')
190 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200191 let l = getcompletion('horse', 'filetype')
192 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200193
194 let l = getcompletion('z', 'syntax')
195 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200196 let l = getcompletion('emacs', 'syntax')
197 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200198
199 let l = getcompletion('jikes', 'compiler')
200 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200201 let l = getcompletion('break', 'compiler')
202 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200203
204 let l = getcompletion('last', 'help')
205 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200206 let l = getcompletion('giveup', 'help')
207 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200208
209 let l = getcompletion('time', 'option')
210 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200211 let l = getcompletion('space', 'option')
212 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200213
214 let l = getcompletion('er', 'highlight')
215 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200216 let l = getcompletion('dark', 'highlight')
217 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200218
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200219 let l = getcompletion('', 'messages')
220 call assert_true(index(l, 'clear') >= 0)
221 let l = getcompletion('not', 'messages')
222 call assert_equal([], l)
223
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200224 let l = getcompletion('', 'mapclear')
225 call assert_true(index(l, '<buffer>') >= 0)
226 let l = getcompletion('not', 'mapclear')
227 call assert_equal([], l)
228
Bram Moolenaarb650b982016-08-05 20:35:13 +0200229 if has('cscope')
230 let l = getcompletion('', 'cscope')
231 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
232 call assert_equal(cmds, l)
233 " using cmdline completion must not change the result
234 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
235 let l = getcompletion('', 'cscope')
236 call assert_equal(cmds, l)
237 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
238 let l = getcompletion('find ', 'cscope')
239 call assert_equal(keys, l)
240 endif
241
Bram Moolenaar7522f692016-08-06 14:12:50 +0200242 if has('signs')
243 sign define Testing linehl=Comment
244 let l = getcompletion('', 'sign')
245 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
246 call assert_equal(cmds, l)
247 " using cmdline completion must not change the result
248 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
249 let l = getcompletion('', 'sign')
250 call assert_equal(cmds, l)
251 let l = getcompletion('list ', 'sign')
252 call assert_equal(['Testing'], l)
253 endif
254
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200255 " For others test if the name is recognized.
256 let names = ['buffer', 'environment', 'file_in_path',
257 \ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200258 if has('cmdline_hist')
259 call add(names, 'history')
260 endif
261 if has('gettext')
262 call add(names, 'locale')
263 endif
264 if has('profile')
265 call add(names, 'syntime')
266 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200267
268 set tags=Xtags
269 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
270
271 for name in names
272 let matchcount = len(getcompletion('', name))
273 call assert_true(matchcount >= 0, 'No matches for ' . name)
274 endfor
275
276 call delete('Xtags')
277
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200278 call assert_fails('call getcompletion("", "burp")', 'E475:')
279endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200280
281func Test_expand_star_star()
282 call mkdir('a/b', 'p')
283 call writefile(['asdfasdf'], 'a/b/fileXname')
284 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
285 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200286 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200287 call delete('a', 'rf')
288endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100289
290func Test_paste_in_cmdline()
291 let @a = "def"
292 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
293 call assert_equal('"abc def ghi', @:)
294
295 new
296 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
297
298 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
299 call assert_equal('"aaa asdf bbb', @:)
300
301 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
302 call assert_equal('"aaa /tmp/some bbb', @:)
303
304 set incsearch
305 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
306 call assert_equal('"aaa verylongword bbb', @:)
307
308 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
309 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100310
311 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
312 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100313 bwipe!
314endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100315
316func Test_remove_char_in_cmdline()
317 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
318 call assert_equal('"abc ef', @:)
319
320 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
321 call assert_equal('"abcdef', @:)
322
323 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
324 call assert_equal('"abc ghi', @:)
325
326 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
327 call assert_equal('"def', @:)
328endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100329
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100330func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100331 new
332 2;'(
333 2;')
334 quit
335endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100336
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100337func Test_illegal_address2()
338 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
339 new
340 source Xtest.vim
341 " Trigger calling validate_cursor()
342 diffsp Xtest.vim
343 quit!
344 bwipe!
345 call delete('Xtest.vim')
346endfunc
347
Bram Moolenaarba47b512017-01-24 21:18:19 +0100348func Test_cmdline_complete_wildoptions()
349 help
350 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
351 let a = join(sort(split(@:)),' ')
352 set wildoptions=tagfile
353 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
354 let b = join(sort(split(@:)),' ')
355 call assert_equal(a, b)
356 bw!
357endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100358
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200359func Test_cmdline_complete_user_cmd()
360 command! -complete=color -nargs=1 Foo :
361 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
362 call assert_equal('"Foo blue', @:)
363 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
364 call assert_equal('"Foo blue', @:)
365 delcommand Foo
366endfunc
367
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100368" using a leading backslash here
369set cpo+=C
370
371func Test_cmdline_search_range()
372 new
373 call setline(1, ['a', 'b', 'c', 'd'])
374 /d
375 1,\/s/b/B/
376 call assert_equal('B', getline(2))
377
378 /a
379 $
380 \?,4s/c/C/
381 call assert_equal('C', getline(3))
382
383 call setline(1, ['a', 'b', 'c', 'd'])
384 %s/c/c/
385 1,\&s/b/B/
386 call assert_equal('B', getline(2))
387
388 bwipe!
389endfunc
390
Bram Moolenaar65189a12017-02-06 22:22:17 +0100391" Tests for getcmdline(), getcmdpos() and getcmdtype()
392func Check_cmdline(cmdtype)
393 call assert_equal('MyCmd a', getcmdline())
394 call assert_equal(8, getcmdpos())
395 call assert_equal(a:cmdtype, getcmdtype())
396 return ''
397endfunc
398
399func Test_getcmdtype()
400 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
401
402 let cmdtype = ''
403 debuggreedy
404 call feedkeys(":debug echo 'test'\<CR>", "t")
405 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
406 call feedkeys("cont\<CR>", "xt")
407 0debuggreedy
408 call assert_equal('>', cmdtype)
409
410 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
411 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
412
413 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100414 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100415
416 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
417
418 cnoremap <expr> <F6> Check_cmdline('=')
419 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
420 cunmap <F6>
421endfunc
422
Bram Moolenaar52604f22017-04-07 16:17:39 +0200423func Test_verbosefile()
424 set verbosefile=Xlog
425 echomsg 'foo'
426 echomsg 'bar'
427 set verbosefile=
428 let log = readfile('Xlog')
429 call assert_match("foo\nbar", join(log, "\n"))
430 call delete('Xlog')
431endfunc
432
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100433set cpo&