blob: 757ba05784f79e72c50f0fa0e4f61e5bb7668f12 [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!
Bram Moolenaar34112652022-09-05 21:40:44 +01008 call writefile(['B1'], 'Xb1', 'D')
9 call writefile(['B2'], 'Xb2', 'D')
10 call writefile(['B3'], 'Xb3', 'D')
11 call writefile(['B4'], 'Xb4', 'D')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010012
13 " Load four buffers. Unload the second and third buffers and then
14 " execute .+3bunload to unload the last buffer.
Bram Moolenaar34112652022-09-05 21:40:44 +010015 edit Xb1
16 new Xb2
17 new Xb3
18 new Xb4
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010019
Bram Moolenaar34112652022-09-05 21:40:44 +010020 bunload Xb2
21 bunload Xb3
22 exe bufwinnr('Xb1') . 'wincmd w'
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010023 .+3bunload
Bram Moolenaar34112652022-09-05 21:40:44 +010024 call assert_equal(0, getbufinfo('Xb4')[0].loaded)
25 call assert_equal('Xb1',
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010026 \ 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
Bram Moolenaar34112652022-09-05 21:40:44 +010031 bunload Xb3
32 bunload Xb4
33 exe bufwinnr('Xb1') . 'wincmd w'
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010034 .+3bunload
Bram Moolenaar34112652022-09-05 21:40:44 +010035 call assert_equal(0, getbufinfo('Xb2')[0].loaded)
36 call assert_equal('Xb1',
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010037 \ 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
Bram Moolenaar34112652022-09-05 21:40:44 +010042 bunload Xb2
43 bunload Xb3
44 exe bufwinnr('Xb4') . 'wincmd w'
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010045 .-3bunload
Bram Moolenaar34112652022-09-05 21:40:44 +010046 call assert_equal(0, getbufinfo('Xb1')[0].loaded)
47 call assert_equal('Xb4',
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010048 \ 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
Bram Moolenaar34112652022-09-05 21:40:44 +010053 bunload Xb1
54 bunload Xb2
55 exe bufwinnr('Xb4') . 'wincmd w'
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010056 .-3bunload
Bram Moolenaar34112652022-09-05 21:40:44 +010057 call assert_equal(0, getbufinfo('Xb3')[0].loaded)
58 call assert_equal('Xb4',
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010059 \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
60
61 %bwipe!
Bram Moolenaarf0cee192020-02-16 13:33:56 +010062
63 call assert_fails('1,4bunload', 'E16:')
64 call assert_fails(',100bunload', 'E16:')
65
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +020066 call assert_fails('$bunload', 'E90:')
Bram Moolenaarf0cee192020-02-16 13:33:56 +010067endfunc
68
69" Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified
70" commands
71func Test_buflist_browse()
72 %bwipe!
73 call assert_fails('buffer 1000', 'E86:')
74
Bram Moolenaar34112652022-09-05 21:40:44 +010075 call writefile(['foo1', 'foo2', 'foo3', 'foo4'], 'Xbrowse1', 'D')
76 call writefile(['bar1', 'bar2', 'bar3', 'bar4'], 'Xbrowse2', 'D')
77 call writefile(['baz1', 'baz2', 'baz3', 'baz4'], 'Xbrowse3', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010078 edit Xbrowse1
Bram Moolenaarf0cee192020-02-16 13:33:56 +010079 let b1 = bufnr()
Bram Moolenaare7cda972022-08-29 11:02:59 +010080 edit Xbrowse2
Bram Moolenaarf0cee192020-02-16 13:33:56 +010081 let b2 = bufnr()
Bram Moolenaare7cda972022-08-29 11:02:59 +010082 edit +/baz4 Xbrowse3
Bram Moolenaarf0cee192020-02-16 13:33:56 +010083 let b3 = bufnr()
84
85 call assert_fails('buffer ' .. b1 .. ' abc', 'E488:')
86 call assert_equal(b3, bufnr())
87 call assert_equal(4, line('.'))
88 exe 'buffer +/bar2 ' .. b2
89 call assert_equal(b2, bufnr())
90 call assert_equal(2, line('.'))
91 exe 'buffer +/bar1'
92 call assert_equal(b2, bufnr())
93 call assert_equal(1, line('.'))
94
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010095 brewind +
Bram Moolenaarf0cee192020-02-16 13:33:56 +010096 call assert_equal(b1, bufnr())
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010097 call assert_equal(4, line('.'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +010098
99 blast +/baz2
100 call assert_equal(b3, bufnr())
101 call assert_equal(2, line('.'))
102
103 bprevious +/bar4
104 call assert_equal(b2, bufnr())
105 call assert_equal(4, line('.'))
106
107 bnext +/baz3
108 call assert_equal(b3, bufnr())
109 call assert_equal(3, line('.'))
110
111 call assert_fails('bmodified', 'E84:')
112 call setbufvar(b2, '&modified', 1)
113 exe 'bmodified +/bar3'
114 call assert_equal(b2, bufnr())
115 call assert_equal(3, line('.'))
116
117 " With no listed buffers in the list, :bnext and :bprev should fail
118 %bwipe!
119 set nobuflisted
120 call assert_fails('bnext', 'E85:')
121 call assert_fails('bprev', 'E85:')
122 set buflisted
123
124 call assert_fails('sandbox bnext', 'E48:')
125
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100126 %bwipe!
127endfunc
128
LemonBoy893eeeb2024-07-10 20:20:48 +0200129" Test for :bnext and :bprev when called from help and non-help buffers.
130func Test_bnext_bprev_help()
131 %bwipe!
132
133 e XHelp1 | set bt=help
134 let b1 = bufnr()
135 e Xbuf1
136 let b2 = bufnr()
137
138 " There's only one buffer of each type.
139 b XHelp1
140 bnext | call assert_equal(b1, bufnr())
141 bprev | call assert_equal(b1, bufnr())
142 b Xbuf1
143 bnext | call assert_equal(b2, bufnr())
144 bprev | call assert_equal(b2, bufnr())
145
146 " Add one more buffer of each type.
147 e XHelp2 | set bt=help
148 let b3 = bufnr()
149 e Xbuf2
150 let b4 = bufnr()
151
152 " Help buffer jumps to help buffer.
153 b XHelp1
154 bnext | call assert_equal(b3, bufnr())
155 bnext | call assert_equal(b1, bufnr())
156 bprev | call assert_equal(b3, bufnr())
157 bprev | call assert_equal(b1, bufnr())
158
159 " Regular buffer jumps to regular buffer.
160 b Xbuf1
161 bnext | call assert_equal(b4, bufnr())
162 bnext | call assert_equal(b2, bufnr())
163 bprev | call assert_equal(b4, bufnr())
164 bprev | call assert_equal(b2, bufnr())
165
166 " :brewind and :blast are not affected by the buffer type.
167 b Xbuf2
168 brewind | call assert_equal(b1, bufnr())
169 b XHelp1
170 blast | call assert_equal(b4, bufnr())
171
172 %bwipe!
173endfunc
174
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100175" Test for :bdelete
176func Test_bdelete_cmd()
177 %bwipe!
178 call assert_fails('bdelete 5', 'E516:')
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100179 call assert_fails('1,1bdelete 1 2', 'E488:')
Bram Moolenaar531be472020-09-23 22:38:05 +0200180 call assert_fails('bdelete \)', 'E55:')
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100181
zeertzjqc029c132024-03-28 11:37:26 +0100182 " Deleting an unlisted and unloaded buffer
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100183 edit Xbdelfile1
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100184 let bnr = bufnr()
185 set nobuflisted
186 enew
187 call assert_fails('bdelete ' .. bnr, 'E516:')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200188
189 " Deleting more than one buffer
190 new Xbuf1
191 new Xbuf2
192 exe 'bdel ' .. bufnr('Xbuf2') .. ' ' .. bufnr('Xbuf1')
193 call assert_equal(1, winnr('$'))
194 call assert_equal(0, getbufinfo('Xbuf1')[0].loaded)
195 call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
196
197 " Deleting more than one buffer and an invalid buffer
198 new Xbuf1
199 new Xbuf2
200 let cmd = "exe 'bdel ' .. bufnr('Xbuf2') .. ' xxx ' .. bufnr('Xbuf1')"
201 call assert_fails(cmd, 'E94:')
202 call assert_equal(2, winnr('$'))
203 call assert_equal(1, getbufinfo('Xbuf1')[0].loaded)
204 call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
205
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100206 %bwipe!
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100207endfunc
208
Bram Moolenaar067297e2020-04-13 19:55:50 +0200209func Test_buffer_error()
210 new foo1
211 new foo2
212
213 call assert_fails('buffer foo', 'E93:')
214 call assert_fails('buffer bar', 'E94:')
215 call assert_fails('buffer 0', 'E939:')
216
217 %bwipe
218endfunc
219
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200220" Test for the status messages displayed when unloading, deleting or wiping
221" out buffers
222func Test_buffer_statusmsg()
223 CheckEnglish
224 set report=1
225 new Xbuf1
226 new Xbuf2
227 let bnr = bufnr()
228 exe "normal 2\<C-G>"
229 call assert_match('buf ' .. bnr .. ':', v:statusmsg)
230 bunload Xbuf1 Xbuf2
231 call assert_equal('2 buffers unloaded', v:statusmsg)
232 bdel Xbuf1 Xbuf2
233 call assert_equal('2 buffers deleted', v:statusmsg)
234 bwipe Xbuf1 Xbuf2
235 call assert_equal('2 buffers wiped out', v:statusmsg)
236 set report&
237endfunc
238
239" Test for quitting the 'swapfile exists' dialog with the split buffer
240" command.
241func Test_buffer_sbuf_cleanup()
Bram Moolenaar34112652022-09-05 21:40:44 +0100242 call writefile([], 'XsplitCleanup', 'D')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200243 " first open the file in a buffer
Bram Moolenaare7cda972022-08-29 11:02:59 +0100244 new XsplitCleanup
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200245 let bnr = bufnr()
246 close
247 " create the swap file
Bram Moolenaar34112652022-09-05 21:40:44 +0100248 call writefile([], '.XsplitCleanup.swp', 'D')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200249 " Remove the catch-all that runtest.vim adds
250 au! SwapExists
251 augroup BufTest
252 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +0100253 autocmd SwapExists XsplitCleanup let v:swapchoice='q'
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200254 augroup END
255 exe 'sbuf ' . bnr
256 call assert_equal(1, winnr('$'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100257 call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200258
259 " test for :sball
260 sball
261 call assert_equal(1, winnr('$'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100262 call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200263
264 %bw!
265 set shortmess+=F
266 let v:statusmsg = ''
Bram Moolenaare7cda972022-08-29 11:02:59 +0100267 edit XsplitCleanup
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200268 call assert_equal('', v:statusmsg)
269 call assert_equal(1, winnr('$'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100270 call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200271 set shortmess&
272
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200273 augroup BufTest
274 au!
275 augroup END
276 augroup! BufTest
277endfunc
278
279" Test for deleting a modified buffer with :confirm
280func Test_bdel_with_confirm()
281 CheckUnix
282 CheckNotGui
283 CheckFeature dialog_con
284 new
285 call setline(1, 'test')
286 call assert_fails('bdel', 'E89:')
287 call feedkeys('c', 'L')
288 confirm bdel
289 call assert_equal(2, winnr('$'))
290 call assert_equal(1, &modified)
291 call feedkeys('n', 'L')
292 confirm bdel
293 call assert_equal(1, winnr('$'))
294endfunc
295
296" Test for editing another buffer from a modified buffer with :confirm
297func Test_goto_buf_with_confirm()
298 CheckUnix
299 CheckNotGui
300 CheckFeature dialog_con
glepnirdf461152024-04-04 22:23:29 +0200301 " When dialog_con_gui is defined, Vim is compiled with GUI support
302 " and FEAT_BROWSE will be defined, which causes :confirm :b to
303 " call do_browse(), which will try to use a GUI file browser,
304 " which aborts if a GUI is not available.
305 CheckNotFeature dialog_con_gui
Bram Moolenaare7cda972022-08-29 11:02:59 +0100306 new XgotoConf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200307 enew
308 call setline(1, 'test')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100309 call assert_fails('b XgotoConf', 'E37:')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200310 call feedkeys('c', 'L')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100311 call assert_fails('confirm b XgotoConf', 'E37:')
glepnirdf461152024-04-04 22:23:29 +0200312 call assert_true(&modified)
313 call assert_true(empty(bufname('%')))
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200314 call feedkeys('y', 'L')
glepnirdf461152024-04-04 22:23:29 +0200315 confirm b XgotoConf
316 call assert_equal('XgotoConf', bufname('%'))
317 call assert_equal(['test'], readfile('Untitled'))
318 e Untitled
319 call setline(2, 'test2')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200320 call feedkeys('n', 'L')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100321 confirm b XgotoConf
glepnirdf461152024-04-04 22:23:29 +0200322 call assert_equal('XgotoConf', bufname('%'))
323 call assert_equal(['test'], readfile('Untitled'))
324 call delete('Untitled')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200325 close!
326endfunc
327
328" Test for splitting buffer with 'switchbuf'
329func Test_buffer_switchbuf()
Bram Moolenaare7cda972022-08-29 11:02:59 +0100330 new Xswitchbuf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200331 wincmd w
332 set switchbuf=useopen
Bram Moolenaare7cda972022-08-29 11:02:59 +0100333 sbuf Xswitchbuf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200334 call assert_equal(1, winnr())
335 call assert_equal(2, winnr('$'))
336 set switchbuf=usetab
337 tabnew
Bram Moolenaare7cda972022-08-29 11:02:59 +0100338 sbuf Xswitchbuf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200339 call assert_equal(1, tabpagenr())
340 call assert_equal(2, tabpagenr('$'))
341 set switchbuf&
342 %bw
343endfunc
344
345" Test for BufAdd autocommand wiping out the buffer
346func Test_bufadd_autocmd_bwipe()
347 %bw!
348 augroup BufAdd_Wipe
349 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +0100350 autocmd BufAdd Xbwipe %bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200351 augroup END
Bram Moolenaare7cda972022-08-29 11:02:59 +0100352 edit Xbwipe
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200353 call assert_equal('', @%)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100354 call assert_equal(0, bufexists('Xbwipe'))
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200355 augroup BufAdd_Wipe
356 au!
357 augroup END
358 augroup! BufAdd_Wipe
359endfunc
360
361" Test for trying to load a buffer with text locked
362" <C-\>e in the command line is used to lock the text
363func Test_load_buf_with_text_locked()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100364 new Xlockfile1
365 edit Xlockfile2
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200366 let cmd = ":\<C-\>eexecute(\"normal \<C-O>\")\<CR>\<C-C>"
367 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
368 %bw!
369endfunc
370
371" Test for using CTRL-^ to edit the alternative file keeping the cursor
372" position with 'nostartofline'. Also test using the 'buf' command.
373func Test_buffer_edit_altfile()
Bram Moolenaar34112652022-09-05 21:40:44 +0100374 call writefile(repeat(['one two'], 50), 'Xaltfile1', 'D')
375 call writefile(repeat(['five six'], 50), 'Xaltfile2', 'D')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200376 set nosol
Bram Moolenaare7cda972022-08-29 11:02:59 +0100377 edit Xaltfile1
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200378 call cursor(25, 5)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100379 edit Xaltfile2
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200380 call cursor(30, 4)
381 exe "normal \<C-^>"
382 call assert_equal([0, 25, 5, 0], getpos('.'))
383 exe "normal \<C-^>"
384 call assert_equal([0, 30, 4, 0], getpos('.'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100385 buf Xaltfile1
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200386 call assert_equal([0, 25, 5, 0], getpos('.'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100387 buf Xaltfile2
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200388 call assert_equal([0, 30, 4, 0], getpos('.'))
389 set sol&
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200390endfunc
391
392" Test for running the :sball command with a maximum window count and a
393" modified buffer
394func Test_sball_with_count()
395 %bw!
Bram Moolenaare7cda972022-08-29 11:02:59 +0100396 edit Xcountfile1
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200397 call setline(1, ['abc'])
Bram Moolenaare7cda972022-08-29 11:02:59 +0100398 new Xcountfile2
399 new Xcountfile3
400 new Xcountfile4
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200401 2sball
Bram Moolenaare7cda972022-08-29 11:02:59 +0100402 call assert_equal(bufnr('Xcountfile4'), winbufnr(1))
403 call assert_equal(bufnr('Xcountfile1'), winbufnr(2))
404 call assert_equal(0, getbufinfo('Xcountfile2')[0].loaded)
405 call assert_equal(0, getbufinfo('Xcountfile3')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200406 %bw!
407endfunc
408
Bram Moolenaare974fa72020-10-25 15:02:51 +0100409func Test_badd_options()
410 new SomeNewBuffer
Bram Moolenaar37e4e032020-10-25 16:18:26 +0100411 setlocal numberwidth=3
Bram Moolenaare974fa72020-10-25 15:02:51 +0100412 wincmd p
Bram Moolenaar89b693e2020-10-25 17:09:50 +0100413 badd +1 SomeNewBuffer
Bram Moolenaare974fa72020-10-25 15:02:51 +0100414 new SomeNewBuffer
Bram Moolenaar37e4e032020-10-25 16:18:26 +0100415 call assert_equal(3, &numberwidth)
Bram Moolenaare974fa72020-10-25 15:02:51 +0100416 close
417 close
418 bwipe! SomeNewBuffer
Bram Moolenaara2b91032022-09-19 18:20:08 +0100419
420 badd +3 XbaddFile
421 call writefile(range(6), 'XbaddFile', 'D')
422 buf XbaddFile
423 call assert_equal([0, 3, 1, 0], getpos('.'))
424
425 bwipe! XbaddFile
Bram Moolenaare974fa72020-10-25 15:02:51 +0100426endfunc
427
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100428func Test_balt()
429 new SomeNewBuffer
430 balt +3 OtherBuffer
431 e #
432 call assert_equal('OtherBuffer', bufname())
433endfunc
434
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200435" Test for buffer match URL(scheme) check
436" scheme is alpha and inner hyphen only.
437func Test_buffer_scheme()
438 CheckMSWindows
439
K.Takata0500e872022-09-08 12:28:02 +0100440 set noswapfile
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200441 set noshellslash
442 %bwipe!
443 let bufnames = [
Bram Moolenaarf8bc0ce2021-12-02 12:30:22 +0000444 \ #{id: 'ssb0', name: 'test://xyz/foo/ssb0' , match: 1},
445 \ #{id: 'ssb1', name: 'test+abc://xyz/foo/ssb1', match: 0},
446 \ #{id: 'ssb2', name: 'test_abc://xyz/foo/ssb2', match: 0},
447 \ #{id: 'ssb3', name: 'test-abc://xyz/foo/ssb3', match: 1},
448 \ #{id: 'ssb4', name: '-test://xyz/foo/ssb4' , match: 0},
449 \ #{id: 'ssb5', name: 'test-://xyz/foo/ssb5' , match: 0},
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200450 \]
451 for buf in bufnames
452 new `=buf.name`
453 if buf.match
454 call assert_equal(buf.name, getbufinfo(buf.id)[0].name)
455 else
456 " slashes will have become backslashes
457 call assert_notequal(buf.name, getbufinfo(buf.id)[0].name)
458 endif
459 bwipe
460 endfor
461
462 set shellslash&
K.Takata0500e872022-09-08 12:28:02 +0100463 set swapfile&
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200464endfunc
465
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +0100466" this was using a NULL pointer after failing to use the pattern
467func Test_buf_pattern_invalid()
468 vsplit 0000000
469 silent! buf [0--]\&\zs*\zs*e
470 bwipe!
Bram Moolenaara59f2df2022-05-11 11:42:28 +0100471
472 vsplit 00000000000000000000000000
473 silent! buf [0--]\&\zs*\zs*e
474 bwipe!
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +0100475
476 " similar case with different code path
477 split 0
478 edit ΓΏ
479 silent! buf [0--]\&\zs*\zs*0
480 bwipe!
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +0100481endfunc
482
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +0200483" Test for the 'maxmem' and 'maxmemtot' options
484func Test_buffer_maxmem()
485 " use 1KB per buffer and 2KB for all the buffers
486 set maxmem=1 maxmemtot=2
487 new
488 let v:errmsg = ''
489 " try opening some files
490 edit test_arglist.vim
491 call assert_equal('test_arglist.vim', bufname())
492 edit test_eval_stuff.vim
493 call assert_equal('test_eval_stuff.vim', bufname())
494 b test_arglist.vim
495 call assert_equal('test_arglist.vim', bufname())
496 b test_eval_stuff.vim
497 call assert_equal('test_eval_stuff.vim', bufname())
498 close
499 call assert_equal('', v:errmsg)
500 set maxmem& maxmemtot&
501endfunc
502
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100503" Test for buffer allocation failure
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100504func Test_buflist_alloc_failure()
505 %bw!
506
Bram Moolenaare7cda972022-08-29 11:02:59 +0100507 edit XallocFail1
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100508 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100509 call assert_fails('edit XallocFail2', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100510
511 " test for bufadd()
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100512 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100513 call assert_fails('call bufadd("Xbuffer")', 'E342:')
514
515 " test for setting the arglist
Bram Moolenaare7cda972022-08-29 11:02:59 +0100516 edit XallocFail2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100517 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100518 call assert_fails('next XallocFail3', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100519
520 " test for setting the alternate buffer name when writing a file
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100521 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100522 call assert_fails('write Xother', 'E342:')
523 call delete('Xother')
524
525 " test for creating a buffer using bufnr()
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100526 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100527 call assert_fails("call bufnr('Xnewbuf', v:true)", 'E342:')
528
529 " test for renaming buffer using :file
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100530 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100531 call assert_fails('file Xnewfile', 'E342:')
532
533 " test for creating a buffer for a popup window
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100534 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100535 call assert_fails('call popup_create("mypop", {})', 'E342:')
536
537 if has('terminal')
538 " test for creating a buffer for a terminal window
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100539 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100540 call assert_fails('call term_start(&shell)', 'E342:')
541 %bw!
542 endif
543
544 " test for loading a new buffer after wiping out all the buffers
Bram Moolenaare7cda972022-08-29 11:02:59 +0100545 edit XallocFail4
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100546 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100547 call assert_fails('%bw!', 'E342:')
548
549 " test for :checktime loading the buffer
Bram Moolenaar34112652022-09-05 21:40:44 +0100550 call writefile(['one'], 'XallocFail5', 'D')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100551 if has('unix')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100552 edit XallocFail5
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100553 " sleep for some time to make sure the timestamp is different
554 sleep 200m
Bram Moolenaare7cda972022-08-29 11:02:59 +0100555 call writefile(['two'], 'XallocFail5')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100556 set autoread
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100557 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100558 call assert_fails('checktime', 'E342:')
559 set autoread&
560 bw!
561 endif
562
563 " test for :vimgrep loading a dummy buffer
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100564 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100565 call assert_fails('vimgrep two XallocFail5', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100566
567 " test for quickfix command loading a buffer
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100568 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100569 call assert_fails('cexpr "XallocFail6:10:Line10"', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100570endfunc
571
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100572" vim: shiftwidth=2 sts=2 expandtab