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 | 11aa62f | 2017-09-04 22:56:01 +0200 | [diff] [blame] | 4 | |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 5 | func Test_setbufline_getbufline() |
| 6 | new |
| 7 | let b = bufnr('%') |
| 8 | hide |
| 9 | call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) |
| 10 | call assert_equal(['foo'], getbufline(b, 1)) |
| 11 | call assert_equal(['bar'], getbufline(b, 2)) |
| 12 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 13 | exe "bd!" b |
| 14 | call assert_equal([], getbufline(b, 1, 2)) |
| 15 | |
| 16 | split Xtest |
| 17 | call setline(1, ['a', 'b', 'c']) |
| 18 | let b = bufnr('%') |
| 19 | wincmd w |
| 20 | call assert_equal(1, setbufline(b, 5, ['x'])) |
| 21 | call assert_equal(1, setbufline(1234, 1, ['x'])) |
| 22 | call assert_equal(0, setbufline(b, 4, ['d', 'e'])) |
| 23 | call assert_equal(['c'], getbufline(b, 3)) |
| 24 | call assert_equal(['d'], getbufline(b, 4)) |
| 25 | call assert_equal(['e'], getbufline(b, 5)) |
| 26 | call assert_equal([], getbufline(b, 6)) |
| 27 | exe "bwipe! " . b |
| 28 | endfunc |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 29 | |
Bram Moolenaar | 0c4dc88 | 2017-11-06 21:32:54 +0100 | [diff] [blame] | 30 | func Test_setbufline_getbufline_fold() |
| 31 | split Xtest |
| 32 | setlocal foldmethod=expr foldexpr=0 |
| 33 | let b = bufnr('%') |
| 34 | new |
| 35 | call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) |
| 36 | call assert_equal(['foo'], getbufline(b, 1)) |
| 37 | call assert_equal(['bar'], getbufline(b, 2)) |
| 38 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 39 | exe "bwipe!" b |
| 40 | bwipe! |
| 41 | endfunc |
| 42 | |
| 43 | func Test_setbufline_getbufline_fold_tab() |
| 44 | split Xtest |
| 45 | setlocal foldmethod=expr foldexpr=0 |
| 46 | let b = bufnr('%') |
| 47 | tab new |
| 48 | call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) |
| 49 | call assert_equal(['foo'], getbufline(b, 1)) |
| 50 | call assert_equal(['bar'], getbufline(b, 2)) |
| 51 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 52 | exe "bwipe!" b |
| 53 | bwipe! |
| 54 | endfunc |
| 55 | |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 56 | func Test_setline_startup() |
| 57 | let cmd = GetVimCommand('Xscript') |
| 58 | if cmd == '' |
| 59 | return |
| 60 | endif |
Bram Moolenaar | 11aa62f | 2017-09-04 22:56:01 +0200 | [diff] [blame] | 61 | call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript') |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 62 | call system(cmd) |
| 63 | call assert_equal(['Hello'], readfile('Xtest')) |
| 64 | |
| 65 | call delete('Xscript') |
| 66 | call delete('Xtest') |
| 67 | endfunc |
Bram Moolenaar | ca85159 | 2018-06-06 21:04:07 +0200 | [diff] [blame] | 68 | |
| 69 | func Test_appendbufline() |
| 70 | new |
| 71 | let b = bufnr('%') |
| 72 | hide |
| 73 | call assert_equal(0, appendbufline(b, 0, ['foo', 'bar'])) |
| 74 | call assert_equal(['foo'], getbufline(b, 1)) |
| 75 | call assert_equal(['bar'], getbufline(b, 2)) |
| 76 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
| 77 | exe "bd!" b |
| 78 | call assert_equal([], getbufline(b, 1, 2)) |
| 79 | |
| 80 | split Xtest |
| 81 | call setline(1, ['a', 'b', 'c']) |
| 82 | let b = bufnr('%') |
| 83 | wincmd w |
| 84 | call assert_equal(1, appendbufline(b, 4, ['x'])) |
| 85 | call assert_equal(1, appendbufline(1234, 1, ['x'])) |
| 86 | call assert_equal(0, appendbufline(b, 3, ['d', 'e'])) |
| 87 | call assert_equal(['c'], getbufline(b, 3)) |
| 88 | call assert_equal(['d'], getbufline(b, 4)) |
| 89 | call assert_equal(['e'], getbufline(b, 5)) |
| 90 | call assert_equal([], getbufline(b, 6)) |
| 91 | exe "bwipe! " . b |
| 92 | endfunc |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 93 | |
Bram Moolenaar | 9cea87c | 2018-09-21 16:59:45 +0200 | [diff] [blame] | 94 | func Test_appendbufline_no_E315() |
| 95 | let after = [ |
| 96 | \ 'set stl=%f ls=2', |
| 97 | \ 'new', |
| 98 | \ 'let buf = bufnr("%")', |
| 99 | \ 'quit', |
| 100 | \ 'vsp', |
| 101 | \ 'exec "buffer" buf', |
| 102 | \ 'wincmd w', |
| 103 | \ 'call appendbufline(buf, 0, "abc")', |
| 104 | \ 'redraw', |
| 105 | \ 'while getbufline(buf, 1)[0] =~ "^\\s*$"', |
| 106 | \ ' sleep 10m', |
| 107 | \ 'endwhile', |
| 108 | \ 'au VimLeavePre * call writefile([v:errmsg], "Xerror")', |
| 109 | \ 'au VimLeavePre * call writefile(["done"], "Xdone")', |
| 110 | \ 'qall!', |
| 111 | \ ] |
| 112 | if !RunVim([], after, '--clean') |
| 113 | return |
| 114 | endif |
| 115 | call assert_notmatch("^E315:", readfile("Xerror")[0]) |
| 116 | call assert_equal("done", readfile("Xdone")[0]) |
| 117 | call delete("Xerror") |
| 118 | call delete("Xdone") |
| 119 | endfunc |
| 120 | |
Bram Moolenaar | d79a262 | 2018-06-07 18:17:46 +0200 | [diff] [blame] | 121 | func Test_deletebufline() |
| 122 | new |
| 123 | let b = bufnr('%') |
| 124 | call setline(1, ['aaa', 'bbb', 'ccc']) |
| 125 | hide |
| 126 | call assert_equal(0, deletebufline(b, 2)) |
| 127 | call assert_equal(['aaa', 'ccc'], getbufline(b, 1, 2)) |
| 128 | call assert_equal(0, deletebufline(b, 2, 8)) |
| 129 | call assert_equal(['aaa'], getbufline(b, 1, 2)) |
| 130 | exe "bd!" b |
| 131 | call assert_equal(1, deletebufline(b, 1)) |
| 132 | |
| 133 | split Xtest |
| 134 | call setline(1, ['a', 'b', 'c']) |
| 135 | let b = bufnr('%') |
| 136 | wincmd w |
| 137 | call assert_equal(1, deletebufline(b, 4)) |
| 138 | call assert_equal(0, deletebufline(b, 1)) |
| 139 | call assert_equal(['b', 'c'], getbufline(b, 1, 2)) |
| 140 | exe "bwipe! " . b |
| 141 | endfunc |