Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 1 | " Tests for Vim buffer |
| 2 | |
| 3 | " Test for the :bunload command with an offset |
| 4 | func Test_bunload_with_offset() |
| 5 | %bwipe! |
| 6 | call writefile(['B1'], 'b1') |
| 7 | call writefile(['B2'], 'b2') |
| 8 | call writefile(['B3'], 'b3') |
| 9 | call writefile(['B4'], 'b4') |
| 10 | |
| 11 | " Load four buffers. Unload the second and third buffers and then |
| 12 | " execute .+3bunload to unload the last buffer. |
| 13 | edit b1 |
| 14 | new b2 |
| 15 | new b3 |
| 16 | new b4 |
| 17 | |
| 18 | bunload b2 |
| 19 | bunload b3 |
| 20 | exe bufwinnr('b1') . 'wincmd w' |
| 21 | .+3bunload |
| 22 | call assert_equal(0, getbufinfo('b4')[0].loaded) |
| 23 | call assert_equal('b1', |
| 24 | \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t')) |
| 25 | |
| 26 | " Load four buffers. Unload the third and fourth buffers. Execute .+3bunload |
| 27 | " and check whether the second buffer is unloaded. |
| 28 | ball |
| 29 | bunload b3 |
| 30 | bunload b4 |
| 31 | exe bufwinnr('b1') . 'wincmd w' |
| 32 | .+3bunload |
| 33 | call assert_equal(0, getbufinfo('b2')[0].loaded) |
| 34 | call assert_equal('b1', |
| 35 | \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t')) |
| 36 | |
| 37 | " Load four buffers. Unload the second and third buffers and from the last |
| 38 | " buffer execute .-3bunload to unload the first buffer. |
| 39 | ball |
| 40 | bunload b2 |
| 41 | bunload b3 |
| 42 | exe bufwinnr('b4') . 'wincmd w' |
| 43 | .-3bunload |
| 44 | call assert_equal(0, getbufinfo('b1')[0].loaded) |
| 45 | call assert_equal('b4', |
| 46 | \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t')) |
| 47 | |
| 48 | " Load four buffers. Unload the first and second buffers. Execute .-3bunload |
| 49 | " from the last buffer and check whether the third buffer is unloaded. |
| 50 | ball |
| 51 | bunload b1 |
| 52 | bunload b2 |
| 53 | exe bufwinnr('b4') . 'wincmd w' |
| 54 | .-3bunload |
| 55 | call assert_equal(0, getbufinfo('b3')[0].loaded) |
| 56 | call assert_equal('b4', |
| 57 | \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t')) |
| 58 | |
| 59 | %bwipe! |
| 60 | call delete('b1') |
| 61 | call delete('b2') |
| 62 | call delete('b3') |
| 63 | call delete('b4') |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 64 | |
| 65 | call assert_fails('1,4bunload', 'E16:') |
| 66 | call assert_fails(',100bunload', 'E16:') |
| 67 | |
| 68 | " Use a try-catch for this test. When assert_fails() is used for this |
| 69 | " test, the command fails with E515: instead of E90: |
| 70 | let caught_E90 = 0 |
| 71 | try |
| 72 | $bunload |
| 73 | catch /E90:/ |
| 74 | let caught_E90 = 1 |
| 75 | endtry |
| 76 | call assert_equal(1, caught_E90) |
| 77 | call assert_fails('$bunload', 'E515:') |
| 78 | endfunc |
| 79 | |
| 80 | " Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified |
| 81 | " commands |
| 82 | func Test_buflist_browse() |
| 83 | %bwipe! |
| 84 | call assert_fails('buffer 1000', 'E86:') |
| 85 | |
| 86 | call writefile(['foo1', 'foo2', 'foo3', 'foo4'], 'Xfile1') |
| 87 | call writefile(['bar1', 'bar2', 'bar3', 'bar4'], 'Xfile2') |
| 88 | call writefile(['baz1', 'baz2', 'baz3', 'baz4'], 'Xfile3') |
| 89 | edit Xfile1 |
| 90 | let b1 = bufnr() |
| 91 | edit Xfile2 |
| 92 | let b2 = bufnr() |
| 93 | edit +/baz4 Xfile3 |
| 94 | let b3 = bufnr() |
| 95 | |
| 96 | call assert_fails('buffer ' .. b1 .. ' abc', 'E488:') |
| 97 | call assert_equal(b3, bufnr()) |
| 98 | call assert_equal(4, line('.')) |
| 99 | exe 'buffer +/bar2 ' .. b2 |
| 100 | call assert_equal(b2, bufnr()) |
| 101 | call assert_equal(2, line('.')) |
| 102 | exe 'buffer +/bar1' |
| 103 | call assert_equal(b2, bufnr()) |
| 104 | call assert_equal(1, line('.')) |
| 105 | |
| 106 | brewind +/foo3 |
| 107 | call assert_equal(b1, bufnr()) |
| 108 | call assert_equal(3, line('.')) |
| 109 | |
| 110 | blast +/baz2 |
| 111 | call assert_equal(b3, bufnr()) |
| 112 | call assert_equal(2, line('.')) |
| 113 | |
| 114 | bprevious +/bar4 |
| 115 | call assert_equal(b2, bufnr()) |
| 116 | call assert_equal(4, line('.')) |
| 117 | |
| 118 | bnext +/baz3 |
| 119 | call assert_equal(b3, bufnr()) |
| 120 | call assert_equal(3, line('.')) |
| 121 | |
| 122 | call assert_fails('bmodified', 'E84:') |
| 123 | call setbufvar(b2, '&modified', 1) |
| 124 | exe 'bmodified +/bar3' |
| 125 | call assert_equal(b2, bufnr()) |
| 126 | call assert_equal(3, line('.')) |
| 127 | |
| 128 | " With no listed buffers in the list, :bnext and :bprev should fail |
| 129 | %bwipe! |
| 130 | set nobuflisted |
| 131 | call assert_fails('bnext', 'E85:') |
| 132 | call assert_fails('bprev', 'E85:') |
| 133 | set buflisted |
| 134 | |
| 135 | call assert_fails('sandbox bnext', 'E48:') |
| 136 | |
| 137 | call delete('Xfile1') |
| 138 | call delete('Xfile2') |
| 139 | call delete('Xfile3') |
| 140 | %bwipe! |
| 141 | endfunc |
| 142 | |
| 143 | " Test for :bdelete |
| 144 | func Test_bdelete_cmd() |
| 145 | %bwipe! |
| 146 | call assert_fails('bdelete 5', 'E516:') |
| 147 | |
| 148 | " Deleting a unlisted and unloaded buffer |
| 149 | edit Xfile1 |
| 150 | let bnr = bufnr() |
| 151 | set nobuflisted |
| 152 | enew |
| 153 | call assert_fails('bdelete ' .. bnr, 'E516:') |
| 154 | %bwipe! |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 155 | endfunc |
| 156 | |
| 157 | " vim: shiftwidth=2 sts=2 expandtab |