2019-08-22 20:27:04 -07:00
|
|
|
" Tests for setbufline(), getbufline(), appendbufline(), deletebufline()
|
2019-06-17 05:42:51 -07:00
|
|
|
|
2019-06-17 15:09:45 -07:00
|
|
|
source shared.vim
|
2020-04-28 20:13:23 -07:00
|
|
|
source screendump.vim
|
2022-10-26 06:53:11 -07:00
|
|
|
source check.vim
|
2019-06-17 15:09:45 -07:00
|
|
|
|
2019-06-17 05:42:51 -07:00
|
|
|
func Test_setbufline_getbufline()
|
2022-11-21 15:56:56 -07:00
|
|
|
" similar to Test_set_get_bufline()
|
2019-06-17 05:42:51 -07:00
|
|
|
new
|
|
|
|
let b = bufnr('%')
|
|
|
|
hide
|
|
|
|
call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
|
|
|
|
call assert_equal(['foo'], getbufline(b, 1))
|
2022-11-21 15:52:21 -07:00
|
|
|
call assert_equal('foo', getbufoneline(b, 1))
|
2021-08-27 14:07:34 -07:00
|
|
|
call assert_equal(['bar'], getbufline(b, '$'))
|
2022-11-21 15:52:21 -07:00
|
|
|
call assert_equal('bar', getbufoneline(b, '$'))
|
2019-06-17 05:42:51 -07:00
|
|
|
call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
|
|
|
|
exe "bd!" b
|
|
|
|
call assert_equal([], getbufline(b, 1, 2))
|
|
|
|
|
|
|
|
split Xtest
|
|
|
|
call setline(1, ['a', 'b', 'c'])
|
|
|
|
let b = bufnr('%')
|
|
|
|
wincmd w
|
2022-11-04 21:26:17 -07:00
|
|
|
|
|
|
|
call assert_equal(1, setbufline(b, 5, 'x'))
|
2019-06-17 05:42:51 -07:00
|
|
|
call assert_equal(1, setbufline(b, 5, ['x']))
|
2023-08-22 03:21:15 -07:00
|
|
|
call assert_equal(0, setbufline(b, 5, []))
|
|
|
|
call assert_equal(0, setbufline(b, 5, v:_null_list))
|
2022-11-04 21:26:17 -07:00
|
|
|
|
|
|
|
call assert_equal(1, 'x'->setbufline(bufnr('$') + 1, 1))
|
2022-08-22 17:36:22 -07:00
|
|
|
call assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1))
|
2022-11-04 21:26:17 -07:00
|
|
|
call assert_equal(1, []->setbufline(bufnr('$') + 1, 1))
|
|
|
|
call assert_equal(1, v:_null_list->setbufline(bufnr('$') + 1, 1))
|
|
|
|
|
|
|
|
call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
|
|
|
|
|
2019-06-17 05:42:51 -07:00
|
|
|
call assert_equal(0, setbufline(b, 4, ['d', 'e']))
|
2021-08-27 13:09:37 -07:00
|
|
|
call assert_equal(['c'], b->getbufline(3))
|
2022-11-21 15:52:21 -07:00
|
|
|
call assert_equal('c', b->getbufoneline(3))
|
2019-06-17 05:42:51 -07:00
|
|
|
call assert_equal(['d'], getbufline(b, 4))
|
2022-11-21 15:52:21 -07:00
|
|
|
call assert_equal('d', getbufoneline(b, 4))
|
2019-06-17 05:42:51 -07:00
|
|
|
call assert_equal(['e'], getbufline(b, 5))
|
2022-11-21 15:52:21 -07:00
|
|
|
call assert_equal('e', getbufoneline(b, 5))
|
2019-06-17 05:42:51 -07:00
|
|
|
call assert_equal([], getbufline(b, 6))
|
2022-11-04 16:34:06 -07:00
|
|
|
call assert_equal([], getbufline(b, 2, 1))
|
2022-11-21 15:56:56 -07:00
|
|
|
|
2022-11-21 16:11:24 -07:00
|
|
|
if has('job')
|
|
|
|
call setbufline(b, 2, [function('eval'), #{key: 123}, test_null_job()])
|
|
|
|
call assert_equal(["function('eval')",
|
|
|
|
\ "{'key': 123}",
|
|
|
|
\ "no process"],
|
|
|
|
\ getbufline(b, 2, 4))
|
|
|
|
endif
|
2019-06-17 05:42:51 -07:00
|
|
|
exe "bwipe! " . b
|
|
|
|
endfunc
|
2019-06-17 15:09:45 -07:00
|
|
|
|
2019-06-17 15:48:09 -07:00
|
|
|
func Test_setbufline_getbufline_fold()
|
|
|
|
split Xtest
|
|
|
|
setlocal foldmethod=expr foldexpr=0
|
|
|
|
let b = bufnr('%')
|
|
|
|
new
|
|
|
|
call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
|
|
|
|
call assert_equal(['foo'], getbufline(b, 1))
|
|
|
|
call assert_equal(['bar'], getbufline(b, 2))
|
|
|
|
call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
|
|
|
|
exe "bwipe!" b
|
|
|
|
bwipe!
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_setbufline_getbufline_fold_tab()
|
|
|
|
split Xtest
|
|
|
|
setlocal foldmethod=expr foldexpr=0
|
|
|
|
let b = bufnr('%')
|
|
|
|
tab new
|
|
|
|
call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
|
|
|
|
call assert_equal(['foo'], getbufline(b, 1))
|
|
|
|
call assert_equal(['bar'], getbufline(b, 2))
|
|
|
|
call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
|
|
|
|
exe "bwipe!" b
|
|
|
|
bwipe!
|
|
|
|
endfunc
|
|
|
|
|
2019-06-17 15:09:45 -07:00
|
|
|
func Test_setline_startup()
|
|
|
|
let cmd = GetVimCommand('Xscript')
|
|
|
|
if cmd == ''
|
|
|
|
return
|
|
|
|
endif
|
2023-08-22 02:44:04 -07:00
|
|
|
call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript', 'D')
|
2019-06-17 15:09:45 -07:00
|
|
|
call system(cmd)
|
2023-08-22 02:45:44 -07:00
|
|
|
sleep 50m
|
2019-06-17 15:09:45 -07:00
|
|
|
call assert_equal(['Hello'], readfile('Xtest'))
|
|
|
|
|
2023-08-22 03:21:15 -07:00
|
|
|
call assert_equal(0, setline(1, []))
|
|
|
|
call assert_equal(0, setline(1, v:_null_list))
|
|
|
|
call assert_equal(0, setline(5, []))
|
|
|
|
call assert_equal(0, setline(6, v:_null_list))
|
|
|
|
|
2019-06-17 15:09:45 -07:00
|
|
|
call delete('Xtest')
|
|
|
|
endfunc
|
2019-08-22 19:39:28 -07:00
|
|
|
|
|
|
|
func Test_appendbufline()
|
|
|
|
new
|
|
|
|
let b = bufnr('%')
|
|
|
|
hide
|
2022-11-29 01:55:46 -07:00
|
|
|
|
|
|
|
new
|
|
|
|
call setline(1, ['line1', 'line2', 'line3'])
|
|
|
|
normal! 2gggg
|
|
|
|
call assert_equal(2, line("''"))
|
|
|
|
|
2019-08-22 19:39:28 -07:00
|
|
|
call assert_equal(0, appendbufline(b, 0, ['foo', 'bar']))
|
|
|
|
call assert_equal(['foo'], getbufline(b, 1))
|
|
|
|
call assert_equal(['bar'], getbufline(b, 2))
|
|
|
|
call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
|
2022-11-29 01:55:46 -07:00
|
|
|
call assert_equal(0, appendbufline(b, 0, 'baz'))
|
|
|
|
call assert_equal(['baz', 'foo', 'bar'], getbufline(b, 1, 3))
|
|
|
|
|
|
|
|
" appendbufline() in a hidden buffer shouldn't move marks in current window.
|
|
|
|
call assert_equal(2, line("''"))
|
|
|
|
bwipe!
|
|
|
|
|
2019-08-22 19:39:28 -07:00
|
|
|
exe "bd!" b
|
2022-11-29 01:55:46 -07:00
|
|
|
call assert_equal([], getbufline(b, 1, 3))
|
2019-08-22 19:39:28 -07:00
|
|
|
|
|
|
|
split Xtest
|
|
|
|
call setline(1, ['a', 'b', 'c'])
|
|
|
|
let b = bufnr('%')
|
|
|
|
wincmd w
|
2022-11-04 21:26:17 -07:00
|
|
|
|
|
|
|
call assert_equal(1, appendbufline(b, -1, 'x'))
|
2021-08-27 14:07:34 -07:00
|
|
|
call assert_equal(1, appendbufline(b, -1, ['x']))
|
2022-11-04 21:26:17 -07:00
|
|
|
call assert_equal(1, appendbufline(b, -1, []))
|
|
|
|
call assert_equal(1, appendbufline(b, -1, v:_null_list))
|
|
|
|
|
|
|
|
call assert_equal(1, appendbufline(b, 4, 'x'))
|
2019-08-22 19:39:28 -07:00
|
|
|
call assert_equal(1, appendbufline(b, 4, ['x']))
|
2023-08-22 03:21:15 -07:00
|
|
|
call assert_equal(0, appendbufline(b, 4, []))
|
|
|
|
call assert_equal(0, appendbufline(b, 4, v:_null_list))
|
2022-11-04 21:26:17 -07:00
|
|
|
|
|
|
|
call assert_equal(1, appendbufline(1234, 1, 'x'))
|
2019-08-22 19:39:28 -07:00
|
|
|
call assert_equal(1, appendbufline(1234, 1, ['x']))
|
2022-11-04 21:26:17 -07:00
|
|
|
call assert_equal(1, appendbufline(1234, 1, []))
|
|
|
|
call assert_equal(1, appendbufline(1234, 1, v:_null_list))
|
|
|
|
|
|
|
|
call assert_equal(0, appendbufline(b, 1, []))
|
|
|
|
call assert_equal(0, appendbufline(b, 1, v:_null_list))
|
2023-08-22 03:21:15 -07:00
|
|
|
call assert_equal(0, appendbufline(b, 3, []))
|
|
|
|
call assert_equal(0, appendbufline(b, 3, v:_null_list))
|
2022-11-04 21:26:17 -07:00
|
|
|
|
|
|
|
call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
|
|
|
|
|
2019-08-22 19:39:28 -07:00
|
|
|
call assert_equal(0, appendbufline(b, 3, ['d', 'e']))
|
|
|
|
call assert_equal(['c'], getbufline(b, 3))
|
|
|
|
call assert_equal(['d'], getbufline(b, 4))
|
|
|
|
call assert_equal(['e'], getbufline(b, 5))
|
|
|
|
call assert_equal([], getbufline(b, 6))
|
|
|
|
exe "bwipe! " . b
|
|
|
|
endfunc
|
2019-08-22 20:27:04 -07:00
|
|
|
|
|
|
|
func Test_deletebufline()
|
|
|
|
new
|
|
|
|
let b = bufnr('%')
|
|
|
|
call setline(1, ['aaa', 'bbb', 'ccc'])
|
|
|
|
hide
|
2022-11-29 01:55:46 -07:00
|
|
|
|
|
|
|
new
|
|
|
|
call setline(1, ['line1', 'line2', 'line3'])
|
|
|
|
normal! 2gggg
|
|
|
|
call assert_equal(2, line("''"))
|
|
|
|
|
2019-08-22 20:27:04 -07:00
|
|
|
call assert_equal(0, deletebufline(b, 2))
|
|
|
|
call assert_equal(['aaa', 'ccc'], getbufline(b, 1, 2))
|
|
|
|
call assert_equal(0, deletebufline(b, 2, 8))
|
|
|
|
call assert_equal(['aaa'], getbufline(b, 1, 2))
|
2022-11-29 01:55:46 -07:00
|
|
|
|
|
|
|
" deletebufline() in a hidden buffer shouldn't move marks in current window.
|
|
|
|
call assert_equal(2, line("''"))
|
|
|
|
bwipe!
|
|
|
|
|
2019-08-22 20:27:04 -07:00
|
|
|
exe "bd!" b
|
2021-08-07 14:39:23 -07:00
|
|
|
call assert_equal(1, b->deletebufline(1))
|
2019-08-22 20:27:04 -07:00
|
|
|
|
2021-08-27 14:07:34 -07:00
|
|
|
call assert_equal(1, deletebufline(-1, 1))
|
|
|
|
|
2019-08-22 20:27:04 -07:00
|
|
|
split Xtest
|
|
|
|
call setline(1, ['a', 'b', 'c'])
|
2021-08-27 14:07:34 -07:00
|
|
|
call cursor(line('$'), 1)
|
2019-08-22 20:27:04 -07:00
|
|
|
let b = bufnr('%')
|
|
|
|
wincmd w
|
|
|
|
call assert_equal(1, deletebufline(b, 4))
|
|
|
|
call assert_equal(0, deletebufline(b, 1))
|
|
|
|
call assert_equal(['b', 'c'], getbufline(b, 1, 2))
|
|
|
|
exe "bwipe! " . b
|
2021-02-09 19:26:41 -07:00
|
|
|
|
|
|
|
edit XbufOne
|
|
|
|
let one = bufnr()
|
|
|
|
call setline(1, ['a', 'b', 'c'])
|
|
|
|
setlocal nomodifiable
|
|
|
|
split XbufTwo
|
|
|
|
let two = bufnr()
|
|
|
|
call assert_fails('call deletebufline(one, 1)', 'E21:')
|
|
|
|
call assert_equal(two, bufnr())
|
|
|
|
bwipe! XbufTwo
|
|
|
|
bwipe! XbufOne
|
2019-08-22 20:27:04 -07:00
|
|
|
endfunc
|
2019-08-24 11:02:51 -07:00
|
|
|
|
|
|
|
func Test_appendbufline_redraw()
|
2022-10-26 06:53:11 -07:00
|
|
|
CheckScreendump
|
|
|
|
|
2019-08-24 11:02:51 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
new foo
|
2021-08-07 09:53:43 -07:00
|
|
|
let winnr = 'foo'->bufwinnr()
|
|
|
|
let buf = bufnr('foo')
|
2019-08-24 11:02:51 -07:00
|
|
|
wincmd p
|
|
|
|
call appendbufline(buf, '$', range(1,200))
|
2021-08-07 09:53:43 -07:00
|
|
|
exe winnr .. 'wincmd w'
|
2019-08-24 11:02:51 -07:00
|
|
|
norm! G
|
|
|
|
wincmd p
|
|
|
|
call deletebufline(buf, 1, '$')
|
|
|
|
call appendbufline(buf, '$', 'Hello Vim world...')
|
|
|
|
END
|
2023-08-22 02:44:04 -07:00
|
|
|
call writefile(lines, 'XscriptMatchCommon', 'D')
|
2019-08-24 11:02:51 -07:00
|
|
|
let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 10})
|
|
|
|
call VerifyScreenDump(buf, 'Test_appendbufline_1', {})
|
|
|
|
|
|
|
|
call StopVimInTerminal(buf)
|
|
|
|
endfunc
|
2022-05-22 04:03:17 -07:00
|
|
|
|
|
|
|
func Test_setbufline_select_mode()
|
|
|
|
new
|
|
|
|
call setline(1, ['foo', 'bar'])
|
|
|
|
call feedkeys("j^v2l\<C-G>", 'nx')
|
|
|
|
|
|
|
|
let bufnr = bufadd('Xdummy')
|
|
|
|
call bufload(bufnr)
|
|
|
|
call setbufline(bufnr, 1, ['abc'])
|
|
|
|
|
|
|
|
call feedkeys("x", 'nx')
|
|
|
|
call assert_equal(['foo', 'x'], getline(1, 2))
|
|
|
|
|
|
|
|
exe "bwipe! " .. bufnr
|
|
|
|
bwipe!
|
|
|
|
endfunc
|
|
|
|
|
2022-05-22 15:54:27 -07:00
|
|
|
func Test_deletebufline_select_mode()
|
|
|
|
new
|
|
|
|
call setline(1, ['foo', 'bar'])
|
|
|
|
call feedkeys("j^v2l\<C-G>", 'nx')
|
|
|
|
|
|
|
|
let bufnr = bufadd('Xdummy')
|
|
|
|
call bufload(bufnr)
|
|
|
|
call setbufline(bufnr, 1, ['abc', 'def'])
|
|
|
|
call deletebufline(bufnr, 1)
|
|
|
|
|
|
|
|
call feedkeys("x", 'nx')
|
|
|
|
call assert_equal(['foo', 'x'], getline(1, 2))
|
|
|
|
|
|
|
|
exe "bwipe! " .. bufnr
|
|
|
|
bwipe!
|
|
|
|
endfunc
|
|
|
|
|
2022-08-26 07:39:13 -07:00
|
|
|
func Test_setbufline_startup_nofile()
|
|
|
|
let before =<< trim [CODE]
|
|
|
|
set shortmess+=F
|
|
|
|
file Xresult
|
|
|
|
set buftype=nofile
|
|
|
|
call setbufline('', 1, 'success')
|
|
|
|
[CODE]
|
|
|
|
let after =<< trim [CODE]
|
|
|
|
set buftype=
|
|
|
|
write
|
|
|
|
quit
|
|
|
|
[CODE]
|
|
|
|
|
|
|
|
if !RunVim(before, after, '--clean')
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
call assert_equal(['success'], readfile('Xresult'))
|
|
|
|
call delete('Xresult')
|
|
|
|
endfunc
|
|
|
|
|
2022-11-06 16:03:31 -07:00
|
|
|
" Test that setbufline(), appendbufline() and deletebufline() should fail and
|
|
|
|
" return 1 when "textlock" is active.
|
|
|
|
func Test_change_bufline_with_textlock()
|
|
|
|
new
|
|
|
|
inoremap <buffer> <expr> <F2> setbufline('', 1, '')
|
|
|
|
call assert_fails("normal a\<F2>", 'E565:')
|
|
|
|
call assert_equal('1', getline(1))
|
|
|
|
inoremap <buffer> <expr> <F2> appendbufline('', 1, '')
|
|
|
|
call assert_fails("normal a\<F2>", 'E565:')
|
|
|
|
call assert_equal('11', getline(1))
|
|
|
|
inoremap <buffer> <expr> <F2> deletebufline('', 1)
|
|
|
|
call assert_fails("normal a\<F2>", 'E565:')
|
|
|
|
call assert_equal('111', getline(1))
|
|
|
|
bwipe!
|
|
|
|
endfunc
|
|
|
|
|
2022-05-22 04:03:17 -07:00
|
|
|
" vim: shiftwidth=2 sts=2 expandtab
|