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