Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 1 | " Tests for setbufline(), getbufline(), appendbufline(), deletebufline() |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | 660b85e | 2017-09-30 14:26:58 +0200 | [diff] [blame] | 3 | source shared.vim |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 4 | source screendump.vim |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 5 | source check.vim |
Bram Moolenaar | 11aa62f | 2017-09-04 22:56:01 +0200 | [diff] [blame] | 6 | |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 7 | func Test_setbufline_getbufline() |
| 8 | new |
| 9 | let b = bufnr('%') |
| 10 | hide |
| 11 | call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) |
| 12 | call assert_equal(['foo'], getbufline(b, 1)) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 13 | call assert_equal(['bar'], getbufline(b, '$')) |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 14 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 15 | exe "bd!" b |
| 16 | call assert_equal([], getbufline(b, 1, 2)) |
| 17 | |
| 18 | split Xtest |
| 19 | call setline(1, ['a', 'b', 'c']) |
| 20 | let b = bufnr('%') |
| 21 | wincmd w |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 22 | |
| 23 | call assert_equal(1, setbufline(b, 5, 'x')) |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 24 | call assert_equal(1, setbufline(b, 5, ['x'])) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 25 | call assert_equal(1, setbufline(b, 5, [])) |
| 26 | call assert_equal(1, setbufline(b, 5, test_null_list())) |
| 27 | |
| 28 | call assert_equal(1, 'x'->setbufline(bufnr('$') + 1, 1)) |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 29 | call assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1)) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 30 | call assert_equal(1, []->setbufline(bufnr('$') + 1, 1)) |
| 31 | call assert_equal(1, test_null_list()->setbufline(bufnr('$') + 1, 1)) |
| 32 | |
| 33 | call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$')) |
| 34 | |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 35 | call assert_equal(0, setbufline(b, 4, ['d', 'e'])) |
Bram Moolenaar | 4c313b1 | 2019-08-24 22:58:31 +0200 | [diff] [blame] | 36 | call assert_equal(['c'], b->getbufline(3)) |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 37 | call assert_equal(['d'], getbufline(b, 4)) |
| 38 | call assert_equal(['e'], getbufline(b, 5)) |
| 39 | call assert_equal([], getbufline(b, 6)) |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 40 | call assert_equal([], getbufline(b, 2, 1)) |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 41 | exe "bwipe! " . b |
| 42 | endfunc |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 43 | |
Bram Moolenaar | 0c4dc88 | 2017-11-06 21:32:54 +0100 | [diff] [blame] | 44 | func Test_setbufline_getbufline_fold() |
| 45 | split Xtest |
| 46 | setlocal foldmethod=expr foldexpr=0 |
| 47 | let b = bufnr('%') |
| 48 | new |
| 49 | call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) |
| 50 | call assert_equal(['foo'], getbufline(b, 1)) |
| 51 | call assert_equal(['bar'], getbufline(b, 2)) |
| 52 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 53 | exe "bwipe!" b |
| 54 | bwipe! |
| 55 | endfunc |
| 56 | |
| 57 | func Test_setbufline_getbufline_fold_tab() |
| 58 | split Xtest |
| 59 | setlocal foldmethod=expr foldexpr=0 |
| 60 | let b = bufnr('%') |
| 61 | tab new |
| 62 | call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) |
| 63 | call assert_equal(['foo'], getbufline(b, 1)) |
| 64 | call assert_equal(['bar'], getbufline(b, 2)) |
| 65 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 66 | exe "bwipe!" b |
| 67 | bwipe! |
| 68 | endfunc |
| 69 | |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 70 | func Test_setline_startup() |
| 71 | let cmd = GetVimCommand('Xscript') |
| 72 | if cmd == '' |
| 73 | return |
| 74 | endif |
Bram Moolenaar | 11aa62f | 2017-09-04 22:56:01 +0200 | [diff] [blame] | 75 | call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript') |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 76 | call system(cmd) |
| 77 | call assert_equal(['Hello'], readfile('Xtest')) |
| 78 | |
| 79 | call delete('Xscript') |
| 80 | call delete('Xtest') |
| 81 | endfunc |
Bram Moolenaar | ca85159 | 2018-06-06 21:04:07 +0200 | [diff] [blame] | 82 | |
| 83 | func Test_appendbufline() |
| 84 | new |
| 85 | let b = bufnr('%') |
| 86 | hide |
| 87 | call assert_equal(0, appendbufline(b, 0, ['foo', 'bar'])) |
| 88 | call assert_equal(['foo'], getbufline(b, 1)) |
| 89 | call assert_equal(['bar'], getbufline(b, 2)) |
| 90 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 91 | exe "bd!" b |
| 92 | call assert_equal([], getbufline(b, 1, 2)) |
| 93 | |
| 94 | split Xtest |
| 95 | call setline(1, ['a', 'b', 'c']) |
| 96 | let b = bufnr('%') |
| 97 | wincmd w |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 98 | |
| 99 | call assert_equal(1, appendbufline(b, -1, 'x')) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 100 | call assert_equal(1, appendbufline(b, -1, ['x'])) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 101 | call assert_equal(1, appendbufline(b, -1, [])) |
| 102 | call assert_equal(1, appendbufline(b, -1, test_null_list())) |
| 103 | |
| 104 | call assert_equal(1, appendbufline(b, 4, 'x')) |
Bram Moolenaar | ca85159 | 2018-06-06 21:04:07 +0200 | [diff] [blame] | 105 | call assert_equal(1, appendbufline(b, 4, ['x'])) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 106 | call assert_equal(1, appendbufline(b, 4, [])) |
| 107 | call assert_equal(1, appendbufline(b, 4, test_null_list())) |
| 108 | |
| 109 | call assert_equal(1, appendbufline(1234, 1, 'x')) |
Bram Moolenaar | ca85159 | 2018-06-06 21:04:07 +0200 | [diff] [blame] | 110 | call assert_equal(1, appendbufline(1234, 1, ['x'])) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 111 | call assert_equal(1, appendbufline(1234, 1, [])) |
| 112 | call assert_equal(1, appendbufline(1234, 1, test_null_list())) |
| 113 | |
| 114 | call assert_equal(0, appendbufline(b, 1, [])) |
| 115 | call assert_equal(0, appendbufline(b, 1, test_null_list())) |
| 116 | call assert_equal(1, appendbufline(b, 3, [])) |
| 117 | call assert_equal(1, appendbufline(b, 3, test_null_list())) |
| 118 | |
| 119 | call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$')) |
| 120 | |
Bram Moolenaar | ca85159 | 2018-06-06 21:04:07 +0200 | [diff] [blame] | 121 | call assert_equal(0, appendbufline(b, 3, ['d', 'e'])) |
| 122 | call assert_equal(['c'], getbufline(b, 3)) |
| 123 | call assert_equal(['d'], getbufline(b, 4)) |
| 124 | call assert_equal(['e'], getbufline(b, 5)) |
| 125 | call assert_equal([], getbufline(b, 6)) |
| 126 | exe "bwipe! " . b |
| 127 | endfunc |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 128 | |
Bram Moolenaar | 9cea87c | 2018-09-21 16:59:45 +0200 | [diff] [blame] | 129 | func Test_appendbufline_no_E315() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 130 | let after =<< trim [CODE] |
| 131 | set stl=%f ls=2 |
| 132 | new |
| 133 | let buf = bufnr("%") |
| 134 | quit |
| 135 | vsp |
| 136 | exec "buffer" buf |
| 137 | wincmd w |
| 138 | call appendbufline(buf, 0, "abc") |
| 139 | redraw |
| 140 | while getbufline(buf, 1)[0] =~ "^\\s*$" |
| 141 | sleep 10m |
| 142 | endwhile |
| 143 | au VimLeavePre * call writefile([v:errmsg], "Xerror") |
| 144 | au VimLeavePre * call writefile(["done"], "Xdone") |
| 145 | qall! |
| 146 | [CODE] |
| 147 | |
Bram Moolenaar | 9cea87c | 2018-09-21 16:59:45 +0200 | [diff] [blame] | 148 | if !RunVim([], after, '--clean') |
| 149 | return |
| 150 | endif |
| 151 | call assert_notmatch("^E315:", readfile("Xerror")[0]) |
| 152 | call assert_equal("done", readfile("Xdone")[0]) |
| 153 | call delete("Xerror") |
| 154 | call delete("Xdone") |
| 155 | endfunc |
| 156 | |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 157 | func Test_deletebufline() |
| 158 | new |
| 159 | let b = bufnr('%') |
| 160 | call setline(1, ['aaa', 'bbb', 'ccc']) |
| 161 | hide |
| 162 | call assert_equal(0, deletebufline(b, 2)) |
| 163 | call assert_equal(['aaa', 'ccc'], getbufline(b, 1, 2)) |
| 164 | call assert_equal(0, deletebufline(b, 2, 8)) |
| 165 | call assert_equal(['aaa'], getbufline(b, 1, 2)) |
| 166 | exe "bd!" b |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 167 | call assert_equal(1, b->deletebufline(1)) |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 168 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 169 | call assert_equal(1, deletebufline(-1, 1)) |
| 170 | |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 171 | split Xtest |
| 172 | call setline(1, ['a', 'b', 'c']) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 173 | call cursor(line('$'), 1) |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 174 | let b = bufnr('%') |
| 175 | wincmd w |
| 176 | call assert_equal(1, deletebufline(b, 4)) |
| 177 | call assert_equal(0, deletebufline(b, 1)) |
| 178 | call assert_equal(['b', 'c'], getbufline(b, 1, 2)) |
| 179 | exe "bwipe! " . b |
| 180 | endfunc |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 181 | |
| 182 | func Test_appendbufline_redraw() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 183 | CheckScreendump |
| 184 | |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 185 | let lines =<< trim END |
| 186 | new foo |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 187 | let winnr = 'foo'->bufwinnr() |
| 188 | let buf = bufnr('foo') |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 189 | wincmd p |
| 190 | call appendbufline(buf, '$', range(1,200)) |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 191 | exe winnr .. 'wincmd w' |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 192 | norm! G |
| 193 | wincmd p |
| 194 | call deletebufline(buf, 1, '$') |
| 195 | call appendbufline(buf, '$', 'Hello Vim world...') |
| 196 | END |
| 197 | call writefile(lines, 'XscriptMatchCommon') |
| 198 | let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 10}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 199 | call TermWait(buf) |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 200 | call VerifyScreenDump(buf, 'Test_appendbufline_1', {}) |
| 201 | |
| 202 | call StopVimInTerminal(buf) |
| 203 | call delete('XscriptMatchCommon') |
| 204 | endfunc |