blob: 329591239782dc429367e9bd7ecee06ac60ce3bc [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(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +020074
75 " A cleared group does not show up in completions.
76 hi Anders ctermfg=green
77 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
78 hi clear Aardig
79 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
80 hi clear Anders
81 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +010082endfunc
83
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010084func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +010085 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010086 return
87 endif
88 for cmd in [
89 \ 'let a = ',
90 \ 'if',
91 \ 'elseif',
92 \ 'while',
93 \ 'for',
94 \ 'echo',
95 \ 'echon',
96 \ 'execute',
97 \ 'echomsg',
98 \ 'echoerr',
99 \ 'call',
100 \ 'return',
101 \ 'cexpr',
102 \ 'caddexpr',
103 \ 'cgetexpr',
104 \ 'lexpr',
105 \ 'laddexpr',
106 \ 'lgetexpr']
107 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
108 call assert_equal('"' . cmd . ' getline(', getreg(':'))
109 endfor
110endfunc
111
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200112func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200113 if !has('cmdline_compl')
114 return
115 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200116 let groupcount = len(getcompletion('', 'event'))
117 call assert_true(groupcount > 0)
118 let matchcount = len(getcompletion('File', 'event'))
119 call assert_true(matchcount > 0)
120 call assert_true(groupcount > matchcount)
121
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200122 if has('menu')
123 source $VIMRUNTIME/menu.vim
124 let matchcount = len(getcompletion('', 'menu'))
125 call assert_true(matchcount > 0)
126 call assert_equal(['File.'], getcompletion('File', 'menu'))
127 call assert_true(matchcount > 0)
128 let matchcount = len(getcompletion('File.', 'menu'))
129 call assert_true(matchcount > 0)
130 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200131
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200132 let l = getcompletion('v:n', 'var')
133 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200134 let l = getcompletion('v:notexists', 'var')
135 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200136
137 let l = getcompletion('', 'augroup')
138 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200139 let l = getcompletion('blahblah', 'augroup')
140 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200141
142 let l = getcompletion('', 'behave')
143 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200144 let l = getcompletion('not', 'behave')
145 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200146
147 let l = getcompletion('', 'color')
148 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200149 let l = getcompletion('dirty', 'color')
150 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200151
152 let l = getcompletion('', 'command')
153 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200154 let l = getcompletion('awake', 'command')
155 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200156
157 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200158 call assert_true(index(l, 'samples/') >= 0)
159 let l = getcompletion('NoMatch', 'dir')
160 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200161
162 let l = getcompletion('exe', 'expression')
163 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200164 let l = getcompletion('kill', 'expression')
165 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200166
167 let l = getcompletion('tag', 'function')
168 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200169 let l = getcompletion('paint', 'function')
170 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200171
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200172 let Flambda = {-> 'hello'}
173 let l = getcompletion('', 'function')
174 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200175 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200176
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200177 let l = getcompletion('run', 'file')
178 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200179 let l = getcompletion('walk', 'file')
180 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200181 set wildignore=*.vim
182 let l = getcompletion('run', 'file', 1)
183 call assert_true(index(l, 'runtest.vim') < 0)
184 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200185
186 let l = getcompletion('ha', 'filetype')
187 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200188 let l = getcompletion('horse', 'filetype')
189 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200190
191 let l = getcompletion('z', 'syntax')
192 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200193 let l = getcompletion('emacs', 'syntax')
194 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200195
196 let l = getcompletion('jikes', 'compiler')
197 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200198 let l = getcompletion('break', 'compiler')
199 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200200
201 let l = getcompletion('last', 'help')
202 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200203 let l = getcompletion('giveup', 'help')
204 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200205
206 let l = getcompletion('time', 'option')
207 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200208 let l = getcompletion('space', 'option')
209 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200210
211 let l = getcompletion('er', 'highlight')
212 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200213 let l = getcompletion('dark', 'highlight')
214 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200215
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200216 let l = getcompletion('', 'messages')
217 call assert_true(index(l, 'clear') >= 0)
218 let l = getcompletion('not', 'messages')
219 call assert_equal([], l)
220
Bram Moolenaarb650b982016-08-05 20:35:13 +0200221 if has('cscope')
222 let l = getcompletion('', 'cscope')
223 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
224 call assert_equal(cmds, l)
225 " using cmdline completion must not change the result
226 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
227 let l = getcompletion('', 'cscope')
228 call assert_equal(cmds, l)
229 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
230 let l = getcompletion('find ', 'cscope')
231 call assert_equal(keys, l)
232 endif
233
Bram Moolenaar7522f692016-08-06 14:12:50 +0200234 if has('signs')
235 sign define Testing linehl=Comment
236 let l = getcompletion('', 'sign')
237 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
238 call assert_equal(cmds, l)
239 " using cmdline completion must not change the result
240 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
241 let l = getcompletion('', 'sign')
242 call assert_equal(cmds, l)
243 let l = getcompletion('list ', 'sign')
244 call assert_equal(['Testing'], l)
245 endif
246
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200247 " For others test if the name is recognized.
248 let names = ['buffer', 'environment', 'file_in_path',
249 \ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200250 if has('cmdline_hist')
251 call add(names, 'history')
252 endif
253 if has('gettext')
254 call add(names, 'locale')
255 endif
256 if has('profile')
257 call add(names, 'syntime')
258 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200259
260 set tags=Xtags
261 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
262
263 for name in names
264 let matchcount = len(getcompletion('', name))
265 call assert_true(matchcount >= 0, 'No matches for ' . name)
266 endfor
267
268 call delete('Xtags')
269
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200270 call assert_fails('call getcompletion("", "burp")', 'E475:')
271endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200272
273func Test_expand_star_star()
274 call mkdir('a/b', 'p')
275 call writefile(['asdfasdf'], 'a/b/fileXname')
276 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
277 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200278 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200279 call delete('a', 'rf')
280endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100281
282func Test_paste_in_cmdline()
283 let @a = "def"
284 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
285 call assert_equal('"abc def ghi', @:)
286
287 new
288 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
289
290 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
291 call assert_equal('"aaa asdf bbb', @:)
292
293 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
294 call assert_equal('"aaa /tmp/some bbb', @:)
295
296 set incsearch
297 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
298 call assert_equal('"aaa verylongword bbb', @:)
299
300 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
301 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100302
303 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
304 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100305 bwipe!
306endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100307
308func Test_remove_char_in_cmdline()
309 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
310 call assert_equal('"abc ef', @:)
311
312 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
313 call assert_equal('"abcdef', @:)
314
315 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
316 call assert_equal('"abc ghi', @:)
317
318 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
319 call assert_equal('"def', @:)
320endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100321
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100322func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100323 new
324 2;'(
325 2;')
326 quit
327endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100328
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100329func Test_illegal_address2()
330 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
331 new
332 source Xtest.vim
333 " Trigger calling validate_cursor()
334 diffsp Xtest.vim
335 quit!
336 bwipe!
337 call delete('Xtest.vim')
338endfunc
339
Bram Moolenaarba47b512017-01-24 21:18:19 +0100340func Test_cmdline_complete_wildoptions()
341 help
342 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
343 let a = join(sort(split(@:)),' ')
344 set wildoptions=tagfile
345 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
346 let b = join(sort(split(@:)),' ')
347 call assert_equal(a, b)
348 bw!
349endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100350
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200351func Test_cmdline_complete_user_cmd()
352 command! -complete=color -nargs=1 Foo :
353 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
354 call assert_equal('"Foo blue', @:)
355 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
356 call assert_equal('"Foo blue', @:)
357 delcommand Foo
358endfunc
359
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100360" using a leading backslash here
361set cpo+=C
362
363func Test_cmdline_search_range()
364 new
365 call setline(1, ['a', 'b', 'c', 'd'])
366 /d
367 1,\/s/b/B/
368 call assert_equal('B', getline(2))
369
370 /a
371 $
372 \?,4s/c/C/
373 call assert_equal('C', getline(3))
374
375 call setline(1, ['a', 'b', 'c', 'd'])
376 %s/c/c/
377 1,\&s/b/B/
378 call assert_equal('B', getline(2))
379
380 bwipe!
381endfunc
382
Bram Moolenaar65189a12017-02-06 22:22:17 +0100383" Tests for getcmdline(), getcmdpos() and getcmdtype()
384func Check_cmdline(cmdtype)
385 call assert_equal('MyCmd a', getcmdline())
386 call assert_equal(8, getcmdpos())
387 call assert_equal(a:cmdtype, getcmdtype())
388 return ''
389endfunc
390
391func Test_getcmdtype()
392 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
393
394 let cmdtype = ''
395 debuggreedy
396 call feedkeys(":debug echo 'test'\<CR>", "t")
397 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
398 call feedkeys("cont\<CR>", "xt")
399 0debuggreedy
400 call assert_equal('>', cmdtype)
401
402 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
403 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
404
405 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100406 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100407
408 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
409
410 cnoremap <expr> <F6> Check_cmdline('=')
411 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
412 cunmap <F6>
413endfunc
414
Bram Moolenaar52604f22017-04-07 16:17:39 +0200415func Test_verbosefile()
416 set verbosefile=Xlog
417 echomsg 'foo'
418 echomsg 'bar'
419 set verbosefile=
420 let log = readfile('Xlog')
421 call assert_match("foo\nbar", join(log, "\n"))
422 call delete('Xlog')
423endfunc
424
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100425set cpo&