11" Tests for Vim buffer
22
3+ source check.vim
4+
35" Test for the :bunload command with an offset
46func Test_bunload_with_offset ()
57 % bwipe!
@@ -152,6 +154,24 @@ func Test_bdelete_cmd()
152154 set nobuflisted
153155 enew
154156 call assert_fails (' bdelete ' .. bnr, ' E516:' )
157+
158+ " Deleting more than one buffer
159+ new Xbuf1
160+ new Xbuf2
161+ exe ' bdel ' .. bufnr (' Xbuf2' ) .. ' ' .. bufnr (' Xbuf1' )
162+ call assert_equal (1 , winnr (' $' ))
163+ call assert_equal (0 , getbufinfo (' Xbuf1' )[0 ].loaded)
164+ call assert_equal (0 , getbufinfo (' Xbuf2' )[0 ].loaded)
165+
166+ " Deleting more than one buffer and an invalid buffer
167+ new Xbuf1
168+ new Xbuf2
169+ let cmd = " exe 'bdel ' .. bufnr('Xbuf2') .. ' xxx ' .. bufnr('Xbuf1')"
170+ call assert_fails (cmd, ' E94:' )
171+ call assert_equal (2 , winnr (' $' ))
172+ call assert_equal (1 , getbufinfo (' Xbuf1' )[0 ].loaded)
173+ call assert_equal (0 , getbufinfo (' Xbuf2' )[0 ].loaded)
174+
155175 % bwipe!
156176endfunc
157177
@@ -166,4 +186,188 @@ func Test_buffer_error()
166186 % bwipe
167187endfunc
168188
189+ " Test for the status messages displayed when unloading, deleting or wiping
190+ " out buffers
191+ func Test_buffer_statusmsg ()
192+ CheckEnglish
193+ set report = 1
194+ new Xbuf1
195+ new Xbuf2
196+ let bnr = bufnr ()
197+ exe " normal 2\<C-G> "
198+ call assert_match (' buf ' .. bnr .. ' :' , v: statusmsg )
199+ bunload Xbuf1 Xbuf2
200+ call assert_equal (' 2 buffers unloaded' , v: statusmsg )
201+ bdel Xbuf1 Xbuf2
202+ call assert_equal (' 2 buffers deleted' , v: statusmsg )
203+ bwipe Xbuf1 Xbuf2
204+ call assert_equal (' 2 buffers wiped out' , v: statusmsg )
205+ set report &
206+ endfunc
207+
208+ " Test for quitting the 'swapfile exists' dialog with the split buffer
209+ " command.
210+ func Test_buffer_sbuf_cleanup ()
211+ call writefile ([], ' Xfile' )
212+ " first open the file in a buffer
213+ new Xfile
214+ let bnr = bufnr ()
215+ close
216+ " create the swap file
217+ call writefile ([], ' .Xfile.swp' )
218+ " Remove the catch-all that runtest.vim adds
219+ au ! SwapExists
220+ augroup BufTest
221+ au !
222+ autocmd SwapExists Xfile let v: swapchoice= ' q'
223+ augroup END
224+ exe ' sbuf ' . bnr
225+ call assert_equal (1 , winnr (' $' ))
226+ call assert_equal (0 , getbufinfo (' Xfile' )[0 ].loaded)
227+
228+ " test for :sball
229+ sball
230+ call assert_equal (1 , winnr (' $' ))
231+ call assert_equal (0 , getbufinfo (' Xfile' )[0 ].loaded)
232+
233+ % bw !
234+ set shortmess += F
235+ let v: statusmsg = ' '
236+ edit Xfile
237+ call assert_equal (' ' , v: statusmsg )
238+ call assert_equal (1 , winnr (' $' ))
239+ call assert_equal (0 , getbufinfo (' Xfile' )[0 ].loaded)
240+ set shortmess &
241+
242+ call delete (' Xfile' )
243+ call delete (' .Xfile.swp' )
244+ augroup BufTest
245+ au !
246+ augroup END
247+ augroup ! BufTest
248+ endfunc
249+
250+ " Test for deleting a modified buffer with :confirm
251+ func Test_bdel_with_confirm ()
252+ CheckUnix
253+ CheckNotGui
254+ CheckFeature dialog_con
255+ new
256+ call setline (1 , ' test' )
257+ call assert_fails (' bdel' , ' E89:' )
258+ call feedkeys (' c' , ' L' )
259+ confirm bdel
260+ call assert_equal (2 , winnr (' $' ))
261+ call assert_equal (1 , &modified )
262+ call feedkeys (' n' , ' L' )
263+ confirm bdel
264+ call assert_equal (1 , winnr (' $' ))
265+ endfunc
266+
267+ " Test for editing another buffer from a modified buffer with :confirm
268+ func Test_goto_buf_with_confirm ()
269+ CheckUnix
270+ CheckNotGui
271+ CheckFeature dialog_con
272+ new Xfile
273+ enew
274+ call setline (1 , ' test' )
275+ call assert_fails (' b Xfile' , ' E37:' )
276+ call feedkeys (' c' , ' L' )
277+ call assert_fails (' confirm b Xfile' , ' E37:' )
278+ call assert_equal (1 , &modified )
279+ call assert_equal (' ' , @% )
280+ call feedkeys (' y' , ' L' )
281+ call assert_fails (' confirm b Xfile' , ' E37:' )
282+ call assert_equal (1 , &modified )
283+ call assert_equal (' ' , @% )
284+ call feedkeys (' n' , ' L' )
285+ confirm b Xfile
286+ call assert_equal (' Xfile' , @% )
287+ close !
288+ endfunc
289+
290+ " Test for splitting buffer with 'switchbuf'
291+ func Test_buffer_switchbuf ()
292+ new Xfile
293+ wincmd w
294+ set switchbuf = useopen
295+ sbuf Xfile
296+ call assert_equal (1 , winnr ())
297+ call assert_equal (2 , winnr (' $' ))
298+ set switchbuf = usetab
299+ tabnew
300+ sbuf Xfile
301+ call assert_equal (1 , tabpagenr ())
302+ call assert_equal (2 , tabpagenr (' $' ))
303+ set switchbuf &
304+ % bw
305+ endfunc
306+
307+ " Test for BufAdd autocommand wiping out the buffer
308+ func Test_bufadd_autocmd_bwipe ()
309+ % bw !
310+ augroup BufAdd_Wipe
311+ au !
312+ autocmd BufAdd Xfile % bw !
313+ augroup END
314+ edit Xfile
315+ call assert_equal (' ' , @% )
316+ call assert_equal (0 , bufexists (' Xfile' ))
317+ augroup BufAdd_Wipe
318+ au !
319+ augroup END
320+ augroup ! BufAdd_Wipe
321+ endfunc
322+
323+ " Test for trying to load a buffer with text locked
324+ " <C-\>e in the command line is used to lock the text
325+ func Test_load_buf_with_text_locked ()
326+ new Xfile1
327+ edit Xfile2
328+ let cmd = " :\<C-\>eexecute(\"normal \<C-O> \" )\<CR>\<C-C> "
329+ call assert_fails (" call feedkeys(cmd, 'xt')" , ' E565:' )
330+ % bw !
331+ endfunc
332+
333+ " Test for using CTRL-^ to edit the alternative file keeping the cursor
334+ " position with 'nostartofline'. Also test using the 'buf' command.
335+ func Test_buffer_edit_altfile ()
336+ call writefile (repeat ([' one two' ], 50 ), ' Xfile1' )
337+ call writefile (repeat ([' five six' ], 50 ), ' Xfile2' )
338+ set nosol
339+ edit Xfile1
340+ call cursor (25 , 5 )
341+ edit Xfile2
342+ call cursor (30 , 4 )
343+ exe " normal \<C-^> "
344+ call assert_equal ([0 , 25 , 5 , 0 ], getpos (' .' ))
345+ exe " normal \<C-^> "
346+ call assert_equal ([0 , 30 , 4 , 0 ], getpos (' .' ))
347+ buf Xfile1
348+ call assert_equal ([0 , 25 , 5 , 0 ], getpos (' .' ))
349+ buf Xfile2
350+ call assert_equal ([0 , 30 , 4 , 0 ], getpos (' .' ))
351+ set sol &
352+ call delete (' Xfile1' )
353+ call delete (' Xfile2' )
354+ endfunc
355+
356+ " Test for running the :sball command with a maximum window count and a
357+ " modified buffer
358+ func Test_sball_with_count ()
359+ % bw !
360+ edit Xfile1
361+ call setline (1 , [' abc' ])
362+ new Xfile2
363+ new Xfile3
364+ new Xfile4
365+ 2 sball
366+ call assert_equal (bufnr (' Xfile4' ), winbufnr (1 ))
367+ call assert_equal (bufnr (' Xfile1' ), winbufnr (2 ))
368+ call assert_equal (0 , getbufinfo (' Xfile2' )[0 ].loaded)
369+ call assert_equal (0 , getbufinfo (' Xfile3' )[0 ].loaded)
370+ % bw !
371+ endfunc
372+
169373" vim: shiftwidth = 2 sts = 2 expandtab
0 commit comments