robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Tests for various Ex commands. 2 3source check.vim 4source shared.vim 5source term_util.vim 6source screendump.vim 7 8func Test_ex_delete () 9new 10 11call setline ( 1 , [ 'a' , 'b' , 'c' ]) 122 13" :dl is :delete with the "l" flag, not :dlist 14 .dl 15call assert_equal ([ 'a' , 'c' ], getline ( 1 , 2 )) 16 %delete _ 17 18" :delete # used to clobber "0 19call setreg ( '#' , '' ) 20call setreg ( '0' , '' ) 21call setline ( 1 , [ 'a' , 'b' , 'c' ]) 22call assert_fails ( "1delete #" , 'E488:' ) 23call assert_equal ([ 'a' , 'b' , 'c' ], getline ( 1 , '$' )) 24call assert_equal ( '' , getreg ( '#' )) 25call assert_equal ( '' , getreg ( '0' )) 26 27bw ! 28endfunc 29 30func Test_range_error () 31call assert_fails ( ':.echo 1' , 'E481:' ) 32call assert_fails ( ':$echo 1' , 'E481:' ) 33call assert_fails ( ':1,2echo 1' , 'E481:' ) 34call assert_fails ( ':+1echo 1' , 'E481:' ) 35call assert_fails ( ':/1/echo 1' , 'E481:' ) 36call assert_fails ( ':\/echo 1' , 'E481:' ) 37normal vv 38call assert_fails ( ":'<,'>echo 1" , 'E481:' ) 39call assert_fails ( ":\\xcenter" , 'E10:' ) 40endfunc 41 42func Test_buffers_lastused () 43edit bufc " oldest 44 45sleep 1200m 46edit bufa " middle 47 48sleep 1200m 49edit bufb " newest 50 51enew 52 53let ls = split ( execute ( 'buffers t' , 'silent!' ), '\n' ) 54let bufs = [] 55for line in ls 56let bufs += [ split ( line , '"\s*' )[ 1 : 2 ]] 57endfor 58 59let names = [] 60for buf in bufs 61if buf [ 0 ] !=# '[No Name]' 62let names += [ buf [ 0 ]] 63endif 64endfor 65 66call assert_equal ([ 'bufb' , 'bufa' , 'bufc' ], names ) 67call assert_match ( '[0-2] seconds\= ago' , bufs [ 1 ][ 1 ]) 68 69bwipeout bufa 70bwipeout bufb 71bwipeout bufc 72endfunc 73 74" Test for the :copy command 75func Test_copy () 76new 77 78call setline ( 1 , [ 'L1' , 'L2' , 'L3' , 'L4' ]) 79" copy lines in a range to inside the range 801 , 3 copy 2 81call assert_equal ([ 'L1' , 'L2' , 'L1' , 'L2' , 'L3' , 'L3' , 'L4' ], getline ( 1 , 7 )) 82 83" Specifying a count before using : to run an ex-command 84exe "normal! gg4:yank\<CR>" 85call assert_equal ( "L1\nL2\nL1\nL2\n" , @") 86 87bw ! 88endfunc 89 90" Test for the :file command 91func Test_file_cmd () 92call assert_fails ( '3file' , 'E474:' ) 93call assert_fails ( '0,0file' , 'E474:' ) 94call assert_fails ( '0file abc' , 'E474:' ) 95if ! has ( 'win32' ) 96" Change the name of the buffer to the same name 97new Xfile1 98file Xfile1 99call assert_equal ( 'Xfile1' , @%) 100call assert_equal ( 'Xfile1' , @#) 101bw ! 102endif 103endfunc 104 105" Test for the :drop command 106func Test_drop_cmd () 107call writefile ([ 'L1' , 'L2' ], 'Xdropfile' , 'D' ) 108" Test for reusing the current buffer 109enew |only 110let expected_nr = bufnr () 111drop Xdropfile 112call assert_equal ( expected_nr , bufnr ()) 113call assert_equal ( 'L2' , getline ( 2 )) 114" Test for switching to an existing window 115below new 116drop Xdropfile 117call assert_equal ( 1 , winnr ()) 118" Test for splitting the current window (set nohidden) 119enew |only 120set modified 121drop Xdropfile 122call assert_equal ( 2 , winnr ( '$' )) 123" Not splitting the current window even if modified (set hidden) 124set hidden 125enew |only 126set modified 127drop Xdropfile 128call assert_equal ( 1 , winnr ( '$' )) 129" Check for setting the argument list 130call assert_equal ([ 'Xdropfile' ], argv ()) 131enew |only ! |bw ! Xdropfile 132endfunc 133 134" Test for the :append command 135func Test_append_cmd () 136new 137call setline ( 1 , [ ' L1' ]) 138call feedkeys ( ":append\<CR> L2\<CR> L3\<CR>.\<CR>" , 'xt' ) 139call assert_equal ([ ' L1' , ' L2' , ' L3' ], getline ( 1 , '$' )) 140 %delete _ 141" append after a specific line 142call setline ( 1 , [ ' L1' , ' L2' , ' L3' ]) 143call feedkeys ( ":2append\<CR> L4\<CR> L5\<CR>.\<CR>" , 'xt' ) 144call assert_equal ([ ' L1' , ' L2' , ' L4' , ' L5' , ' L3' ], getline ( 1 , '$' )) 145 %delete _ 146" append with toggling 'autoindent' 147call setline ( 1 , [ ' L1' ]) 148call feedkeys ( ":append!\<CR> L2\<CR> L3\<CR>.\<CR>" , 'xt' ) 149call assert_equal ([ ' L1' , ' L2' , ' L3' ], getline ( 1 , '$' )) 150call assert_false ( & autoindent ) 151 %delete _ 152" append with 'autoindent' set and toggling 'autoindent' 153set autoindent 154call setline ( 1 , [ ' L1' ]) 155call feedkeys ( ":append!\<CR> L2\<CR> L3\<CR>.\<CR>" , 'xt' ) 156call assert_equal ([ ' L1' , ' L2' , ' L3' ], getline ( 1 , '$' )) 157call assert_true ( & autoindent ) 158set autoindent & 159bw ! 160endfunc 161 162func Test_append_cmd_empty_buf () 163CheckRunVimInTerminal 164let lines =<< trim END 165func Timer(timer) 166append 167aaaaa 168bbbbb 169. 170endfunc 171call timer_start(10, 'Timer') 172END 173call writefile ( lines , 'Xtest_append_cmd_empty_buf' , 'D' ) 174let buf = RunVimInTerminal ( '-S Xtest_append_cmd_empty_buf' , { 'rows' : 6 }) 175call WaitForAssert ({ ->assert_equal ( 'bbbbb' , term_getline ( buf , 2 ))}) 176call WaitForAssert ({ ->assert_equal ( 'aaaaa' , term_getline ( buf , 1 ))}) 177 178" clean up 179call StopVimInTerminal ( buf ) 180endfunc 181 182" Test for :append, :insert and :change in a block that is not executed: the 183" lines up to the "." are skipped, not executed as commands. 184func Test_append_cmd_skipped () 185let lines =<< trim END 186if 0 187append 188endif 189. 190endif 191let g:skipped = 'append' 192if 0 193insert 194else 195. 196endif 197let g:skipped ..= ' insert' 198if 0 1991change 200endif 201. 202endif 203let g:skipped ..= ' change' 204while 0 205append 206endwhile 207. 208endwhile 209let g:skipped ..= ' while' 210END 211call writefile ( lines , 'Xappend_skipped' , 'D' ) 212new 213call setline ( 1 , 'text' ) 214source Xappend_skipped 215call assert_equal ( 'append insert change while' , g: skipped ) 216call assert_equal ([ 'text' ], getline ( 1 , '$' )) 217 218" In Vim9 script :append is not allowed, but it is not executed here. 219let lines =<< trim END 220vim9script 221if false 222append 223enddef 224. 225endif 226g:skipped = 'vim9' 227END 228call writefile ( lines , 'Xappend_skipped_vim9' , 'D' ) 229"source Xappend_skipped_vim9 230"call assert_equal('vim9', g:skipped) 231call assert_equal ([ 'text' ], getline ( 1 , '$' )) 232 233unlet g: skipped 234bwipe ! 235endfunc 236 237" Test for the :insert command 238func Test_insert_cmd () 239new 240call setline ( 1 , [ ' L1' ]) 241call feedkeys ( ":insert\<CR> L2\<CR> L3\<CR>.\<CR>" , 'xt' ) 242call assert_equal ([ ' L2' , ' L3' , ' L1' ], getline ( 1 , '$' )) 243 %delete _ 244" insert before a specific line 245call setline ( 1 , [ ' L1' , ' L2' , ' L3' ]) 246call feedkeys ( ":2insert\<CR> L4\<CR> L5\<CR>.\<CR>" , 'xt' ) 247call assert_equal ([ ' L1' , ' L4' , ' L5' , ' L2' , ' L3' ], getline ( 1 , '$' )) 248 %delete _ 249" insert with toggling 'autoindent' 250call setline ( 1 , [ ' L1' ]) 251call feedkeys ( ":insert!\<CR> L2\<CR> L3\<CR>.\<CR>" , 'xt' ) 252call assert_equal ([ ' L2' , ' L3' , ' L1' ], getline ( 1 , '$' )) 253call assert_false ( & autoindent ) 254 %delete _ 255" insert with 'autoindent' set and toggling 'autoindent' 256set autoindent 257call setline ( 1 , [ ' L1' ]) 258call feedkeys ( ":insert!\<CR> L2\<CR> L3\<CR>.\<CR>" , 'xt' ) 259call assert_equal ([ ' L2' , ' L3' , ' L1' ], getline ( 1 , '$' )) 260call assert_true ( & autoindent ) 261set autoindent & 262bw ! 263endfunc 264 265func Test_insert_cmd_empty_buf () 266CheckRunVimInTerminal 267let lines =<< trim END 268func Timer(timer) 269insert 270aaaaa 271bbbbb 272. 273endfunc 274call timer_start(10, 'Timer') 275END 276call writefile ( lines , 'Xtest_insert_cmd_empty_buf' , 'D' ) 277let buf = RunVimInTerminal ( '-S Xtest_insert_cmd_empty_buf' , { 'rows' : 6 }) 278call WaitForAssert ({ ->assert_equal ( 'bbbbb' , term_getline ( buf , 2 ))}) 279call WaitForAssert ({ ->assert_equal ( 'aaaaa' , term_getline ( buf , 1 ))}) 280 281" clean up 282call StopVimInTerminal ( buf ) 283endfunc 284 285" Test for the :change command 286func Test_change_cmd () 287new 288call setline ( 1 , [ ' L1' , 'L2' , 'L3' ]) 289call feedkeys ( ":change\<CR> L4\<CR> L5\<CR>.\<CR>" , 'xt' ) 290call assert_equal ([ ' L4' , ' L5' , 'L2' , 'L3' ], getline ( 1 , '$' )) 291 %delete _ 292" change a specific line 293call setline ( 1 , [ ' L1' , ' L2' , ' L3' ]) 294call feedkeys ( ":2change\<CR> L4\<CR> L5\<CR>.\<CR>" , 'xt' ) 295call assert_equal ([ ' L1' , ' L4' , ' L5' , ' L3' ], getline ( 1 , '$' )) 296 %delete _ 297" change with toggling 'autoindent' 298call setline ( 1 , [ ' L1' , 'L2' , 'L3' ]) 299call feedkeys ( ":change!\<CR> L4\<CR> L5\<CR>.\<CR>" , 'xt' ) 300call assert_equal ([ ' L4' , ' L5' , 'L2' , 'L3' ], getline ( 1 , '$' )) 301call assert_false ( & autoindent ) 302 %delete _ 303" change with 'autoindent' set and toggling 'autoindent' 304set autoindent 305call setline ( 1 , [ ' L1' , 'L2' , 'L3' ]) 306call feedkeys ( ":change!\<CR> L4\<CR> L5\<CR>.\<CR>" , 'xt' ) 307call assert_equal ([ ' L4' , ' L5' , 'L2' , 'L3' ], getline ( 1 , '$' )) 308call assert_true ( & autoindent ) 309set autoindent & 310bw ! 311endfunc 312 313" Test for the :language command 314func Test_language_cmd () 315CheckFeature multi_lang 316 317" OpenBSD allows nearly arbitrary locale names, since it largely ignores them 318" (see setlocale(3)). One useful exception for this test is that in doesn't 319" allow names containing dots unless they end in '.UTF-8'. 320" 321" Windows also allows nonsensical locale names, though it seems to reject 322" names with multiple underscores (possibly expecting 'language_region', but 323" not 'language_region_additional'). 324call assert_fails ( 'language ctype non_existing_lang.bad' , 'E197:' ) 325call assert_fails ( 'language time non_existing_lang.bad' , 'E197:' ) 326endfunc 327 328" Test for the :confirm command dialog 329func Test_confirm_cmd () 330CheckNotGui 331CheckRunVimInTerminal 332 333call writefile ([ 'foo1' ], 'Xfoo' ) 334call writefile ([ 'bar1' ], 'Xbar' ) 335 336" Test for saving all the modified buffers 337let lines =<< trim END 338set nomore 339new Xfoo 340call setline(1, 'foo2') 341new Xbar 342call setline(1, 'bar2') 343wincmd b 344END 345call writefile ( lines , 'Xscript' ) 346let buf = RunVimInTerminal ( '-S Xscript' , { 'rows' : 20 }) 347call term_sendkeys ( buf , ":confirm qall\n" ) 348call WaitForAssert ({ ->assert_match ( '\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ' , term_getline ( buf , 20 ))}, 1000 ) 349call term_sendkeys ( buf , "A" ) 350call StopVimInTerminal ( buf ) 351 352call assert_equal ([ 'foo2' ], readfile ( 'Xfoo' )) 353call assert_equal ([ 'bar2' ], readfile ( 'Xbar' )) 354 355" Test for discarding all the changes to modified buffers 356let lines =<< trim END 357set nomore 358new Xfoo 359call setline(1, 'foo3') 360new Xbar 361call setline(1, 'bar3') 362wincmd b 363END 364call writefile ( lines , 'Xscript' ) 365let buf = RunVimInTerminal ( '-S Xscript' , { 'rows' : 20 }) 366call term_sendkeys ( buf , ":confirm qall\n" ) 367call WaitForAssert ({ ->assert_match ( '\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ' , term_getline ( buf , 20 ))}, 1000 ) 368call term_sendkeys ( buf , "D" ) 369call StopVimInTerminal ( buf ) 370 371call assert_equal ([ 'foo2' ], readfile ( 'Xfoo' )) 372call assert_equal ([ 'bar2' ], readfile ( 'Xbar' )) 373 374" Test for saving and discarding changes to some buffers 375let lines =<< trim END 376set nomore 377new Xfoo 378call setline(1, 'foo4') 379new Xbar 380call setline(1, 'bar4') 381wincmd b 382END 383call writefile ( lines , 'Xscript' ) 384let buf = RunVimInTerminal ( '-S Xscript' , { 'rows' : 20 }) 385call term_sendkeys ( buf , ":confirm qall\n" ) 386call WaitForAssert ({ ->assert_match ( '\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ' , term_getline ( buf , 20 ))}, 1000 ) 387call term_sendkeys ( buf , "N" ) 388call WaitForAssert ({ ->assert_match ( '\[Y\]es, (N)o, (C)ancel: ' , term_getline ( buf , 20 ))}, 1000 ) 389call term_sendkeys ( buf , "Y" ) 390call StopVimInTerminal ( buf ) 391 392call assert_equal ([ 'foo4' ], readfile ( 'Xfoo' )) 393call assert_equal ([ 'bar2' ], readfile ( 'Xbar' )) 394 395call delete ( 'Xscript' ) 396call delete ( 'Xfoo' ) 397call delete ( 'Xbar' ) 398endfunc 399 400func Test_confirm_cmd_cancel () 401CheckNotGui 402CheckRunVimInTerminal 403 404" Test for closing a window with a modified buffer 405let lines =<< trim END 406set nomore 407new 408call setline(1, 'abc') 409END 410call writefile ( lines , 'Xscript' ) 411let buf = RunVimInTerminal ( '-S Xscript' , { 'rows' : 20 }) 412call term_sendkeys ( buf , ":confirm close\n" ) 413call WaitForAssert ({ ->assert_match ( '^\[Y\]es, (N)o, (C)ancel: *$' , 414 \term_getline ( buf , 20 ))}, 1000 ) 415call term_sendkeys ( buf , "C" ) 416call WaitForAssert ({ ->assert_equal ( '' , term_getline ( buf , 20 ))}, 1000 ) 417call term_sendkeys ( buf , ":confirm close\n" ) 418call WaitForAssert ({ ->assert_match ( '^\[Y\]es, (N)o, (C)ancel: *$' , 419 \term_getline ( buf , 20 ))}, 1000 ) 420call term_sendkeys ( buf , "N" ) 421call WaitForAssert ({ ->assert_match ( '^ *0,0-1 All$' , 422 \term_getline ( buf , 20 ))}, 1000 ) 423call StopVimInTerminal ( buf ) 424call delete ( 'Xscript' ) 425endfunc 426 427" The ":confirm" prompt was sometimes used with the terminal in cooked mode. 428" This test verifies that a "\<CR>" character is NOT required to respond to a 429" prompt from the ":conf q" and ":conf wq" commands. 430func Test_confirm_q_wq () 431CheckNotGui 432CheckRunVimInTerminal 433 434call writefile ([ 'foo' ], 'Xfoo' ) 435 436let lines =<< trim END 437set hidden nomore 438call setline(1, 'abc') 439edit Xfoo 440END 441call writefile ( lines , 'Xscript' ) 442let buf = RunVimInTerminal ( '-S Xscript' , { 'rows' : 20 }) 443call term_sendkeys ( buf , ":confirm q\n" ) 444call WaitForAssert ({ ->assert_match ( '^\[Y\]es, (N)o, (C)ancel: *$' , 445 \term_getline ( buf , 20 ))}, 1000 ) 446call term_sendkeys ( buf , 'C' ) 447call WaitForAssert ({ ->assert_notmatch ( '^\[Y\]es, (N)o, (C)ancel: C*$' , 448 \term_getline ( buf , 20 ))}, 1000 ) 449 450call term_sendkeys ( buf , ":edit Xfoo\n" ) 451call term_sendkeys ( buf , ":confirm wq\n" ) 452call WaitForAssert ({ ->assert_match ( '^\[Y\]es, (N)o, (C)ancel: *$' , 453 \term_getline ( buf , 20 ))}, 1000 ) 454call term_sendkeys ( buf , 'C' ) 455call WaitForAssert ({ ->assert_notmatch ( '^\[Y\]es, (N)o, (C)ancel: C*$' , 456 \term_getline ( buf , 20 ))}, 1000 ) 457call StopVimInTerminal ( buf ) 458 459call delete ( 'Xscript' ) 460call delete ( 'Xfoo' ) 461endfunc 462 463func Test_confirm_write_ro () 464CheckNotGui 465CheckRunVimInTerminal 466 467call writefile ([ 'foo' ], 'Xconfirm_write_ro' ) 468let lines =<< trim END 469set nobackup ff=unix cmdheight=2 470edit Xconfirm_write_ro 471norm Abar 472END 473call writefile ( lines , 'Xscript' ) 474let buf = RunVimInTerminal ( '-S Xscript' , { 'rows' : 20 }) 475 476" Try to write with 'ro' option. 477call term_sendkeys ( buf , ":set ro | confirm w\n" ) 478call WaitForAssert ({ ->assert_match ( "^'readonly' option is set for \"Xconfirm_write_ro\"\. *$" , 479 \term_getline ( buf , 18 ))}, 1000 ) 480call WaitForAssert ({ ->assert_match ( '^Do you wish to write anyway? *$' , 481 \term_getline ( buf , 19 ))}, 1000 ) 482call WaitForAssert ({ ->assert_match ( '^(Y)es, \[N\]o: *$' , term_getline ( buf , 20 ))}, 1000 ) 483call term_sendkeys ( buf , 'N' ) 484call WaitForAssert ({ ->assert_match ( '^ *$' , term_getline ( buf , 19 ))}, 1000 ) 485call WaitForAssert ({ ->assert_match ( '.* All$' , term_getline ( buf , 20 ))}, 1000 ) 486call assert_equal ([ 'foo' ], readfile ( 'Xconfirm_write_ro' )) 487 488call term_sendkeys ( buf , ":confirm w\n" ) 489call WaitForAssert ({ ->assert_match ( "^'readonly' option is set for \"Xconfirm_write_ro\"\. *$" , 490 \term_getline ( buf , 18 ))}, 1000 ) 491call WaitForAssert ({ ->assert_match ( '^Do you wish to write anyway? *$' , 492 \term_getline ( buf , 19 ))}, 1000 ) 493call WaitForAssert ({ ->assert_match ( '^(Y)es, \[N\]o: *$' , term_getline ( buf , 20 ))}, 1000 ) 494call term_sendkeys ( buf , 'Y' ) 495call WaitForAssert ({ ->assert_match ( '^"Xconfirm_write_ro" 1L, 7B written$' , 496 \term_getline ( buf , 19 ))}, 1000 ) 497call assert_equal ([ 'foobar' ], readfile ( 'Xconfirm_write_ro' )) 498 499" Try to write with read-only file permissions. 500call setfperm ( 'Xconfirm_write_ro' , 'r--r--r--' ) 501call term_sendkeys ( buf , ":set noro | undo | confirm w\n" ) 502call WaitForAssert ({ ->assert_match ( "^File permissions of \"Xconfirm_write_ro\" are read-only\. *$" , 503 \term_getline ( buf , 17 ))}, 1000 ) 504call WaitForAssert ({ ->assert_match ( '^It may still be possible to write it\. *$' , 505 \term_getline ( buf , 18 ))}, 1000 ) 506call WaitForAssert ({ ->assert_match ( '^Do you wish to try? *$' , term_getline ( buf , 19 ))}, 1000 ) 507call WaitForAssert ({ ->assert_match ( '^(Y)es, \[N\]o: *$' , term_getline ( buf , 20 ))}, 1000 ) 508call term_sendkeys ( buf , 'Y' ) 509call WaitForAssert ({ ->assert_match ( '^"Xconfirm_write_ro" 1L, 4B written$' , 510 \term_getline ( buf , 19 ))}, 1000 ) 511call assert_equal ([ 'foo' ], readfile ( 'Xconfirm_write_ro' )) 512 513call StopVimInTerminal ( buf ) 514call delete ( 'Xscript' ) 515call delete ( 'Xconfirm_write_ro' ) 516endfunc 517 518func Test_confirm_write_partial_file () 519CheckNotGui 520CheckRunVimInTerminal 521 522call writefile ([ 'a' , 'b' , 'c' , 'd' ], 'Xwrite_partial' ) 523call writefile ([ 'set nobackup ff=unix cmdheight=2' , 524 \'edit Xwrite_partial' ], 'Xscript' ) 525let buf = RunVimInTerminal ( '-S Xscript' , { 'rows' : 20 }) 526 527call term_sendkeys ( buf , ":confirm 2,3w\n" ) 528call WaitForAssert ({ ->assert_match ( '^Write partial file? *$' , 529 \term_getline ( buf , 19 ))}, 1000 ) 530call WaitForAssert ({ ->assert_match ( '^(Y)es, \[N\]o: *$' , 531 \term_getline ( buf , 20 ))}, 1000 ) 532call term_sendkeys ( buf , 'N' ) 533call WaitForAssert ({ ->assert_match ( '.* All$' , term_getline ( buf , 20 ))}, 1000 ) 534call assert_equal ([ 'a' , 'b' , 'c' , 'd' ], readfile ( 'Xwrite_partial' )) 535call delete ( 'Xwrite_partial' ) 536 537call term_sendkeys ( buf , ":confirm 2,3w\n" ) 538call WaitForAssert ({ ->assert_match ( '^Write partial file? *$' , 539 \term_getline ( buf , 19 ))}, 1000 ) 540call WaitForAssert ({ ->assert_match ( '^(Y)es, \[N\]o: *$' , 541 \term_getline ( buf , 20 ))}, 1000 ) 542call term_sendkeys ( buf , 'Y' ) 543call WaitForAssert ({ ->assert_match ( '^"Xwrite_partial" \[New\] 2L, 4B written *$' , 544 \term_getline ( buf , 19 ))}, 1000 ) 545call WaitForAssert ({ ->assert_match ( '^Press ENTER or type command to continue *$' , 546 \term_getline ( buf , 20 ))}, 1000 ) 547call assert_equal ([ 'b' , 'c' ], readfile ( 'Xwrite_partial' )) 548 549call StopVimInTerminal ( buf ) 550call delete ( 'Xwrite_partial' ) 551call delete ( 'Xscript' ) 552endfunc 553 554" Test for the :print command 555func Test_print_cmd () 556call assert_fails ( 'print' , 'E749:' ) 557endfunc 558 559" Test for the :winsize command 560func Test_winsize_cmd () 561call assert_fails ( 'winsize 1' , 'E465:' ) 562call assert_fails ( 'winsize 1 x' , 'E465:' ) 563call assert_fails ( 'win_getid(1)' , 'E475: Invalid argument: _getid(1)' ) 564" Actually changing the window size would be flaky. 565endfunc 566 567" Test for the :redir command 568" NOTE: if you run tests as root this will fail. Don't run tests as root! 569func Test_redir_cmd () 570call assert_fails ( 'redir @@' , 'E475:' ) 571call assert_fails ( 'redir abc' , 'E475:' ) 572call assert_fails ( 'redir => 1abc' , 'E474:' ) 573call assert_fails ( 'redir => a b' , 'E488:' ) 574call assert_fails ( 'redir => abc[1]' , 'E121:' ) 575let b = 0 zFF 576call assert_fails ( 'redir =>> b' , 'E734:' ) 577unlet b 578 579if has ( 'unix' ) 580" Redirecting to a directory name 581call mkdir ( 'Xdir' ) 582call assert_fails ( 'redir > Xdir' , 'E17:' ) 583call delete ( 'Xdir' , 'd' ) 584endif 585 586" Test for redirecting to a register 587redir @q> |echon 'clean ' |redir END 588redir @q>> |echon 'water' |redir END 589call assert_equal ( 'clean water' , @q) 590 591" Test for redirecting to a variable 592redir => color |echon 'blue ' |redir END 593redir =>> color |echon 'sky' |redir END 594call assert_equal ( 'blue sky' , color ) 595endfunc 596 597func Test_redir_cmd_readonly () 598CheckNotRoot 599 600" Redirecting to a read-only file 601call writefile ([], 'Xfile' ) 602call setfperm ( 'Xfile' , 'r--r--r--' ) 603call assert_fails ( 'redir! > Xfile' , 'E190:' ) 604call delete ( 'Xfile' ) 605endfunc 606 607" Test for the :filetype command 608func Test_filetype_cmd () 609call assert_fails ( 'filetype abc' , 'E475:' ) 610endfunc 611 612" Test for the :mode command 613func Test_mode_cmd () 614call assert_fails ( 'mode abc' , 'E359:' ) 615endfunc 616 617" Test for the :sleep command 618func Test_sleep_cmd () 619call assert_fails ( 'sleep x' , 'E475:' ) 620endfunc 621 622" Test for the :read command 623func Test_read_cmd () 624call writefile ([ 'one' ], 'Xcmdfile' , 'D' ) 625new 626call assert_fails ( 'read' , 'E32:' ) 627edit Xcmdfile 628read 629call assert_equal ([ 'one' , 'one' ], getline ( 1 , '$' )) 630bw ! 631new 632read Xcmdfile 633call assert_equal ([ '' , 'one' ], getline ( 1 , '$' )) 634" Nvim: Ex mode is a cmdwin wrapper; cannot run inside feedkeys('x'), and 635" ":read" no longer deletes the empty first line in Ex mode. 636"call deletebufline('', 1, '$') 637"call feedkeys("Qr Xcmdfile\<CR>visual\<CR>", 'xt') 638"call assert_equal(['one'], getline(1, '$')) 639bw ! 640endfunc 641 642" Test for running Ex commands when text is locked. 643" <C-\>e in the command line is used to lock the text 644func Test_run_excmd_with_text_locked () 645" :quit 646let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>" 647call assert_fails ( "call feedkeys(cmd, 'xt')" , 'E565:' ) 648 649" :qall 650let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>" 651call assert_fails ( "call feedkeys(cmd, 'xt')" , 'E565:' ) 652 653" :exit 654let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>" 655call assert_fails ( "call feedkeys(cmd, 'xt')" , 'E565:' ) 656 657" :close - should be ignored 658new 659let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>" 660call assert_equal ( 2 , winnr ( '$' )) 661close 662 663call assert_fails ( "call feedkeys(\":\<C-R>=execute('bnext')\<CR>\", 'xt')" , 'E565:' ) 664 665" :tabfirst 666tabnew 667call assert_fails ( "call feedkeys(\":\<C-R>=execute('tabfirst')\<CR>\", 'xt')" , 'E565:' ) 668tabclose 669endfunc 670 671" Test for the :verbose command 672func Test_verbose_cmd () 673set verbose = 3 674call assert_match ( ' verbose=1\n\s*Last set from ' , execute ( 'verbose set vbs' ), "\n" ) 675call assert_equal ([ ' verbose=0' ], split ( execute ( '0verbose set vbs' ), "\n" )) 676set verbose = 0 677call assert_match ( ' verbose=4\n\s*Last set from .*\n verbose=0' , 678 \execute ( "4verbose set verbose | set verbose" )) 679endfunc 680 681" Test for the :delete command and the related abbreviated commands 682func Test_excmd_delete () 683new 684call setline ( 1 , [ 'foo' , "\tbar" ]) 685call assert_equal ([ '^Ibar$' ], split ( execute ( 'dl' ), "\n" )) 686call setline ( 1 , [ 'foo' , "\tbar" ]) 687call assert_equal ([ '^Ibar$' ], split ( execute ( 'dell' ), "\n" )) 688call setline ( 1 , [ 'foo' , "\tbar" ]) 689call assert_equal ([ '^Ibar$' ], split ( execute ( 'delel' ), "\n" )) 690call setline ( 1 , [ 'foo' , "\tbar" ]) 691call assert_equal ([ '^Ibar$' ], split ( execute ( 'deletl' ), "\n" )) 692call setline ( 1 , [ 'foo' , "\tbar" ]) 693call assert_equal ([ '^Ibar$' ], split ( execute ( 'deletel' ), "\n" )) 694call setline ( 1 , [ 'foo' , "\tbar" ]) 695call assert_equal ([ ' bar' ], split ( execute ( 'dp' ), "\n" )) 696call setline ( 1 , [ 'foo' , "\tbar" ]) 697call assert_equal ([ ' bar' ], split ( execute ( 'dep' ), "\n" )) 698call setline ( 1 , [ 'foo' , "\tbar" ]) 699call assert_equal ([ ' bar' ], split ( execute ( 'delp' ), "\n" )) 700call setline ( 1 , [ 'foo' , "\tbar" ]) 701call assert_equal ([ ' bar' ], split ( execute ( 'delep' ), "\n" )) 702call setline ( 1 , [ 'foo' , "\tbar" ]) 703call assert_equal ([ ' bar' ], split ( execute ( 'deletp' ), "\n" )) 704call setline ( 1 , [ 'foo' , "\tbar" ]) 705call assert_equal ([ ' bar' ], split ( execute ( 'deletep' ), "\n" )) 706bw ! 707endfunc 708 709" Test for commands that are blocked in a sandbox 710func Sandbox_tests () 711call assert_fails ( "call histadd(':', 'ls')" , 'E48:' ) 712call assert_fails ( "call mkdir('Xdir')" , 'E48:' ) 713call assert_fails ( "call rename('a', 'b')" , 'E48:' ) 714call assert_fails ( "call setbufvar(1, 'myvar', 1)" , 'E48:' ) 715call assert_fails ( "call settabvar(1, 'myvar', 1)" , 'E48:' ) 716call assert_fails ( "call settabwinvar(1, 1, 'myvar', 1)" , 'E48:' ) 717call assert_fails ( "call setwinvar(1, 'myvar', 1)" , 'E48:' ) 718call assert_fails ( "call timer_start(100, '')" , 'E48:' ) 719if has ( 'channel' ) 720call assert_fails ( "call prompt_setcallback(1, '')" , 'E48:' ) 721call assert_fails ( "call prompt_setinterrupt(1, '')" , 'E48:' ) 722call assert_fails ( "call prompt_setprompt(1, '')" , 'E48:' ) 723endif 724call assert_fails ( "let $TESTVAR=1" , 'E48:' ) 725call assert_fails ( "call feedkeys('ivim')" , 'E48:' ) 726call assert_fails ( "source! Xfile" , 'E48:' ) 727call assert_fails ( "call delete('Xfile')" , 'E48:' ) 728call assert_fails ( "call writefile([], 'Xfile')" , 'E48:' ) 729call assert_fails ( '!ls' , 'E48:' ) 730" call assert_fails('shell', 'E48:') 731call assert_fails ( 'stop' , 'E48:' ) 732call assert_fails ( 'exe "normal \<C-Z>"' , 'E48:' ) 733" set insertmode 734" call assert_fails('call feedkeys("\<C-Z>", "xt")', 'E48:') 735" set insertmode& 736call assert_fails ( 'suspend' , 'E48:' ) 737call assert_fails ( 'call system("ls")' , 'E48:' ) 738call assert_fails ( 'call systemlist("ls")' , 'E48:' ) 739if has ( 'clientserver' ) 740call assert_fails ( 'let s=remote_expr("gvim", "2+2")' , 'E48:' ) 741if ! has ( 'win32' ) 742" remote_foreground() doesn't throw an error message on MS-Windows 743call assert_fails ( 'call remote_foreground("gvim")' , 'E48:' ) 744endif 745call assert_fails ( 'let s=remote_peek("gvim")' , 'E48:' ) 746call assert_fails ( 'let s=remote_read("gvim")' , 'E48:' ) 747call assert_fails ( 'let s=remote_send("gvim", "abc")' , 'E48:' ) 748call assert_fails ( 'let s=server2client("gvim", "abc")' , 'E48:' ) 749endif 750if has ( 'terminal' ) 751call assert_fails ( 'terminal' , 'E48:' ) 752call assert_fails ( 'call term_start("vim")' , 'E48:' ) 753call assert_fails ( 'call term_dumpwrite(1, "Xfile")' , 'E48:' ) 754endif 755if has ( 'channel' ) 756call assert_fails ( "call ch_logfile('chlog')" , 'E48:' ) 757call assert_fails ( "call ch_open('localhost:8765')" , 'E48:' ) 758endif 759if has ( 'job' ) 760call assert_fails ( "call job_start('vim')" , 'E48:' ) 761endif 762if has ( 'unix' ) && has ( 'libcall' ) 763call assert_fails ( "echo libcall('libc.so', 'getenv', 'HOME')" , 'E48:' ) 764endif 765if has ( 'unix' ) 766call assert_fails ( 'cd `pwd`' , 'E48:' ) 767endif 768"call assert_fails("call echoraw('test')", 'E48:') 769"call assert_fails("echoconsole 'test'", 'E48:') 770call assert_fails ( "call readfile('Xsomefile')" , 'E48:' ) 771call assert_fails ( "call readblob('Xsomefile')" , 'E48:' ) 772call assert_fails ( "call readdir('.')" , 'E48:' ) 773"call assert_fails("call readdirex('.')", 'E48:') 774call assert_fails ( "call chdir('.')" , 'E48:' ) 775" some options cannot be changed in a sandbox 776call assert_fails ( 'set exrc' , 'E48:' ) 777call assert_fails ( 'set cdpath' , 'E48:' ) 778if has ( 'xim' ) && has ( 'gui_gtk' ) 779call assert_fails ( 'set imstyle' , 'E48:' ) 780endif 781endfunc 782 783func Test_sandbox () 784sandbox call Sandbox_tests() 785endfunc 786 787func Test_command_not_implemented_E319 () 788if ! has ( 'mzscheme' ) 789call assert_fails ( 'mzscheme' , 'E319:' ) 790endif 791endfunc 792 793func Test_not_break_expression_register () 794call setreg ( '=' , '1+1' ) 795if 0 796put =1 797endif 798call assert_equal ( '1+1' , getreg ( '=' , 1 )) 799endfunc 800 801func Test_address_line_overflow () 802if ! has ( 'nvim' ) && v: sizeoflong < 8 803throw 'Skipped: only works with 64 bit long ints' 804endif 805new 806call setline ( 1 , range ( 100 )) 807call assert_fails ( '|.44444444444444444444444' , 'E1247:' ) 808call assert_fails ( '|.9223372036854775806' , 'E1247:' ) 809call assert_fails ( '.44444444444444444444444d' , 'E1247:' ) 810call assert_equal ( range ( 100 ) ->map ( 'string(v:val)' ), getline ( 1 , '$' )) 811 812$ 813yank 77777777777777777777 814call assert_equal ( "99\n" , @") 815 816bwipe ! 817endfunc 818 819" This was leaving the cursor in line zero 820func Test_using_zero_in_range () 821new 822norm o00 823silent ! 0 ;s/\%') 824bwipe ! 825endfunc 826 827" Test :write after changing name with :file and loading it with :edit 828func Test_write_after_rename () 829call writefile ([ 'text' ], 'Xfile' ) 830 831enew 832file Xfile 833call assert_fails ( 'write' , 'E13: File exists (add ! to override)' ) 834 835" works OK after ":edit" 836edit 837write 838 839call delete ( 'Xfile' ) 840bwipe ! 841endfunc 842 843" catch address lines overflow 844func Test_ex_address_range_overflow () 845call assert_fails ( ':--+foobar' , 'E492:' ) 846endfunc 847 848func Test_drop_modified_file () 849CheckScreendump 850let lines =<< trim END 851call setline(1, 'The quick brown fox jumped over the lazy dogs') 852END 853call writefile ([ '' ], 'Xdrop_modified.txt' , 'D' ) 854call writefile ( lines , 'Xtest_drop_modified' , 'D' ) 855let buf = RunVimInTerminal ( '-S Xtest_drop_modified Xdrop_modified.txt' , { 'rows' : 10 , 'columns' : 40 }) 856call term_sendkeys ( buf , ":drop Xdrop_modified.txt\<CR>" ) 857call VerifyScreenDump ( buf , 'Test_drop_modified_1' , {}) 858 859" clean up 860call StopVimInTerminal ( buf ) 861endfunc 862 863" vim: shiftwidth=2 sts=2 expandtab