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