xref: /vim-8.2.3635/src/testdir/test_cmdline.vim (revision cd5c8f82)
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 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(':'))
74
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'))
82endfunc
83
84func Test_expr_completion()
85  if !has('cmdline_compl')
86    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
112func Test_getcompletion()
113  if !has('cmdline_compl')
114    return
115  endif
116  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
122  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
131
132  let l = getcompletion('v:n', 'var')
133  call assert_true(index(l, 'v:null') >= 0)
134  let l = getcompletion('v:notexists', 'var')
135  call assert_equal([], l)
136
137  let l = getcompletion('', 'augroup')
138  call assert_true(index(l, 'END') >= 0)
139  let l = getcompletion('blahblah', 'augroup')
140  call assert_equal([], l)
141
142  let l = getcompletion('', 'behave')
143  call assert_true(index(l, 'mswin') >= 0)
144  let l = getcompletion('not', 'behave')
145  call assert_equal([], l)
146
147  let l = getcompletion('', 'color')
148  call assert_true(index(l, 'default') >= 0)
149  let l = getcompletion('dirty', 'color')
150  call assert_equal([], l)
151
152  let l = getcompletion('', 'command')
153  call assert_true(index(l, 'sleep') >= 0)
154  let l = getcompletion('awake', 'command')
155  call assert_equal([], l)
156
157  let l = getcompletion('', 'dir')
158  call assert_true(index(l, 'samples/') >= 0)
159  let l = getcompletion('NoMatch', 'dir')
160  call assert_equal([], l)
161
162  let l = getcompletion('exe', 'expression')
163  call assert_true(index(l, 'executable(') >= 0)
164  let l = getcompletion('kill', 'expression')
165  call assert_equal([], l)
166
167  let l = getcompletion('tag', 'function')
168  call assert_true(index(l, 'taglist(') >= 0)
169  let l = getcompletion('paint', 'function')
170  call assert_equal([], l)
171
172  let Flambda = {-> 'hello'}
173  let l = getcompletion('', 'function')
174  let l = filter(l, {i, v -> v =~ 'lambda'})
175  call assert_equal([], l)
176
177  let l = getcompletion('run', 'file')
178  call assert_true(index(l, 'runtest.vim') >= 0)
179  let l = getcompletion('walk', 'file')
180  call assert_equal([], l)
181  set wildignore=*.vim
182  let l = getcompletion('run', 'file', 1)
183  call assert_true(index(l, 'runtest.vim') < 0)
184  set wildignore&
185
186  let l = getcompletion('ha', 'filetype')
187  call assert_true(index(l, 'hamster') >= 0)
188  let l = getcompletion('horse', 'filetype')
189  call assert_equal([], l)
190
191  let l = getcompletion('z', 'syntax')
192  call assert_true(index(l, 'zimbu') >= 0)
193  let l = getcompletion('emacs', 'syntax')
194  call assert_equal([], l)
195
196  let l = getcompletion('jikes', 'compiler')
197  call assert_true(index(l, 'jikes') >= 0)
198  let l = getcompletion('break', 'compiler')
199  call assert_equal([], l)
200
201  let l = getcompletion('last', 'help')
202  call assert_true(index(l, ':tablast') >= 0)
203  let l = getcompletion('giveup', 'help')
204  call assert_equal([], l)
205
206  let l = getcompletion('time', 'option')
207  call assert_true(index(l, 'timeoutlen') >= 0)
208  let l = getcompletion('space', 'option')
209  call assert_equal([], l)
210
211  let l = getcompletion('er', 'highlight')
212  call assert_true(index(l, 'ErrorMsg') >= 0)
213  let l = getcompletion('dark', 'highlight')
214  call assert_equal([], l)
215
216  let l = getcompletion('', 'messages')
217  call assert_true(index(l, 'clear') >= 0)
218  let l = getcompletion('not', 'messages')
219  call assert_equal([], l)
220
221  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
234  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
247  " For others test if the name is recognized.
248  let names = ['buffer', 'environment', 'file_in_path',
249	\ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
250  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
259
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
270  call assert_fails('call getcompletion("", "burp")', 'E475:')
271endfunc
272
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(':'))
278  bwipe!
279  call delete('a', 'rf')
280endfunc
281
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', @:)
302
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 ', @:)
305  bwipe!
306endfunc
307
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
321
322func Test_illegal_address1()
323  new
324  2;'(
325  2;')
326  quit
327endfunc
328
329func 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
340func 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
350
351func 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
360" 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
383" 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")
406  call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
407
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
415func 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
425set cpo&
426