1" Tests for editing the command line. 2 3source check.vim 4source screendump.vim 5 6func Test_complete_tab() 7 call writefile(['testfile'], 'Xtestfile') 8 call feedkeys(":e Xtest\t\r", "tx") 9 call assert_equal('testfile', getline(1)) 10 call delete('Xtestfile') 11endfunc 12 13func Test_complete_list() 14 " We can't see the output, but at least we check the code runs properly. 15 call feedkeys(":e test\<C-D>\r", "tx") 16 call assert_equal('test', expand('%:t')) 17endfunc 18 19func Test_complete_wildmenu() 20 call mkdir('Xdir1/Xdir2', 'p') 21 call writefile(['testfile1'], 'Xdir1/Xtestfile1') 22 call writefile(['testfile2'], 'Xdir1/Xtestfile2') 23 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile3') 24 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile4') 25 set wildmenu 26 27 " Pressing <Tab> completes, and moves to next files when pressing again. 28 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<CR>", 'tx') 29 call assert_equal('testfile1', getline(1)) 30 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx') 31 call assert_equal('testfile2', getline(1)) 32 33 " <S-Tab> is like <Tab> but begin with the last match and then go to 34 " previous. 35 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<CR>", 'tx') 36 call assert_equal('testfile2', getline(1)) 37 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx') 38 call assert_equal('testfile1', getline(1)) 39 40 " <Left>/<Right> to move to previous/next file. 41 call feedkeys(":e Xdir1/\<Tab>\<Right>\<CR>", 'tx') 42 call assert_equal('testfile1', getline(1)) 43 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<CR>", 'tx') 44 call assert_equal('testfile2', getline(1)) 45 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx') 46 call assert_equal('testfile1', getline(1)) 47 48 " <Up>/<Down> to go up/down directories. 49 call feedkeys(":e Xdir1/\<Tab>\<Down>\<CR>", 'tx') 50 call assert_equal('testfile3', getline(1)) 51 call feedkeys(":e Xdir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx') 52 call assert_equal('testfile1', getline(1)) 53 54 " Completion using a relative path 55 cd Xdir1/Xdir2 56 call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx') 57 call assert_equal('"e Xtestfile3 Xtestfile4', @:) 58 cd - 59 60 " cleanup 61 %bwipe 62 call delete('Xdir1/Xdir2/Xtestfile4') 63 call delete('Xdir1/Xdir2/Xtestfile3') 64 call delete('Xdir1/Xtestfile2') 65 call delete('Xdir1/Xtestfile1') 66 call delete('Xdir1/Xdir2', 'd') 67 call delete('Xdir1', 'd') 68 set nowildmenu 69endfunc 70 71func Test_map_completion() 72 if !has('cmdline_compl') 73 return 74 endif 75 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt') 76 call assert_equal('"map <unique> <silent>', getreg(':')) 77 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt') 78 call assert_equal('"map <script> <unique>', getreg(':')) 79 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt') 80 call assert_equal('"map <expr> <script>', getreg(':')) 81 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt') 82 call assert_equal('"map <buffer> <expr>', getreg(':')) 83 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt') 84 call assert_equal('"map <nowait> <buffer>', getreg(':')) 85 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt') 86 call assert_equal('"map <special> <nowait>', getreg(':')) 87 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt') 88 call assert_equal('"map <silent> <special>', getreg(':')) 89 90 map <Middle>x middle 91 92 map ,f commaf 93 map ,g commaf 94 map <Left> left 95 map <A-Left>x shiftleft 96 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt') 97 call assert_equal('"map ,f', getreg(':')) 98 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt') 99 call assert_equal('"map ,g', getreg(':')) 100 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt') 101 call assert_equal('"map <Left>', getreg(':')) 102 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt') 103 call assert_equal("\"map <A-Left>\<Tab>", getreg(':')) 104 unmap ,f 105 unmap ,g 106 unmap <Left> 107 unmap <A-Left>x 108 109 set cpo-=< cpo-=B cpo-=k 110 map <Left> left 111 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt') 112 call assert_equal('"map <Left>', getreg(':')) 113 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt') 114 call assert_equal("\"map <M\<Tab>", getreg(':')) 115 unmap <Left> 116 117 set cpo+=< 118 map <Left> left 119 exe "set t_k6=\<Esc>[17~" 120 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt') 121 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt') 122 call assert_equal('"map <Left>', getreg(':')) 123 if !has('gui_running') 124 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt') 125 call assert_equal("\"map <F6>x", getreg(':')) 126 endif 127 unmap <Left> 128 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt') 129 set cpo-=< 130 131 set cpo+=B 132 map <Left> left 133 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt') 134 call assert_equal('"map <Left>', getreg(':')) 135 unmap <Left> 136 set cpo-=B 137 138 set cpo+=k 139 map <Left> left 140 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt') 141 call assert_equal('"map <Left>', getreg(':')) 142 unmap <Left> 143 set cpo-=k 144 145 unmap <Middle>x 146 set cpo&vim 147endfunc 148 149func Test_match_completion() 150 if !has('cmdline_compl') 151 return 152 endif 153 hi Aardig ctermfg=green 154 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt') 155 call assert_equal('"match Aardig', getreg(':')) 156 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt') 157 call assert_equal('"match none', getreg(':')) 158endfunc 159 160func Test_highlight_completion() 161 if !has('cmdline_compl') 162 return 163 endif 164 hi Aardig ctermfg=green 165 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt') 166 call assert_equal('"hi Aardig', getreg(':')) 167 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt') 168 call assert_equal('"hi default Aardig', getreg(':')) 169 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt') 170 call assert_equal('"hi clear Aardig', getreg(':')) 171 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt') 172 call assert_equal('"hi link', getreg(':')) 173 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt') 174 call assert_equal('"hi default', getreg(':')) 175 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt') 176 call assert_equal('"hi clear', getreg(':')) 177 178 " A cleared group does not show up in completions. 179 hi Anders ctermfg=green 180 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight')) 181 hi clear Aardig 182 call assert_equal(['Anders'], getcompletion('A', 'highlight')) 183 hi clear Anders 184 call assert_equal([], getcompletion('A', 'highlight')) 185endfunc 186 187func Test_expr_completion() 188 if !has('cmdline_compl') 189 return 190 endif 191 for cmd in [ 192 \ 'let a = ', 193 \ 'const a = ', 194 \ 'if', 195 \ 'elseif', 196 \ 'while', 197 \ 'for', 198 \ 'echo', 199 \ 'echon', 200 \ 'execute', 201 \ 'echomsg', 202 \ 'echoerr', 203 \ 'call', 204 \ 'return', 205 \ 'cexpr', 206 \ 'caddexpr', 207 \ 'cgetexpr', 208 \ 'lexpr', 209 \ 'laddexpr', 210 \ 'lgetexpr'] 211 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt') 212 call assert_equal('"' . cmd . ' getline(', getreg(':')) 213 endfor 214endfunc 215 216func Test_getcompletion() 217 if !has('cmdline_compl') 218 return 219 endif 220 let groupcount = len(getcompletion('', 'event')) 221 call assert_true(groupcount > 0) 222 let matchcount = len('File'->getcompletion('event')) 223 call assert_true(matchcount > 0) 224 call assert_true(groupcount > matchcount) 225 226 if has('menu') 227 source $VIMRUNTIME/menu.vim 228 let matchcount = len(getcompletion('', 'menu')) 229 call assert_true(matchcount > 0) 230 call assert_equal(['File.'], getcompletion('File', 'menu')) 231 call assert_true(matchcount > 0) 232 let matchcount = len(getcompletion('File.', 'menu')) 233 call assert_true(matchcount > 0) 234 endif 235 236 let l = getcompletion('v:n', 'var') 237 call assert_true(index(l, 'v:null') >= 0) 238 let l = getcompletion('v:notexists', 'var') 239 call assert_equal([], l) 240 241 args a.c b.c 242 let l = getcompletion('', 'arglist') 243 call assert_equal(['a.c', 'b.c'], l) 244 %argdelete 245 246 let l = getcompletion('', 'augroup') 247 call assert_true(index(l, 'END') >= 0) 248 let l = getcompletion('blahblah', 'augroup') 249 call assert_equal([], l) 250 251 let l = getcompletion('', 'behave') 252 call assert_true(index(l, 'mswin') >= 0) 253 let l = getcompletion('not', 'behave') 254 call assert_equal([], l) 255 256 let l = getcompletion('', 'color') 257 call assert_true(index(l, 'default') >= 0) 258 let l = getcompletion('dirty', 'color') 259 call assert_equal([], l) 260 261 let l = getcompletion('', 'command') 262 call assert_true(index(l, 'sleep') >= 0) 263 let l = getcompletion('awake', 'command') 264 call assert_equal([], l) 265 266 let l = getcompletion('', 'dir') 267 call assert_true(index(l, 'samples/') >= 0) 268 let l = getcompletion('NoMatch', 'dir') 269 call assert_equal([], l) 270 271 let l = getcompletion('exe', 'expression') 272 call assert_true(index(l, 'executable(') >= 0) 273 let l = getcompletion('kill', 'expression') 274 call assert_equal([], l) 275 276 let l = getcompletion('tag', 'function') 277 call assert_true(index(l, 'taglist(') >= 0) 278 let l = getcompletion('paint', 'function') 279 call assert_equal([], l) 280 281 let Flambda = {-> 'hello'} 282 let l = getcompletion('', 'function') 283 let l = filter(l, {i, v -> v =~ 'lambda'}) 284 call assert_equal([], l) 285 286 let l = getcompletion('run', 'file') 287 call assert_true(index(l, 'runtest.vim') >= 0) 288 let l = getcompletion('walk', 'file') 289 call assert_equal([], l) 290 set wildignore=*.vim 291 let l = getcompletion('run', 'file', 1) 292 call assert_true(index(l, 'runtest.vim') < 0) 293 set wildignore& 294 295 let l = getcompletion('ha', 'filetype') 296 call assert_true(index(l, 'hamster') >= 0) 297 let l = getcompletion('horse', 'filetype') 298 call assert_equal([], l) 299 300 let l = getcompletion('z', 'syntax') 301 call assert_true(index(l, 'zimbu') >= 0) 302 let l = getcompletion('emacs', 'syntax') 303 call assert_equal([], l) 304 305 let l = getcompletion('jikes', 'compiler') 306 call assert_true(index(l, 'jikes') >= 0) 307 let l = getcompletion('break', 'compiler') 308 call assert_equal([], l) 309 310 let l = getcompletion('last', 'help') 311 call assert_true(index(l, ':tablast') >= 0) 312 let l = getcompletion('giveup', 'help') 313 call assert_equal([], l) 314 315 let l = getcompletion('time', 'option') 316 call assert_true(index(l, 'timeoutlen') >= 0) 317 let l = getcompletion('space', 'option') 318 call assert_equal([], l) 319 320 let l = getcompletion('er', 'highlight') 321 call assert_true(index(l, 'ErrorMsg') >= 0) 322 let l = getcompletion('dark', 'highlight') 323 call assert_equal([], l) 324 325 let l = getcompletion('', 'messages') 326 call assert_true(index(l, 'clear') >= 0) 327 let l = getcompletion('not', 'messages') 328 call assert_equal([], l) 329 330 let l = getcompletion('', 'mapclear') 331 call assert_true(index(l, '<buffer>') >= 0) 332 let l = getcompletion('not', 'mapclear') 333 call assert_equal([], l) 334 335 let l = getcompletion('.', 'shellcmd') 336 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"')) 337 call assert_equal(-1, match(l[2:], '^\.\.\?/$')) 338 let root = has('win32') ? 'C:\\' : '/' 339 let l = getcompletion(root, 'shellcmd') 340 let expected = map(filter(glob(root . '*', 0, 1), 341 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val') 342 call assert_equal(expected, l) 343 344 if has('cscope') 345 let l = getcompletion('', 'cscope') 346 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show'] 347 call assert_equal(cmds, l) 348 " using cmdline completion must not change the result 349 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt') 350 let l = getcompletion('', 'cscope') 351 call assert_equal(cmds, l) 352 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't'] 353 let l = getcompletion('find ', 'cscope') 354 call assert_equal(keys, l) 355 endif 356 357 if has('signs') 358 sign define Testing linehl=Comment 359 let l = getcompletion('', 'sign') 360 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace'] 361 call assert_equal(cmds, l) 362 " using cmdline completion must not change the result 363 call feedkeys(":sign list \<c-d>\<c-c>", 'xt') 364 let l = getcompletion('', 'sign') 365 call assert_equal(cmds, l) 366 let l = getcompletion('list ', 'sign') 367 call assert_equal(['Testing'], l) 368 endif 369 370 " For others test if the name is recognized. 371 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user'] 372 if has('cmdline_hist') 373 call add(names, 'history') 374 endif 375 if has('gettext') 376 call add(names, 'locale') 377 endif 378 if has('profile') 379 call add(names, 'syntime') 380 endif 381 382 set tags=Xtags 383 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags') 384 385 for name in names 386 let matchcount = len(getcompletion('', name)) 387 call assert_true(matchcount >= 0, 'No matches for ' . name) 388 endfor 389 390 call delete('Xtags') 391 set tags& 392 393 call assert_fails('call getcompletion("", "burp")', 'E475:') 394endfunc 395 396func Test_shellcmd_completion() 397 let save_path = $PATH 398 399 call mkdir('Xpathdir/Xpathsubdir', 'p') 400 call writefile([''], 'Xpathdir/Xfile.exe') 401 call setfperm('Xpathdir/Xfile.exe', 'rwx------') 402 403 " Set PATH to example directory without trailing slash. 404 let $PATH = getcwd() . '/Xpathdir' 405 406 " Test for the ":!<TAB>" case. Previously, this would include subdirs of 407 " dirs in the PATH, even though they won't be executed. We check that only 408 " subdirs of the PWD and executables from the PATH are included in the 409 " suggestions. 410 let actual = getcompletion('X', 'shellcmd') 411 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"') 412 call insert(expected, 'Xfile.exe') 413 call assert_equal(expected, actual) 414 415 call delete('Xpathdir', 'rf') 416 let $PATH = save_path 417endfunc 418 419func Test_expand_star_star() 420 call mkdir('a/b', 'p') 421 call writefile(['asdfasdf'], 'a/b/fileXname') 422 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt') 423 call assert_equal('find a/b/fileXname', getreg(':')) 424 bwipe! 425 call delete('a', 'rf') 426endfunc 427 428func Test_cmdline_paste() 429 let @a = "def" 430 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx') 431 call assert_equal('"abc def ghi', @:) 432 433 new 434 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ') 435 436 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx') 437 call assert_equal('"aaa asdf bbb', @:) 438 439 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx') 440 call assert_equal('"aaa /tmp/some bbb', @:) 441 442 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx') 443 call assert_equal('"aaa '.getline(1).' bbb', @:) 444 445 set incsearch 446 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx') 447 call assert_equal('"aaa verylongword bbb', @:) 448 449 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx') 450 call assert_equal('"aaa a;b-c*d bbb', @:) 451 452 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx') 453 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:) 454 bwipe! 455 456 " Error while typing a command used to cause that it was not executed 457 " in the end. 458 new 459 try 460 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx') 461 catch /^Vim\%((\a\+)\)\=:E32/ 462 " ignore error E32 463 endtry 464 call assert_equal("Xtestfile", bufname("%")) 465 466 " Use an invalid expression for <C-\>e 467 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")') 468 469 bwipe! 470endfunc 471 472func Test_cmdline_remove_char() 473 let encoding_save = &encoding 474 475 for e in ['utf8', 'latin1'] 476 exe 'set encoding=' . e 477 478 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx') 479 call assert_equal('"abc ef', @:, e) 480 481 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx') 482 call assert_equal('"abcdef', @:) 483 484 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx') 485 call assert_equal('"abc ghi', @:, e) 486 487 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx') 488 call assert_equal('"def', @:, e) 489 endfor 490 491 let &encoding = encoding_save 492endfunc 493 494func Test_cmdline_keymap_ctrl_hat() 495 if !has('keymap') 496 return 497 endif 498 499 set keymap=esperanto 500 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx') 501 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:) 502 set keymap= 503endfunc 504 505func Test_illegal_address1() 506 new 507 2;'( 508 2;') 509 quit 510endfunc 511 512func Test_illegal_address2() 513 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim') 514 new 515 source Xtest.vim 516 " Trigger calling validate_cursor() 517 diffsp Xtest.vim 518 quit! 519 bwipe! 520 call delete('Xtest.vim') 521endfunc 522 523func Test_cmdline_complete_wildoptions() 524 help 525 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx') 526 let a = join(sort(split(@:)),' ') 527 set wildoptions=tagfile 528 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx') 529 let b = join(sort(split(@:)),' ') 530 call assert_equal(a, b) 531 bw! 532endfunc 533 534func Test_cmdline_complete_user_cmd() 535 command! -complete=color -nargs=1 Foo : 536 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx') 537 call assert_equal('"Foo blue', @:) 538 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx') 539 call assert_equal('"Foo blue', @:) 540 delcommand Foo 541endfunc 542 543func Test_cmdline_complete_user_names() 544 if has('unix') && executable('whoami') 545 let whoami = systemlist('whoami')[0] 546 let first_letter = whoami[0] 547 if len(first_letter) > 0 548 " Trying completion of :e ~x where x is the first letter of 549 " the user name should complete to at least the user name. 550 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx') 551 call assert_match('^"e \~.*\<' . whoami . '\>', @:) 552 endif 553 endif 554 if has('win32') 555 " Just in case: check that the system has an Administrator account. 556 let names = system('net user') 557 if names =~ 'Administrator' 558 " Trying completion of :e ~A should complete to Administrator. 559 " There could be other names starting with "A" before Administrator. 560 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx') 561 call assert_match('^"e \~.*Administrator', @:) 562 endif 563 endif 564endfunc 565 566func Test_cmdline_complete_bang() 567 if executable('whoami') 568 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx') 569 call assert_match('^".*\<whoami\>', @:) 570 endif 571endfunc 572 573funct Test_cmdline_complete_languages() 574 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '') 575 576 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx') 577 call assert_match('^"language .*\<ctype\>.*\<messages\>.*\<time\>', @:) 578 579 if has('unix') 580 " TODO: these tests don't work on Windows. lang appears to be 'C' 581 " but C does not appear in the completion. Why? 582 call assert_match('^"language .*\<' . lang . '\>', @:) 583 584 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx') 585 call assert_match('^"language .*\<' . lang . '\>', @:) 586 587 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx') 588 call assert_match('^"language .*\<' . lang . '\>', @:) 589 590 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx') 591 call assert_match('^"language .*\<' . lang . '\>', @:) 592 endif 593endfunc 594 595func Test_cmdline_complete_env_variable() 596 let $X_VIM_TEST_COMPLETE_ENV = 'foo' 597 598 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx') 599 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:) 600 601 unlet $X_VIM_TEST_COMPLETE_ENV 602endfunc 603 604func Test_cmdline_write_alternatefile() 605 new 606 call setline('.', ['one', 'two']) 607 f foo.txt 608 new 609 f #-A 610 call assert_equal('foo.txt-A', expand('%')) 611 f #<-B.txt 612 call assert_equal('foo-B.txt', expand('%')) 613 f %< 614 call assert_equal('foo-B', expand('%')) 615 new 616 call assert_fails('f #<', 'E95') 617 bw! 618 f foo-B.txt 619 f %<-A 620 call assert_equal('foo-B-A', expand('%')) 621 bw! 622 bw! 623endfunc 624 625" using a leading backslash here 626set cpo+=C 627 628func Test_cmdline_search_range() 629 new 630 call setline(1, ['a', 'b', 'c', 'd']) 631 /d 632 1,\/s/b/B/ 633 call assert_equal('B', getline(2)) 634 635 /a 636 $ 637 \?,4s/c/C/ 638 call assert_equal('C', getline(3)) 639 640 call setline(1, ['a', 'b', 'c', 'd']) 641 %s/c/c/ 642 1,\&s/b/B/ 643 call assert_equal('B', getline(2)) 644 645 let @/ = 'apple' 646 call assert_fails('\/print', 'E486:') 647 648 bwipe! 649endfunc 650 651" Test for the tick mark (') in an excmd range 652func Test_tick_mark_in_range() 653 " If only the tick is passed as a range and no command is specified, there 654 " should not be an error 655 call feedkeys(":'\<CR>", 'xt') 656 call assert_equal("'", getreg(':')) 657 call assert_fails("',print", 'E78:') 658endfunc 659 660" Test for using a line number followed by a search pattern as range 661func Test_lnum_and_pattern_as_range() 662 new 663 call setline(1, ['foo 1', 'foo 2', 'foo 3']) 664 let @" = '' 665 2/foo/yank 666 call assert_equal("foo 3\n", @") 667 call assert_equal(1, line('.')) 668 close! 669endfunc 670 671" Tests for getcmdline(), getcmdpos() and getcmdtype() 672func Check_cmdline(cmdtype) 673 call assert_equal('MyCmd a', getcmdline()) 674 call assert_equal(8, getcmdpos()) 675 call assert_equal(a:cmdtype, getcmdtype()) 676 return '' 677endfunc 678 679set cpo& 680 681func Test_getcmdtype() 682 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt") 683 684 let cmdtype = '' 685 debuggreedy 686 call feedkeys(":debug echo 'test'\<CR>", "t") 687 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t") 688 call feedkeys("cont\<CR>", "xt") 689 0debuggreedy 690 call assert_equal('>', cmdtype) 691 692 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt") 693 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt") 694 695 call feedkeys(":call input('Answer?')\<CR>", "t") 696 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt") 697 698 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt") 699 700 cnoremap <expr> <F6> Check_cmdline('=') 701 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt") 702 cunmap <F6> 703 704 call assert_equal('', getcmdline()) 705endfunc 706 707func Test_getcmdwintype() 708 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!') 709 call assert_equal('/', a) 710 711 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!') 712 call assert_equal('?', a) 713 714 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!') 715 call assert_equal(':', a) 716 717 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!') 718 call assert_equal(':', a) 719 720 call assert_equal('', getcmdwintype()) 721endfunc 722 723func Test_getcmdwin_autocmd() 724 let s:seq = [] 725 augroup CmdWin 726 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid()) 727 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid()) 728 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr()) 729 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr()) 730 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid()) 731 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid()) 732 733 let org_winid = win_getid() 734 let org_bufnr = bufnr() 735 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!') 736 call assert_equal(':', a) 737 call assert_equal([ 738 \ 'WinLeave ' .. org_winid, 739 \ 'WinEnter ' .. s:cmd_winid, 740 \ 'BufLeave ' .. org_bufnr, 741 \ 'BufEnter ' .. s:cmd_bufnr, 742 \ 'CmdWinEnter ' .. s:cmd_winid, 743 \ 'CmdWinLeave ' .. s:cmd_winid, 744 \ 'BufLeave ' .. s:cmd_bufnr, 745 \ 'WinLeave ' .. s:cmd_winid, 746 \ 'WinEnter ' .. org_winid, 747 \ 'BufEnter ' .. org_bufnr, 748 \ ], s:seq) 749 750 au! 751 augroup END 752endfunc 753 754func Test_verbosefile() 755 set verbosefile=Xlog 756 echomsg 'foo' 757 echomsg 'bar' 758 set verbosefile= 759 let log = readfile('Xlog') 760 call assert_match("foo\nbar", join(log, "\n")) 761 call delete('Xlog') 762endfunc 763 764func Test_verbose_option() 765 CheckScreendump 766 767 let lines =<< trim [SCRIPT] 768 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v 769 call feedkeys("\r", 't') " for the hit-enter prompt 770 set verbose=20 771 [SCRIPT] 772 call writefile(lines, 'XTest_verbose') 773 774 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12}) 775 call term_wait(buf, 100) 776 call term_sendkeys(buf, ":DoSomething\<CR>") 777 call VerifyScreenDump(buf, 'Test_verbose_option_1', {}) 778 779 " clean up 780 call StopVimInTerminal(buf) 781 call delete('XTest_verbose') 782endfunc 783 784func Test_setcmdpos() 785 func InsertTextAtPos(text, pos) 786 call assert_equal(0, setcmdpos(a:pos)) 787 return a:text 788 endfunc 789 790 " setcmdpos() with position in the middle of the command line. 791 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt') 792 call assert_equal('"1ab2', @:) 793 794 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt') 795 call assert_equal('"1b2a', @:) 796 797 " setcmdpos() with position beyond the end of the command line. 798 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt') 799 call assert_equal('"12ab', @:) 800 801 " setcmdpos() returns 1 when not editing the command line. 802 call assert_equal(1, 3->setcmdpos()) 803endfunc 804 805func Test_cmdline_overstrike() 806 let encodings = ['latin1', 'utf8'] 807 let encoding_save = &encoding 808 809 for e in encodings 810 exe 'set encoding=' . e 811 812 " Test overstrike in the middle of the command line. 813 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt') 814 call assert_equal('"0ab1cd4', @:, e) 815 816 " Test overstrike going beyond end of command line. 817 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt') 818 call assert_equal('"0ab1cdefgh', @:, e) 819 820 " Test toggling insert/overstrike a few times. 821 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt') 822 call assert_equal('"ab0cd3ef4', @:, e) 823 endfor 824 825 " Test overstrike with multi-byte characters. 826 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt') 827 call assert_equal('"テabキcdエディタ', @:, e) 828 829 let &encoding = encoding_save 830endfunc 831 832func Test_cmdwin_bug() 833 let winid = win_getid() 834 sp 835 try 836 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!') 837 catch /^Vim\%((\a\+)\)\=:E11/ 838 endtry 839 bw! 840endfunc 841 842func Test_cmdwin_restore() 843 CheckScreendump 844 845 let lines =<< trim [SCRIPT] 846 call setline(1, range(30)) 847 2split 848 [SCRIPT] 849 call writefile(lines, 'XTest_restore') 850 851 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12}) 852 call term_wait(buf, 100) 853 call term_sendkeys(buf, "q:") 854 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {}) 855 856 " normal restore 857 call term_sendkeys(buf, ":q\<CR>") 858 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {}) 859 860 " restore after setting 'lines' with one window 861 call term_sendkeys(buf, ":close\<CR>") 862 call term_sendkeys(buf, "q:") 863 call term_sendkeys(buf, ":set lines=18\<CR>") 864 call term_sendkeys(buf, ":q\<CR>") 865 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {}) 866 867 " clean up 868 call StopVimInTerminal(buf) 869 call delete('XTest_restore') 870endfunc 871 872func Test_buffers_lastused() 873 " check that buffers are sorted by time when wildmode has lastused 874 call test_settime(1550020000) " middle 875 edit bufa 876 enew 877 call test_settime(1550030000) " newest 878 edit bufb 879 enew 880 call test_settime(1550010000) " oldest 881 edit bufc 882 enew 883 call test_settime(0) 884 enew 885 886 call assert_equal(['bufa', 'bufb', 'bufc'], 887 \ getcompletion('', 'buffer')) 888 889 let save_wildmode = &wildmode 890 set wildmode=full:lastused 891 892 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>" 893 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt') 894 call assert_equal('b bufb', X) 895 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt') 896 call assert_equal('b bufa', X) 897 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt') 898 call assert_equal('b bufc', X) 899 enew 900 901 edit other 902 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt') 903 call assert_equal('b bufb', X) 904 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt') 905 call assert_equal('b bufa', X) 906 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt') 907 call assert_equal('b bufc', X) 908 enew 909 910 let &wildmode = save_wildmode 911 912 bwipeout bufa 913 bwipeout bufb 914 bwipeout bufc 915endfunc 916 917func Test_cmdwin_feedkeys() 918 " This should not generate E488 919 call feedkeys("q:\<CR>", 'x') 920endfunc 921 922" Tests for the issues fixed in 7.4.441. 923" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim 924func Test_cmdwin_cedit() 925 exe "set cedit=\<C-c>" 926 normal! : 927 call assert_equal(1, winnr('$')) 928 929 let g:cmd_wintype = '' 930 func CmdWinType() 931 let g:cmd_wintype = getcmdwintype() 932 let g:wintype = win_gettype() 933 return '' 934 endfunc 935 936 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>") 937 echo input('') 938 call assert_equal('@', g:cmd_wintype) 939 call assert_equal('command', g:wintype) 940 941 set cedit&vim 942 delfunc CmdWinType 943endfunc 944 945" Test for CmdwinEnter autocmd 946func Test_cmdwin_autocmd() 947 augroup CmdWin 948 au! 949 autocmd CmdwinEnter * startinsert 950 augroup END 951 952 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:') 953 call assert_equal('xyz', @:) 954 955 augroup CmdWin 956 au! 957 augroup END 958 augroup! CmdWin 959endfunc 960 961func Test_cmdlineclear_tabenter() 962 CheckScreendump 963 964 let lines =<< trim [SCRIPT] 965 call setline(1, range(30)) 966 [SCRIPT] 967 968 call writefile(lines, 'XtestCmdlineClearTabenter') 969 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10}) 970 call term_wait(buf, 50) 971 " in one tab make the command line higher with CTRL-W - 972 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt") 973 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {}) 974 975 call StopVimInTerminal(buf) 976 call delete('XtestCmdlineClearTabenter') 977endfunc 978 979" Test for failure in expanding special keywords in cmdline 980func Test_cmdline_expand_special() 981 %bwipe! 982 call assert_fails('e #', 'E499:') 983 call assert_fails('e <afile>', 'E495:') 984 call assert_fails('e <abuf>', 'E496:') 985 call assert_fails('e <amatch>', 'E497:') 986endfunc 987 988func Test_cmdwin_jump_to_win() 989 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:') 990 new 991 set modified 992 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', 'E162:') 993 close! 994 call feedkeys("q/:close\<CR>", "xt") 995 call assert_equal(1, winnr('$')) 996 call feedkeys("q/:exit\<CR>", "xt") 997 call assert_equal(1, winnr('$')) 998 999 " opening command window twice should fail 1000 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")') 1001 call assert_equal(1, winnr('$')) 1002endfunc 1003 1004" Test for backtick expression in the command line 1005func Test_cmd_backtick() 1006 %argd 1007 argadd `=['a', 'b', 'c']` 1008 call assert_equal(['a', 'b', 'c'], argv()) 1009 %argd 1010endfunc 1011 1012" Test for the :! command 1013func Test_cmd_bang() 1014 if !has('unix') 1015 return 1016 endif 1017 1018 let lines =<< trim [SCRIPT] 1019 " Test for no previous command 1020 call assert_fails('!!', 'E34:') 1021 set nomore 1022 " Test for cmdline expansion with :! 1023 call setline(1, 'foo!') 1024 silent !echo <cWORD> > Xfile.out 1025 call assert_equal(['foo!'], readfile('Xfile.out')) 1026 " Test for using previous command 1027 silent !echo \! ! 1028 call assert_equal(['! echo foo!'], readfile('Xfile.out')) 1029 call writefile(v:errors, 'Xresult') 1030 call delete('Xfile.out') 1031 qall! 1032 [SCRIPT] 1033 call writefile(lines, 'Xscript') 1034 if RunVim([], [], '--clean -S Xscript') 1035 call assert_equal([], readfile('Xresult')) 1036 endif 1037 call delete('Xscript') 1038 call delete('Xresult') 1039endfunc 1040 1041" Test for using ~ for home directory in cmdline completion matches 1042func Test_cmdline_expand_home() 1043 call mkdir('Xdir') 1044 call writefile([], 'Xdir/Xfile1') 1045 call writefile([], 'Xdir/Xfile2') 1046 cd Xdir 1047 let save_HOME = $HOME 1048 let $HOME = getcwd() 1049 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt') 1050 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:) 1051 let $HOME = save_HOME 1052 cd .. 1053 call delete('Xdir', 'rf') 1054endfunc 1055 1056" vim: shiftwidth=2 sts=2 expandtab 1057