xref: /vim-8.2.3635/src/testdir/test_bufline.vim (revision 963ffa0a)
1" Tests for setbufline(), getbufline(), appendbufline(), deletebufline()
2
3source shared.vim
4source screendump.vim
5source check.vim
6
7func Test_setbufline_getbufline()
8  " similar to Test_set_get_bufline()
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))
14  call assert_equal(['bar'], getbufline(b, '$'))
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
23
24  call assert_equal(1, setbufline(b, 5, 'x'))
25  call assert_equal(1, setbufline(b, 5, ['x']))
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))
30  call assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1))
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
36  call assert_equal(0, setbufline(b, 4, ['d', 'e']))
37  call assert_equal(['c'], b->getbufline(3))
38  call assert_equal(['d'], getbufline(b, 4))
39  call assert_equal(['e'], getbufline(b, 5))
40  call assert_equal([], getbufline(b, 6))
41  call assert_equal([], getbufline(b, 2, 1))
42
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
50  exe "bwipe! " . b
51endfunc
52
53func 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!
64endfunc
65
66func 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!
77endfunc
78
79func Test_setline_startup()
80  let cmd = GetVimCommand('Xscript')
81  if cmd == ''
82    return
83  endif
84  call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript')
85  call system(cmd)
86  call assert_equal(['Hello'], readfile('Xtest'))
87
88  call delete('Xscript')
89  call delete('Xtest')
90endfunc
91
92func 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
107
108  call assert_equal(1, appendbufline(b, -1, 'x'))
109  call assert_equal(1, appendbufline(b, -1, ['x']))
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'))
114  call assert_equal(1, appendbufline(b, 4, ['x']))
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'))
119  call assert_equal(1, appendbufline(1234, 1, ['x']))
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
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
136endfunc
137
138func Test_appendbufline_no_E315()
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
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")
164endfunc
165
166func 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
176  call assert_equal(1, b->deletebufline(1))
177
178  call assert_equal(1, deletebufline(-1, 1))
179
180  split Xtest
181  call setline(1, ['a', 'b', 'c'])
182  call cursor(line('$'), 1)
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
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
200endfunc
201
202func Test_appendbufline_redraw()
203  CheckScreendump
204
205  let lines =<< trim END
206    new foo
207    let winnr = 'foo'->bufwinnr()
208    let buf = bufnr('foo')
209    wincmd p
210    call appendbufline(buf, '$', range(1,200))
211    exe winnr .. 'wincmd w'
212    norm! G
213    wincmd p
214    call deletebufline(buf, 1, '$')
215    call appendbufline(buf, '$', 'Hello Vim world...')
216  END
217  call writefile(lines, 'XscriptMatchCommon')
218  let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 10})
219  call TermWait(buf)
220  call VerifyScreenDump(buf, 'Test_appendbufline_1', {})
221
222  call StopVimInTerminal(buf)
223  call delete('XscriptMatchCommon')
224endfunc
225
226" vim: shiftwidth=2 sts=2 expandtab
227