xref: /vim-8.2.3635/src/testdir/test_bufline.vim (revision fcfe1a9b)
1" Tests for setbufline(), getbufline(), appendbufline(), deletebufline()
2
3source shared.vim
4source screendump.vim
5
6func Test_setbufline_getbufline()
7  new
8  let b = bufnr('%')
9  hide
10  call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
11  call assert_equal(['foo'], getbufline(b, 1))
12  call assert_equal(['bar'], getbufline(b, '$'))
13  call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
14  exe "bd!" b
15  call assert_equal([], getbufline(b, 1, 2))
16
17  split Xtest
18  call setline(1, ['a', 'b', 'c'])
19  let b = bufnr('%')
20  wincmd w
21  call assert_equal(1, setbufline(b, 5, ['x']))
22  call assert_equal(1, setbufline(bufnr('$') + 1, 1, ['x']))
23  call assert_equal(0, setbufline(b, 4, ['d', 'e']))
24  call assert_equal(['c'], getbufline(b, 3))
25  call assert_equal(['d'], getbufline(b, 4))
26  call assert_equal(['e'], getbufline(b, 5))
27  call assert_equal([], getbufline(b, 6))
28  exe "bwipe! " . b
29endfunc
30
31func Test_setbufline_getbufline_fold()
32  split Xtest
33  setlocal foldmethod=expr foldexpr=0
34  let b = bufnr('%')
35  new
36  call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
37  call assert_equal(['foo'], getbufline(b, 1))
38  call assert_equal(['bar'], getbufline(b, 2))
39  call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
40  exe "bwipe!" b
41  bwipe!
42endfunc
43
44func Test_setbufline_getbufline_fold_tab()
45  split Xtest
46  setlocal foldmethod=expr foldexpr=0
47  let b = bufnr('%')
48  tab 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_setline_startup()
58  let cmd = GetVimCommand('Xscript')
59  if cmd == ''
60    return
61  endif
62  call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript')
63  call system(cmd)
64  call assert_equal(['Hello'], readfile('Xtest'))
65
66  call delete('Xscript')
67  call delete('Xtest')
68endfunc
69
70func Test_appendbufline()
71  new
72  let b = bufnr('%')
73  hide
74  call assert_equal(0, appendbufline(b, 0, ['foo', 'bar']))
75  call assert_equal(['foo'], getbufline(b, 1))
76  call assert_equal(['bar'], getbufline(b, 2))
77  call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
78  exe "bd!" b
79  call assert_equal([], getbufline(b, 1, 2))
80
81  split Xtest
82  call setline(1, ['a', 'b', 'c'])
83  let b = bufnr('%')
84  wincmd w
85  call assert_equal(1, appendbufline(b, -1, ['x']))
86  call assert_equal(1, appendbufline(b, 4, ['x']))
87  call assert_equal(1, appendbufline(1234, 1, ['x']))
88  call assert_equal(0, appendbufline(b, 3, ['d', 'e']))
89  call assert_equal(['c'], getbufline(b, 3))
90  call assert_equal(['d'], getbufline(b, 4))
91  call assert_equal(['e'], getbufline(b, 5))
92  call assert_equal([], getbufline(b, 6))
93  exe "bwipe! " . b
94endfunc
95
96func Test_appendbufline_no_E315()
97  let after =<< trim [CODE]
98    set stl=%f ls=2
99    new
100    let buf = bufnr("%")
101    quit
102    vsp
103    exec "buffer" buf
104    wincmd w
105    call appendbufline(buf, 0, "abc")
106    redraw
107    while getbufline(buf, 1)[0] =~ "^\\s*$"
108      sleep 10m
109    endwhile
110    au VimLeavePre * call writefile([v:errmsg], "Xerror")
111    au VimLeavePre * call writefile(["done"], "Xdone")
112    qall!
113  [CODE]
114
115  if !RunVim([], after, '--clean')
116    return
117  endif
118  call assert_notmatch("^E315:", readfile("Xerror")[0])
119  call assert_equal("done", readfile("Xdone")[0])
120  call delete("Xerror")
121  call delete("Xdone")
122endfunc
123
124func Test_deletebufline()
125  new
126  let b = bufnr('%')
127  call setline(1, ['aaa', 'bbb', 'ccc'])
128  hide
129  call assert_equal(0, deletebufline(b, 2))
130  call assert_equal(['aaa', 'ccc'], getbufline(b, 1, 2))
131  call assert_equal(0, deletebufline(b, 2, 8))
132  call assert_equal(['aaa'], getbufline(b, 1, 2))
133  exe "bd!" b
134  call assert_equal(1, deletebufline(b, 1))
135
136  call assert_equal(1, deletebufline(-1, 1))
137
138  split Xtest
139  call setline(1, ['a', 'b', 'c'])
140  call cursor(line('$'), 1)
141  let b = bufnr('%')
142  wincmd w
143  call assert_equal(1, deletebufline(b, 4))
144  call assert_equal(0, deletebufline(b, 1))
145  call assert_equal(['b', 'c'], getbufline(b, 1, 2))
146  exe "bwipe! " . b
147endfunc
148
149func Test_appendbufline_redraw()
150  if !CanRunVimInTerminal()
151    throw 'Skipped: cannot make screendumps'
152  endif
153  let lines =<< trim END
154    new foo
155    let winnr=bufwinnr('foo')
156    let buf=bufnr('foo')
157    wincmd p
158    call appendbufline(buf, '$', range(1,200))
159    exe winnr. 'wincmd w'
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})
167  call term_wait(buf)
168  call VerifyScreenDump(buf, 'Test_appendbufline_1', {})
169
170  call StopVimInTerminal(buf)
171  call delete('XscriptMatchCommon')
172endfunc
173