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