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