blob: de088bd8efe3714ec01b8988d9c52bf99c5df1d8 [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
129" Test for :bdelete
130func Test_bdelete_cmd()
131 %bwipe!
132 call assert_fails('bdelete 5', 'E516:')
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100133 call assert_fails('1,1bdelete 1 2', 'E488:')
Bram Moolenaar531be472020-09-23 22:38:05 +0200134 call assert_fails('bdelete \)', 'E55:')
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100135
zeertzjqc029c132024-03-28 11:37:26 +0100136 " Deleting an unlisted and unloaded buffer
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100137 edit Xbdelfile1
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100138 let bnr = bufnr()
139 set nobuflisted
140 enew
141 call assert_fails('bdelete ' .. bnr, 'E516:')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200142
143 " Deleting more than one buffer
144 new Xbuf1
145 new Xbuf2
146 exe 'bdel ' .. bufnr('Xbuf2') .. ' ' .. bufnr('Xbuf1')
147 call assert_equal(1, winnr('$'))
148 call assert_equal(0, getbufinfo('Xbuf1')[0].loaded)
149 call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
150
151 " Deleting more than one buffer and an invalid buffer
152 new Xbuf1
153 new Xbuf2
154 let cmd = "exe 'bdel ' .. bufnr('Xbuf2') .. ' xxx ' .. bufnr('Xbuf1')"
155 call assert_fails(cmd, 'E94:')
156 call assert_equal(2, winnr('$'))
157 call assert_equal(1, getbufinfo('Xbuf1')[0].loaded)
158 call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
159
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100160 %bwipe!
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100161endfunc
162
Bram Moolenaar067297e2020-04-13 19:55:50 +0200163func Test_buffer_error()
164 new foo1
165 new foo2
166
167 call assert_fails('buffer foo', 'E93:')
168 call assert_fails('buffer bar', 'E94:')
169 call assert_fails('buffer 0', 'E939:')
170
171 %bwipe
172endfunc
173
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200174" Test for the status messages displayed when unloading, deleting or wiping
175" out buffers
176func Test_buffer_statusmsg()
177 CheckEnglish
178 set report=1
179 new Xbuf1
180 new Xbuf2
181 let bnr = bufnr()
182 exe "normal 2\<C-G>"
183 call assert_match('buf ' .. bnr .. ':', v:statusmsg)
184 bunload Xbuf1 Xbuf2
185 call assert_equal('2 buffers unloaded', v:statusmsg)
186 bdel Xbuf1 Xbuf2
187 call assert_equal('2 buffers deleted', v:statusmsg)
188 bwipe Xbuf1 Xbuf2
189 call assert_equal('2 buffers wiped out', v:statusmsg)
190 set report&
191endfunc
192
193" Test for quitting the 'swapfile exists' dialog with the split buffer
194" command.
195func Test_buffer_sbuf_cleanup()
Bram Moolenaar34112652022-09-05 21:40:44 +0100196 call writefile([], 'XsplitCleanup', 'D')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200197 " first open the file in a buffer
Bram Moolenaare7cda972022-08-29 11:02:59 +0100198 new XsplitCleanup
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200199 let bnr = bufnr()
200 close
201 " create the swap file
Bram Moolenaar34112652022-09-05 21:40:44 +0100202 call writefile([], '.XsplitCleanup.swp', 'D')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200203 " Remove the catch-all that runtest.vim adds
204 au! SwapExists
205 augroup BufTest
206 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +0100207 autocmd SwapExists XsplitCleanup let v:swapchoice='q'
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200208 augroup END
209 exe 'sbuf ' . bnr
210 call assert_equal(1, winnr('$'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100211 call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200212
213 " test for :sball
214 sball
215 call assert_equal(1, winnr('$'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100216 call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200217
218 %bw!
219 set shortmess+=F
220 let v:statusmsg = ''
Bram Moolenaare7cda972022-08-29 11:02:59 +0100221 edit XsplitCleanup
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200222 call assert_equal('', v:statusmsg)
223 call assert_equal(1, winnr('$'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100224 call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200225 set shortmess&
226
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200227 augroup BufTest
228 au!
229 augroup END
230 augroup! BufTest
231endfunc
232
233" Test for deleting a modified buffer with :confirm
234func Test_bdel_with_confirm()
235 CheckUnix
236 CheckNotGui
237 CheckFeature dialog_con
238 new
239 call setline(1, 'test')
240 call assert_fails('bdel', 'E89:')
241 call feedkeys('c', 'L')
242 confirm bdel
243 call assert_equal(2, winnr('$'))
244 call assert_equal(1, &modified)
245 call feedkeys('n', 'L')
246 confirm bdel
247 call assert_equal(1, winnr('$'))
248endfunc
249
250" Test for editing another buffer from a modified buffer with :confirm
251func Test_goto_buf_with_confirm()
252 CheckUnix
253 CheckNotGui
254 CheckFeature dialog_con
glepnirdf461152024-04-04 22:23:29 +0200255 " When dialog_con_gui is defined, Vim is compiled with GUI support
256 " and FEAT_BROWSE will be defined, which causes :confirm :b to
257 " call do_browse(), which will try to use a GUI file browser,
258 " which aborts if a GUI is not available.
259 CheckNotFeature dialog_con_gui
Bram Moolenaare7cda972022-08-29 11:02:59 +0100260 new XgotoConf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200261 enew
262 call setline(1, 'test')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100263 call assert_fails('b XgotoConf', 'E37:')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200264 call feedkeys('c', 'L')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100265 call assert_fails('confirm b XgotoConf', 'E37:')
glepnirdf461152024-04-04 22:23:29 +0200266 call assert_true(&modified)
267 call assert_true(empty(bufname('%')))
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200268 call feedkeys('y', 'L')
glepnirdf461152024-04-04 22:23:29 +0200269 confirm b XgotoConf
270 call assert_equal('XgotoConf', bufname('%'))
271 call assert_equal(['test'], readfile('Untitled'))
272 e Untitled
273 call setline(2, 'test2')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200274 call feedkeys('n', 'L')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100275 confirm b XgotoConf
glepnirdf461152024-04-04 22:23:29 +0200276 call assert_equal('XgotoConf', bufname('%'))
277 call assert_equal(['test'], readfile('Untitled'))
278 call delete('Untitled')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200279 close!
280endfunc
281
282" Test for splitting buffer with 'switchbuf'
283func Test_buffer_switchbuf()
Bram Moolenaare7cda972022-08-29 11:02:59 +0100284 new Xswitchbuf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200285 wincmd w
286 set switchbuf=useopen
Bram Moolenaare7cda972022-08-29 11:02:59 +0100287 sbuf Xswitchbuf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200288 call assert_equal(1, winnr())
289 call assert_equal(2, winnr('$'))
290 set switchbuf=usetab
291 tabnew
Bram Moolenaare7cda972022-08-29 11:02:59 +0100292 sbuf Xswitchbuf
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200293 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!
Bram Moolenaare7cda972022-08-29 11:02:59 +0100304 autocmd BufAdd Xbwipe %bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200305 augroup END
Bram Moolenaare7cda972022-08-29 11:02:59 +0100306 edit Xbwipe
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200307 call assert_equal('', @%)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100308 call assert_equal(0, bufexists('Xbwipe'))
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200309 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()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100318 new Xlockfile1
319 edit Xlockfile2
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200320 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()
Bram Moolenaar34112652022-09-05 21:40:44 +0100328 call writefile(repeat(['one two'], 50), 'Xaltfile1', 'D')
329 call writefile(repeat(['five six'], 50), 'Xaltfile2', 'D')
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200330 set nosol
Bram Moolenaare7cda972022-08-29 11:02:59 +0100331 edit Xaltfile1
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200332 call cursor(25, 5)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100333 edit Xaltfile2
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200334 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('.'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100339 buf Xaltfile1
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200340 call assert_equal([0, 25, 5, 0], getpos('.'))
Bram Moolenaare7cda972022-08-29 11:02:59 +0100341 buf Xaltfile2
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200342 call assert_equal([0, 30, 4, 0], getpos('.'))
343 set sol&
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200344endfunc
345
346" Test for running the :sball command with a maximum window count and a
347" modified buffer
348func Test_sball_with_count()
349 %bw!
Bram Moolenaare7cda972022-08-29 11:02:59 +0100350 edit Xcountfile1
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200351 call setline(1, ['abc'])
Bram Moolenaare7cda972022-08-29 11:02:59 +0100352 new Xcountfile2
353 new Xcountfile3
354 new Xcountfile4
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200355 2sball
Bram Moolenaare7cda972022-08-29 11:02:59 +0100356 call assert_equal(bufnr('Xcountfile4'), winbufnr(1))
357 call assert_equal(bufnr('Xcountfile1'), winbufnr(2))
358 call assert_equal(0, getbufinfo('Xcountfile2')[0].loaded)
359 call assert_equal(0, getbufinfo('Xcountfile3')[0].loaded)
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200360 %bw!
361endfunc
362
Bram Moolenaare974fa72020-10-25 15:02:51 +0100363func Test_badd_options()
364 new SomeNewBuffer
Bram Moolenaar37e4e032020-10-25 16:18:26 +0100365 setlocal numberwidth=3
Bram Moolenaare974fa72020-10-25 15:02:51 +0100366 wincmd p
Bram Moolenaar89b693e2020-10-25 17:09:50 +0100367 badd +1 SomeNewBuffer
Bram Moolenaare974fa72020-10-25 15:02:51 +0100368 new SomeNewBuffer
Bram Moolenaar37e4e032020-10-25 16:18:26 +0100369 call assert_equal(3, &numberwidth)
Bram Moolenaare974fa72020-10-25 15:02:51 +0100370 close
371 close
372 bwipe! SomeNewBuffer
Bram Moolenaara2b91032022-09-19 18:20:08 +0100373
374 badd +3 XbaddFile
375 call writefile(range(6), 'XbaddFile', 'D')
376 buf XbaddFile
377 call assert_equal([0, 3, 1, 0], getpos('.'))
378
379 bwipe! XbaddFile
Bram Moolenaare974fa72020-10-25 15:02:51 +0100380endfunc
381
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100382func Test_balt()
383 new SomeNewBuffer
384 balt +3 OtherBuffer
385 e #
386 call assert_equal('OtherBuffer', bufname())
387endfunc
388
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200389" Test for buffer match URL(scheme) check
390" scheme is alpha and inner hyphen only.
391func Test_buffer_scheme()
392 CheckMSWindows
393
K.Takata0500e872022-09-08 12:28:02 +0100394 set noswapfile
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200395 set noshellslash
396 %bwipe!
397 let bufnames = [
Bram Moolenaarf8bc0ce2021-12-02 12:30:22 +0000398 \ #{id: 'ssb0', name: 'test://xyz/foo/ssb0' , match: 1},
399 \ #{id: 'ssb1', name: 'test+abc://xyz/foo/ssb1', match: 0},
400 \ #{id: 'ssb2', name: 'test_abc://xyz/foo/ssb2', match: 0},
401 \ #{id: 'ssb3', name: 'test-abc://xyz/foo/ssb3', match: 1},
402 \ #{id: 'ssb4', name: '-test://xyz/foo/ssb4' , match: 0},
403 \ #{id: 'ssb5', name: 'test-://xyz/foo/ssb5' , match: 0},
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200404 \]
405 for buf in bufnames
406 new `=buf.name`
407 if buf.match
408 call assert_equal(buf.name, getbufinfo(buf.id)[0].name)
409 else
410 " slashes will have become backslashes
411 call assert_notequal(buf.name, getbufinfo(buf.id)[0].name)
412 endif
413 bwipe
414 endfor
415
416 set shellslash&
K.Takata0500e872022-09-08 12:28:02 +0100417 set swapfile&
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +0200418endfunc
419
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +0100420" this was using a NULL pointer after failing to use the pattern
421func Test_buf_pattern_invalid()
422 vsplit 0000000
423 silent! buf [0--]\&\zs*\zs*e
424 bwipe!
Bram Moolenaara59f2df2022-05-11 11:42:28 +0100425
426 vsplit 00000000000000000000000000
427 silent! buf [0--]\&\zs*\zs*e
428 bwipe!
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +0100429
430 " similar case with different code path
431 split 0
432 edit ΓΏ
433 silent! buf [0--]\&\zs*\zs*0
434 bwipe!
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +0100435endfunc
436
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +0200437" Test for the 'maxmem' and 'maxmemtot' options
438func Test_buffer_maxmem()
439 " use 1KB per buffer and 2KB for all the buffers
440 set maxmem=1 maxmemtot=2
441 new
442 let v:errmsg = ''
443 " try opening some files
444 edit test_arglist.vim
445 call assert_equal('test_arglist.vim', bufname())
446 edit test_eval_stuff.vim
447 call assert_equal('test_eval_stuff.vim', bufname())
448 b test_arglist.vim
449 call assert_equal('test_arglist.vim', bufname())
450 b test_eval_stuff.vim
451 call assert_equal('test_eval_stuff.vim', bufname())
452 close
453 call assert_equal('', v:errmsg)
454 set maxmem& maxmemtot&
455endfunc
456
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100457" Test for buffer allocation failure
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100458func Test_buflist_alloc_failure()
459 %bw!
460
Bram Moolenaare7cda972022-08-29 11:02:59 +0100461 edit XallocFail1
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100462 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100463 call assert_fails('edit XallocFail2', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100464
465 " test for bufadd()
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100466 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100467 call assert_fails('call bufadd("Xbuffer")', 'E342:')
468
469 " test for setting the arglist
Bram Moolenaare7cda972022-08-29 11:02:59 +0100470 edit XallocFail2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100471 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100472 call assert_fails('next XallocFail3', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100473
474 " test for setting the alternate buffer name when writing a file
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100475 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100476 call assert_fails('write Xother', 'E342:')
477 call delete('Xother')
478
479 " test for creating a buffer using bufnr()
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100480 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100481 call assert_fails("call bufnr('Xnewbuf', v:true)", 'E342:')
482
483 " test for renaming buffer using :file
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100484 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100485 call assert_fails('file Xnewfile', 'E342:')
486
487 " test for creating a buffer for a popup window
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100488 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100489 call assert_fails('call popup_create("mypop", {})', 'E342:')
490
491 if has('terminal')
492 " test for creating a buffer for a terminal window
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100493 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100494 call assert_fails('call term_start(&shell)', 'E342:')
495 %bw!
496 endif
497
498 " test for loading a new buffer after wiping out all the buffers
Bram Moolenaare7cda972022-08-29 11:02:59 +0100499 edit XallocFail4
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100500 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100501 call assert_fails('%bw!', 'E342:')
502
503 " test for :checktime loading the buffer
Bram Moolenaar34112652022-09-05 21:40:44 +0100504 call writefile(['one'], 'XallocFail5', 'D')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100505 if has('unix')
Bram Moolenaare7cda972022-08-29 11:02:59 +0100506 edit XallocFail5
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100507 " sleep for some time to make sure the timestamp is different
508 sleep 200m
Bram Moolenaare7cda972022-08-29 11:02:59 +0100509 call writefile(['two'], 'XallocFail5')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100510 set autoread
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100511 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100512 call assert_fails('checktime', 'E342:')
513 set autoread&
514 bw!
515 endif
516
517 " test for :vimgrep loading a dummy buffer
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100518 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100519 call assert_fails('vimgrep two XallocFail5', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100520
521 " test for quickfix command loading a buffer
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100522 call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
Bram Moolenaare7cda972022-08-29 11:02:59 +0100523 call assert_fails('cexpr "XallocFail6:10:Line10"', 'E342:')
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +0100524endfunc
525
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100526" vim: shiftwidth=2 sts=2 expandtab