xref: /vim-8.2.3635/src/testdir/test_cmdline.vim (revision e16b00a1)
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  let l = getcompletion('', 'mapclear')
226  call assert_true(index(l, '<buffer>') >= 0)
227  let l = getcompletion('not', 'mapclear')
228  call assert_equal([], l)
229
230  if has('cscope')
231    let l = getcompletion('', 'cscope')
232    let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
233    call assert_equal(cmds, l)
234    " using cmdline completion must not change the result
235    call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
236    let l = getcompletion('', 'cscope')
237    call assert_equal(cmds, l)
238    let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
239    let l = getcompletion('find ', 'cscope')
240    call assert_equal(keys, l)
241  endif
242
243  if has('signs')
244    sign define Testing linehl=Comment
245    let l = getcompletion('', 'sign')
246    let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
247    call assert_equal(cmds, l)
248    " using cmdline completion must not change the result
249    call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
250    let l = getcompletion('', 'sign')
251    call assert_equal(cmds, l)
252    let l = getcompletion('list ', 'sign')
253    call assert_equal(['Testing'], l)
254  endif
255
256  " For others test if the name is recognized.
257  let names = ['buffer', 'environment', 'file_in_path',
258	\ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
259  if has('cmdline_hist')
260    call add(names, 'history')
261  endif
262  if has('gettext')
263    call add(names, 'locale')
264  endif
265  if has('profile')
266    call add(names, 'syntime')
267  endif
268
269  set tags=Xtags
270  call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
271
272  for name in names
273    let matchcount = len(getcompletion('', name))
274    call assert_true(matchcount >= 0, 'No matches for ' . name)
275  endfor
276
277  call delete('Xtags')
278
279  call assert_fails('call getcompletion("", "burp")', 'E475:')
280endfunc
281
282func Test_expand_star_star()
283  call mkdir('a/b', 'p')
284  call writefile(['asdfasdf'], 'a/b/fileXname')
285  call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
286  call assert_equal('find a/b/fileXname', getreg(':'))
287  bwipe!
288  call delete('a', 'rf')
289endfunc
290
291func Test_paste_in_cmdline()
292  let @a = "def"
293  call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
294  call assert_equal('"abc def ghi', @:)
295
296  new
297  call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
298
299  call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
300  call assert_equal('"aaa asdf bbb', @:)
301
302  call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
303  call assert_equal('"aaa /tmp/some bbb', @:)
304
305  set incsearch
306  call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
307  call assert_equal('"aaa verylongword bbb', @:)
308
309  call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
310  call assert_equal('"aaa a;b-c*d bbb', @:)
311
312  call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
313  call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
314  bwipe!
315endfunc
316
317func Test_remove_char_in_cmdline()
318  call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
319  call assert_equal('"abc ef', @:)
320
321  call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
322  call assert_equal('"abcdef', @:)
323
324  call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
325  call assert_equal('"abc ghi', @:)
326
327  call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
328  call assert_equal('"def', @:)
329endfunc
330
331func Test_illegal_address1()
332  new
333  2;'(
334  2;')
335  quit
336endfunc
337
338func Test_illegal_address2()
339  call writefile(['c', 'x', '  x', '.', '1;y'], 'Xtest.vim')
340  new
341  source Xtest.vim
342  " Trigger calling validate_cursor()
343  diffsp Xtest.vim
344  quit!
345  bwipe!
346  call delete('Xtest.vim')
347endfunc
348
349func Test_cmdline_complete_wildoptions()
350  help
351  call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
352  let a = join(sort(split(@:)),' ')
353  set wildoptions=tagfile
354  call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
355  let b = join(sort(split(@:)),' ')
356  call assert_equal(a, b)
357  bw!
358endfunc
359
360func Test_cmdline_complete_user_cmd()
361  command! -complete=color -nargs=1 Foo :
362  call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
363  call assert_equal('"Foo blue', @:)
364  call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
365  call assert_equal('"Foo blue', @:)
366  delcommand Foo
367endfunc
368
369" using a leading backslash here
370set cpo+=C
371
372func Test_cmdline_search_range()
373  new
374  call setline(1, ['a', 'b', 'c', 'd'])
375  /d
376  1,\/s/b/B/
377  call assert_equal('B', getline(2))
378
379  /a
380  $
381  \?,4s/c/C/
382  call assert_equal('C', getline(3))
383
384  call setline(1, ['a', 'b', 'c', 'd'])
385  %s/c/c/
386  1,\&s/b/B/
387  call assert_equal('B', getline(2))
388
389  bwipe!
390endfunc
391
392" Tests for getcmdline(), getcmdpos() and getcmdtype()
393func Check_cmdline(cmdtype)
394  call assert_equal('MyCmd a', getcmdline())
395  call assert_equal(8, getcmdpos())
396  call assert_equal(a:cmdtype, getcmdtype())
397  return ''
398endfunc
399
400func Test_getcmdtype()
401  call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
402
403  let cmdtype = ''
404  debuggreedy
405  call feedkeys(":debug echo 'test'\<CR>", "t")
406  call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
407  call feedkeys("cont\<CR>", "xt")
408  0debuggreedy
409  call assert_equal('>', cmdtype)
410
411  call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
412  call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
413
414  call feedkeys(":call input('Answer?')\<CR>", "t")
415  call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
416
417  call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
418
419  cnoremap <expr> <F6> Check_cmdline('=')
420  call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
421  cunmap <F6>
422endfunc
423
424func Test_verbosefile()
425  set verbosefile=Xlog
426  echomsg 'foo'
427  echomsg 'bar'
428  set verbosefile=
429  let log = readfile('Xlog')
430  call assert_match("foo\nbar", join(log, "\n"))
431  call delete('Xlog')
432endfunc
433
434set cpo&
435