blob: f9fd80d2db0b77a5307df5e002333cdd965418e4 [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()
Bram Moolenaar37db6422019-03-28 21:26:23 +010017 call mkdir('Xdir1/Xdir2', 'p')
18 call writefile(['testfile1'], 'Xdir1/Xtestfile1')
19 call writefile(['testfile2'], 'Xdir1/Xtestfile2')
20 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile3')
21 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile4')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020022 set wildmenu
Bram Moolenaar37db6422019-03-28 21:26:23 +010023
24 " Pressing <Tab> completes, and moves to next files when pressing again.
25 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<CR>", 'tx')
26 call assert_equal('testfile1', getline(1))
27 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020028 call assert_equal('testfile2', getline(1))
29
Bram Moolenaar37db6422019-03-28 21:26:23 +010030 " <S-Tab> is like <Tab> but begin with the last match and then go to
31 " previous.
32 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<CR>", 'tx')
33 call assert_equal('testfile2', getline(1))
34 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx')
35 call assert_equal('testfile1', getline(1))
36
37 " <Left>/<Right> to move to previous/next file.
38 call feedkeys(":e Xdir1/\<Tab>\<Right>\<CR>", 'tx')
39 call assert_equal('testfile1', getline(1))
40 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<CR>", 'tx')
41 call assert_equal('testfile2', getline(1))
42 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx')
43 call assert_equal('testfile1', getline(1))
44
45 " <Up>/<Down> to go up/down directories.
46 call feedkeys(":e Xdir1/\<Tab>\<Down>\<CR>", 'tx')
47 call assert_equal('testfile3', getline(1))
48 call feedkeys(":e Xdir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx')
49 call assert_equal('testfile1', getline(1))
50
51 " cleanup
52 %bwipe
53 call delete('Xdir1/Xdir2/Xtestfile4')
54 call delete('Xdir1/Xdir2/Xtestfile3')
55 call delete('Xdir1/Xtestfile2')
56 call delete('Xdir1/Xtestfile1')
57 call delete('Xdir1/Xdir2', 'd')
58 call delete('Xdir1', 'd')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020059 set nowildmenu
60endfunc
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020061
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +010062func Test_map_completion()
63 if !has('cmdline_compl')
64 return
65 endif
66 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
67 call assert_equal('"map <unique> <silent>', getreg(':'))
68 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
69 call assert_equal('"map <script> <unique>', getreg(':'))
70 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
71 call assert_equal('"map <expr> <script>', getreg(':'))
72 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
73 call assert_equal('"map <buffer> <expr>', getreg(':'))
74 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
75 call assert_equal('"map <nowait> <buffer>', getreg(':'))
76 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
77 call assert_equal('"map <special> <nowait>', getreg(':'))
78 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
79 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +020080
81 map ,f commaf
82 map ,g commaf
83 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
84 call assert_equal('"map ,f', getreg(':'))
85 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
86 call assert_equal('"map ,g', getreg(':'))
87 unmap ,f
88 unmap ,g
89
90 set cpo-=< cpo-=B cpo-=k
91 map <Left> left
92 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
93 call assert_equal('"map <Left>', getreg(':'))
94 unmap <Left>
95
96 set cpo+=<
97 map <Left> left
98 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
99 call assert_equal('"map <Left>', getreg(':'))
100 unmap <Left>
101 set cpo-=<
102
103 set cpo+=B
104 map <Left> left
105 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
106 call assert_equal('"map <Left>', getreg(':'))
107 unmap <Left>
108 set cpo-=B
109
110 set cpo+=k
111 map <Left> left
112 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
113 call assert_equal('"map <Left>', getreg(':'))
114 unmap <Left>
115 set cpo-=k
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100116endfunc
117
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100118func Test_match_completion()
119 if !has('cmdline_compl')
120 return
121 endif
122 hi Aardig ctermfg=green
123 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
124 call assert_equal('"match Aardig', getreg(':'))
125 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
126 call assert_equal('"match none', getreg(':'))
127endfunc
128
129func Test_highlight_completion()
130 if !has('cmdline_compl')
131 return
132 endif
133 hi Aardig ctermfg=green
134 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
135 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200136 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
137 call assert_equal('"hi default Aardig', getreg(':'))
138 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
139 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100140 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
141 call assert_equal('"hi link', getreg(':'))
142 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
143 call assert_equal('"hi default', getreg(':'))
144 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
145 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200146
147 " A cleared group does not show up in completions.
148 hi Anders ctermfg=green
149 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
150 hi clear Aardig
151 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
152 hi clear Anders
153 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100154endfunc
155
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100156func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100157 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100158 return
159 endif
160 for cmd in [
161 \ 'let a = ',
162 \ 'if',
163 \ 'elseif',
164 \ 'while',
165 \ 'for',
166 \ 'echo',
167 \ 'echon',
168 \ 'execute',
169 \ 'echomsg',
170 \ 'echoerr',
171 \ 'call',
172 \ 'return',
173 \ 'cexpr',
174 \ 'caddexpr',
175 \ 'cgetexpr',
176 \ 'lexpr',
177 \ 'laddexpr',
178 \ 'lgetexpr']
179 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
180 call assert_equal('"' . cmd . ' getline(', getreg(':'))
181 endfor
182endfunc
183
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200184func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200185 if !has('cmdline_compl')
186 return
187 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200188 let groupcount = len(getcompletion('', 'event'))
189 call assert_true(groupcount > 0)
190 let matchcount = len(getcompletion('File', 'event'))
191 call assert_true(matchcount > 0)
192 call assert_true(groupcount > matchcount)
193
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200194 if has('menu')
195 source $VIMRUNTIME/menu.vim
196 let matchcount = len(getcompletion('', 'menu'))
197 call assert_true(matchcount > 0)
198 call assert_equal(['File.'], getcompletion('File', 'menu'))
199 call assert_true(matchcount > 0)
200 let matchcount = len(getcompletion('File.', 'menu'))
201 call assert_true(matchcount > 0)
202 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200203
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200204 let l = getcompletion('v:n', 'var')
205 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200206 let l = getcompletion('v:notexists', 'var')
207 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200208
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200209 args a.c b.c
210 let l = getcompletion('', 'arglist')
211 call assert_equal(['a.c', 'b.c'], l)
212 %argdelete
213
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200214 let l = getcompletion('', 'augroup')
215 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200216 let l = getcompletion('blahblah', 'augroup')
217 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200218
219 let l = getcompletion('', 'behave')
220 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200221 let l = getcompletion('not', 'behave')
222 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200223
224 let l = getcompletion('', 'color')
225 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200226 let l = getcompletion('dirty', 'color')
227 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200228
229 let l = getcompletion('', 'command')
230 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200231 let l = getcompletion('awake', 'command')
232 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200233
234 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200235 call assert_true(index(l, 'samples/') >= 0)
236 let l = getcompletion('NoMatch', 'dir')
237 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200238
239 let l = getcompletion('exe', 'expression')
240 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200241 let l = getcompletion('kill', 'expression')
242 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200243
244 let l = getcompletion('tag', 'function')
245 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200246 let l = getcompletion('paint', 'function')
247 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200248
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200249 let Flambda = {-> 'hello'}
250 let l = getcompletion('', 'function')
251 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200252 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200253
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200254 let l = getcompletion('run', 'file')
255 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200256 let l = getcompletion('walk', 'file')
257 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200258 set wildignore=*.vim
259 let l = getcompletion('run', 'file', 1)
260 call assert_true(index(l, 'runtest.vim') < 0)
261 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200262
263 let l = getcompletion('ha', 'filetype')
264 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200265 let l = getcompletion('horse', 'filetype')
266 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200267
268 let l = getcompletion('z', 'syntax')
269 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200270 let l = getcompletion('emacs', 'syntax')
271 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200272
273 let l = getcompletion('jikes', 'compiler')
274 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200275 let l = getcompletion('break', 'compiler')
276 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200277
278 let l = getcompletion('last', 'help')
279 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200280 let l = getcompletion('giveup', 'help')
281 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200282
283 let l = getcompletion('time', 'option')
284 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200285 let l = getcompletion('space', 'option')
286 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200287
288 let l = getcompletion('er', 'highlight')
289 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200290 let l = getcompletion('dark', 'highlight')
291 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200292
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200293 let l = getcompletion('', 'messages')
294 call assert_true(index(l, 'clear') >= 0)
295 let l = getcompletion('not', 'messages')
296 call assert_equal([], l)
297
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200298 let l = getcompletion('', 'mapclear')
299 call assert_true(index(l, '<buffer>') >= 0)
300 let l = getcompletion('not', 'mapclear')
301 call assert_equal([], l)
302
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200303 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200304 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200305 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
306 let root = has('win32') ? 'C:\\' : '/'
307 let l = getcompletion(root, 'shellcmd')
308 let expected = map(filter(glob(root . '*', 0, 1),
309 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
310 call assert_equal(expected, l)
311
Bram Moolenaarb650b982016-08-05 20:35:13 +0200312 if has('cscope')
313 let l = getcompletion('', 'cscope')
314 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
315 call assert_equal(cmds, l)
316 " using cmdline completion must not change the result
317 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
318 let l = getcompletion('', 'cscope')
319 call assert_equal(cmds, l)
320 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
321 let l = getcompletion('find ', 'cscope')
322 call assert_equal(keys, l)
323 endif
324
Bram Moolenaar7522f692016-08-06 14:12:50 +0200325 if has('signs')
326 sign define Testing linehl=Comment
327 let l = getcompletion('', 'sign')
328 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
329 call assert_equal(cmds, l)
330 " using cmdline completion must not change the result
331 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
332 let l = getcompletion('', 'sign')
333 call assert_equal(cmds, l)
334 let l = getcompletion('list ', 'sign')
335 call assert_equal(['Testing'], l)
336 endif
337
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200338 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200339 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200340 if has('cmdline_hist')
341 call add(names, 'history')
342 endif
343 if has('gettext')
344 call add(names, 'locale')
345 endif
346 if has('profile')
347 call add(names, 'syntime')
348 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200349
350 set tags=Xtags
351 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
352
353 for name in names
354 let matchcount = len(getcompletion('', name))
355 call assert_true(matchcount >= 0, 'No matches for ' . name)
356 endfor
357
358 call delete('Xtags')
359
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200360 call assert_fails('call getcompletion("", "burp")', 'E475:')
361endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200362
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200363func Test_shellcmd_completion()
364 let save_path = $PATH
365
366 call mkdir('Xpathdir/Xpathsubdir', 'p')
367 call writefile([''], 'Xpathdir/Xfile.exe')
368 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
369
370 " Set PATH to example directory without trailing slash.
371 let $PATH = getcwd() . '/Xpathdir'
372
373 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
374 " dirs in the PATH, even though they won't be executed. We check that only
375 " subdirs of the PWD and executables from the PATH are included in the
376 " suggestions.
377 let actual = getcompletion('X', 'shellcmd')
378 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
379 call insert(expected, 'Xfile.exe')
380 call assert_equal(expected, actual)
381
382 call delete('Xpathdir', 'rf')
383 let $PATH = save_path
384endfunc
385
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200386func Test_expand_star_star()
387 call mkdir('a/b', 'p')
388 call writefile(['asdfasdf'], 'a/b/fileXname')
389 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
390 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200391 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200392 call delete('a', 'rf')
393endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100394
395func Test_paste_in_cmdline()
396 let @a = "def"
397 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
398 call assert_equal('"abc def ghi', @:)
399
400 new
401 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
402
403 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
404 call assert_equal('"aaa asdf bbb', @:)
405
406 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
407 call assert_equal('"aaa /tmp/some bbb', @:)
408
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200409 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
410 call assert_equal('"aaa '.getline(1).' bbb', @:)
411
Bram Moolenaar21efc362016-12-03 14:05:49 +0100412 set incsearch
413 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
414 call assert_equal('"aaa verylongword bbb', @:)
415
416 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
417 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100418
419 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
420 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100421 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200422
423 " Error while typing a command used to cause that it was not executed
424 " in the end.
425 new
426 try
427 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
428 catch /^Vim\%((\a\+)\)\=:E32/
429 " ignore error E32
430 endtry
431 call assert_equal("Xtestfile", bufname("%"))
432 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100433endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100434
435func Test_remove_char_in_cmdline()
436 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
437 call assert_equal('"abc ef', @:)
438
439 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
440 call assert_equal('"abcdef', @:)
441
442 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
443 call assert_equal('"abc ghi', @:)
444
445 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
446 call assert_equal('"def', @:)
447endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100448
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100449func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100450 new
451 2;'(
452 2;')
453 quit
454endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100455
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100456func Test_illegal_address2()
457 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
458 new
459 source Xtest.vim
460 " Trigger calling validate_cursor()
461 diffsp Xtest.vim
462 quit!
463 bwipe!
464 call delete('Xtest.vim')
465endfunc
466
Bram Moolenaarba47b512017-01-24 21:18:19 +0100467func Test_cmdline_complete_wildoptions()
468 help
469 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
470 let a = join(sort(split(@:)),' ')
471 set wildoptions=tagfile
472 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
473 let b = join(sort(split(@:)),' ')
474 call assert_equal(a, b)
475 bw!
476endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100477
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200478func Test_cmdline_complete_user_cmd()
479 command! -complete=color -nargs=1 Foo :
480 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
481 call assert_equal('"Foo blue', @:)
482 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
483 call assert_equal('"Foo blue', @:)
484 delcommand Foo
485endfunc
486
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200487func Test_cmdline_complete_user_names()
488 if has('unix') && executable('whoami')
489 let whoami = systemlist('whoami')[0]
490 let first_letter = whoami[0]
491 if len(first_letter) > 0
492 " Trying completion of :e ~x where x is the first letter of
493 " the user name should complete to at least the user name.
494 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
495 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
496 endif
497 endif
498 if has('win32')
499 " Just in case: check that the system has an Administrator account.
500 let names = system('net user')
501 if names =~ 'Administrator'
502 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100503 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200504 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100505 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200506 endif
507 endif
508endfunc
509
510funct Test_cmdline_complete_languages()
511 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
512
513 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
514 call assert_match('^"language .*\<ctype\>.*\<messages\>.*\<time\>', @:)
515
516 if has('unix')
517 " TODO: these tests don't work on Windows. lang appears to be 'C'
518 " but C does not appear in the completion. Why?
519 call assert_match('^"language .*\<' . lang . '\>', @:)
520
521 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
522 call assert_match('^"language .*\<' . lang . '\>', @:)
523
524 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
525 call assert_match('^"language .*\<' . lang . '\>', @:)
526
527 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
528 call assert_match('^"language .*\<' . lang . '\>', @:)
529 endif
530endfunc
531
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200532func Test_cmdline_write_alternatefile()
533 new
534 call setline('.', ['one', 'two'])
535 f foo.txt
536 new
537 f #-A
538 call assert_equal('foo.txt-A', expand('%'))
539 f #<-B.txt
540 call assert_equal('foo-B.txt', expand('%'))
541 f %<
542 call assert_equal('foo-B', expand('%'))
543 new
544 call assert_fails('f #<', 'E95')
545 bw!
546 f foo-B.txt
547 f %<-A
548 call assert_equal('foo-B-A', expand('%'))
549 bw!
550 bw!
551endfunc
552
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100553" using a leading backslash here
554set cpo+=C
555
556func Test_cmdline_search_range()
557 new
558 call setline(1, ['a', 'b', 'c', 'd'])
559 /d
560 1,\/s/b/B/
561 call assert_equal('B', getline(2))
562
563 /a
564 $
565 \?,4s/c/C/
566 call assert_equal('C', getline(3))
567
568 call setline(1, ['a', 'b', 'c', 'd'])
569 %s/c/c/
570 1,\&s/b/B/
571 call assert_equal('B', getline(2))
572
573 bwipe!
574endfunc
575
Bram Moolenaar65189a12017-02-06 22:22:17 +0100576" Tests for getcmdline(), getcmdpos() and getcmdtype()
577func Check_cmdline(cmdtype)
578 call assert_equal('MyCmd a', getcmdline())
579 call assert_equal(8, getcmdpos())
580 call assert_equal(a:cmdtype, getcmdtype())
581 return ''
582endfunc
583
584func Test_getcmdtype()
585 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
586
587 let cmdtype = ''
588 debuggreedy
589 call feedkeys(":debug echo 'test'\<CR>", "t")
590 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
591 call feedkeys("cont\<CR>", "xt")
592 0debuggreedy
593 call assert_equal('>', cmdtype)
594
595 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
596 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
597
598 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100599 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100600
601 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
602
603 cnoremap <expr> <F6> Check_cmdline('=')
604 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
605 cunmap <F6>
606endfunc
607
Bram Moolenaar81612b72018-06-23 14:55:03 +0200608func Test_getcmdwintype()
609 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
610 call assert_equal('/', a)
611
612 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
613 call assert_equal('?', a)
614
615 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
616 call assert_equal(':', a)
617
618 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
619 call assert_equal(':', a)
620
621 call assert_equal('', getcmdwintype())
622endfunc
623
Bram Moolenaar52604f22017-04-07 16:17:39 +0200624func Test_verbosefile()
625 set verbosefile=Xlog
626 echomsg 'foo'
627 echomsg 'bar'
628 set verbosefile=
629 let log = readfile('Xlog')
630 call assert_match("foo\nbar", join(log, "\n"))
631 call delete('Xlog')
632endfunc
633
Bram Moolenaarff3be4f2018-05-12 13:18:46 +0200634func Test_setcmdpos()
635 func InsertTextAtPos(text, pos)
636 call assert_equal(0, setcmdpos(a:pos))
637 return a:text
638 endfunc
639
640 " setcmdpos() with position in the middle of the command line.
641 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
642 call assert_equal('"1ab2', @:)
643
644 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
645 call assert_equal('"1b2a', @:)
646
647 " setcmdpos() with position beyond the end of the command line.
648 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
649 call assert_equal('"12ab', @:)
650
651 " setcmdpos() returns 1 when not editing the command line.
652 call assert_equal(1, setcmdpos(3))
653endfunc
654
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100655func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100656 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100657 let encoding_save = &encoding
658
659 for e in encodings
660 exe 'set encoding=' . e
661
662 " Test overstrike in the middle of the command line.
663 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
664 call assert_equal('"0ab1cd4', @:)
665
666 " Test overstrike going beyond end of command line.
667 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
668 call assert_equal('"0ab1cdefgh', @:)
669
670 " Test toggling insert/overstrike a few times.
671 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
672 call assert_equal('"ab0cd3ef4', @:)
673 endfor
674
Bram Moolenaar30276f22019-01-24 17:59:39 +0100675 " Test overstrike with multi-byte characters.
676 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
677 call assert_equal('"テabキcdエディタ', @:)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100678
679 let &encoding = encoding_save
680endfunc
681
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100682set cpo&