blob: 3a849cbd15647216395a2b49ae6f548d18907511 [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(':'))
80endfunc
81
Bram Moolenaar15eedf12017-01-22 19:25:33 +010082func Test_match_completion()
83 if !has('cmdline_compl')
84 return
85 endif
86 hi Aardig ctermfg=green
87 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
88 call assert_equal('"match Aardig', getreg(':'))
89 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
90 call assert_equal('"match none', getreg(':'))
91endfunc
92
93func Test_highlight_completion()
94 if !has('cmdline_compl')
95 return
96 endif
97 hi Aardig ctermfg=green
98 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
99 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200100 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
101 call assert_equal('"hi default Aardig', getreg(':'))
102 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
103 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100104 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
105 call assert_equal('"hi link', getreg(':'))
106 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
107 call assert_equal('"hi default', getreg(':'))
108 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
109 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200110
111 " A cleared group does not show up in completions.
112 hi Anders ctermfg=green
113 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
114 hi clear Aardig
115 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
116 hi clear Anders
117 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100118endfunc
119
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100120func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100121 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100122 return
123 endif
124 for cmd in [
125 \ 'let a = ',
126 \ 'if',
127 \ 'elseif',
128 \ 'while',
129 \ 'for',
130 \ 'echo',
131 \ 'echon',
132 \ 'execute',
133 \ 'echomsg',
134 \ 'echoerr',
135 \ 'call',
136 \ 'return',
137 \ 'cexpr',
138 \ 'caddexpr',
139 \ 'cgetexpr',
140 \ 'lexpr',
141 \ 'laddexpr',
142 \ 'lgetexpr']
143 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
144 call assert_equal('"' . cmd . ' getline(', getreg(':'))
145 endfor
146endfunc
147
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200148func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200149 if !has('cmdline_compl')
150 return
151 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200152 let groupcount = len(getcompletion('', 'event'))
153 call assert_true(groupcount > 0)
154 let matchcount = len(getcompletion('File', 'event'))
155 call assert_true(matchcount > 0)
156 call assert_true(groupcount > matchcount)
157
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200158 if has('menu')
159 source $VIMRUNTIME/menu.vim
160 let matchcount = len(getcompletion('', 'menu'))
161 call assert_true(matchcount > 0)
162 call assert_equal(['File.'], getcompletion('File', 'menu'))
163 call assert_true(matchcount > 0)
164 let matchcount = len(getcompletion('File.', 'menu'))
165 call assert_true(matchcount > 0)
166 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200167
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200168 let l = getcompletion('v:n', 'var')
169 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200170 let l = getcompletion('v:notexists', 'var')
171 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200172
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200173 args a.c b.c
174 let l = getcompletion('', 'arglist')
175 call assert_equal(['a.c', 'b.c'], l)
176 %argdelete
177
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200178 let l = getcompletion('', 'augroup')
179 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200180 let l = getcompletion('blahblah', 'augroup')
181 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200182
183 let l = getcompletion('', 'behave')
184 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200185 let l = getcompletion('not', 'behave')
186 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200187
188 let l = getcompletion('', 'color')
189 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200190 let l = getcompletion('dirty', 'color')
191 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200192
193 let l = getcompletion('', 'command')
194 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200195 let l = getcompletion('awake', 'command')
196 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200197
198 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200199 call assert_true(index(l, 'samples/') >= 0)
200 let l = getcompletion('NoMatch', 'dir')
201 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200202
203 let l = getcompletion('exe', 'expression')
204 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200205 let l = getcompletion('kill', 'expression')
206 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200207
208 let l = getcompletion('tag', 'function')
209 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200210 let l = getcompletion('paint', 'function')
211 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200212
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200213 let Flambda = {-> 'hello'}
214 let l = getcompletion('', 'function')
215 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200216 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200217
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200218 let l = getcompletion('run', 'file')
219 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200220 let l = getcompletion('walk', 'file')
221 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200222 set wildignore=*.vim
223 let l = getcompletion('run', 'file', 1)
224 call assert_true(index(l, 'runtest.vim') < 0)
225 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200226
227 let l = getcompletion('ha', 'filetype')
228 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200229 let l = getcompletion('horse', 'filetype')
230 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200231
232 let l = getcompletion('z', 'syntax')
233 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200234 let l = getcompletion('emacs', 'syntax')
235 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200236
237 let l = getcompletion('jikes', 'compiler')
238 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200239 let l = getcompletion('break', 'compiler')
240 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200241
242 let l = getcompletion('last', 'help')
243 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200244 let l = getcompletion('giveup', 'help')
245 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200246
247 let l = getcompletion('time', 'option')
248 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200249 let l = getcompletion('space', 'option')
250 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200251
252 let l = getcompletion('er', 'highlight')
253 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200254 let l = getcompletion('dark', 'highlight')
255 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200256
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200257 let l = getcompletion('', 'messages')
258 call assert_true(index(l, 'clear') >= 0)
259 let l = getcompletion('not', 'messages')
260 call assert_equal([], l)
261
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200262 let l = getcompletion('', 'mapclear')
263 call assert_true(index(l, '<buffer>') >= 0)
264 let l = getcompletion('not', 'mapclear')
265 call assert_equal([], l)
266
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200267 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200268 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200269 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
270 let root = has('win32') ? 'C:\\' : '/'
271 let l = getcompletion(root, 'shellcmd')
272 let expected = map(filter(glob(root . '*', 0, 1),
273 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
274 call assert_equal(expected, l)
275
Bram Moolenaarb650b982016-08-05 20:35:13 +0200276 if has('cscope')
277 let l = getcompletion('', 'cscope')
278 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
279 call assert_equal(cmds, l)
280 " using cmdline completion must not change the result
281 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
282 let l = getcompletion('', 'cscope')
283 call assert_equal(cmds, l)
284 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
285 let l = getcompletion('find ', 'cscope')
286 call assert_equal(keys, l)
287 endif
288
Bram Moolenaar7522f692016-08-06 14:12:50 +0200289 if has('signs')
290 sign define Testing linehl=Comment
291 let l = getcompletion('', 'sign')
292 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
293 call assert_equal(cmds, l)
294 " using cmdline completion must not change the result
295 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
296 let l = getcompletion('', 'sign')
297 call assert_equal(cmds, l)
298 let l = getcompletion('list ', 'sign')
299 call assert_equal(['Testing'], l)
300 endif
301
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200302 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200303 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200304 if has('cmdline_hist')
305 call add(names, 'history')
306 endif
307 if has('gettext')
308 call add(names, 'locale')
309 endif
310 if has('profile')
311 call add(names, 'syntime')
312 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200313
314 set tags=Xtags
315 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
316
317 for name in names
318 let matchcount = len(getcompletion('', name))
319 call assert_true(matchcount >= 0, 'No matches for ' . name)
320 endfor
321
322 call delete('Xtags')
323
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200324 call assert_fails('call getcompletion("", "burp")', 'E475:')
325endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200326
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200327func Test_shellcmd_completion()
328 let save_path = $PATH
329
330 call mkdir('Xpathdir/Xpathsubdir', 'p')
331 call writefile([''], 'Xpathdir/Xfile.exe')
332 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
333
334 " Set PATH to example directory without trailing slash.
335 let $PATH = getcwd() . '/Xpathdir'
336
337 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
338 " dirs in the PATH, even though they won't be executed. We check that only
339 " subdirs of the PWD and executables from the PATH are included in the
340 " suggestions.
341 let actual = getcompletion('X', 'shellcmd')
342 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
343 call insert(expected, 'Xfile.exe')
344 call assert_equal(expected, actual)
345
346 call delete('Xpathdir', 'rf')
347 let $PATH = save_path
348endfunc
349
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200350func Test_expand_star_star()
351 call mkdir('a/b', 'p')
352 call writefile(['asdfasdf'], 'a/b/fileXname')
353 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
354 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200355 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200356 call delete('a', 'rf')
357endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100358
359func Test_paste_in_cmdline()
360 let @a = "def"
361 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
362 call assert_equal('"abc def ghi', @:)
363
364 new
365 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
366
367 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
368 call assert_equal('"aaa asdf bbb', @:)
369
370 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
371 call assert_equal('"aaa /tmp/some bbb', @:)
372
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200373 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
374 call assert_equal('"aaa '.getline(1).' bbb', @:)
375
Bram Moolenaar21efc362016-12-03 14:05:49 +0100376 set incsearch
377 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
378 call assert_equal('"aaa verylongword bbb', @:)
379
380 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
381 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100382
383 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
384 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100385 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200386
387 " Error while typing a command used to cause that it was not executed
388 " in the end.
389 new
390 try
391 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
392 catch /^Vim\%((\a\+)\)\=:E32/
393 " ignore error E32
394 endtry
395 call assert_equal("Xtestfile", bufname("%"))
396 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100397endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100398
399func Test_remove_char_in_cmdline()
400 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
401 call assert_equal('"abc ef', @:)
402
403 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
404 call assert_equal('"abcdef', @:)
405
406 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
407 call assert_equal('"abc ghi', @:)
408
409 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
410 call assert_equal('"def', @:)
411endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100412
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100413func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100414 new
415 2;'(
416 2;')
417 quit
418endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100419
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100420func Test_illegal_address2()
421 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
422 new
423 source Xtest.vim
424 " Trigger calling validate_cursor()
425 diffsp Xtest.vim
426 quit!
427 bwipe!
428 call delete('Xtest.vim')
429endfunc
430
Bram Moolenaarba47b512017-01-24 21:18:19 +0100431func Test_cmdline_complete_wildoptions()
432 help
433 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
434 let a = join(sort(split(@:)),' ')
435 set wildoptions=tagfile
436 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
437 let b = join(sort(split(@:)),' ')
438 call assert_equal(a, b)
439 bw!
440endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100441
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200442func Test_cmdline_complete_user_cmd()
443 command! -complete=color -nargs=1 Foo :
444 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
445 call assert_equal('"Foo blue', @:)
446 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
447 call assert_equal('"Foo blue', @:)
448 delcommand Foo
449endfunc
450
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200451func Test_cmdline_complete_user_names()
452 if has('unix') && executable('whoami')
453 let whoami = systemlist('whoami')[0]
454 let first_letter = whoami[0]
455 if len(first_letter) > 0
456 " Trying completion of :e ~x where x is the first letter of
457 " the user name should complete to at least the user name.
458 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
459 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
460 endif
461 endif
462 if has('win32')
463 " Just in case: check that the system has an Administrator account.
464 let names = system('net user')
465 if names =~ 'Administrator'
466 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100467 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200468 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100469 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200470 endif
471 endif
472endfunc
473
474funct Test_cmdline_complete_languages()
475 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
476
477 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
478 call assert_match('^"language .*\<ctype\>.*\<messages\>.*\<time\>', @:)
479
480 if has('unix')
481 " TODO: these tests don't work on Windows. lang appears to be 'C'
482 " but C does not appear in the completion. Why?
483 call assert_match('^"language .*\<' . lang . '\>', @:)
484
485 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
486 call assert_match('^"language .*\<' . lang . '\>', @:)
487
488 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
489 call assert_match('^"language .*\<' . lang . '\>', @:)
490
491 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
492 call assert_match('^"language .*\<' . lang . '\>', @:)
493 endif
494endfunc
495
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200496func Test_cmdline_write_alternatefile()
497 new
498 call setline('.', ['one', 'two'])
499 f foo.txt
500 new
501 f #-A
502 call assert_equal('foo.txt-A', expand('%'))
503 f #<-B.txt
504 call assert_equal('foo-B.txt', expand('%'))
505 f %<
506 call assert_equal('foo-B', expand('%'))
507 new
508 call assert_fails('f #<', 'E95')
509 bw!
510 f foo-B.txt
511 f %<-A
512 call assert_equal('foo-B-A', expand('%'))
513 bw!
514 bw!
515endfunc
516
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100517" using a leading backslash here
518set cpo+=C
519
520func Test_cmdline_search_range()
521 new
522 call setline(1, ['a', 'b', 'c', 'd'])
523 /d
524 1,\/s/b/B/
525 call assert_equal('B', getline(2))
526
527 /a
528 $
529 \?,4s/c/C/
530 call assert_equal('C', getline(3))
531
532 call setline(1, ['a', 'b', 'c', 'd'])
533 %s/c/c/
534 1,\&s/b/B/
535 call assert_equal('B', getline(2))
536
537 bwipe!
538endfunc
539
Bram Moolenaar65189a12017-02-06 22:22:17 +0100540" Tests for getcmdline(), getcmdpos() and getcmdtype()
541func Check_cmdline(cmdtype)
542 call assert_equal('MyCmd a', getcmdline())
543 call assert_equal(8, getcmdpos())
544 call assert_equal(a:cmdtype, getcmdtype())
545 return ''
546endfunc
547
548func Test_getcmdtype()
549 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
550
551 let cmdtype = ''
552 debuggreedy
553 call feedkeys(":debug echo 'test'\<CR>", "t")
554 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
555 call feedkeys("cont\<CR>", "xt")
556 0debuggreedy
557 call assert_equal('>', cmdtype)
558
559 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
560 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
561
562 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100563 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100564
565 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
566
567 cnoremap <expr> <F6> Check_cmdline('=')
568 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
569 cunmap <F6>
570endfunc
571
Bram Moolenaar81612b72018-06-23 14:55:03 +0200572func Test_getcmdwintype()
573 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
574 call assert_equal('/', a)
575
576 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
577 call assert_equal('?', a)
578
579 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
580 call assert_equal(':', a)
581
582 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
583 call assert_equal(':', a)
584
585 call assert_equal('', getcmdwintype())
586endfunc
587
Bram Moolenaar52604f22017-04-07 16:17:39 +0200588func Test_verbosefile()
589 set verbosefile=Xlog
590 echomsg 'foo'
591 echomsg 'bar'
592 set verbosefile=
593 let log = readfile('Xlog')
594 call assert_match("foo\nbar", join(log, "\n"))
595 call delete('Xlog')
596endfunc
597
Bram Moolenaarff3be4f2018-05-12 13:18:46 +0200598func Test_setcmdpos()
599 func InsertTextAtPos(text, pos)
600 call assert_equal(0, setcmdpos(a:pos))
601 return a:text
602 endfunc
603
604 " setcmdpos() with position in the middle of the command line.
605 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
606 call assert_equal('"1ab2', @:)
607
608 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
609 call assert_equal('"1b2a', @:)
610
611 " setcmdpos() with position beyond the end of the command line.
612 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
613 call assert_equal('"12ab', @:)
614
615 " setcmdpos() returns 1 when not editing the command line.
616 call assert_equal(1, setcmdpos(3))
617endfunc
618
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100619func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100620 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100621 let encoding_save = &encoding
622
623 for e in encodings
624 exe 'set encoding=' . e
625
626 " Test overstrike in the middle of the command line.
627 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
628 call assert_equal('"0ab1cd4', @:)
629
630 " Test overstrike going beyond end of command line.
631 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
632 call assert_equal('"0ab1cdefgh', @:)
633
634 " Test toggling insert/overstrike a few times.
635 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
636 call assert_equal('"ab0cd3ef4', @:)
637 endfor
638
Bram Moolenaar30276f22019-01-24 17:59:39 +0100639 " Test overstrike with multi-byte characters.
640 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
641 call assert_equal('"テabキcdエディタ', @:)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100642
643 let &encoding = encoding_save
644endfunc
645
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100646set cpo&