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