Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 1 | " Tests for Vim buffer |
| 2 | |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 5 | " Test for the :bunload command with an offset |
| 6 | func Test_bunload_with_offset() |
| 7 | %bwipe! |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 8 | call writefile(['B1'], 'Xb1', 'D') |
| 9 | call writefile(['B2'], 'Xb2', 'D') |
| 10 | call writefile(['B3'], 'Xb3', 'D') |
| 11 | call writefile(['B4'], 'Xb4', 'D') |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 12 | |
| 13 | " Load four buffers. Unload the second and third buffers and then |
| 14 | " execute .+3bunload to unload the last buffer. |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 15 | edit Xb1 |
| 16 | new Xb2 |
| 17 | new Xb3 |
| 18 | new Xb4 |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 19 | |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 20 | bunload Xb2 |
| 21 | bunload Xb3 |
| 22 | exe bufwinnr('Xb1') . 'wincmd w' |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 23 | .+3bunload |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 24 | call assert_equal(0, getbufinfo('Xb4')[0].loaded) |
| 25 | call assert_equal('Xb1', |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 31 | bunload Xb3 |
| 32 | bunload Xb4 |
| 33 | exe bufwinnr('Xb1') . 'wincmd w' |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 34 | .+3bunload |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 35 | call assert_equal(0, getbufinfo('Xb2')[0].loaded) |
| 36 | call assert_equal('Xb1', |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 42 | bunload Xb2 |
| 43 | bunload Xb3 |
| 44 | exe bufwinnr('Xb4') . 'wincmd w' |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 45 | .-3bunload |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 46 | call assert_equal(0, getbufinfo('Xb1')[0].loaded) |
| 47 | call assert_equal('Xb4', |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 53 | bunload Xb1 |
| 54 | bunload Xb2 |
| 55 | exe bufwinnr('Xb4') . 'wincmd w' |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 56 | .-3bunload |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 57 | call assert_equal(0, getbufinfo('Xb3')[0].loaded) |
| 58 | call assert_equal('Xb4', |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 59 | \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t')) |
| 60 | |
| 61 | %bwipe! |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 62 | |
| 63 | call assert_fails('1,4bunload', 'E16:') |
| 64 | call assert_fails(',100bunload', 'E16:') |
| 65 | |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 66 | call assert_fails('$bunload', 'E90:') |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 67 | endfunc |
| 68 | |
| 69 | " Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified |
| 70 | " commands |
| 71 | func Test_buflist_browse() |
| 72 | %bwipe! |
| 73 | call assert_fails('buffer 1000', 'E86:') |
| 74 | |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 75 | 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 Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 78 | edit Xbrowse1 |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 79 | let b1 = bufnr() |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 80 | edit Xbrowse2 |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 81 | let b2 = bufnr() |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 82 | edit +/baz4 Xbrowse3 |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 83 | 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 Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 95 | brewind + |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 96 | call assert_equal(b1, bufnr()) |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 97 | call assert_equal(4, line('.')) |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 98 | |
| 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 Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 126 | %bwipe! |
| 127 | endfunc |
| 128 | |
LemonBoy | 893eeeb | 2024-07-10 20:20:48 +0200 | [diff] [blame] | 129 | " Test for :bnext and :bprev when called from help and non-help buffers. |
| 130 | func 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! |
| 173 | endfunc |
| 174 | |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 175 | " Test for :bdelete |
| 176 | func Test_bdelete_cmd() |
| 177 | %bwipe! |
| 178 | call assert_fails('bdelete 5', 'E516:') |
Bram Moolenaar | 9b9be00 | 2020-03-22 14:41:22 +0100 | [diff] [blame] | 179 | call assert_fails('1,1bdelete 1 2', 'E488:') |
Bram Moolenaar | 531be47 | 2020-09-23 22:38:05 +0200 | [diff] [blame] | 180 | call assert_fails('bdelete \)', 'E55:') |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 181 | |
zeertzjq | c029c13 | 2024-03-28 11:37:26 +0100 | [diff] [blame] | 182 | " Deleting an unlisted and unloaded buffer |
Bram Moolenaar | 61abe7d | 2022-08-30 21:46:08 +0100 | [diff] [blame] | 183 | edit Xbdelfile1 |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 184 | let bnr = bufnr() |
| 185 | set nobuflisted |
| 186 | enew |
| 187 | call assert_fails('bdelete ' .. bnr, 'E516:') |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 188 | |
| 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 Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 206 | %bwipe! |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 207 | endfunc |
| 208 | |
Bram Moolenaar | 067297e | 2020-04-13 19:55:50 +0200 | [diff] [blame] | 209 | func 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 |
| 218 | endfunc |
| 219 | |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 220 | " Test for the status messages displayed when unloading, deleting or wiping |
| 221 | " out buffers |
| 222 | func 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& |
| 237 | endfunc |
| 238 | |
| 239 | " Test for quitting the 'swapfile exists' dialog with the split buffer |
| 240 | " command. |
| 241 | func Test_buffer_sbuf_cleanup() |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 242 | call writefile([], 'XsplitCleanup', 'D') |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 243 | " first open the file in a buffer |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 244 | new XsplitCleanup |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 245 | let bnr = bufnr() |
| 246 | close |
| 247 | " create the swap file |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 248 | call writefile([], '.XsplitCleanup.swp', 'D') |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 249 | " Remove the catch-all that runtest.vim adds |
| 250 | au! SwapExists |
| 251 | augroup BufTest |
| 252 | au! |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 253 | autocmd SwapExists XsplitCleanup let v:swapchoice='q' |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 254 | augroup END |
| 255 | exe 'sbuf ' . bnr |
| 256 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 257 | call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded) |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 258 | |
| 259 | " test for :sball |
| 260 | sball |
| 261 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 262 | call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded) |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 263 | |
| 264 | %bw! |
| 265 | set shortmess+=F |
| 266 | let v:statusmsg = '' |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 267 | edit XsplitCleanup |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 268 | call assert_equal('', v:statusmsg) |
| 269 | call assert_equal(1, winnr('$')) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 270 | call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded) |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 271 | set shortmess& |
| 272 | |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 273 | augroup BufTest |
| 274 | au! |
| 275 | augroup END |
| 276 | augroup! BufTest |
| 277 | endfunc |
| 278 | |
| 279 | " Test for deleting a modified buffer with :confirm |
| 280 | func 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('$')) |
| 294 | endfunc |
| 295 | |
| 296 | " Test for editing another buffer from a modified buffer with :confirm |
| 297 | func Test_goto_buf_with_confirm() |
| 298 | CheckUnix |
| 299 | CheckNotGui |
| 300 | CheckFeature dialog_con |
glepnir | df46115 | 2024-04-04 22:23:29 +0200 | [diff] [blame] | 301 | " 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 Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 306 | new XgotoConf |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 307 | enew |
| 308 | call setline(1, 'test') |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 309 | call assert_fails('b XgotoConf', 'E37:') |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 310 | call feedkeys('c', 'L') |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 311 | call assert_fails('confirm b XgotoConf', 'E37:') |
glepnir | df46115 | 2024-04-04 22:23:29 +0200 | [diff] [blame] | 312 | call assert_true(&modified) |
| 313 | call assert_true(empty(bufname('%'))) |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 314 | call feedkeys('y', 'L') |
glepnir | df46115 | 2024-04-04 22:23:29 +0200 | [diff] [blame] | 315 | 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 Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 320 | call feedkeys('n', 'L') |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 321 | confirm b XgotoConf |
glepnir | df46115 | 2024-04-04 22:23:29 +0200 | [diff] [blame] | 322 | call assert_equal('XgotoConf', bufname('%')) |
| 323 | call assert_equal(['test'], readfile('Untitled')) |
| 324 | call delete('Untitled') |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 325 | close! |
| 326 | endfunc |
| 327 | |
| 328 | " Test for splitting buffer with 'switchbuf' |
| 329 | func Test_buffer_switchbuf() |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 330 | new Xswitchbuf |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 331 | wincmd w |
| 332 | set switchbuf=useopen |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 333 | sbuf Xswitchbuf |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 334 | call assert_equal(1, winnr()) |
| 335 | call assert_equal(2, winnr('$')) |
| 336 | set switchbuf=usetab |
| 337 | tabnew |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 338 | sbuf Xswitchbuf |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 339 | call assert_equal(1, tabpagenr()) |
| 340 | call assert_equal(2, tabpagenr('$')) |
| 341 | set switchbuf& |
| 342 | %bw |
| 343 | endfunc |
| 344 | |
| 345 | " Test for BufAdd autocommand wiping out the buffer |
| 346 | func Test_bufadd_autocmd_bwipe() |
| 347 | %bw! |
| 348 | augroup BufAdd_Wipe |
| 349 | au! |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 350 | autocmd BufAdd Xbwipe %bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 351 | augroup END |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 352 | edit Xbwipe |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 353 | call assert_equal('', @%) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 354 | call assert_equal(0, bufexists('Xbwipe')) |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 355 | augroup BufAdd_Wipe |
| 356 | au! |
| 357 | augroup END |
| 358 | augroup! BufAdd_Wipe |
| 359 | endfunc |
| 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 |
| 363 | func Test_load_buf_with_text_locked() |
Bram Moolenaar | 61abe7d | 2022-08-30 21:46:08 +0100 | [diff] [blame] | 364 | new Xlockfile1 |
| 365 | edit Xlockfile2 |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 366 | let cmd = ":\<C-\>eexecute(\"normal \<C-O>\")\<CR>\<C-C>" |
| 367 | call assert_fails("call feedkeys(cmd, 'xt')", 'E565:') |
| 368 | %bw! |
| 369 | endfunc |
| 370 | |
| 371 | " Test for using CTRL-^ to edit the alternative file keeping the cursor |
| 372 | " position with 'nostartofline'. Also test using the 'buf' command. |
| 373 | func Test_buffer_edit_altfile() |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 374 | call writefile(repeat(['one two'], 50), 'Xaltfile1', 'D') |
| 375 | call writefile(repeat(['five six'], 50), 'Xaltfile2', 'D') |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 376 | set nosol |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 377 | edit Xaltfile1 |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 378 | call cursor(25, 5) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 379 | edit Xaltfile2 |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 380 | 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 Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 385 | buf Xaltfile1 |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 386 | call assert_equal([0, 25, 5, 0], getpos('.')) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 387 | buf Xaltfile2 |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 388 | call assert_equal([0, 30, 4, 0], getpos('.')) |
| 389 | set sol& |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 390 | endfunc |
| 391 | |
| 392 | " Test for running the :sball command with a maximum window count and a |
| 393 | " modified buffer |
| 394 | func Test_sball_with_count() |
| 395 | %bw! |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 396 | edit Xcountfile1 |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 397 | call setline(1, ['abc']) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 398 | new Xcountfile2 |
| 399 | new Xcountfile3 |
| 400 | new Xcountfile4 |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 401 | 2sball |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 402 | 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 Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 406 | %bw! |
| 407 | endfunc |
| 408 | |
Bram Moolenaar | e974fa7 | 2020-10-25 15:02:51 +0100 | [diff] [blame] | 409 | func Test_badd_options() |
| 410 | new SomeNewBuffer |
Bram Moolenaar | 37e4e03 | 2020-10-25 16:18:26 +0100 | [diff] [blame] | 411 | setlocal numberwidth=3 |
Bram Moolenaar | e974fa7 | 2020-10-25 15:02:51 +0100 | [diff] [blame] | 412 | wincmd p |
Bram Moolenaar | 89b693e | 2020-10-25 17:09:50 +0100 | [diff] [blame] | 413 | badd +1 SomeNewBuffer |
Bram Moolenaar | e974fa7 | 2020-10-25 15:02:51 +0100 | [diff] [blame] | 414 | new SomeNewBuffer |
Bram Moolenaar | 37e4e03 | 2020-10-25 16:18:26 +0100 | [diff] [blame] | 415 | call assert_equal(3, &numberwidth) |
Bram Moolenaar | e974fa7 | 2020-10-25 15:02:51 +0100 | [diff] [blame] | 416 | close |
| 417 | close |
| 418 | bwipe! SomeNewBuffer |
Bram Moolenaar | a2b9103 | 2022-09-19 18:20:08 +0100 | [diff] [blame] | 419 | |
| 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 Moolenaar | e974fa7 | 2020-10-25 15:02:51 +0100 | [diff] [blame] | 426 | endfunc |
| 427 | |
Bram Moolenaar | 59d8e56 | 2020-11-07 18:41:10 +0100 | [diff] [blame] | 428 | func Test_balt() |
| 429 | new SomeNewBuffer |
| 430 | balt +3 OtherBuffer |
| 431 | e # |
| 432 | call assert_equal('OtherBuffer', bufname()) |
| 433 | endfunc |
| 434 | |
Tsuyoshi CHO | 7b7a118 | 2021-07-11 21:51:17 +0200 | [diff] [blame] | 435 | " Test for buffer match URL(scheme) check |
| 436 | " scheme is alpha and inner hyphen only. |
| 437 | func Test_buffer_scheme() |
| 438 | CheckMSWindows |
| 439 | |
K.Takata | 0500e87 | 2022-09-08 12:28:02 +0100 | [diff] [blame] | 440 | set noswapfile |
Tsuyoshi CHO | 7b7a118 | 2021-07-11 21:51:17 +0200 | [diff] [blame] | 441 | set noshellslash |
| 442 | %bwipe! |
| 443 | let bufnames = [ |
Bram Moolenaar | f8bc0ce | 2021-12-02 12:30:22 +0000 | [diff] [blame] | 444 | \ #{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 CHO | 7b7a118 | 2021-07-11 21:51:17 +0200 | [diff] [blame] | 450 | \] |
| 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.Takata | 0500e87 | 2022-09-08 12:28:02 +0100 | [diff] [blame] | 463 | set swapfile& |
Tsuyoshi CHO | 7b7a118 | 2021-07-11 21:51:17 +0200 | [diff] [blame] | 464 | endfunc |
| 465 | |
Bram Moolenaar | 8e4b76d | 2022-05-07 11:28:06 +0100 | [diff] [blame] | 466 | " this was using a NULL pointer after failing to use the pattern |
| 467 | func Test_buf_pattern_invalid() |
| 468 | vsplit 0000000 |
| 469 | silent! buf [0--]\&\zs*\zs*e |
| 470 | bwipe! |
Bram Moolenaar | a59f2df | 2022-05-11 11:42:28 +0100 | [diff] [blame] | 471 | |
| 472 | vsplit 00000000000000000000000000 |
| 473 | silent! buf [0--]\&\zs*\zs*e |
| 474 | bwipe! |
Bram Moolenaar | b62dc5e | 2022-05-15 14:50:12 +0100 | [diff] [blame] | 475 | |
| 476 | " similar case with different code path |
| 477 | split 0 |
| 478 | edit ΓΏ |
| 479 | silent! buf [0--]\&\zs*\zs*0 |
| 480 | bwipe! |
Bram Moolenaar | 8e4b76d | 2022-05-07 11:28:06 +0100 | [diff] [blame] | 481 | endfunc |
| 482 | |
Yegappan Lakshmanan | 46aa6f9 | 2021-05-19 17:15:04 +0200 | [diff] [blame] | 483 | " Test for the 'maxmem' and 'maxmemtot' options |
| 484 | func 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& |
| 501 | endfunc |
| 502 | |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 503 | " Test for buffer allocation failure |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 504 | func Test_buflist_alloc_failure() |
| 505 | %bw! |
| 506 | |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 507 | edit XallocFail1 |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 508 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 509 | call assert_fails('edit XallocFail2', 'E342:') |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 510 | |
| 511 | " test for bufadd() |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 512 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 513 | call assert_fails('call bufadd("Xbuffer")', 'E342:') |
| 514 | |
| 515 | " test for setting the arglist |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 516 | edit XallocFail2 |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 517 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 518 | call assert_fails('next XallocFail3', 'E342:') |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 519 | |
| 520 | " test for setting the alternate buffer name when writing a file |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 521 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 522 | call assert_fails('write Xother', 'E342:') |
| 523 | call delete('Xother') |
| 524 | |
| 525 | " test for creating a buffer using bufnr() |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 526 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 527 | call assert_fails("call bufnr('Xnewbuf', v:true)", 'E342:') |
| 528 | |
| 529 | " test for renaming buffer using :file |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 530 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 531 | call assert_fails('file Xnewfile', 'E342:') |
| 532 | |
| 533 | " test for creating a buffer for a popup window |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 534 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 535 | call assert_fails('call popup_create("mypop", {})', 'E342:') |
| 536 | |
| 537 | if has('terminal') |
| 538 | " test for creating a buffer for a terminal window |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 539 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 540 | 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 Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 545 | edit XallocFail4 |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 546 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 547 | call assert_fails('%bw!', 'E342:') |
| 548 | |
| 549 | " test for :checktime loading the buffer |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 550 | call writefile(['one'], 'XallocFail5', 'D') |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 551 | if has('unix') |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 552 | edit XallocFail5 |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 553 | " sleep for some time to make sure the timestamp is different |
| 554 | sleep 200m |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 555 | call writefile(['two'], 'XallocFail5') |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 556 | set autoread |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 557 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 558 | call assert_fails('checktime', 'E342:') |
| 559 | set autoread& |
| 560 | bw! |
| 561 | endif |
| 562 | |
| 563 | " test for :vimgrep loading a dummy buffer |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 564 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 565 | call assert_fails('vimgrep two XallocFail5', 'E342:') |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 566 | |
| 567 | " test for quickfix command loading a buffer |
Yegappan Lakshmanan | 72bb47e | 2022-04-03 11:22:38 +0100 | [diff] [blame] | 568 | call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 569 | call assert_fails('cexpr "XallocFail6:10:Line10"', 'E342:') |
Yegappan Lakshmanan | 0dac1ab | 2022-04-02 21:46:19 +0100 | [diff] [blame] | 570 | endfunc |
| 571 | |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 572 | " vim: shiftwidth=2 sts=2 expandtab |