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! |
| 8 | call writefile(['B1'], 'b1') |
| 9 | call writefile(['B2'], 'b2') |
| 10 | call writefile(['B3'], 'b3') |
| 11 | call writefile(['B4'], 'b4') |
| 12 | |
| 13 | " Load four buffers. Unload the second and third buffers and then |
| 14 | " execute .+3bunload to unload the last buffer. |
| 15 | edit b1 |
| 16 | new b2 |
| 17 | new b3 |
| 18 | new b4 |
| 19 | |
| 20 | bunload b2 |
| 21 | bunload b3 |
| 22 | exe bufwinnr('b1') . 'wincmd w' |
| 23 | .+3bunload |
| 24 | call assert_equal(0, getbufinfo('b4')[0].loaded) |
| 25 | call assert_equal('b1', |
| 26 | \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t')) |
| 27 | |
| 28 | " Load four buffers. Unload the third and fourth buffers. Execute .+3bunload |
| 29 | " and check whether the second buffer is unloaded. |
| 30 | ball |
| 31 | bunload b3 |
| 32 | bunload b4 |
| 33 | exe bufwinnr('b1') . 'wincmd w' |
| 34 | .+3bunload |
| 35 | call assert_equal(0, getbufinfo('b2')[0].loaded) |
| 36 | call assert_equal('b1', |
| 37 | \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t')) |
| 38 | |
| 39 | " Load four buffers. Unload the second and third buffers and from the last |
| 40 | " buffer execute .-3bunload to unload the first buffer. |
| 41 | ball |
| 42 | bunload b2 |
| 43 | bunload b3 |
| 44 | exe bufwinnr('b4') . 'wincmd w' |
| 45 | .-3bunload |
| 46 | call assert_equal(0, getbufinfo('b1')[0].loaded) |
| 47 | call assert_equal('b4', |
| 48 | \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t')) |
| 49 | |
| 50 | " Load four buffers. Unload the first and second buffers. Execute .-3bunload |
| 51 | " from the last buffer and check whether the third buffer is unloaded. |
| 52 | ball |
| 53 | bunload b1 |
| 54 | bunload b2 |
| 55 | exe bufwinnr('b4') . 'wincmd w' |
| 56 | .-3bunload |
| 57 | call assert_equal(0, getbufinfo('b3')[0].loaded) |
| 58 | call assert_equal('b4', |
| 59 | \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t')) |
| 60 | |
| 61 | %bwipe! |
| 62 | call delete('b1') |
| 63 | call delete('b2') |
| 64 | call delete('b3') |
| 65 | call delete('b4') |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 66 | |
| 67 | call assert_fails('1,4bunload', 'E16:') |
| 68 | call assert_fails(',100bunload', 'E16:') |
| 69 | |
| 70 | " Use a try-catch for this test. When assert_fails() is used for this |
| 71 | " test, the command fails with E515: instead of E90: |
| 72 | let caught_E90 = 0 |
| 73 | try |
| 74 | $bunload |
| 75 | catch /E90:/ |
| 76 | let caught_E90 = 1 |
| 77 | endtry |
| 78 | call assert_equal(1, caught_E90) |
| 79 | call assert_fails('$bunload', 'E515:') |
| 80 | endfunc |
| 81 | |
| 82 | " Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified |
| 83 | " commands |
| 84 | func Test_buflist_browse() |
| 85 | %bwipe! |
| 86 | call assert_fails('buffer 1000', 'E86:') |
| 87 | |
| 88 | call writefile(['foo1', 'foo2', 'foo3', 'foo4'], 'Xfile1') |
| 89 | call writefile(['bar1', 'bar2', 'bar3', 'bar4'], 'Xfile2') |
| 90 | call writefile(['baz1', 'baz2', 'baz3', 'baz4'], 'Xfile3') |
| 91 | edit Xfile1 |
| 92 | let b1 = bufnr() |
| 93 | edit Xfile2 |
| 94 | let b2 = bufnr() |
| 95 | edit +/baz4 Xfile3 |
| 96 | let b3 = bufnr() |
| 97 | |
| 98 | call assert_fails('buffer ' .. b1 .. ' abc', 'E488:') |
| 99 | call assert_equal(b3, bufnr()) |
| 100 | call assert_equal(4, line('.')) |
| 101 | exe 'buffer +/bar2 ' .. b2 |
| 102 | call assert_equal(b2, bufnr()) |
| 103 | call assert_equal(2, line('.')) |
| 104 | exe 'buffer +/bar1' |
| 105 | call assert_equal(b2, bufnr()) |
| 106 | call assert_equal(1, line('.')) |
| 107 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 108 | brewind + |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 109 | call assert_equal(b1, bufnr()) |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 110 | call assert_equal(4, line('.')) |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 111 | |
| 112 | blast +/baz2 |
| 113 | call assert_equal(b3, bufnr()) |
| 114 | call assert_equal(2, line('.')) |
| 115 | |
| 116 | bprevious +/bar4 |
| 117 | call assert_equal(b2, bufnr()) |
| 118 | call assert_equal(4, line('.')) |
| 119 | |
| 120 | bnext +/baz3 |
| 121 | call assert_equal(b3, bufnr()) |
| 122 | call assert_equal(3, line('.')) |
| 123 | |
| 124 | call assert_fails('bmodified', 'E84:') |
| 125 | call setbufvar(b2, '&modified', 1) |
| 126 | exe 'bmodified +/bar3' |
| 127 | call assert_equal(b2, bufnr()) |
| 128 | call assert_equal(3, line('.')) |
| 129 | |
| 130 | " With no listed buffers in the list, :bnext and :bprev should fail |
| 131 | %bwipe! |
| 132 | set nobuflisted |
| 133 | call assert_fails('bnext', 'E85:') |
| 134 | call assert_fails('bprev', 'E85:') |
| 135 | set buflisted |
| 136 | |
| 137 | call assert_fails('sandbox bnext', 'E48:') |
| 138 | |
| 139 | call delete('Xfile1') |
| 140 | call delete('Xfile2') |
| 141 | call delete('Xfile3') |
| 142 | %bwipe! |
| 143 | endfunc |
| 144 | |
| 145 | " Test for :bdelete |
| 146 | func Test_bdelete_cmd() |
| 147 | %bwipe! |
| 148 | call assert_fails('bdelete 5', 'E516:') |
Bram Moolenaar | 9b9be00 | 2020-03-22 14:41:22 +0100 | [diff] [blame] | 149 | call assert_fails('1,1bdelete 1 2', 'E488:') |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 150 | |
| 151 | " Deleting a unlisted and unloaded buffer |
| 152 | edit Xfile1 |
| 153 | let bnr = bufnr() |
| 154 | set nobuflisted |
| 155 | enew |
| 156 | call assert_fails('bdelete ' .. bnr, 'E516:') |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 157 | |
| 158 | " Deleting more than one buffer |
| 159 | new Xbuf1 |
| 160 | new Xbuf2 |
| 161 | exe 'bdel ' .. bufnr('Xbuf2') .. ' ' .. bufnr('Xbuf1') |
| 162 | call assert_equal(1, winnr('$')) |
| 163 | call assert_equal(0, getbufinfo('Xbuf1')[0].loaded) |
| 164 | call assert_equal(0, getbufinfo('Xbuf2')[0].loaded) |
| 165 | |
| 166 | " Deleting more than one buffer and an invalid buffer |
| 167 | new Xbuf1 |
| 168 | new Xbuf2 |
| 169 | let cmd = "exe 'bdel ' .. bufnr('Xbuf2') .. ' xxx ' .. bufnr('Xbuf1')" |
| 170 | call assert_fails(cmd, 'E94:') |
| 171 | call assert_equal(2, winnr('$')) |
| 172 | call assert_equal(1, getbufinfo('Xbuf1')[0].loaded) |
| 173 | call assert_equal(0, getbufinfo('Xbuf2')[0].loaded) |
| 174 | |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 175 | %bwipe! |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 176 | endfunc |
| 177 | |
Bram Moolenaar | 067297e | 2020-04-13 19:55:50 +0200 | [diff] [blame] | 178 | func Test_buffer_error() |
| 179 | new foo1 |
| 180 | new foo2 |
| 181 | |
| 182 | call assert_fails('buffer foo', 'E93:') |
| 183 | call assert_fails('buffer bar', 'E94:') |
| 184 | call assert_fails('buffer 0', 'E939:') |
| 185 | |
| 186 | %bwipe |
| 187 | endfunc |
| 188 | |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 189 | " Test for the status messages displayed when unloading, deleting or wiping |
| 190 | " out buffers |
| 191 | func Test_buffer_statusmsg() |
| 192 | CheckEnglish |
| 193 | set report=1 |
| 194 | new Xbuf1 |
| 195 | new Xbuf2 |
| 196 | let bnr = bufnr() |
| 197 | exe "normal 2\<C-G>" |
| 198 | call assert_match('buf ' .. bnr .. ':', v:statusmsg) |
| 199 | bunload Xbuf1 Xbuf2 |
| 200 | call assert_equal('2 buffers unloaded', v:statusmsg) |
| 201 | bdel Xbuf1 Xbuf2 |
| 202 | call assert_equal('2 buffers deleted', v:statusmsg) |
| 203 | bwipe Xbuf1 Xbuf2 |
| 204 | call assert_equal('2 buffers wiped out', v:statusmsg) |
| 205 | set report& |
| 206 | endfunc |
| 207 | |
| 208 | " Test for quitting the 'swapfile exists' dialog with the split buffer |
| 209 | " command. |
| 210 | func Test_buffer_sbuf_cleanup() |
| 211 | call writefile([], 'Xfile') |
| 212 | " first open the file in a buffer |
| 213 | new Xfile |
| 214 | let bnr = bufnr() |
| 215 | close |
| 216 | " create the swap file |
| 217 | call writefile([], '.Xfile.swp') |
| 218 | " Remove the catch-all that runtest.vim adds |
| 219 | au! SwapExists |
| 220 | augroup BufTest |
| 221 | au! |
| 222 | autocmd SwapExists Xfile let v:swapchoice='q' |
| 223 | augroup END |
| 224 | exe 'sbuf ' . bnr |
| 225 | call assert_equal(1, winnr('$')) |
| 226 | call assert_equal(0, getbufinfo('Xfile')[0].loaded) |
| 227 | |
| 228 | " test for :sball |
| 229 | sball |
| 230 | call assert_equal(1, winnr('$')) |
| 231 | call assert_equal(0, getbufinfo('Xfile')[0].loaded) |
| 232 | |
| 233 | %bw! |
| 234 | set shortmess+=F |
| 235 | let v:statusmsg = '' |
| 236 | edit Xfile |
| 237 | call assert_equal('', v:statusmsg) |
| 238 | call assert_equal(1, winnr('$')) |
| 239 | call assert_equal(0, getbufinfo('Xfile')[0].loaded) |
| 240 | set shortmess& |
| 241 | |
| 242 | call delete('Xfile') |
| 243 | call delete('.Xfile.swp') |
| 244 | augroup BufTest |
| 245 | au! |
| 246 | augroup END |
| 247 | augroup! BufTest |
| 248 | endfunc |
| 249 | |
| 250 | " Test for deleting a modified buffer with :confirm |
| 251 | func Test_bdel_with_confirm() |
| 252 | CheckUnix |
| 253 | CheckNotGui |
| 254 | CheckFeature dialog_con |
| 255 | new |
| 256 | call setline(1, 'test') |
| 257 | call assert_fails('bdel', 'E89:') |
| 258 | call feedkeys('c', 'L') |
| 259 | confirm bdel |
| 260 | call assert_equal(2, winnr('$')) |
| 261 | call assert_equal(1, &modified) |
| 262 | call feedkeys('n', 'L') |
| 263 | confirm bdel |
| 264 | call assert_equal(1, winnr('$')) |
| 265 | endfunc |
| 266 | |
| 267 | " Test for editing another buffer from a modified buffer with :confirm |
| 268 | func Test_goto_buf_with_confirm() |
| 269 | CheckUnix |
| 270 | CheckNotGui |
| 271 | CheckFeature dialog_con |
| 272 | new Xfile |
| 273 | enew |
| 274 | call setline(1, 'test') |
| 275 | call assert_fails('b Xfile', 'E37:') |
| 276 | call feedkeys('c', 'L') |
| 277 | call assert_fails('confirm b Xfile', 'E37:') |
| 278 | call assert_equal(1, &modified) |
| 279 | call assert_equal('', @%) |
| 280 | call feedkeys('y', 'L') |
| 281 | call assert_fails('confirm b Xfile', 'E37:') |
| 282 | call assert_equal(1, &modified) |
| 283 | call assert_equal('', @%) |
| 284 | call feedkeys('n', 'L') |
| 285 | confirm b Xfile |
| 286 | call assert_equal('Xfile', @%) |
| 287 | close! |
| 288 | endfunc |
| 289 | |
| 290 | " Test for splitting buffer with 'switchbuf' |
| 291 | func Test_buffer_switchbuf() |
| 292 | new Xfile |
| 293 | wincmd w |
| 294 | set switchbuf=useopen |
| 295 | sbuf Xfile |
| 296 | call assert_equal(1, winnr()) |
| 297 | call assert_equal(2, winnr('$')) |
| 298 | set switchbuf=usetab |
| 299 | tabnew |
| 300 | sbuf Xfile |
| 301 | call assert_equal(1, tabpagenr()) |
| 302 | call assert_equal(2, tabpagenr('$')) |
| 303 | set switchbuf& |
| 304 | %bw |
| 305 | endfunc |
| 306 | |
| 307 | " Test for BufAdd autocommand wiping out the buffer |
| 308 | func Test_bufadd_autocmd_bwipe() |
| 309 | %bw! |
| 310 | augroup BufAdd_Wipe |
| 311 | au! |
| 312 | autocmd BufAdd Xfile %bw! |
| 313 | augroup END |
| 314 | edit Xfile |
| 315 | call assert_equal('', @%) |
| 316 | call assert_equal(0, bufexists('Xfile')) |
| 317 | augroup BufAdd_Wipe |
| 318 | au! |
| 319 | augroup END |
| 320 | augroup! BufAdd_Wipe |
| 321 | endfunc |
| 322 | |
| 323 | " Test for trying to load a buffer with text locked |
| 324 | " <C-\>e in the command line is used to lock the text |
| 325 | func Test_load_buf_with_text_locked() |
| 326 | new Xfile1 |
| 327 | edit Xfile2 |
| 328 | let cmd = ":\<C-\>eexecute(\"normal \<C-O>\")\<CR>\<C-C>" |
| 329 | call assert_fails("call feedkeys(cmd, 'xt')", 'E565:') |
| 330 | %bw! |
| 331 | endfunc |
| 332 | |
| 333 | " Test for using CTRL-^ to edit the alternative file keeping the cursor |
| 334 | " position with 'nostartofline'. Also test using the 'buf' command. |
| 335 | func Test_buffer_edit_altfile() |
| 336 | call writefile(repeat(['one two'], 50), 'Xfile1') |
| 337 | call writefile(repeat(['five six'], 50), 'Xfile2') |
| 338 | set nosol |
| 339 | edit Xfile1 |
| 340 | call cursor(25, 5) |
| 341 | edit Xfile2 |
| 342 | call cursor(30, 4) |
| 343 | exe "normal \<C-^>" |
| 344 | call assert_equal([0, 25, 5, 0], getpos('.')) |
| 345 | exe "normal \<C-^>" |
| 346 | call assert_equal([0, 30, 4, 0], getpos('.')) |
| 347 | buf Xfile1 |
| 348 | call assert_equal([0, 25, 5, 0], getpos('.')) |
| 349 | buf Xfile2 |
| 350 | call assert_equal([0, 30, 4, 0], getpos('.')) |
| 351 | set sol& |
| 352 | call delete('Xfile1') |
| 353 | call delete('Xfile2') |
| 354 | endfunc |
| 355 | |
| 356 | " Test for running the :sball command with a maximum window count and a |
| 357 | " modified buffer |
| 358 | func Test_sball_with_count() |
| 359 | %bw! |
| 360 | edit Xfile1 |
| 361 | call setline(1, ['abc']) |
| 362 | new Xfile2 |
| 363 | new Xfile3 |
| 364 | new Xfile4 |
| 365 | 2sball |
| 366 | call assert_equal(bufnr('Xfile4'), winbufnr(1)) |
| 367 | call assert_equal(bufnr('Xfile1'), winbufnr(2)) |
| 368 | call assert_equal(0, getbufinfo('Xfile2')[0].loaded) |
| 369 | call assert_equal(0, getbufinfo('Xfile3')[0].loaded) |
| 370 | %bw! |
| 371 | endfunc |
| 372 | |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 373 | " vim: shiftwidth=2 sts=2 expandtab |