blob: 8d5ae286e6d26429978a5a79beb157d30f5eab8a [file] [log] [blame]
Bram Moolenaarae3150e2016-06-11 23:22:36 +02001" Tests for editing the command line.
2
3func Test_complete_tab()
4 call writefile(['testfile'], 'Xtestfile')
5 call feedkeys(":e Xtest\t\r", "tx")
6 call assert_equal('testfile', getline(1))
7 call delete('Xtestfile')
8endfunc
9
10func Test_complete_list()
11 " We can't see the output, but at least we check the code runs properly.
12 call feedkeys(":e test\<C-D>\r", "tx")
13 call assert_equal('test', expand('%:t'))
14endfunc
15
16func Test_complete_wildmenu()
17 call writefile(['testfile1'], 'Xtestfile1')
18 call writefile(['testfile2'], 'Xtestfile2')
19 set wildmenu
20 call feedkeys(":e Xtest\t\t\r", "tx")
21 call assert_equal('testfile2', getline(1))
22
23 call delete('Xtestfile1')
24 call delete('Xtestfile2')
25 set nowildmenu
26endfunc
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020027
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +010028func Test_map_completion()
29 if !has('cmdline_compl')
30 return
31 endif
32 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
33 call assert_equal('"map <unique> <silent>', getreg(':'))
34 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
35 call assert_equal('"map <script> <unique>', getreg(':'))
36 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
37 call assert_equal('"map <expr> <script>', getreg(':'))
38 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
39 call assert_equal('"map <buffer> <expr>', getreg(':'))
40 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
41 call assert_equal('"map <nowait> <buffer>', getreg(':'))
42 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
43 call assert_equal('"map <special> <nowait>', getreg(':'))
44 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
45 call assert_equal('"map <silent> <special>', getreg(':'))
46endfunc
47
Bram Moolenaar15eedf12017-01-22 19:25:33 +010048func Test_match_completion()
49 if !has('cmdline_compl')
50 return
51 endif
52 hi Aardig ctermfg=green
53 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
54 call assert_equal('"match Aardig', getreg(':'))
55 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
56 call assert_equal('"match none', getreg(':'))
57endfunc
58
59func Test_highlight_completion()
60 if !has('cmdline_compl')
61 return
62 endif
63 hi Aardig ctermfg=green
64 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
65 call assert_equal('"hi Aardig', getreg(':'))
66 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
67 call assert_equal('"hi link', getreg(':'))
68 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
69 call assert_equal('"hi default', getreg(':'))
70 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
71 call assert_equal('"hi clear', getreg(':'))
72endfunc
73
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010074func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +010075 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010076 return
77 endif
78 for cmd in [
79 \ 'let a = ',
80 \ 'if',
81 \ 'elseif',
82 \ 'while',
83 \ 'for',
84 \ 'echo',
85 \ 'echon',
86 \ 'execute',
87 \ 'echomsg',
88 \ 'echoerr',
89 \ 'call',
90 \ 'return',
91 \ 'cexpr',
92 \ 'caddexpr',
93 \ 'cgetexpr',
94 \ 'lexpr',
95 \ 'laddexpr',
96 \ 'lgetexpr']
97 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
98 call assert_equal('"' . cmd . ' getline(', getreg(':'))
99 endfor
100endfunc
101
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200102func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200103 if !has('cmdline_compl')
104 return
105 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200106 let groupcount = len(getcompletion('', 'event'))
107 call assert_true(groupcount > 0)
108 let matchcount = len(getcompletion('File', 'event'))
109 call assert_true(matchcount > 0)
110 call assert_true(groupcount > matchcount)
111
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200112 if has('menu')
113 source $VIMRUNTIME/menu.vim
114 let matchcount = len(getcompletion('', 'menu'))
115 call assert_true(matchcount > 0)
116 call assert_equal(['File.'], getcompletion('File', 'menu'))
117 call assert_true(matchcount > 0)
118 let matchcount = len(getcompletion('File.', 'menu'))
119 call assert_true(matchcount > 0)
120 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200121
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200122 let l = getcompletion('v:n', 'var')
123 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200124 let l = getcompletion('v:notexists', 'var')
125 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200126
127 let l = getcompletion('', 'augroup')
128 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200129 let l = getcompletion('blahblah', 'augroup')
130 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200131
132 let l = getcompletion('', 'behave')
133 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200134 let l = getcompletion('not', 'behave')
135 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200136
137 let l = getcompletion('', 'color')
138 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200139 let l = getcompletion('dirty', 'color')
140 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200141
142 let l = getcompletion('', 'command')
143 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200144 let l = getcompletion('awake', 'command')
145 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200146
147 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200148 call assert_true(index(l, 'samples/') >= 0)
149 let l = getcompletion('NoMatch', 'dir')
150 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200151
152 let l = getcompletion('exe', 'expression')
153 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200154 let l = getcompletion('kill', 'expression')
155 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200156
157 let l = getcompletion('tag', 'function')
158 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200159 let l = getcompletion('paint', 'function')
160 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200161
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200162 let Flambda = {-> 'hello'}
163 let l = getcompletion('', 'function')
164 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200165 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200166
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200167 let l = getcompletion('run', 'file')
168 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200169 let l = getcompletion('walk', 'file')
170 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200171 set wildignore=*.vim
172 let l = getcompletion('run', 'file', 1)
173 call assert_true(index(l, 'runtest.vim') < 0)
174 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200175
176 let l = getcompletion('ha', 'filetype')
177 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200178 let l = getcompletion('horse', 'filetype')
179 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200180
181 let l = getcompletion('z', 'syntax')
182 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200183 let l = getcompletion('emacs', 'syntax')
184 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200185
186 let l = getcompletion('jikes', 'compiler')
187 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200188 let l = getcompletion('break', 'compiler')
189 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200190
191 let l = getcompletion('last', 'help')
192 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200193 let l = getcompletion('giveup', 'help')
194 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200195
196 let l = getcompletion('time', 'option')
197 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200198 let l = getcompletion('space', 'option')
199 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200200
201 let l = getcompletion('er', 'highlight')
202 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200203 let l = getcompletion('dark', 'highlight')
204 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200205
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200206 let l = getcompletion('', 'messages')
207 call assert_true(index(l, 'clear') >= 0)
208 let l = getcompletion('not', 'messages')
209 call assert_equal([], l)
210
Bram Moolenaarb650b982016-08-05 20:35:13 +0200211 if has('cscope')
212 let l = getcompletion('', 'cscope')
213 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
214 call assert_equal(cmds, l)
215 " using cmdline completion must not change the result
216 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
217 let l = getcompletion('', 'cscope')
218 call assert_equal(cmds, l)
219 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
220 let l = getcompletion('find ', 'cscope')
221 call assert_equal(keys, l)
222 endif
223
Bram Moolenaar7522f692016-08-06 14:12:50 +0200224 if has('signs')
225 sign define Testing linehl=Comment
226 let l = getcompletion('', 'sign')
227 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
228 call assert_equal(cmds, l)
229 " using cmdline completion must not change the result
230 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
231 let l = getcompletion('', 'sign')
232 call assert_equal(cmds, l)
233 let l = getcompletion('list ', 'sign')
234 call assert_equal(['Testing'], l)
235 endif
236
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200237 " For others test if the name is recognized.
238 let names = ['buffer', 'environment', 'file_in_path',
239 \ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200240 if has('cmdline_hist')
241 call add(names, 'history')
242 endif
243 if has('gettext')
244 call add(names, 'locale')
245 endif
246 if has('profile')
247 call add(names, 'syntime')
248 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200249
250 set tags=Xtags
251 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
252
253 for name in names
254 let matchcount = len(getcompletion('', name))
255 call assert_true(matchcount >= 0, 'No matches for ' . name)
256 endfor
257
258 call delete('Xtags')
259
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200260 call assert_fails('call getcompletion("", "burp")', 'E475:')
261endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200262
263func Test_expand_star_star()
264 call mkdir('a/b', 'p')
265 call writefile(['asdfasdf'], 'a/b/fileXname')
266 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
267 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200268 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200269 call delete('a', 'rf')
270endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100271
272func Test_paste_in_cmdline()
273 let @a = "def"
274 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
275 call assert_equal('"abc def ghi', @:)
276
277 new
278 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
279
280 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
281 call assert_equal('"aaa asdf bbb', @:)
282
283 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
284 call assert_equal('"aaa /tmp/some bbb', @:)
285
286 set incsearch
287 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
288 call assert_equal('"aaa verylongword bbb', @:)
289
290 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
291 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100292
293 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
294 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100295 bwipe!
296endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100297
298func Test_remove_char_in_cmdline()
299 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
300 call assert_equal('"abc ef', @:)
301
302 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
303 call assert_equal('"abcdef', @:)
304
305 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
306 call assert_equal('"abc ghi', @:)
307
308 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
309 call assert_equal('"def', @:)
310endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100311
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100312func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100313 new
314 2;'(
315 2;')
316 quit
317endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100318
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100319func Test_illegal_address2()
320 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
321 new
322 source Xtest.vim
323 " Trigger calling validate_cursor()
324 diffsp Xtest.vim
325 quit!
326 bwipe!
327 call delete('Xtest.vim')
328endfunc
329
Bram Moolenaarba47b512017-01-24 21:18:19 +0100330func Test_cmdline_complete_wildoptions()
331 help
332 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
333 let a = join(sort(split(@:)),' ')
334 set wildoptions=tagfile
335 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
336 let b = join(sort(split(@:)),' ')
337 call assert_equal(a, b)
338 bw!
339endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100340
341" using a leading backslash here
342set cpo+=C
343
344func Test_cmdline_search_range()
345 new
346 call setline(1, ['a', 'b', 'c', 'd'])
347 /d
348 1,\/s/b/B/
349 call assert_equal('B', getline(2))
350
351 /a
352 $
353 \?,4s/c/C/
354 call assert_equal('C', getline(3))
355
356 call setline(1, ['a', 'b', 'c', 'd'])
357 %s/c/c/
358 1,\&s/b/B/
359 call assert_equal('B', getline(2))
360
361 bwipe!
362endfunc
363
Bram Moolenaar65189a12017-02-06 22:22:17 +0100364" Tests for getcmdline(), getcmdpos() and getcmdtype()
365func Check_cmdline(cmdtype)
366 call assert_equal('MyCmd a', getcmdline())
367 call assert_equal(8, getcmdpos())
368 call assert_equal(a:cmdtype, getcmdtype())
369 return ''
370endfunc
371
372func Test_getcmdtype()
373 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
374
375 let cmdtype = ''
376 debuggreedy
377 call feedkeys(":debug echo 'test'\<CR>", "t")
378 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
379 call feedkeys("cont\<CR>", "xt")
380 0debuggreedy
381 call assert_equal('>', cmdtype)
382
383 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
384 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
385
386 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100387 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100388
389 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
390
391 cnoremap <expr> <F6> Check_cmdline('=')
392 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
393 cunmap <F6>
394endfunc
395
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100396set cpo&