robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:9.2.0882: :bwipe crashes if WinLeave wipes all other buffers (#41082)f8872bccbe

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