xref: /vim-8.2.3635/src/testdir/test_buffer.vim (revision 89a9c159)
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'], 'b1')
9  call writefile(['B2'], 'b2')
10  call writefile(['B3'], 'b3')
11  call writefile(['B4'], 'b4')
12
13  " Load four buffers. Unload the second and third buffers and then
14  " execute .+3bunload to unload the last buffer.
15  edit b1
16  new b2
17  new b3
18  new b4
19
20  bunload b2
21  bunload b3
22  exe bufwinnr('b1') . 'wincmd w'
23  .+3bunload
24  call assert_equal(0, getbufinfo('b4')[0].loaded)
25  call assert_equal('b1',
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 b3
32  bunload b4
33  exe bufwinnr('b1') . 'wincmd w'
34  .+3bunload
35  call assert_equal(0, getbufinfo('b2')[0].loaded)
36  call assert_equal('b1',
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 b2
43  bunload b3
44  exe bufwinnr('b4') . 'wincmd w'
45  .-3bunload
46  call assert_equal(0, getbufinfo('b1')[0].loaded)
47  call assert_equal('b4',
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 b1
54  bunload b2
55  exe bufwinnr('b4') . 'wincmd w'
56  .-3bunload
57  call assert_equal(0, getbufinfo('b3')[0].loaded)
58  call assert_equal('b4',
59        \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
60
61  %bwipe!
62  call delete('b1')
63  call delete('b2')
64  call delete('b3')
65  call delete('b4')
66
67  call assert_fails('1,4bunload', 'E16:')
68  call assert_fails(',100bunload', 'E16:')
69
70  call assert_fails('$bunload', 'E90:')
71endfunc
72
73" Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified
74" commands
75func Test_buflist_browse()
76  %bwipe!
77  call assert_fails('buffer 1000', 'E86:')
78
79  call writefile(['foo1', 'foo2', 'foo3', 'foo4'], 'Xfile1')
80  call writefile(['bar1', 'bar2', 'bar3', 'bar4'], 'Xfile2')
81  call writefile(['baz1', 'baz2', 'baz3', 'baz4'], 'Xfile3')
82  edit Xfile1
83  let b1 = bufnr()
84  edit Xfile2
85  let b2 = bufnr()
86  edit +/baz4 Xfile3
87  let b3 = bufnr()
88
89  call assert_fails('buffer ' .. b1 .. ' abc', 'E488:')
90  call assert_equal(b3, bufnr())
91  call assert_equal(4, line('.'))
92  exe 'buffer +/bar2 ' .. b2
93  call assert_equal(b2, bufnr())
94  call assert_equal(2, line('.'))
95  exe 'buffer +/bar1'
96  call assert_equal(b2, bufnr())
97  call assert_equal(1, line('.'))
98
99  brewind +
100  call assert_equal(b1, bufnr())
101  call assert_equal(4, line('.'))
102
103  blast +/baz2
104  call assert_equal(b3, bufnr())
105  call assert_equal(2, line('.'))
106
107  bprevious +/bar4
108  call assert_equal(b2, bufnr())
109  call assert_equal(4, line('.'))
110
111  bnext +/baz3
112  call assert_equal(b3, bufnr())
113  call assert_equal(3, line('.'))
114
115  call assert_fails('bmodified', 'E84:')
116  call setbufvar(b2, '&modified', 1)
117  exe 'bmodified +/bar3'
118  call assert_equal(b2, bufnr())
119  call assert_equal(3, line('.'))
120
121  " With no listed buffers in the list, :bnext and :bprev should fail
122  %bwipe!
123  set nobuflisted
124  call assert_fails('bnext', 'E85:')
125  call assert_fails('bprev', 'E85:')
126  set buflisted
127
128  call assert_fails('sandbox bnext', 'E48:')
129
130  call delete('Xfile1')
131  call delete('Xfile2')
132  call delete('Xfile3')
133  %bwipe!
134endfunc
135
136" Test for :bdelete
137func Test_bdelete_cmd()
138  %bwipe!
139  call assert_fails('bdelete 5', 'E516:')
140  call assert_fails('1,1bdelete 1 2', 'E488:')
141  call assert_fails('bdelete \)', 'E55:')
142
143  " Deleting a unlisted and unloaded buffer
144  edit Xfile1
145  let bnr = bufnr()
146  set nobuflisted
147  enew
148  call assert_fails('bdelete ' .. bnr, 'E516:')
149
150  " Deleting more than one buffer
151  new Xbuf1
152  new Xbuf2
153  exe 'bdel ' .. bufnr('Xbuf2') .. ' ' .. bufnr('Xbuf1')
154  call assert_equal(1, winnr('$'))
155  call assert_equal(0, getbufinfo('Xbuf1')[0].loaded)
156  call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
157
158  " Deleting more than one buffer and an invalid buffer
159  new Xbuf1
160  new Xbuf2
161  let cmd = "exe 'bdel ' .. bufnr('Xbuf2') .. ' xxx ' .. bufnr('Xbuf1')"
162  call assert_fails(cmd, 'E94:')
163  call assert_equal(2, winnr('$'))
164  call assert_equal(1, getbufinfo('Xbuf1')[0].loaded)
165  call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
166
167  %bwipe!
168endfunc
169
170func Test_buffer_error()
171  new foo1
172  new foo2
173
174  call assert_fails('buffer foo', 'E93:')
175  call assert_fails('buffer bar', 'E94:')
176  call assert_fails('buffer 0', 'E939:')
177
178  %bwipe
179endfunc
180
181" Test for the status messages displayed when unloading, deleting or wiping
182" out buffers
183func Test_buffer_statusmsg()
184  CheckEnglish
185  set report=1
186  new Xbuf1
187  new Xbuf2
188  let bnr = bufnr()
189  exe "normal 2\<C-G>"
190  call assert_match('buf ' .. bnr .. ':', v:statusmsg)
191  bunload Xbuf1 Xbuf2
192  call assert_equal('2 buffers unloaded', v:statusmsg)
193  bdel Xbuf1 Xbuf2
194  call assert_equal('2 buffers deleted', v:statusmsg)
195  bwipe Xbuf1 Xbuf2
196  call assert_equal('2 buffers wiped out', v:statusmsg)
197  set report&
198endfunc
199
200" Test for quitting the 'swapfile exists' dialog with the split buffer
201" command.
202func Test_buffer_sbuf_cleanup()
203  call writefile([], 'Xfile')
204  " first open the file in a buffer
205  new Xfile
206  let bnr = bufnr()
207  close
208  " create the swap file
209  call writefile([], '.Xfile.swp')
210  " Remove the catch-all that runtest.vim adds
211  au! SwapExists
212  augroup BufTest
213    au!
214    autocmd SwapExists Xfile let v:swapchoice='q'
215  augroup END
216  exe 'sbuf ' . bnr
217  call assert_equal(1, winnr('$'))
218  call assert_equal(0, getbufinfo('Xfile')[0].loaded)
219
220  " test for :sball
221  sball
222  call assert_equal(1, winnr('$'))
223  call assert_equal(0, getbufinfo('Xfile')[0].loaded)
224
225  %bw!
226  set shortmess+=F
227  let v:statusmsg = ''
228  edit Xfile
229  call assert_equal('', v:statusmsg)
230  call assert_equal(1, winnr('$'))
231  call assert_equal(0, getbufinfo('Xfile')[0].loaded)
232  set shortmess&
233
234  call delete('Xfile')
235  call delete('.Xfile.swp')
236  augroup BufTest
237    au!
238  augroup END
239  augroup! BufTest
240endfunc
241
242" Test for deleting a modified buffer with :confirm
243func Test_bdel_with_confirm()
244  CheckUnix
245  CheckNotGui
246  CheckFeature dialog_con
247  new
248  call setline(1, 'test')
249  call assert_fails('bdel', 'E89:')
250  call feedkeys('c', 'L')
251  confirm bdel
252  call assert_equal(2, winnr('$'))
253  call assert_equal(1, &modified)
254  call feedkeys('n', 'L')
255  confirm bdel
256  call assert_equal(1, winnr('$'))
257endfunc
258
259" Test for editing another buffer from a modified buffer with :confirm
260func Test_goto_buf_with_confirm()
261  CheckUnix
262  CheckNotGui
263  CheckFeature dialog_con
264  new Xfile
265  enew
266  call setline(1, 'test')
267  call assert_fails('b Xfile', 'E37:')
268  call feedkeys('c', 'L')
269  call assert_fails('confirm b Xfile', 'E37:')
270  call assert_equal(1, &modified)
271  call assert_equal('', @%)
272  call feedkeys('y', 'L')
273  call assert_fails('confirm b Xfile', ['', 'E37:'])
274  call assert_equal(1, &modified)
275  call assert_equal('', @%)
276  call feedkeys('n', 'L')
277  confirm b Xfile
278  call assert_equal('Xfile', @%)
279  close!
280endfunc
281
282" Test for splitting buffer with 'switchbuf'
283func Test_buffer_switchbuf()
284  new Xfile
285  wincmd w
286  set switchbuf=useopen
287  sbuf Xfile
288  call assert_equal(1, winnr())
289  call assert_equal(2, winnr('$'))
290  set switchbuf=usetab
291  tabnew
292  sbuf Xfile
293  call assert_equal(1, tabpagenr())
294  call assert_equal(2, tabpagenr('$'))
295  set switchbuf&
296  %bw
297endfunc
298
299" Test for BufAdd autocommand wiping out the buffer
300func Test_bufadd_autocmd_bwipe()
301  %bw!
302  augroup BufAdd_Wipe
303    au!
304    autocmd BufAdd Xfile %bw!
305  augroup END
306  edit Xfile
307  call assert_equal('', @%)
308  call assert_equal(0, bufexists('Xfile'))
309  augroup BufAdd_Wipe
310    au!
311  augroup END
312  augroup! BufAdd_Wipe
313endfunc
314
315" Test for trying to load a buffer with text locked
316" <C-\>e in the command line is used to lock the text
317func Test_load_buf_with_text_locked()
318  new Xfile1
319  edit Xfile2
320  let cmd = ":\<C-\>eexecute(\"normal \<C-O>\")\<CR>\<C-C>"
321  call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
322  %bw!
323endfunc
324
325" Test for using CTRL-^ to edit the alternative file keeping the cursor
326" position with 'nostartofline'. Also test using the 'buf' command.
327func Test_buffer_edit_altfile()
328  call writefile(repeat(['one two'], 50), 'Xfile1')
329  call writefile(repeat(['five six'], 50), 'Xfile2')
330  set nosol
331  edit Xfile1
332  call cursor(25, 5)
333  edit Xfile2
334  call cursor(30, 4)
335  exe "normal \<C-^>"
336  call assert_equal([0, 25, 5, 0], getpos('.'))
337  exe "normal \<C-^>"
338  call assert_equal([0, 30, 4, 0], getpos('.'))
339  buf Xfile1
340  call assert_equal([0, 25, 5, 0], getpos('.'))
341  buf Xfile2
342  call assert_equal([0, 30, 4, 0], getpos('.'))
343  set sol&
344  call delete('Xfile1')
345  call delete('Xfile2')
346endfunc
347
348" Test for running the :sball command with a maximum window count and a
349" modified buffer
350func Test_sball_with_count()
351  %bw!
352  edit Xfile1
353  call setline(1, ['abc'])
354  new Xfile2
355  new Xfile3
356  new Xfile4
357  2sball
358  call assert_equal(bufnr('Xfile4'), winbufnr(1))
359  call assert_equal(bufnr('Xfile1'), winbufnr(2))
360  call assert_equal(0, getbufinfo('Xfile2')[0].loaded)
361  call assert_equal(0, getbufinfo('Xfile3')[0].loaded)
362  %bw!
363endfunc
364
365func Test_badd_options()
366  new SomeNewBuffer
367  setlocal numberwidth=3
368  wincmd p
369  badd +1 SomeNewBuffer
370  new SomeNewBuffer
371  call assert_equal(3, &numberwidth)
372  close
373  close
374  bwipe! SomeNewBuffer
375endfunc
376
377func Test_balt()
378  new SomeNewBuffer
379  balt +3 OtherBuffer
380  e #
381  call assert_equal('OtherBuffer', bufname())
382endfunc
383
384" Test for buffer match URL(scheme) check
385" scheme is alpha and inner hyphen only.
386func Test_buffer_scheme()
387  CheckMSWindows
388
389  set noshellslash
390  %bwipe!
391  let bufnames = [
392    \ #{id: 'b0', name: 'test://xyz/foo/b0'    , match: 1},
393    \ #{id: 'b1', name: 'test+abc://xyz/foo/b1', match: 0},
394    \ #{id: 'b2', name: 'test_abc://xyz/foo/b2', match: 0},
395    \ #{id: 'b3', name: 'test-abc://xyz/foo/b3', match: 1},
396    \ #{id: 'b4', name: '-test://xyz/foo/b4'   , match: 0},
397    \ #{id: 'b5', name: 'test-://xyz/foo/b5'   , match: 0},
398    \]
399  for buf in bufnames
400    new `=buf.name`
401    if buf.match
402      call assert_equal(buf.name,    getbufinfo(buf.id)[0].name)
403    else
404      " slashes will have become backslashes
405      call assert_notequal(buf.name, getbufinfo(buf.id)[0].name)
406    endif
407    bwipe
408  endfor
409
410  set shellslash&
411endfunc
412
413" Test for the 'maxmem' and 'maxmemtot' options
414func Test_buffer_maxmem()
415  " use 1KB per buffer and 2KB for all the buffers
416  set maxmem=1 maxmemtot=2
417  new
418  let v:errmsg = ''
419  " try opening some files
420  edit test_arglist.vim
421  call assert_equal('test_arglist.vim', bufname())
422  edit test_eval_stuff.vim
423  call assert_equal('test_eval_stuff.vim', bufname())
424  b test_arglist.vim
425  call assert_equal('test_arglist.vim', bufname())
426  b test_eval_stuff.vim
427  call assert_equal('test_eval_stuff.vim', bufname())
428  close
429  call assert_equal('', v:errmsg)
430  set maxmem& maxmemtot&
431endfunc
432
433" vim: shiftwidth=2 sts=2 expandtab
434