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 | |
Bram Moolenaar | 0038511 | 2021-02-07 14:31:06 +0100 | [diff] [blame] | 43 | if has('job') |
| 44 | call setbufline(b, 2, [function('eval'), #{key: 123}, test_null_job()]) |
| 45 | call assert_equal(["function('eval')", |
| 46 | \ "{'key': 123}", |
| 47 | \ "no process"], |
| 48 | \ getbufline(b, 2, 4)) |
| 49 | endif |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 50 | exe "bwipe! " . b |
| 51 | endfunc |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 52 | |
Bram Moolenaar | 0c4dc88 | 2017-11-06 21:32:54 +0100 | [diff] [blame] | 53 | func Test_setbufline_getbufline_fold() |
| 54 | split Xtest |
| 55 | setlocal foldmethod=expr foldexpr=0 |
| 56 | let b = bufnr('%') |
| 57 | new |
| 58 | call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) |
| 59 | call assert_equal(['foo'], getbufline(b, 1)) |
| 60 | call assert_equal(['bar'], getbufline(b, 2)) |
| 61 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 62 | exe "bwipe!" b |
| 63 | bwipe! |
| 64 | endfunc |
| 65 | |
| 66 | func Test_setbufline_getbufline_fold_tab() |
| 67 | split Xtest |
| 68 | setlocal foldmethod=expr foldexpr=0 |
| 69 | let b = bufnr('%') |
| 70 | tab new |
| 71 | call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) |
| 72 | call assert_equal(['foo'], getbufline(b, 1)) |
| 73 | call assert_equal(['bar'], getbufline(b, 2)) |
| 74 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 75 | exe "bwipe!" b |
| 76 | bwipe! |
| 77 | endfunc |
| 78 | |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 79 | func Test_setline_startup() |
| 80 | let cmd = GetVimCommand('Xscript') |
| 81 | if cmd == '' |
| 82 | return |
| 83 | endif |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 84 | call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript', 'D') |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 85 | call system(cmd) |
K.Takata | 0500e87 | 2022-09-08 12:28:02 +0100 | [diff] [blame] | 86 | sleep 50m |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 87 | call assert_equal(['Hello'], readfile('Xtest')) |
| 88 | |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 89 | call delete('Xtest') |
| 90 | endfunc |
Bram Moolenaar | ca85159 | 2018-06-06 21:04:07 +0200 | [diff] [blame] | 91 | |
| 92 | func Test_appendbufline() |
| 93 | new |
| 94 | let b = bufnr('%') |
| 95 | hide |
| 96 | call assert_equal(0, appendbufline(b, 0, ['foo', 'bar'])) |
| 97 | call assert_equal(['foo'], getbufline(b, 1)) |
| 98 | call assert_equal(['bar'], getbufline(b, 2)) |
| 99 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 100 | exe "bd!" b |
| 101 | call assert_equal([], getbufline(b, 1, 2)) |
| 102 | |
| 103 | split Xtest |
| 104 | call setline(1, ['a', 'b', 'c']) |
| 105 | let b = bufnr('%') |
| 106 | wincmd w |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 107 | |
| 108 | call assert_equal(1, appendbufline(b, -1, 'x')) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 109 | call assert_equal(1, appendbufline(b, -1, ['x'])) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 110 | call assert_equal(1, appendbufline(b, -1, [])) |
| 111 | call assert_equal(1, appendbufline(b, -1, test_null_list())) |
| 112 | |
| 113 | call assert_equal(1, appendbufline(b, 4, 'x')) |
Bram Moolenaar | ca85159 | 2018-06-06 21:04:07 +0200 | [diff] [blame] | 114 | call assert_equal(1, appendbufline(b, 4, ['x'])) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 115 | call assert_equal(1, appendbufline(b, 4, [])) |
| 116 | call assert_equal(1, appendbufline(b, 4, test_null_list())) |
| 117 | |
| 118 | call assert_equal(1, appendbufline(1234, 1, 'x')) |
Bram Moolenaar | ca85159 | 2018-06-06 21:04:07 +0200 | [diff] [blame] | 119 | call assert_equal(1, appendbufline(1234, 1, ['x'])) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 120 | call assert_equal(1, appendbufline(1234, 1, [])) |
| 121 | call assert_equal(1, appendbufline(1234, 1, test_null_list())) |
| 122 | |
| 123 | call assert_equal(0, appendbufline(b, 1, [])) |
| 124 | call assert_equal(0, appendbufline(b, 1, test_null_list())) |
| 125 | call assert_equal(1, appendbufline(b, 3, [])) |
| 126 | call assert_equal(1, appendbufline(b, 3, test_null_list())) |
| 127 | |
| 128 | call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$')) |
| 129 | |
Bram Moolenaar | ca85159 | 2018-06-06 21:04:07 +0200 | [diff] [blame] | 130 | call assert_equal(0, appendbufline(b, 3, ['d', 'e'])) |
| 131 | call assert_equal(['c'], getbufline(b, 3)) |
| 132 | call assert_equal(['d'], getbufline(b, 4)) |
| 133 | call assert_equal(['e'], getbufline(b, 5)) |
| 134 | call assert_equal([], getbufline(b, 6)) |
| 135 | exe "bwipe! " . b |
| 136 | endfunc |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 137 | |
Bram Moolenaar | 9cea87c | 2018-09-21 16:59:45 +0200 | [diff] [blame] | 138 | func Test_appendbufline_no_E315() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 139 | let after =<< trim [CODE] |
| 140 | set stl=%f ls=2 |
| 141 | new |
| 142 | let buf = bufnr("%") |
| 143 | quit |
| 144 | vsp |
| 145 | exec "buffer" buf |
| 146 | wincmd w |
| 147 | call appendbufline(buf, 0, "abc") |
| 148 | redraw |
| 149 | while getbufline(buf, 1)[0] =~ "^\\s*$" |
| 150 | sleep 10m |
| 151 | endwhile |
| 152 | au VimLeavePre * call writefile([v:errmsg], "Xerror") |
| 153 | au VimLeavePre * call writefile(["done"], "Xdone") |
| 154 | qall! |
| 155 | [CODE] |
| 156 | |
Bram Moolenaar | 9cea87c | 2018-09-21 16:59:45 +0200 | [diff] [blame] | 157 | if !RunVim([], after, '--clean') |
| 158 | return |
| 159 | endif |
| 160 | call assert_notmatch("^E315:", readfile("Xerror")[0]) |
| 161 | call assert_equal("done", readfile("Xdone")[0]) |
| 162 | call delete("Xerror") |
| 163 | call delete("Xdone") |
| 164 | endfunc |
| 165 | |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 166 | func Test_deletebufline() |
| 167 | new |
| 168 | let b = bufnr('%') |
| 169 | call setline(1, ['aaa', 'bbb', 'ccc']) |
| 170 | hide |
| 171 | call assert_equal(0, deletebufline(b, 2)) |
| 172 | call assert_equal(['aaa', 'ccc'], getbufline(b, 1, 2)) |
| 173 | call assert_equal(0, deletebufline(b, 2, 8)) |
| 174 | call assert_equal(['aaa'], getbufline(b, 1, 2)) |
| 175 | exe "bd!" b |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 176 | call assert_equal(1, b->deletebufline(1)) |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 177 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 178 | call assert_equal(1, deletebufline(-1, 1)) |
| 179 | |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 180 | split Xtest |
| 181 | call setline(1, ['a', 'b', 'c']) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 182 | call cursor(line('$'), 1) |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 183 | let b = bufnr('%') |
| 184 | wincmd w |
| 185 | call assert_equal(1, deletebufline(b, 4)) |
| 186 | call assert_equal(0, deletebufline(b, 1)) |
| 187 | call assert_equal(['b', 'c'], getbufline(b, 1, 2)) |
| 188 | exe "bwipe! " . b |
Bram Moolenaar | 963ffa0 | 2021-02-09 20:02:55 +0100 | [diff] [blame] | 189 | |
| 190 | edit XbufOne |
| 191 | let one = bufnr() |
| 192 | call setline(1, ['a', 'b', 'c']) |
| 193 | setlocal nomodifiable |
| 194 | split XbufTwo |
| 195 | let two = bufnr() |
| 196 | call assert_fails('call deletebufline(one, 1)', 'E21:') |
| 197 | call assert_equal(two, bufnr()) |
| 198 | bwipe! XbufTwo |
| 199 | bwipe! XbufOne |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 200 | endfunc |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 201 | |
| 202 | func Test_appendbufline_redraw() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 203 | CheckScreendump |
| 204 | |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 205 | let lines =<< trim END |
| 206 | new foo |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 207 | let winnr = 'foo'->bufwinnr() |
| 208 | let buf = bufnr('foo') |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 209 | wincmd p |
| 210 | call appendbufline(buf, '$', range(1,200)) |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 211 | exe winnr .. 'wincmd w' |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 212 | norm! G |
| 213 | wincmd p |
| 214 | call deletebufline(buf, 1, '$') |
| 215 | call appendbufline(buf, '$', 'Hello Vim world...') |
| 216 | END |
Bram Moolenaar | 3411265 | 2022-09-05 21:40:44 +0100 | [diff] [blame] | 217 | call writefile(lines, 'XscriptMatchCommon', 'D') |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 218 | let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 10}) |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 219 | call VerifyScreenDump(buf, 'Test_appendbufline_1', {}) |
| 220 | |
| 221 | call StopVimInTerminal(buf) |
Bram Moolenaar | 2984666 | 2019-07-27 17:39:15 +0200 | [diff] [blame] | 222 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 223 | |
Bram Moolenaar | 0ad00a7 | 2022-05-22 11:59:25 +0100 | [diff] [blame] | 224 | func Test_setbufline_select_mode() |
| 225 | new |
| 226 | call setline(1, ['foo', 'bar']) |
| 227 | call feedkeys("j^v2l\<C-G>", 'nx') |
| 228 | |
| 229 | let bufnr = bufadd('Xdummy') |
| 230 | call bufload(bufnr) |
| 231 | call setbufline(bufnr, 1, ['abc']) |
| 232 | |
| 233 | call feedkeys("x", 'nx') |
| 234 | call assert_equal(['foo', 'x'], getline(1, 2)) |
| 235 | |
| 236 | exe "bwipe! " .. bufnr |
| 237 | bwipe! |
| 238 | endfunc |
| 239 | |
LemonBoy | 9b2edfd | 2022-05-22 15:35:53 +0100 | [diff] [blame] | 240 | func Test_deletebufline_select_mode() |
| 241 | new |
| 242 | call setline(1, ['foo', 'bar']) |
| 243 | call feedkeys("j^v2l\<C-G>", 'nx') |
| 244 | |
| 245 | let bufnr = bufadd('Xdummy') |
| 246 | call bufload(bufnr) |
| 247 | call setbufline(bufnr, 1, ['abc', 'def']) |
| 248 | call deletebufline(bufnr, 1) |
| 249 | |
| 250 | call feedkeys("x", 'nx') |
| 251 | call assert_equal(['foo', 'x'], getline(1, 2)) |
| 252 | |
| 253 | exe "bwipe! " .. bufnr |
| 254 | bwipe! |
| 255 | endfunc |
| 256 | |
zeertzjq | 93f72cc | 2022-08-26 15:34:52 +0100 | [diff] [blame] | 257 | func Test_setbufline_startup_nofile() |
| 258 | let before =<< trim [CODE] |
| 259 | set shortmess+=F |
| 260 | file Xresult |
| 261 | set buftype=nofile |
| 262 | call setbufline('', 1, 'success') |
| 263 | [CODE] |
| 264 | let after =<< trim [CODE] |
| 265 | set buftype= |
| 266 | write |
| 267 | quit |
| 268 | [CODE] |
| 269 | |
| 270 | if !RunVim(before, after, '--clean') |
| 271 | return |
| 272 | endif |
| 273 | call assert_equal(['success'], readfile('Xresult')) |
| 274 | call delete('Xresult') |
| 275 | endfunc |
| 276 | |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 277 | " vim: shiftwidth=2 sts=2 expandtab |