xref: /vim-8.2.3635/src/testdir/test_cmdline.vim (revision 51ad4eaa)
1" Tests for editing the command line.
2
3
4func 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
28
29func 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
49func 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(':'))
67  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(':'))
71  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(':'))
77
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'))
85endfunc
86
87func Test_expr_completion()
88  if !has('cmdline_compl')
89    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
115func Test_getcompletion()
116  if !has('cmdline_compl')
117    return
118  endif
119  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
125  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
134
135  let l = getcompletion('v:n', 'var')
136  call assert_true(index(l, 'v:null') >= 0)
137  let l = getcompletion('v:notexists', 'var')
138  call assert_equal([], l)
139
140  args a.c b.c
141  let l = getcompletion('', 'arglist')
142  call assert_equal(['a.c', 'b.c'], l)
143  %argdelete
144
145  let l = getcompletion('', 'augroup')
146  call assert_true(index(l, 'END') >= 0)
147  let l = getcompletion('blahblah', 'augroup')
148  call assert_equal([], l)
149
150  let l = getcompletion('', 'behave')
151  call assert_true(index(l, 'mswin') >= 0)
152  let l = getcompletion('not', 'behave')
153  call assert_equal([], l)
154
155  let l = getcompletion('', 'color')
156  call assert_true(index(l, 'default') >= 0)
157  let l = getcompletion('dirty', 'color')
158  call assert_equal([], l)
159
160  let l = getcompletion('', 'command')
161  call assert_true(index(l, 'sleep') >= 0)
162  let l = getcompletion('awake', 'command')
163  call assert_equal([], l)
164
165  let l = getcompletion('', 'dir')
166  call assert_true(index(l, 'samples/') >= 0)
167  let l = getcompletion('NoMatch', 'dir')
168  call assert_equal([], l)
169
170  let l = getcompletion('exe', 'expression')
171  call assert_true(index(l, 'executable(') >= 0)
172  let l = getcompletion('kill', 'expression')
173  call assert_equal([], l)
174
175  let l = getcompletion('tag', 'function')
176  call assert_true(index(l, 'taglist(') >= 0)
177  let l = getcompletion('paint', 'function')
178  call assert_equal([], l)
179
180  let Flambda = {-> 'hello'}
181  let l = getcompletion('', 'function')
182  let l = filter(l, {i, v -> v =~ 'lambda'})
183  call assert_equal([], l)
184
185  let l = getcompletion('run', 'file')
186  call assert_true(index(l, 'runtest.vim') >= 0)
187  let l = getcompletion('walk', 'file')
188  call assert_equal([], l)
189  set wildignore=*.vim
190  let l = getcompletion('run', 'file', 1)
191  call assert_true(index(l, 'runtest.vim') < 0)
192  set wildignore&
193
194  let l = getcompletion('ha', 'filetype')
195  call assert_true(index(l, 'hamster') >= 0)
196  let l = getcompletion('horse', 'filetype')
197  call assert_equal([], l)
198
199  let l = getcompletion('z', 'syntax')
200  call assert_true(index(l, 'zimbu') >= 0)
201  let l = getcompletion('emacs', 'syntax')
202  call assert_equal([], l)
203
204  let l = getcompletion('jikes', 'compiler')
205  call assert_true(index(l, 'jikes') >= 0)
206  let l = getcompletion('break', 'compiler')
207  call assert_equal([], l)
208
209  let l = getcompletion('last', 'help')
210  call assert_true(index(l, ':tablast') >= 0)
211  let l = getcompletion('giveup', 'help')
212  call assert_equal([], l)
213
214  let l = getcompletion('time', 'option')
215  call assert_true(index(l, 'timeoutlen') >= 0)
216  let l = getcompletion('space', 'option')
217  call assert_equal([], l)
218
219  let l = getcompletion('er', 'highlight')
220  call assert_true(index(l, 'ErrorMsg') >= 0)
221  let l = getcompletion('dark', 'highlight')
222  call assert_equal([], l)
223
224  let l = getcompletion('', 'messages')
225  call assert_true(index(l, 'clear') >= 0)
226  let l = getcompletion('not', 'messages')
227  call assert_equal([], l)
228
229  let l = getcompletion('', 'mapclear')
230  call assert_true(index(l, '<buffer>') >= 0)
231  let l = getcompletion('not', 'mapclear')
232  call assert_equal([], l)
233
234  if has('cscope')
235    let l = getcompletion('', 'cscope')
236    let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
237    call assert_equal(cmds, l)
238    " using cmdline completion must not change the result
239    call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
240    let l = getcompletion('', 'cscope')
241    call assert_equal(cmds, l)
242    let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
243    let l = getcompletion('find ', 'cscope')
244    call assert_equal(keys, l)
245  endif
246
247  if has('signs')
248    sign define Testing linehl=Comment
249    let l = getcompletion('', 'sign')
250    let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
251    call assert_equal(cmds, l)
252    " using cmdline completion must not change the result
253    call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
254    let l = getcompletion('', 'sign')
255    call assert_equal(cmds, l)
256    let l = getcompletion('list ', 'sign')
257    call assert_equal(['Testing'], l)
258  endif
259
260  " For others test if the name is recognized.
261  let names = ['buffer', 'environment', 'file_in_path',
262	\ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
263  if has('cmdline_hist')
264    call add(names, 'history')
265  endif
266  if has('gettext')
267    call add(names, 'locale')
268  endif
269  if has('profile')
270    call add(names, 'syntime')
271  endif
272
273  set tags=Xtags
274  call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
275
276  for name in names
277    let matchcount = len(getcompletion('', name))
278    call assert_true(matchcount >= 0, 'No matches for ' . name)
279  endfor
280
281  call delete('Xtags')
282
283  call assert_fails('call getcompletion("", "burp")', 'E475:')
284endfunc
285
286func Test_expand_star_star()
287  call mkdir('a/b', 'p')
288  call writefile(['asdfasdf'], 'a/b/fileXname')
289  call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
290  call assert_equal('find a/b/fileXname', getreg(':'))
291  bwipe!
292  call delete('a', 'rf')
293endfunc
294
295func Test_paste_in_cmdline()
296  let @a = "def"
297  call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
298  call assert_equal('"abc def ghi', @:)
299
300  new
301  call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
302
303  call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
304  call assert_equal('"aaa asdf bbb', @:)
305
306  call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
307  call assert_equal('"aaa /tmp/some bbb', @:)
308
309  set incsearch
310  call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
311  call assert_equal('"aaa verylongword bbb', @:)
312
313  call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
314  call assert_equal('"aaa a;b-c*d bbb', @:)
315
316  call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
317  call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
318  bwipe!
319endfunc
320
321func Test_remove_char_in_cmdline()
322  call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
323  call assert_equal('"abc ef', @:)
324
325  call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
326  call assert_equal('"abcdef', @:)
327
328  call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
329  call assert_equal('"abc ghi', @:)
330
331  call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
332  call assert_equal('"def', @:)
333endfunc
334
335func Test_illegal_address1()
336  new
337  2;'(
338  2;')
339  quit
340endfunc
341
342func Test_illegal_address2()
343  call writefile(['c', 'x', '  x', '.', '1;y'], 'Xtest.vim')
344  new
345  source Xtest.vim
346  " Trigger calling validate_cursor()
347  diffsp Xtest.vim
348  quit!
349  bwipe!
350  call delete('Xtest.vim')
351endfunc
352
353func Test_cmdline_complete_wildoptions()
354  help
355  call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
356  let a = join(sort(split(@:)),' ')
357  set wildoptions=tagfile
358  call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
359  let b = join(sort(split(@:)),' ')
360  call assert_equal(a, b)
361  bw!
362endfunc
363
364func Test_cmdline_complete_user_cmd()
365  command! -complete=color -nargs=1 Foo :
366  call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
367  call assert_equal('"Foo blue', @:)
368  call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
369  call assert_equal('"Foo blue', @:)
370  delcommand Foo
371endfunc
372
373func Test_cmdline_write_alternatefile()
374  new
375  call setline('.', ['one', 'two'])
376  f foo.txt
377  new
378  f #-A
379  call assert_equal('foo.txt-A', expand('%'))
380  f #<-B.txt
381  call assert_equal('foo-B.txt', expand('%'))
382  f %<
383  call assert_equal('foo-B', expand('%'))
384  new
385  call assert_fails('f #<', 'E95')
386  bw!
387  f foo-B.txt
388  f %<-A
389  call assert_equal('foo-B-A', expand('%'))
390  bw!
391  bw!
392endfunc
393
394" using a leading backslash here
395set cpo+=C
396
397func Test_cmdline_search_range()
398  new
399  call setline(1, ['a', 'b', 'c', 'd'])
400  /d
401  1,\/s/b/B/
402  call assert_equal('B', getline(2))
403
404  /a
405  $
406  \?,4s/c/C/
407  call assert_equal('C', getline(3))
408
409  call setline(1, ['a', 'b', 'c', 'd'])
410  %s/c/c/
411  1,\&s/b/B/
412  call assert_equal('B', getline(2))
413
414  bwipe!
415endfunc
416
417" Tests for getcmdline(), getcmdpos() and getcmdtype()
418func Check_cmdline(cmdtype)
419  call assert_equal('MyCmd a', getcmdline())
420  call assert_equal(8, getcmdpos())
421  call assert_equal(a:cmdtype, getcmdtype())
422  return ''
423endfunc
424
425func Test_getcmdtype()
426  call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
427
428  let cmdtype = ''
429  debuggreedy
430  call feedkeys(":debug echo 'test'\<CR>", "t")
431  call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
432  call feedkeys("cont\<CR>", "xt")
433  0debuggreedy
434  call assert_equal('>', cmdtype)
435
436  call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
437  call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
438
439  call feedkeys(":call input('Answer?')\<CR>", "t")
440  call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
441
442  call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
443
444  cnoremap <expr> <F6> Check_cmdline('=')
445  call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
446  cunmap <F6>
447endfunc
448
449func Test_verbosefile()
450  set verbosefile=Xlog
451  echomsg 'foo'
452  echomsg 'bar'
453  set verbosefile=
454  let log = readfile('Xlog')
455  call assert_match("foo\nbar", join(log, "\n"))
456  call delete('Xlog')
457endfunc
458
459set cpo&
460