xref: /vim-8.2.3635/src/testdir/test_bufline.vim (revision beae4084)
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  exe "bwipe! " . b
41endfunc
42
43func Test_setbufline_getbufline_fold()
44  split Xtest
45  setlocal foldmethod=expr foldexpr=0
46  let b = bufnr('%')
47  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!
54endfunc
55
56func Test_setbufline_getbufline_fold_tab()
57  split Xtest
58  setlocal foldmethod=expr foldexpr=0
59  let b = bufnr('%')
60  tab new
61  call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
62  call assert_equal(['foo'], getbufline(b, 1))
63  call assert_equal(['bar'], getbufline(b, 2))
64  call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
65  exe "bwipe!" b
66  bwipe!
67endfunc
68
69func Test_setline_startup()
70  let cmd = GetVimCommand('Xscript')
71  if cmd == ''
72    return
73  endif
74  call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript')
75  call system(cmd)
76  call assert_equal(['Hello'], readfile('Xtest'))
77
78  call delete('Xscript')
79  call delete('Xtest')
80endfunc
81
82func Test_appendbufline()
83  new
84  let b = bufnr('%')
85  hide
86  call assert_equal(0, appendbufline(b, 0, ['foo', 'bar']))
87  call assert_equal(['foo'], getbufline(b, 1))
88  call assert_equal(['bar'], getbufline(b, 2))
89  call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
90  exe "bd!" b
91  call assert_equal([], getbufline(b, 1, 2))
92
93  split Xtest
94  call setline(1, ['a', 'b', 'c'])
95  let b = bufnr('%')
96  wincmd w
97
98  call assert_equal(1, appendbufline(b, -1, 'x'))
99  call assert_equal(1, appendbufline(b, -1, ['x']))
100  call assert_equal(1, appendbufline(b, -1, []))
101  call assert_equal(1, appendbufline(b, -1, test_null_list()))
102
103  call assert_equal(1, appendbufline(b, 4, 'x'))
104  call assert_equal(1, appendbufline(b, 4, ['x']))
105  call assert_equal(1, appendbufline(b, 4, []))
106  call assert_equal(1, appendbufline(b, 4, test_null_list()))
107
108  call assert_equal(1, appendbufline(1234, 1, 'x'))
109  call assert_equal(1, appendbufline(1234, 1, ['x']))
110  call assert_equal(1, appendbufline(1234, 1, []))
111  call assert_equal(1, appendbufline(1234, 1, test_null_list()))
112
113  call assert_equal(0, appendbufline(b, 1, []))
114  call assert_equal(0, appendbufline(b, 1, test_null_list()))
115  call assert_equal(1, appendbufline(b, 3, []))
116  call assert_equal(1, appendbufline(b, 3, test_null_list()))
117
118  call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
119
120  call assert_equal(0, appendbufline(b, 3, ['d', 'e']))
121  call assert_equal(['c'], getbufline(b, 3))
122  call assert_equal(['d'], getbufline(b, 4))
123  call assert_equal(['e'], getbufline(b, 5))
124  call assert_equal([], getbufline(b, 6))
125  exe "bwipe! " . b
126endfunc
127
128func Test_appendbufline_no_E315()
129  let after =<< trim [CODE]
130    set stl=%f ls=2
131    new
132    let buf = bufnr("%")
133    quit
134    vsp
135    exec "buffer" buf
136    wincmd w
137    call appendbufline(buf, 0, "abc")
138    redraw
139    while getbufline(buf, 1)[0] =~ "^\\s*$"
140      sleep 10m
141    endwhile
142    au VimLeavePre * call writefile([v:errmsg], "Xerror")
143    au VimLeavePre * call writefile(["done"], "Xdone")
144    qall!
145  [CODE]
146
147  if !RunVim([], after, '--clean')
148    return
149  endif
150  call assert_notmatch("^E315:", readfile("Xerror")[0])
151  call assert_equal("done", readfile("Xdone")[0])
152  call delete("Xerror")
153  call delete("Xdone")
154endfunc
155
156func Test_deletebufline()
157  new
158  let b = bufnr('%')
159  call setline(1, ['aaa', 'bbb', 'ccc'])
160  hide
161  call assert_equal(0, deletebufline(b, 2))
162  call assert_equal(['aaa', 'ccc'], getbufline(b, 1, 2))
163  call assert_equal(0, deletebufline(b, 2, 8))
164  call assert_equal(['aaa'], getbufline(b, 1, 2))
165  exe "bd!" b
166  call assert_equal(1, b->deletebufline(1))
167
168  call assert_equal(1, deletebufline(-1, 1))
169
170  split Xtest
171  call setline(1, ['a', 'b', 'c'])
172  call cursor(line('$'), 1)
173  let b = bufnr('%')
174  wincmd w
175  call assert_equal(1, deletebufline(b, 4))
176  call assert_equal(0, deletebufline(b, 1))
177  call assert_equal(['b', 'c'], getbufline(b, 1, 2))
178  exe "bwipe! " . b
179endfunc
180
181func Test_appendbufline_redraw()
182  CheckScreendump
183
184  let lines =<< trim END
185    new foo
186    let winnr = 'foo'->bufwinnr()
187    let buf = bufnr('foo')
188    wincmd p
189    call appendbufline(buf, '$', range(1,200))
190    exe winnr .. 'wincmd w'
191    norm! G
192    wincmd p
193    call deletebufline(buf, 1, '$')
194    call appendbufline(buf, '$', 'Hello Vim world...')
195  END
196  call writefile(lines, 'XscriptMatchCommon')
197  let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 10})
198  call TermWait(buf)
199  call VerifyScreenDump(buf, 'Test_appendbufline_1', {})
200
201  call StopVimInTerminal(buf)
202  call delete('XscriptMatchCommon')
203endfunc
204