robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f8872bccbe
master
1" Tests for Vim buffer 2 3source check.vim 4 5" Test for the :bunload command with an offset 6func Test_bunload_with_offset () 7 %bwipe ! 8call writefile ([ 'B1' ], 'Xb1' , 'D' ) 9call writefile ([ 'B2' ], 'Xb2' , 'D' ) 10call writefile ([ 'B3' ], 'Xb3' , 'D' ) 11call writefile ([ 'B4' ], 'Xb4' , 'D' ) 12 13" Load four buffers. Unload the second and third buffers and then 14" execute .+3bunload to unload the last buffer. 15edit Xb1 16new Xb2 17new Xb3 18new Xb4 19 20bunload Xb2 21bunload Xb3 22exe bufwinnr ( 'Xb1' ) . 'wincmd w' 23 .+3 bunload 24call assert_equal ( 0 , getbufinfo ( 'Xb4' )[ 0 ]. loaded ) 25call assert_equal ( 'Xb1' , 26 \fnamemodify ( getbufinfo ({ 'bufloaded' : 1 })[ 0 ]. name , ':t' )) 27 28" Load four buffers. Unload the third and fourth buffers. Execute .+3bunload 29" and check whether the second buffer is unloaded. 30ball 31bunload Xb3 32bunload Xb4 33exe bufwinnr ( 'Xb1' ) . 'wincmd w' 34 .+3 bunload 35call assert_equal ( 0 , getbufinfo ( 'Xb2' )[ 0 ]. loaded ) 36call assert_equal ( 'Xb1' , 37 \fnamemodify ( getbufinfo ({ 'bufloaded' : 1 })[ 0 ]. name , ':t' )) 38 39" Load four buffers. Unload the second and third buffers and from the last 40" buffer execute .-3bunload to unload the first buffer. 41ball 42bunload Xb2 43bunload Xb3 44exe bufwinnr ( 'Xb4' ) . 'wincmd w' 45 .-3 bunload 46call assert_equal ( 0 , getbufinfo ( 'Xb1' )[ 0 ]. loaded ) 47call assert_equal ( 'Xb4' , 48 \fnamemodify ( getbufinfo ({ 'bufloaded' : 1 })[ 0 ]. name , ':t' )) 49 50" Load four buffers. Unload the first and second buffers. Execute .-3bunload 51" from the last buffer and check whether the third buffer is unloaded. 52ball 53bunload Xb1 54bunload Xb2 55exe bufwinnr ( 'Xb4' ) . 'wincmd w' 56 .-3 bunload 57call assert_equal ( 0 , getbufinfo ( 'Xb3' )[ 0 ]. loaded ) 58call assert_equal ( 'Xb4' , 59 \fnamemodify ( getbufinfo ({ 'bufloaded' : 1 })[ 0 ]. name , ':t' )) 60 61 %bwipe ! 62 63call assert_fails ( '1,4bunload' , 'E16:' ) 64call assert_fails ( ',100bunload' , 'E16:' ) 65 66call assert_fails ( '$bunload' , 'E90:' ) 67endfunc 68 69" Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified 70" commands 71func Test_buflist_browse () 72 %bwipe ! 73call assert_fails ( 'buffer 1000' , 'E86:' ) 74 75call writefile ([ 'foo1' , 'foo2' , 'foo3' , 'foo4' ], 'Xbrowse1' , 'D' ) 76call writefile ([ 'bar1' , 'bar2' , 'bar3' , 'bar4' ], 'Xbrowse2' , 'D' ) 77call writefile ([ 'baz1' , 'baz2' , 'baz3' , 'baz4' ], 'Xbrowse3' , 'D' ) 78edit Xbrowse1 79let b1 = bufnr () 80edit Xbrowse2 81let b2 = bufnr () 82edit +/ baz4 Xbrowse3 83let b3 = bufnr () 84 85call assert_fails ( 'buffer ' .. b1 .. ' abc' , 'E488:' ) 86call assert_equal ( b3 , bufnr ()) 87call assert_equal ( 4 , line ( '.' )) 88exe 'buffer +/bar2 ' .. b2 89call assert_equal ( b2 , bufnr ()) 90call assert_equal ( 2 , line ( '.' )) 91exe 'buffer +/bar1' 92call assert_equal ( b2 , bufnr ()) 93call assert_equal ( 1 , line ( '.' )) 94 95brewind + 96call assert_equal ( b1 , bufnr ()) 97call assert_equal ( 4 , line ( '.' )) 98 99blast +/baz2 100call assert_equal ( b3 , bufnr ()) 101call assert_equal ( 2 , line ( '.' )) 102 103bprevious +/bar4 104call assert_equal ( b2 , bufnr ()) 105call assert_equal ( 4 , line ( '.' )) 106 107bnext +/baz3 108call assert_equal ( b3 , bufnr ()) 109call assert_equal ( 3 , line ( '.' )) 110 111call assert_fails ( 'bmodified' , 'E84:' ) 112call setbufvar ( b2 , '&modified' , 1 ) 113exe 'bmodified +/bar3' 114call assert_equal ( b2 , bufnr ()) 115call assert_equal ( 3 , line ( '.' )) 116 117" With no listed buffers in the list, :bnext and :bprev should fail 118 %bwipe ! 119set nobuflisted 120call assert_fails ( 'bnext' , 'E85:' ) 121call assert_fails ( 'bprev' , 'E85:' ) 122set buflisted 123 124call assert_fails ( 'sandbox bnext' , 'E48:' ) 125 126 %bwipe ! 127endfunc 128 129" Test for :bnext and :bprev when called from help and non-help buffers. 130func Test_bnext_bprev_help () 131 %bwipe ! 132 133e XHelp1 |set bt = help 134let b1 = bufnr () 135e Xbuf1 136let b2 = bufnr () 137 138" There's only one buffer of each type. 139b XHelp1 140bnext |call assert_equal ( b1 , bufnr ()) 141bprev |call assert_equal ( b1 , bufnr ()) 142b Xbuf1 143bnext |call assert_equal ( b2 , bufnr ()) 144bprev |call assert_equal ( b2 , bufnr ()) 145 146" Add one more buffer of each type. 147e XHelp2 |set bt = help 148let b3 = bufnr () 149e Xbuf2 150let b4 = bufnr () 151 152" Help buffer jumps to help buffer. 153b XHelp1 154bnext |call assert_equal ( b3 , bufnr ()) 155bnext |call assert_equal ( b1 , bufnr ()) 156bprev |call assert_equal ( b3 , bufnr ()) 157bprev |call assert_equal ( b1 , bufnr ()) 158 159" Regular buffer jumps to regular buffer. 160b Xbuf1 161bnext |call assert_equal ( b4 , bufnr ()) 162bnext |call assert_equal ( b2 , bufnr ()) 163bprev |call assert_equal ( b4 , bufnr ()) 164bprev |call assert_equal ( b2 , bufnr ()) 165 166" :brewind and :blast are not affected by the buffer type. 167b Xbuf2 168brewind |call assert_equal ( b1 , bufnr ()) 169b XHelp1 170blast |call assert_equal ( b4 , bufnr ()) 171 172" Cycling through help buffers works even if they aren't listed. 173b XHelp1 174setlocal nobuflisted 175bnext |call assert_equal ( b3 , bufnr ()) 176bnext |call assert_equal ( b1 , bufnr ()) 177bprev |call assert_equal ( b3 , bufnr ()) 178setlocal nobuflisted 179bprev |call assert_equal ( b1 , bufnr ()) 180bprev |call assert_equal ( b3 , bufnr ()) 181bnext |call assert_equal ( b1 , bufnr ()) 182 183 %bwipe ! 184endfunc 185 186" Test for :bdelete 187func Test_bdelete_cmd () 188 %bwipe ! 189call assert_fails ( 'bdelete 5' , 'E516:' ) 190call assert_fails ( '1,1bdelete 1 2' , 'E488:' ) 191call assert_fails ( 'bdelete \)' , 'E55:' ) 192 193" Deleting an unlisted and unloaded buffer 194edit Xbdelfile1 195let bnr = bufnr () 196set nobuflisted 197enew 198call assert_fails ( 'bdelete ' .. bnr , 'E516:' ) 199 200" Deleting more than one buffer 201new Xbuf1 202new Xbuf2 203exe 'bdel ' .. bufnr ( 'Xbuf2' ) .. ' ' .. bufnr ( 'Xbuf1' ) 204call assert_equal ( 1 , winnr ( '$' )) 205call assert_equal ( 0 , getbufinfo ( 'Xbuf1' )[ 0 ]. loaded ) 206call assert_equal ( 0 , getbufinfo ( 'Xbuf2' )[ 0 ]. loaded ) 207 208" Deleting more than one buffer and an invalid buffer 209new Xbuf1 210new Xbuf2 211let cmd = "exe 'bdel ' .. bufnr('Xbuf2') .. ' xxx ' .. bufnr('Xbuf1')" 212call assert_fails ( cmd , 'E94:' ) 213call assert_equal ( 2 , winnr ( '$' )) 214call assert_equal ( 1 , getbufinfo ( 'Xbuf1' )[ 0 ]. loaded ) 215call assert_equal ( 0 , getbufinfo ( 'Xbuf2' )[ 0 ]. loaded ) 216 217 %bwipe ! 218endfunc 219 220func Test_buffer_error () 221new foo1 222new foo2 223 224call assert_fails ( 'buffer foo' , 'E93:' ) 225call assert_fails ( 'buffer bar' , 'E94:' ) 226call assert_fails ( 'buffer 0' , 'E939:' ) 227 228 %bwipe 229endfunc 230 231func Test_bwipe_during_save () 232set charconvert = execute ( '%bw!' ) 233call assert_fails ( 'write ++enc=lmao boom' , 'E937:' ) 234 235set charconvert & 236 %bwipe 237endfunc 238 239" Test for the status messages displayed when unloading, deleting or wiping 240" out buffers 241func Test_buffer_statusmsg () 242CheckEnglish 243set report = 1 244new Xbuf1 245new Xbuf2 246let bnr = bufnr () 247exe "normal 2\<C-G>" 248call assert_match ( 'buf ' .. bnr .. ':' , v: statusmsg ) 249bunload Xbuf1 Xbuf2 250call assert_equal ( '2 buffers unloaded' , v: statusmsg ) 251bdel Xbuf1 Xbuf2 252call assert_equal ( '2 buffers deleted' , v: statusmsg ) 253bwipe Xbuf1 Xbuf2 254call assert_equal ( '2 buffers wiped out' , v: statusmsg ) 255set report & 256endfunc 257 258" Test for quitting the 'swapfile exists' dialog with the split buffer 259" command. 260func Test_buffer_sbuf_cleanup () 261call writefile ([], 'XsplitCleanup' , 'D' ) 262" first open the file in a buffer 263new XsplitCleanup 264let bnr = bufnr () 265close 266" create the swap file 267call writefile ([], '.XsplitCleanup.swp' , 'D' ) 268" Remove the catch-all that runtest.vim adds 269au ! SwapExists 270augroup BufTest 271au ! 272autocmd SwapExists XsplitCleanup let v: swapchoice = 'q' 273augroup END 274exe 'sbuf ' . bnr 275call assert_equal ( 1 , winnr ( '$' )) 276call assert_equal ( 0 , getbufinfo ( 'XsplitCleanup' )[ 0 ]. loaded ) 277 278" test for :sball 279sball 280call assert_equal ( 1 , winnr ( '$' )) 281call assert_equal ( 0 , getbufinfo ( 'XsplitCleanup' )[ 0 ]. loaded ) 282 283 %bw ! 284set shortmess += F 285let v: statusmsg = '' 286edit XsplitCleanup 287call assert_equal ( '' , v: statusmsg ) 288call assert_equal ( 1 , winnr ( '$' )) 289call assert_equal ( 0 , getbufinfo ( 'XsplitCleanup' )[ 0 ]. loaded ) 290set shortmess & 291 292augroup BufTest 293au ! 294augroup END 295augroup ! BufTest 296endfunc 297 298" Test for deleting a modified buffer with :confirm 299func Test_bdel_with_confirm () 300CheckUnix 301CheckNotGui 302CheckFeature dialog_con 303new 304call setline ( 1 , 'test' ) 305call assert_fails ( 'bdel' , 'E89:' ) 306call feedkeys ( 'c' , 'L' ) 307confirm bdel 308call assert_equal ( 2 , winnr ( '$' )) 309call assert_equal ( 1 , & modified ) 310call feedkeys ( 'n' , 'L' ) 311confirm bdel 312call assert_equal ( 1 , winnr ( '$' )) 313endfunc 314 315" Test for editing another buffer from a modified buffer with :confirm 316func Test_goto_buf_with_confirm () 317CheckUnix 318CheckNotGui 319CheckFeature dialog_con 320" When dialog_con_gui is defined, Vim is compiled with GUI support 321" and FEAT_BROWSE will be defined, which causes :confirm :b to 322" call do_browse(), which will try to use a GUI file browser, 323" which aborts if a GUI is not available. 324CheckNotFeature dialog_con_gui 325new XgotoConf 326enew 327call setline ( 1 , 'test' ) 328call assert_fails ( 'b XgotoConf' , 'E37:' ) 329call feedkeys ( 'c' , 'L' ) 330call assert_fails ( 'confirm b XgotoConf' , 'E37:' ) 331call assert_true ( & modified ) 332call assert_true ( empty ( bufname ( '%' ))) 333call feedkeys ( 'y' , 'L' ) 334confirm b XgotoConf 335call assert_equal ( 'XgotoConf' , bufname ( '%' )) 336call assert_equal ([ 'test' ], readfile ( 'Untitled' )) 337e Untitled 338call setline ( 2 , 'test2' ) 339call feedkeys ( 'n' , 'L' ) 340confirm b XgotoConf 341call assert_equal ( 'XgotoConf' , bufname ( '%' )) 342call assert_equal ([ 'test' ], readfile ( 'Untitled' )) 343call delete ( 'Untitled' ) 344close ! 345endfunc 346 347" Test for issue #20132: when saving an unnamed buffer fails the modified 348" flag must be kept, otherwise the buffer is silently discarded. 349func Test_dialog_changed_keep_modified_on_write_fail () 350CheckUnix 351CheckNotGui 352CheckFeature dialog_con 353CheckNotFeature dialog_con_gui 354CheckNotRoot 355 356call writefile ([ 'existing' ], 'Untitled' , 'D' ) 357call setfperm ( 'Untitled' , 'r--r--r--' ) 358 359new 360call setline ( 1 , 'test' ) 361call feedkeys ( 'y' , 'L' ) 362silent ! confirm bdel 363call assert_true ( & modified ) 364call assert_equal ([ 'existing' ], readfile ( 'Untitled' )) 365 366bw ! 367endfunc 368 369" Test for splitting buffer with 'switchbuf' 370func Test_buffer_switchbuf () 371new Xswitchbuf 372wincmd w 373set switchbuf = useopen 374sbuf Xswitchbuf 375call assert_equal ( 1 , winnr ()) 376call assert_equal ( 2 , winnr ( '$' )) 377set switchbuf = usetab 378tabnew 379sbuf Xswitchbuf 380call assert_equal ( 1 , tabpagenr ()) 381call assert_equal ( 2 , tabpagenr ( '$' )) 382set switchbuf & 383 %bw 384endfunc 385 386" Test for BufAdd autocommand wiping out the buffer 387func Test_bufadd_autocmd_bwipe () 388 %bw ! 389augroup BufAdd_Wipe 390au ! 391autocmd BufAdd Xbwipe %bw ! 392augroup END 393edit Xbwipe 394call assert_equal ( '' , @%) 395call assert_equal ( 0 , bufexists ( 'Xbwipe' )) 396augroup BufAdd_Wipe 397au ! 398augroup END 399augroup ! BufAdd_Wipe 400endfunc 401 402" Test for trying to load a buffer with text locked 403" <C-\>e in the command line is used to lock the text 404func Test_load_buf_with_text_locked () 405new Xlockfile1 406edit Xlockfile2 407let cmd = ":\<C-\>eexecute(\"normal \<C-O>\")\<CR>\<C-C>" 408call assert_fails ( "call feedkeys(cmd, 'xt')" , 'E565:' ) 409 %bw ! 410endfunc 411 412" Test for using CTRL-^ to edit the alternative file keeping the cursor 413" position with 'nostartofline'. Also test using the 'buf' command. 414func Test_buffer_edit_altfile () 415call writefile ( repeat ([ 'one two' ], 50 ), 'Xaltfile1' , 'D' ) 416call writefile ( repeat ([ 'five six' ], 50 ), 'Xaltfile2' , 'D' ) 417set nosol 418edit Xaltfile1 419call cursor ( 25 , 5 ) 420edit Xaltfile2 421call cursor ( 30 , 4 ) 422exe "normal \<C-^>" 423call assert_equal ([ 0 , 25 , 5 , 0 ], getpos ( '.' )) 424exe "normal \<C-^>" 425call assert_equal ([ 0 , 30 , 4 , 0 ], getpos ( '.' )) 426buf Xaltfile1 427call assert_equal ([ 0 , 25 , 5 , 0 ], getpos ( '.' )) 428buf Xaltfile2 429call assert_equal ([ 0 , 30 , 4 , 0 ], getpos ( '.' )) 430set sol & 431endfunc 432 433" Test for running the :sball command with a maximum window count and a 434" modified buffer 435func Test_sball_with_count () 436 %bw ! 437edit Xcountfile1 438call setline ( 1 , [ 'abc' ]) 439new Xcountfile2 440new Xcountfile3 441new Xcountfile4 4422 sball 443call assert_equal ( bufnr ( 'Xcountfile4' ), winbufnr ( 1 )) 444call assert_equal ( bufnr ( 'Xcountfile1' ), winbufnr ( 2 )) 445call assert_equal ( 0 , getbufinfo ( 'Xcountfile2' )[ 0 ]. loaded ) 446call assert_equal ( 0 , getbufinfo ( 'Xcountfile3' )[ 0 ]. loaded ) 447 %bw ! 448endfunc 449 450func Test_badd_options () 451new SomeNewBuffer 452setlocal numberwidth = 3 453wincmd p 454badd +1 SomeNewBuffer 455new SomeNewBuffer 456call assert_equal ( 3 , & numberwidth ) 457close 458close 459bwipe ! SomeNewBuffer 460endfunc 461 462func Test_balt () 463new SomeNewBuffer 464balt +3 OtherBuffer 465e # 466call assert_equal ( 'OtherBuffer' , bufname ()) 467endfunc 468 469" Test for buffer match URL(scheme) check 470" scheme is alpha and inner hyphen only. 471func Test_buffer_scheme () 472CheckMSWindows 473 474set noswapfile 475set noshellslash 476 %bwipe ! 477let bufnames = [ 478 \#{ id : 'ssb0' , name : 'test://xyz/foo/ssb0' , match : 1 }, 479 \#{ id : 'ssb1' , name : 'test1234://xyz/foo/ssb1' , match : 1 }, 480 \#{ id : 'ssb2' , name : 'test+abc://xyz/foo/ssb2' , match : 1 }, 481 \#{ id : 'ssb3' , name : 'test-abc://xyz/foo/ssb3' , match : 1 }, 482 \#{ id : 'ssb4' , name : 'test.abc://xyz/foo/ssb4' , match : 1 }, 483 \#{ id : 'ssb5' , name : 'test_abc://xyz/foo/ssb5' , match : 0 }, 484 \#{ id : 'ssb6' , name : '+test://xyz/foo/ssb6' , match : 0 }, 485 \#{ id : 'ssb7' , name : 'test+://xyz/foo/ssb7' , match : 0 }, 486 \#{ id : 'ssb8' , name : '-test://xyz/foo/ssb8' , match : 0 }, 487 \#{ id : 'ssb9' , name : 'test-://xyz/foo/ssb9' , match : 0 }, 488 \#{ id : 'ssb10' , name : '.test://xyz/foo/ssb10' , match : 0 }, 489 \#{ id : 'ssb11' , name : 'test.://xyz/foo/ssb11' , match : 0 }, 490 \] 491for buf in bufnames 492new `=buf.name` 493if buf . match 494call assert_equal ( buf . name , getbufinfo ( buf . id )[ 0 ]. name ) 495else 496" slashes will have become backslashes 497call assert_notequal ( buf . name , getbufinfo ( buf . id )[ 0 ]. name ) 498endif 499bwipe 500endfor 501 502set shellslash & 503set swapfile & 504endfunc 505 506" this was using a NULL pointer after failing to use the pattern 507func Test_buf_pattern_invalid () 508vsplit 0000000 509silent ! buf [0--]\&\zs*\zs*e 510bwipe ! 511 512vsplit 00000000000000000000000000 513silent ! buf [0--]\&\zs*\zs*e 514bwipe ! 515 516" similar case with different code path 517split 0 518edit ÿ 519silent ! buf [0--]\&\zs*\zs*0 520bwipe ! 521endfunc 522 523" Test for the 'maxmem' and 'maxmemtot' options 524func Test_buffer_maxmem () 525" use 1KB per buffer and 2KB for all the buffers 526" set maxmem=1 maxmemtot=2 527new 528let v: errmsg = '' 529" try opening some files 530edit test_arglist.vim 531call assert_equal ( 'test_arglist.vim' , bufname ()) 532edit test_eval_stuff.vim 533call assert_equal ( 'test_eval_stuff.vim' , bufname ()) 534b test_arglist.vim 535call assert_equal ( 'test_arglist.vim' , bufname ()) 536b test_eval_stuff.vim 537call assert_equal ( 'test_eval_stuff.vim' , bufname ()) 538close 539call assert_equal ( '' , v: errmsg ) 540" set maxmem& maxmemtot& 541endfunc 542 543" Test for buffer allocation failure 544func Test_buflist_alloc_failure () 545CheckFunction test_alloc_fail 546 %bw ! 547 548edit XallocFail1 549call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 550call assert_fails ( 'edit XallocFail2' , 'E342:' ) 551 552" test for bufadd() 553call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 554call assert_fails ( 'call bufadd("Xbuffer")' , 'E342:' ) 555 556" test for setting the arglist 557edit XallocFail2 558call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 559call assert_fails ( 'next XallocFail3' , 'E342:' ) 560 561" test for setting the alternate buffer name when writing a file 562call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 563call assert_fails ( 'write Xother' , 'E342:' ) 564call delete ( 'Xother' ) 565 566" test for creating a buffer using bufnr() 567call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 568call assert_fails ( "call bufnr('Xnewbuf', v:true)" , 'E342:' ) 569 570" test for renaming buffer using :file 571call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 572call assert_fails ( 'file Xnewfile' , 'E342:' ) 573 574" test for creating a buffer for a popup window 575call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 576call assert_fails ( 'call popup_create("mypop", {})' , 'E342:' ) 577 578if has ( 'terminal' ) 579" test for creating a buffer for a terminal window 580call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 581call assert_fails ( 'call term_start(&shell)' , 'E342:' ) 582 %bw ! 583endif 584 585" test for loading a new buffer after wiping out all the buffers 586edit XallocFail4 587call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 588call assert_fails ( '%bw!' , 'E342:' ) 589 590" test for :checktime loading the buffer 591call writefile ([ 'one' ], 'XallocFail5' , 'D' ) 592if has ( 'unix' ) 593edit XallocFail5 594" sleep for some time to make sure the timestamp is different 595sleep 200m 596call writefile ([ 'two' ], 'XallocFail5' ) 597set autoread 598call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 599call assert_fails ( 'checktime' , 'E342:' ) 600set autoread & 601bw ! 602endif 603 604" test for :vimgrep loading a dummy buffer 605call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 606call assert_fails ( 'vimgrep two XallocFail5' , 'E342:' ) 607 608" test for quickfix command loading a buffer 609call test_alloc_fail ( GetAllocId ( 'newbuf_bvars' ), 0 , 0 ) 610call assert_fails ( 'cexpr "XallocFail6:10:Line10"' , 'E342:' ) 611endfunc 612 613func Test_closed_buffer_still_in_window () 614 %bw ! 615 616let s: fired = 0 617let s: w = win_getid () 618new 619let s: b = bufnr () 620setl bufhidden = wipe 621augroup ViewClosedBuffer 622autocmd ! 623autocmd BufUnload * ++oncelet s: fired += 1 |call assert_fails ( 624 \'call win_execute(s:w, "' .. s: b .. 'buffer")' , 'E1546:' ) 625augroup END 626quit ! 627" Previously resulted in s:b being curbuf while unloaded (no memfile). 628call assert_equal ( 1 , s: fired ) 629call assert_equal ( 1 , bufloaded ( bufnr ())) 630call assert_equal ( 0 , bufexists ( s: b )) 631 %bw ! 632 633new flobby 634let s: w = win_getid () 635let s: b = bufnr () 636setl bufhidden = wipe 637augroup ViewClosedBuffer 638autocmd ! 639autocmd BufUnload * ++oncelet s: fired += 1 640 \|wincmd p 641 \|call assert_notequal ( s: w , win_getid ()) 642 \|call assert_notequal ( s: b , bufnr ()) 643 \|execute s: b 'sbuffer' 644 \|call assert_equal ( s: w , win_getid ()) 645 \|call assert_equal ( s: b , bufnr ()) 646augroup END 647" Not a problem if 'switchbuf' switches to an existing window instead. 648set switchbuf = useopen 649quit ! 650call assert_equal ( 2 , s: fired ) 651call assert_equal ( 0 , bufexists ( s: b )) 652set switchbuf & 653 %bw ! 654 655edit floob 656let s: b = bufnr () 657enew 658augroup ViewClosedBuffer 659autocmd ! 660autocmd BufWipeout * ++oncelet s: fired += 1 661 \|call assert_fails ( s: b .. 'sbuffer | wincmd p' , 'E1546:' ) 662 \|call assert_equal ( 1 , winnr ( '$' )) " :sbuffer shouldn't have split. 663augroup END 664" Used to be a heap UAF. 665execute s: b 'bwipeout!' 666call assert_equal ( 3 , s: fired ) 667call assert_equal ( 0 , bufexists ( s: b )) 668 %bw ! 669 670edit flarb 671let s: b = bufnr () 672enew 673let b2 = bufnr () 674augroup ViewClosedBuffer 675autocmd ! 676autocmd BufWipeout * ++oncelet s: fired += 1 677 \|call assert_fails ( s: b .. 'sbuffer' , 'E1159:' ) 678augroup END 679" :sbuffer still should fail if curbuf is closing, even if it's not the target 680" buffer (as switching buffers can fail after the split) 681bwipeout ! 682call assert_equal ( 4 , s: fired ) 683call assert_equal ( 0 , bufexists ( b2 )) 684 %bw ! 685 686let s: w = win_getid () 687split 688new 689let s: b = bufnr () 690augroup ViewClosedBuffer 691autocmd ! 692autocmd BufWipeout * ++oncelet s: fired += 1 |call win_gotoid ( s: w ) 693 \|call assert_fails ( s: b .. 'buffer' , 'E1546:' ) |wincmd p 694augroup END 695bw ! " Close only this buffer first; used to be a heap UAF. 696call assert_equal ( 5 , s: fired ) 697 698unlet ! s: w s: b s: fired 699autocmd ! ViewClosedBuffer 700% bw ! 701endfunc 702 703" Cursor position should be restored when switching to a buffer previously 704" viewed in a window, regardless of whether it's visible in another one. 705func Test_switch_to_previously_viewed_buffer () 706set nostartofline 707new Xviewbuf 708call setline ( 1 , range ( 1 , 200 )) 709let oldwin = win_getid () 710vsplit 711 712call cursor ( 100 , 3 ) 713call assert_equal ( '100' , getline ( '.' )) 714edit Xotherbuf 715buffer Xviewbuf 716call assert_equal ([ 0 , 100 , 3 , 0 ], getpos ( '.' )) 717call assert_equal ( '100' , getline ( '.' )) 718 719edit Xotherbuf 720wincmd p 721normal ! gg10dd 722wincmd p 723buffer Xviewbuf 724call assert_equal ([ 0 , 90 , 3 , 0 ], getpos ( '.' )) 725call assert_equal ( '100' , getline ( '.' )) 726 727edit Xotherbuf 728wincmd p 729normal ! ggP 730wincmd p 731buffer Xviewbuf 732call assert_equal ([ 0 , 100 , 3 , 0 ], getpos ( '.' )) 733call assert_equal ( '100' , getline ( '.' )) 734 735edit Xotherbuf 736wincmd p 737normal ! 96gg10ddgg 738wincmd p 739buffer Xviewbuf 740" The original cursor line was deleted, so cursor is restored to the start 741" of the line before the deleted range. 742call assert_equal ([ 0 , 95 , 1 , 0 ], getpos ( '.' )) 743call assert_equal ( '95' , getline ( '.' )) 744 745normal ! u 746exe win_id2win ( oldwin ) .. 'close' 747setlocal bufhidden = hide 748 749call cursor ( 200 , 3 ) 750call assert_equal ( '200' , getline ( '.' )) 751edit Xotherbuf 752buffer Xviewbuf 753call assert_equal ([ 0 , 200 , 3 , 0 ], getpos ( '.' )) 754call assert_equal ( '200' , getline ( '.' )) 755 756bwipe ! Xotherbuf 757bwipe ! Xviewbuf 758set startofline & 759endfunc 760 761func Test_bdelete_skip_closing_bufs () 762set hidden 763let s: fired = 0 764 765edit foo 766edit bar 767let s: next_new_bufnr = bufnr ( '$' ) + 1 768augroup SkipClosing 769autocmd ! 770" Only window and other buffer is closing. 771" No choice but to switch to a new, empty buffer. 772autocmd BufDelete * ++once let s: fired += 1 773\| call assert_equal ( 1 , winnr ( '$' )) 774\| call assert_equal ( 'bar' , bufname ()) 775\| bdelete 776\| call assert_equal ( '' , bufname ()) 777\| call assert_equal ( s: next_new_bufnr , bufnr ()) 778augroup END 779bdelete foo 780call assert_equal ( 1 , s: fired ) 781unlet ! s: next_new_bufnr 782% bw ! 783 784edit baz 785edit bar 786edit fleb 787edit foo 788augroup SkipClosing 789autocmd ! 790" Only window, au_new_curbuf is NOT closing; should end up there. 791autocmd BufDelete * ++once let s: fired += 1 792\| call assert_equal ( 1 , winnr ( '$' )) 793\| call assert_equal ( 'foo' , bufname ()) 794\| bwipeout 795\| call assert_equal ( 'bar' , bufname ()) 796augroup END 797buffer baz 798buffer foo 799augroup SkipClosing 800autocmd BufLeave * ++once ++nested bdelete baz 801augroup END 802edit bar 803call assert_equal ( 2 , s: fired ) 804% bw ! 805 806edit baz 807edit bar 808edit fleb 809edit foo 810augroup SkipClosing 811autocmd ! 812" Like above, but au_new_curbuf IS closing. 813" Should use the most recent jumplist buffer instead. 814autocmd BufDelete * ++once let s: fired += 1 815\| call assert_equal ( 1 , winnr ( '$' )) 816\| call assert_equal ( 'foo' , bufname ()) 817\| bwipeout 818\| call assert_equal ( 'baz' , bufname ()) 819augroup END 820buffer baz 821buffer foo 822augroup SkipClosing 823autocmd BufLeave * ++once ++nested bdelete bar 824augroup END 825edit bar 826call assert_equal ( 3 , s: fired ) 827% bw ! 828 829edit foo 830edit floob 831edit baz 832edit bar 833augroup SkipClosing 834autocmd ! 835" Only window, most recent buffer in jumplist is closing. 836" Should switch to the next most-recent buffer in the jumplist instead. 837autocmd BufDelete * ++once let s: fired += 1 838\| call assert_equal ( 1 , winnr ( '$' )) 839\| call assert_equal ( 'bar' , bufname ()) 840\| bdelete 841\| call assert_equal ( 'floob' , bufname ()) 842augroup END 843buffer baz 844buffer floob 845buffer foo 846buffer bar 847bdelete foo 848call assert_equal ( 4 , s: fired ) 849% bw ! 850 851edit foo 852edit baz 853edit bar 854edit floob 855edit bazinga 856augroup SkipClosing 857autocmd ! 858" Only window, most recent jumplist buffer is gone, next most-recent is 859" closing. Should switch to the 3rd most-recent jumplist buffer. 860autocmd BufDelete * ++once let s: fired += 1 861\| call assert_equal ( 1 , winnr ( '$' )) 862\| call assert_equal ( 'bar' , bufname ()) 863\| bwipeout 864\| call assert_equal ( 'baz' , bufname ()) 865augroup END 866buffer bazinga 867buffer baz 868buffer floob 869buffer foo 870buffer bar 871noautocmd bdelete foo 872bdelete floob 873call assert_equal ( 5 , s: fired ) 874% bw ! 875 876edit foo 877edit baz 878edit floob 879edit bazinga 880edit bar 881augroup SkipClosing 882autocmd ! 883" Like above, but jumplist cleared, no next buffer in the buffer list and 884" previous buffer is closing. Should switch to the buffer before previous. 885autocmd BufDelete * ++once let s: fired += 1 886\| call assert_equal ( 1 , winnr ( '$' )) 887\| call assert_equal ( 'bar' , bufname ()) 888\| bunload 889\| call assert_equal ( 'floob' , bufname ()) 890augroup END 891buffer bazinga 892buffer baz 893buffer floob 894buffer foo 895buffer bar 896noautocmd bdelete foo 897clearjumps 898bdelete bazinga 899call assert_equal ( 6 , s: fired ) 900 901unlet ! s: fired 902autocmd ! SkipClosing 903set hidden & 904% bw ! 905endfunc 906 907func Test_split_window_in_BufLeave_from_tab_sbuffer () 908tabnew Xa 909setlocal bufhidden = wipe 910let t0 = tabpagenr () 911let b0 = bufnr () 912let b1 = bufadd ( 'Xb' ) 913autocmd BufLeave Xa ++once split 914exe 'tab sbuffer' b1 915call assert_equal ( t0 + 1 , tabpagenr ()) 916call assert_equal ([ b1 , b0 ], tabpagebuflist ()) 917call assert_equal ([ b0 ], tabpagebuflist ( t0 )) 918tabclose 919call assert_equal ( t0 , tabpagenr ()) 920call assert_equal ([ b0 ], tabpagebuflist ()) 921 922bwipe ! Xa 923bwipe ! Xb 924endfunc 925 926func Test_split_window_in_BufLeave_from_switching_buffer () 927tabnew Xa 928setlocal bufhidden = wipe 929split 930let b0 = bufnr () 931let b1 = bufadd ( 'Xb' ) 932autocmd BufLeave Xa ++once split 933exe 'buffer' b1 934call assert_equal ([ b1 , b0 , b0 ], tabpagebuflist ()) 935 936bwipe ! Xa 937bwipe ! Xb 938endfunc 939 940func Test_wipe_other_buffers_in_WinLeave_from_bwipe () 941tabnew 942autocmd WinLeave * ++once 1 , $ -1 bwipe ! 943" This should not crash 944$ bwipe ! 945endfunc 946 947" Switch to a buffer whose name contains '%' via completion (#20529). 948func Test_buffer_switch_to_name_with_percent () 949CheckMSWindows 950 951let bufnr = bufadd ( 'Xpercent%name' ) 952call setbufvar ( bufnr , '&buflisted' , 1 ) 953call bufload ( bufnr ) 954enew 955 956call feedkeys ( ":b Xpercent\<Tab>\<CR>" , 'xt' ) 957call assert_equal ( bufnr , bufnr ( '%' )) 958 959exe 'bwipe! ' .. bufnr 960endfunc 961 962" vim: shiftwidth=2 sts=2 expandtab