blob: d6d668799f212196616280752a28e52cf27279f5 [file] [log] [blame]
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001" Tests for Vim buffer
2
3" Test for the :bunload command with an offset
4func Test_bunload_with_offset()
5 %bwipe!
Bram Moolenaar34112652022-09-05 21:40:44 +01006 call writefile(['B1'], 'Xb1', 'D')
7 call writefile(['B2'], 'Xb2', 'D')
8 call writefile(['B3'], 'Xb3', 'D')
9 call writefile(['B4'], 'Xb4', 'D')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010010
11 " Load four buffers. Unload the second and third buffers and then
12 " execute .+3bunload to unload the last buffer.
Bram Moolenaar34112652022-09-05 21:40:44 +010013 edit Xb1
14 new Xb2
15 new Xb3
16 new Xb4
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010017
Bram Moolenaar34112652022-09-05 21:40:44 +010018 bunload Xb2
19 bunload Xb3
20 exe bufwinnr('Xb1') . 'wincmd w'
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010021 .+3bunload
Bram Moolenaar34112652022-09-05 21:40:44 +010022 call assert_equal(0, getbufinfo('Xb4')[0].loaded)
23 call assert_equal('Xb1',
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010024 \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
25
26 " Load four buffers. Unload the third and fourth buffers. Execute .+3bunload
27 " and check whether the second buffer is unloaded.
28 ball
Bram Moolenaar34112652022-09-05 21:40:44 +010029 bunload Xb3
30 bunload Xb4
31 exe bufwinnr('Xb1') . 'wincmd w'
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010032 .+3bunload
Bram Moolenaar34112652022-09-05 21:40:44 +010033 call assert_equal(0, getbufinfo('Xb2')[0].loaded)
34 call assert_equal('Xb1',
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010035 \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
36
37 " Load four buffers. Unload the second and third buffers and from the last
38 " buffer execute .-3bunload to unload the first buffer.
39 ball
Bram Moolenaar34112652022-09-05 21:40:44 +010040 bunload Xb2
41 bunload Xb3
42 exe bufwinnr('Xb4') . 'wincmd w'
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010043 .-3bunload
Bram Moolenaar34112652022-09-05 21:40:44 +010044 call assert_equal(0, getbufinfo('Xb1')[0].loaded)
45 call assert_equal('Xb4',
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010046 \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
47
48 " Load four buffers. Unload the first and second buffers. Execute .-3bunload
49 " from the last buffer and check whether the third buffer is unloaded.
50 ball
Bram Moolenaar34112652022-09-05 21:40:44 +010051 bunload Xb1
52 bunload Xb2
53 exe bufwinnr('Xb4') . 'wincmd w'
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010054 .-3bunload
Bram Moolenaar34112652022-09-05 21:40:44 +010055 call assert_equal(0, getbufinfo('Xb3')[0].loaded)
56 call assert_equal('Xb4',
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010057 \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
58
59 %bwipe!
Bram Moolenaarf0cee192020-02-16 13:33:56 +010060
61 call assert_fails('1,4bunload', 'E16:')
62 call assert_fails(',100bunload', 'E16:')
63
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +020064 call assert_fails('$bunload', 'E90:')
Bram Moolenaarf0cee192020-02-16 13:33:56 +010065endfunc
66
67" Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified
68" commands
69func Test_buflist_browse()
70 %bwipe!
71 call assert_fails('buffer 1000', 'E86:')
72
Bram Moolenaar34112652022-09-05 21:40:44 +010073 call writefile(['foo1', 'foo2', 'foo3', 'foo4'], 'Xbrowse1', 'D')
74 call writefile(['bar1', 'bar2', 'bar3', 'bar4'], 'Xbrowse2', 'D')
75 call writefile(['baz1', 'baz2', 'baz3', 'baz4'], 'Xbrowse3', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010076 edit Xbrowse1
Bram Moolenaarf0cee192020-02-16 13:33:56 +010077 let b1 = bufnr()
Bram Moolenaare7cda972022-08-29 11:02:59 +010078 edit Xbrowse2
Bram Moolenaarf0cee192020-02-16 13:33:56 +010079 let b2 = bufnr()
Bram Moolenaare7cda972022-08-29 11:02:59 +010080 edit +/baz4 Xbrowse3
Bram Moolenaarf0cee192020-02-16 13:33:56 +010081 let b3 = bufnr()
82
83 call assert_fails('buffer ' .. b1 .. ' abc', 'E488:')
84 call assert_equal(b3, bufnr())
85 call assert_equal(4, line('.'))
86 exe 'buffer +/bar2 ' .. b2
87 call assert_equal(b2, bufnr())
88 call assert_equal(2, line('.'))
89 exe 'buffer +/bar1'
90 call assert_equal(b2, bufnr())
91 call assert_equal(1, line('.'))
92
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010093 brewind +
Bram Moolenaarf0cee192020-02-16 13:33:56 +010094 call assert_equal(b1, bufnr())
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010095 call assert_equal(4, line('.'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +010096
97 blast +/baz2
98 call assert_equal(b3, bufnr())
99 call assert_equal(2, line('.'))
100
101 bprevious +/bar4
102 call assert_equal(b2, bufnr())
103 call assert_equal(4, line('.'))
104
105 bnext +/baz3
106 call assert_equal(b3, bufnr())
107 call assert_equal(3, line('.'))
108
109 call assert_fails('bmodified', 'E84:')
110 call setbufvar(b2, '&modified', 1)
111 exe 'bmodified +/bar3'
112 call assert_equal(b2, bufnr())
113 call assert_equal(3, line('.'))
114
115 " With no listed buffers in the list, :bnext and :bprev should fail
116 %bwipe!
117 set nobuflisted
118 call assert_fails('bnext', 'E85:')
119 call assert_fails('bprev', 'E85:')
120 set buflisted
121
122 call assert_fails('sandbox bnext', 'E48:')
123
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100124 %bwipe!
125endfunc
126
LemonBoy893eeeb2024-07-10 20:20:48 +0200127" Test for :bnext and :bprev when called from help and non-help buffers.
128func Test_bnext_bprev_help()
129 %bwipe!
130
131 e XHelp1 | set bt=help
132 let b1 = bufnr()
133 e Xbuf1
134 let b2 = bufnr()
135
136 " There's only one buffer of each type.
137 b XHelp1
138 bnext | call assert_equal(b1, bufnr())
139 bprev | call assert_equal(b1, bufnr())
140 b Xbuf1
141 bnext | call assert_equal(b2, bufnr())
142 bprev | call assert_equal(b2, bufnr())
143
144 " Add one more buffer of each type.
145 e XHelp2 | set bt=help
146 let b3 = bufnr()
147 e Xbuf2
148 let b4 = bufnr()
149
150 " Help buffer jumps to help buffer.
151 b XHelp1
152 bnext | call assert_equal(b3, bufnr())
153 bnext | call assert_equal(b1, bufnr())
154 bprev | call assert_equal(b3, bufnr())
155 bprev | call assert_equal(b1, bufnr())
156
157 " Regular buffer jumps to regular buffer.
158 b Xbuf1
159 bnext | call assert_equal(b4, bufnr())
160 bnext | call assert_equal(b2, bufnr())
161 bprev | call assert_equal(b4, bufnr())
162 bprev | call assert_equal(b2, bufnr())
163
164 " :brewind and :blast are not affected by the buffer type.
165 b Xbuf2
166 brewind | call assert_equal(b1, bufnr())
167 b XHelp1
168 blast | call assert_equal(b4, bufnr())
169
170 %bwipe!
171endfunc
172
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100173" Test for :bdelete
174func Test_bdelete_cmd()
175 %bwipe!
176 call assert_fails('bdelete 5', 'E516:')
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100177 call assert_fails('1,1bdelete 1 2', 'E488:')
Bram Moolenaar531be472020-09-23 22:38:05 +0200178 call assert_fails('bdelete \)', 'E55:')
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100179
zeertzjqc029c132024-03-28 11:37:26 +0100180 " Deleting an unlisted and unloaded buffer
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100181 edit Xbdelfile1
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100182 let bnr = bufnr()
183 set nobuflisted
184 enew
185 call assert_fails('bdelete ' .. bnr, 'E516:')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200186
187 " Deleting more than one buffer
188 new Xbuf1
189 new Xbuf2
190 exe 'bdel ' .. bufnr('Xbuf2') .. ' ' .. bufnr('Xbuf1')
191 call assert_equal(1, winnr('$'))
192 call assert_equal(0, getbufinfo('Xbuf1')[0].loaded)
193 call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
194
195 " Deleting more than one buffer and an invalid buffer
196 new Xbuf1
197 new Xbuf2
198 let cmd = "exe 'bdel ' .. bufnr('Xbuf2') .. ' xxx ' .. bufnr('Xbuf1')"
199 call assert_fails(cmd, 'E94:')
200 call assert_equal(2, winnr('$'))
201 call assert_equal(1, getbufinfo('Xbuf1')[0].loaded)
202 call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
203
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100204 %bwipe!
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100205endfunc
206
Bram Moolenaar067297e2020-04-13 19:55:50 +0200207func Test_buffer_error()
208 new foo1
209 new foo2
210
211 call assert_fails('buffer foo', 'E93:')
212 call assert_fails('buffer bar', 'E94:')
213 call assert_fails('buffer 0', 'E939:')
214
215 %bwipe
216endfunc
217
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200218" Test for the status messages displayed when unloading, deleting or wiping
219" out buffers
220func Test_buffer_statusmsg()
221 CheckEnglish
222 set report=1
223 new Xbuf1
224 new Xbuf2
225 let bnr = bufnr()
226 exe "normal 2\<C-G>"
227 call assert_match('buf ' .. bnr .. ':', v:statusmsg)
228 bunload Xbuf1 Xbuf2
229 call assert_equal('2 buffers unloaded', v:statusmsg)
230 bdel Xbuf1 Xbuf2
231 call assert_equal('2 buffers deleted', v:statusmsg)
232 bwipe Xbuf1 Xbuf2
233 call assert_equal('2 buffers wiped out', v:statusmsg)
234 set report&
235endfunc
236
237" Test for quitting the 'swapfile exists' dialog with the split buffer
238" command.
239func Test_buffer_sbuf_cleanup()
Bram Moolenaar34112652022-09-05 21:40:44 +0100240 call writefile([], 'XsplitCleanup', 'D')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200241 " first open the file in a buffer
Bram Moolenaare7cda972022-08-29 11:02:59 +0100242 new XsplitCleanup
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200243 let bnr = bufnr()
244 close
245 " create the swap file
Bram Moolenaar34112652022-09-05 21:40:44 +0100246 call writefile([], '.XsplitCleanup.swp', 'D')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200247 " Remove the catch-all that runtest.vim adds
248 au! SwapExists
249 augroup BufTest
250 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +0100251 autocmd SwapExists XsplitCleanup let v:swapchoice='q'
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200252 augroup END
253 exe 'sbuf ' . bnr
254 call assert_equal(1, winnr('$'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100255 call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200256
257 " test for :sball
258 sball
259 call assert_equal(1, winnr('$'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100260 call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200261
262 %bw!
263 set shortmess+=F
264 let v:statusmsg = ''
Bram Moolenaare7cda972022-08-29 11:02:59 +0100265 edit XsplitCleanup
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200266 call assert_equal('', v:statusmsg)
267 call assert_equal(1, winnr('$'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100268 call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200269 set shortmess&
270
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200271 augroup BufTest
272 au!
273 augroup END
274 augroup! BufTest
275endfunc
276
277" Test for deleting a modified buffer with :confirm
278func Test_bdel_with_confirm()
279 CheckUnix
280 CheckNotGui
281 CheckFeature dialog_con
282 new
283 call setline(1, 'test')
284 call assert_fails('bdel', 'E89:')
285 call feedkeys('c', 'L')
286 confirm bdel
287 call assert_equal(2, winnr('$'))
288 call assert_equal(1, &modified)
289 call feedkeys('n', 'L')
290 confirm bdel
291 call assert_equal(1, winnr('$'))
292endfunc
293
294" Test for editing another buffer from a modified buffer with :confirm
295func Test_goto_buf_with_confirm()
296 CheckUnix
297 CheckNotGui
298 CheckFeature dialog_con
glepnirdf461152024-04-04 22:23:29 +0200299 " When dialog_con_gui is defined, Vim is compiled with GUI support
300 " and FEAT_BROWSE will be defined, which causes :confirm :b to
301 " call do_browse(), which will try to use a GUI file browser,
302 " which aborts if a GUI is not available.
303 CheckNotFeature dialog_con_gui
Bram Moolenaare7cda972022-08-29 11:02:59 +0100304 new XgotoConf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200305 enew
306 call setline(1, 'test')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100307 call assert_fails('b XgotoConf', 'E37:')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200308 call feedkeys('c', 'L')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100309 call assert_fails('confirm b XgotoConf', 'E37:')
glepnirdf461152024-04-04 22:23:29 +0200310 call assert_true(&modified)
311 call assert_true(empty(bufname('%')))
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200312 call feedkeys('y', 'L')
glepnirdf461152024-04-04 22:23:29 +0200313 confirm b XgotoConf
314 call assert_equal('XgotoConf', bufname('%'))
315 call assert_equal(['test'], readfile('Untitled'))
316 e Untitled
317 call setline(2, 'test2')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200318 call feedkeys('n', 'L')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100319 confirm b XgotoConf
glepnirdf461152024-04-04 22:23:29 +0200320 call assert_equal('XgotoConf', bufname('%'))
321 call assert_equal(['test'], readfile('Untitled'))
322 call delete('Untitled')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200323 close!
324endfunc
325
326" Test for splitting buffer with 'switchbuf'
327func Test_buffer_switchbuf()
Bram Moolenaare7cda972022-08-29 11:02:59 +0100328 new Xswitchbuf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200329 wincmd w
330 set switchbuf=useopen
Bram Moolenaare7cda972022-08-29 11:02:59 +0100331 sbuf Xswitchbuf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200332 call assert_equal(1, winnr())
333 call assert_equal(2, winnr('$'))
334 set switchbuf=usetab
335 tabnew
Bram Moolenaare7cda972022-08-29 11:02:59 +0100336 sbuf Xswitchbuf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200337 call assert_equal(1, tabpagenr())
338 call assert_equal(2, tabpagenr('$'))
339 set switchbuf&
340 %bw
341endfunc
342
343" Test for BufAdd autocommand wiping out the buffer
344func Test_bufadd_autocmd_bwipe()
345 %bw!
346 augroup BufAdd_Wipe
347 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +0100348 autocmd BufAdd Xbwipe %bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200349 augroup END
Bram Moolenaare7cda972022-08-29 11:02:59 +0100350 edit Xbwipe
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200351 call assert_equal('', @%)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100352 call assert_equal(0, bufexists('Xbwipe'))
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200353 augroup BufAdd_Wipe
354 au!
355 augroup END
356 augroup! BufAdd_Wipe
357endfunc
358
359" Test for trying to load a buffer with text locked
360" <C-\>e in the command line is used to lock the text
361func Test_load_buf_with_text_locked()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100362 new Xlockfile1
363 edit Xlockfile2
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200364 let cmd = ":\<C-\>eexecute(\"normal \<C-O>\")\<CR>\<C-C>"
365 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
366 %bw!
367endfunc
368
369" Test for using CTRL-^ to edit the alternative file keeping the cursor
370" position with 'nostartofline'. Also test using the 'buf' command.
371func Test_buffer_edit_altfile()
Bram Moolenaar34112652022-09-05 21:40:44 +0100372 call writefile(repeat(['one two'], 50), 'Xaltfile1', 'D')
373 call writefile(repeat(['five six'], 50), 'Xaltfile2', 'D')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200374 set nosol
Bram Moolenaare7cda972022-08-29 11:02:59 +0100375 edit Xaltfile1
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200376 call cursor(25, 5)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100377 edit Xaltfile2
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200378 call cursor(30, 4)
379 exe "normal \<C-^>"
380 call assert_equal([0, 25, 5, 0], getpos('.'))
381 exe "normal \<C-^>"
382 call assert_equal([0, 30, 4, 0], getpos('.'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100383 buf Xaltfile1
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200384 call assert_equal([0, 25, 5, 0], getpos('.'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100385 buf Xaltfile2
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200386 call assert_equal([0, 30, 4, 0], getpos('.'))
387 set sol&
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200388endfunc
389
390" Test for running the :sball command with a maximum window count and a
391" modified buffer
392func Test_sball_with_count()
393 %bw!
Bram Moolenaare7cda972022-08-29 11:02:59 +0100394 edit Xcountfile1
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200395 call setline(1, ['abc'])
Bram Moolenaare7cda972022-08-29 11:02:59 +0100396 new Xcountfile2
397 new Xcountfile3
398 new Xcountfile4
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200399 2sball
Bram Moolenaare7cda972022-08-29 11:02:59 +0100400 call assert_equal(bufnr('Xcountfile4'), winbufnr(1))
401 call assert_equal(bufnr('Xcountfile1'), winbufnr(2))
402 call assert_equal(0, getbufinfo('Xcountfile2')[0].loaded)
403 call assert_equal(0, getbufinfo('Xcountfile3')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200404 %bw!
405endfunc
406
Bram Moolenaare974fa72020-10-25 15:02:51 +0100407func Test_badd_options()
408 new SomeNewBuffer
Bram Moolenaar37e4e032020-10-25 16:18:26 +0100409 setlocal numberwidth=3
Bram Moolenaare974fa72020-10-25 15:02:51 +0100410 wincmd p
Bram Moolenaar89b693e2020-10-25 17:09:50 +0100411 badd +1 SomeNewBuffer
Bram Moolenaare974fa72020-10-25 15:02:51 +0100412 new SomeNewBuffer
Bram Moolenaar37e4e032020-10-25 16:18:26 +0100413 call assert_equal(3, &numberwidth)
Bram Moolenaare974fa72020-10-25 15:02:51 +0100414 close
415 close
416 bwipe! SomeNewBuffer
Bram Moolenaara2b91032022-09-19 18:20:08 +0100417
418 badd +3 XbaddFile
419 call writefile(range(6), 'XbaddFile', 'D')
420 buf XbaddFile
421 call assert_equal([0, 3, 1, 0], getpos('.'))
422
423 bwipe! XbaddFile
Bram Moolenaare974fa72020-10-25 15:02:51 +0100424endfunc
425
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100426func Test_balt()
427 new SomeNewBuffer
428 balt +3 OtherBuffer
429 e #
430 call assert_equal('OtherBuffer', bufname())
431endfunc
432
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200433" Test for buffer match URL(scheme) check
434" scheme is alpha and inner hyphen only.
435func Test_buffer_scheme()
436 CheckMSWindows
437
K.Takata0500e872022-09-08 12:28:02 +0100438 set noswapfile
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200439 set noshellslash
440 %bwipe!
441 let bufnames = [
Bram Moolenaarf8bc0ce2021-12-02 12:30:22 +0000442 \ #{id: 'ssb0', name: 'test://xyz/foo/ssb0' , match: 1},
443 \ #{id: 'ssb1', name: 'test+abc://xyz/foo/ssb1', match: 0},
444 \ #{id: 'ssb2', name: 'test_abc://xyz/foo/ssb2', match: 0},
445 \ #{id: 'ssb3', name: 'test-abc://xyz/foo/ssb3', match: 1},
446 \ #{id: 'ssb4', name: '-test://xyz/foo/ssb4' , match: 0},
447 \ #{id: 'ssb5', name: 'test-://xyz/foo/ssb5' , match: 0},
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200448 \]
449 for buf in bufnames
450 new `=buf.name`
451 if buf.match
452 call assert_equal(buf.name, getbufinfo(buf.id)[0].name)
453 else
454 " slashes will have become backslashes
455 call assert_notequal(buf.name, getbufinfo(buf.id)[0].name)
456 endif
457 bwipe
458 endfor
459
460 set shellslash&
K.Takata0500e872022-09-08 12:28:02 +0100461 set swapfile&
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200462endfunc
463
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +0100464" this was using a NULL pointer after failing to use the pattern
465func Test_buf_pattern_invalid()
466 vsplit 0000000
467 silent! buf [0--]\&\zs*\zs*e
468 bwipe!
Bram Moolenaara59f2df2022-05-11 11:42:28 +0100469
470 vsplit 00000000000000000000000000
471 silent! buf [0--]\&\zs*\zs*e
472 bwipe!
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +0100473
474 " similar case with different code path
475 split 0
476 edit ÿ
477 silent! buf [0--]\&\zs*\zs*0
478 bwipe!
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +0100479endfunc
480
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +0200481" Test for the 'maxmem' and 'maxmemtot' options
482func Test_buffer_maxmem()
483 " use 1KB per buffer and 2KB for all the buffers
484 set maxmem=1 maxmemtot=2
485 new
486 let v:errmsg = ''
487 " try opening some files
488 edit test_arglist.vim
489 call assert_equal('test_arglist.vim', bufname())
490 edit test_eval_stuff.vim
491 call assert_equal('test_eval_stuff.vim', bufname())
492 b test_arglist.vim
493 call assert_equal('test_arglist.vim', bufname())
494 b test_eval_stuff.vim
495 call assert_equal('test_eval_stuff.vim', bufname())
496 close
497 call assert_equal('', v:errmsg)
498 set maxmem& maxmemtot&
499endfunc
500
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100501" Test for buffer allocation failure
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100502func Test_buflist_alloc_failure()
503 %bw!
504
Bram Moolenaare7cda972022-08-29 11:02:59 +0100505 edit XallocFail1
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100506 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100507 call assert_fails('edit XallocFail2', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100508
509 " test for bufadd()
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100510 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100511 call assert_fails('call bufadd("Xbuffer")', 'E342:')
512
513 " test for setting the arglist
Bram Moolenaare7cda972022-08-29 11:02:59 +0100514 edit XallocFail2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100515 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100516 call assert_fails('next XallocFail3', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100517
518 " test for setting the alternate buffer name when writing a file
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100519 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100520 call assert_fails('write Xother', 'E342:')
521 call delete('Xother')
522
523 " test for creating a buffer using bufnr()
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100524 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100525 call assert_fails("call bufnr('Xnewbuf', v:true)", 'E342:')
526
527 " test for renaming buffer using :file
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100528 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100529 call assert_fails('file Xnewfile', 'E342:')
530
531 " test for creating a buffer for a popup window
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100532 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100533 call assert_fails('call popup_create("mypop", {})', 'E342:')
534
535 if has('terminal')
536 " test for creating a buffer for a terminal window
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100537 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100538 call assert_fails('call term_start(&shell)', 'E342:')
539 %bw!
540 endif
541
542 " test for loading a new buffer after wiping out all the buffers
Bram Moolenaare7cda972022-08-29 11:02:59 +0100543 edit XallocFail4
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100544 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100545 call assert_fails('%bw!', 'E342:')
546
547 " test for :checktime loading the buffer
Bram Moolenaar34112652022-09-05 21:40:44 +0100548 call writefile(['one'], 'XallocFail5', 'D')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100549 if has('unix')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100550 edit XallocFail5
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100551 " sleep for some time to make sure the timestamp is different
552 sleep 200m
Bram Moolenaare7cda972022-08-29 11:02:59 +0100553 call writefile(['two'], 'XallocFail5')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100554 set autoread
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100555 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100556 call assert_fails('checktime', 'E342:')
557 set autoread&
558 bw!
559 endif
560
561 " test for :vimgrep loading a dummy buffer
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100562 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100563 call assert_fails('vimgrep two XallocFail5', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100564
565 " test for quickfix command loading a buffer
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100566 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100567 call assert_fails('cexpr "XallocFail6:10:Line10"', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100568endfunc
569
Sean Dewar6cb1c822025-05-03 18:37:27 +0200570func Test_closed_buffer_still_in_window()
571 %bw!
572
573 let s:w = win_getid()
574 new
575 let s:b = bufnr()
576 setl bufhidden=wipe
577
578 augroup ViewClosedBuffer
579 autocmd!
580 autocmd BufUnload * ++once call assert_fails(
581 \ 'call win_execute(s:w, "' .. s:b .. 'b")', 'E1546:')
582 augroup END
583 quit!
584 " Previously resulted in s:b being curbuf while unloaded (no memfile).
585 call assert_equal(1, bufloaded(bufnr()))
586 call assert_equal(0, bufexists(s:b))
587
588 let s:w = win_getid()
589 split
590 new
591 let s:b = bufnr()
592
593 augroup ViewClosedBuffer
594 autocmd!
595 autocmd BufWipeout * ++once call win_gotoid(s:w)
596 \| call assert_fails(s:b .. 'b', 'E1546:') | wincmd p
597 augroup END
598 bw! " Close only this buffer first; used to be a heap UAF.
599
600 unlet! s:w s:b
601 autocmd! ViewClosedBuffer
602 %bw!
603endfunc
604
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100605" vim: shiftwidth=2 sts=2 expandtab