blob: 8ef41e20fb5b0d454e701a31e954ed878b03c87d [file] [log] [blame]
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001" Tests for Vim buffer
2
Bram Moolenaarb7e24832020-06-24 13:37:35 +02003source check.vim
4
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01005" 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')
Bram Moolenaarf0cee192020-02-16 13:33:56 +010066
67 call assert_fails('1,4bunload', 'E16:')
68 call assert_fails(',100bunload', 'E16:')
69
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +020070 call assert_fails('$bunload', 'E90:')
Bram Moolenaarf0cee192020-02-16 13:33:56 +010071endfunc
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
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010099 brewind +
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100100 call assert_equal(b1, bufnr())
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100101 call assert_equal(4, line('.'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100102
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:')
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100140 call assert_fails('1,1bdelete 1 2', 'E488:')
Bram Moolenaar531be472020-09-23 22:38:05 +0200141 call assert_fails('bdelete \)', 'E55:')
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100142
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:')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200149
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
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100167 %bwipe!
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100168endfunc
169
Bram Moolenaar067297e2020-04-13 19:55:50 +0200170func 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
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200181" 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')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200273 call assert_fails('confirm b Xfile', ['', 'E37:'])
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200274 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
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100365" vim: shiftwidth=2 sts=2 expandtab