xref: /vim-8.2.3635/src/testdir/test_bufline.vim (revision 2d2970ea)
1" Tests for setbufline(), getbufline(), appendbufline(), deletebufline()
2
3source shared.vim
4source screendump.vim
5source check.vim
6
7func 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))
13  call assert_equal(['bar'], getbufline(b, '$'))
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
23  call assert_equal(1, setbufline(b, 5, 'x'))
24  call assert_equal(1, setbufline(b, 5, ['x']))
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))
29  call assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1))
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
35  call assert_equal(0, setbufline(b, 4, ['d', 'e']))
36  call assert_equal(['c'], b->getbufline(3))
37  call assert_equal(['d'], getbufline(b, 4))
38  call assert_equal(['e'], getbufline(b, 5))
39  call assert_equal([], getbufline(b, 6))
40  call assert_equal([], getbufline(b, 2, 1))
41  exe "bwipe! " . b
42endfunc
43
44func 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!
55endfunc
56
57func 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!
68endfunc
69
70func Test_setline_startup()
71  let cmd = GetVimCommand('Xscript')
72  if cmd == ''
73    return
74  endif
75  call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript')
76  call system(cmd)
77  call assert_equal(['Hello'], readfile('Xtest'))
78
79  call delete('Xscript')
80  call delete('Xtest')
81endfunc
82
83func 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
98
99  call assert_equal(1, appendbufline(b, -1, 'x'))
100  call assert_equal(1, appendbufline(b, -1, ['x']))
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'))
105  call assert_equal(1, appendbufline(b, 4, ['x']))
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'))
110  call assert_equal(1, appendbufline(1234, 1, ['x']))
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
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
127endfunc
128
129func Test_appendbufline_no_E315()
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
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")
155endfunc
156
157func 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
167  call assert_equal(1, b->deletebufline(1))
168
169  call assert_equal(1, deletebufline(-1, 1))
170
171  split Xtest
172  call setline(1, ['a', 'b', 'c'])
173  call cursor(line('$'), 1)
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
180endfunc
181
182func Test_appendbufline_redraw()
183  CheckScreendump
184
185  let lines =<< trim END
186    new foo
187    let winnr = 'foo'->bufwinnr()
188    let buf = bufnr('foo')
189    wincmd p
190    call appendbufline(buf, '$', range(1,200))
191    exe winnr .. 'wincmd w'
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})
199  call TermWait(buf)
200  call VerifyScreenDump(buf, 'Test_appendbufline_1', {})
201
202  call StopVimInTerminal(buf)
203  call delete('XscriptMatchCommon')
204endfunc
205