neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

test_bufline.vim (9196B)


      1 " Tests for setbufline(), getbufline(), appendbufline(), deletebufline()
      2 
      3 source shared.vim
      4 source screendump.vim
      5 source check.vim
      6 
      7 func 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('foo', getbufoneline(b, 1))
     15  call assert_equal(['bar'], getbufline(b, '$'))
     16  call assert_equal('bar', getbufoneline(b, '$'))
     17  call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
     18  exe "bd!" b
     19  call assert_equal([], getbufline(b, 1, 2))
     20 
     21  split Xtest
     22  call setline(1, ['a', 'b', 'c'])
     23  let b = bufnr('%')
     24  wincmd w
     25 
     26  call assert_equal(1, setbufline(b, 5, 'x'))
     27  call assert_equal(1, setbufline(b, 5, ['x']))
     28  call assert_equal(0, setbufline(b, 5, []))
     29  call assert_equal(0, setbufline(b, 5, v:_null_list))
     30 
     31  call assert_equal(1, 'x'->setbufline(bufnr('$') + 1, 1))
     32  call assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1))
     33  call assert_equal(1, []->setbufline(bufnr('$') + 1, 1))
     34  call assert_equal(1, v:_null_list->setbufline(bufnr('$') + 1, 1))
     35 
     36  call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
     37 
     38  call assert_equal(0, setbufline(b, 4, ['d', 'e']))
     39  call assert_equal(['c'], b->getbufline(3))
     40  call assert_equal('c', b->getbufoneline(3))
     41  call assert_equal(['d'], getbufline(b, 4))
     42  call assert_equal('d', getbufoneline(b, 4))
     43  call assert_equal(['e'], getbufline(b, 5))
     44  call assert_equal('e', getbufoneline(b, 5))
     45  call assert_equal([], getbufline(b, 6))
     46  call assert_equal([], getbufline(b, 2, 1))
     47 
     48  if has('job')
     49    call setbufline(b, 2, [function('eval'), #{key: 123}, test_null_job()])
     50    call assert_equal(["function('eval')",
     51                    \ "{'key': 123}",
     52                    \ "no process"],
     53                    \ getbufline(b, 2, 4))
     54  endif
     55  exe "bwipe! " . b
     56 endfunc
     57 
     58 func Test_setbufline_getbufline_fold()
     59  split Xtest
     60  setlocal foldmethod=expr foldexpr=0
     61  let b = bufnr('%')
     62  new
     63  call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
     64  call assert_equal(['foo'], getbufline(b, 1))
     65  call assert_equal(['bar'], getbufline(b, 2))
     66  call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
     67  exe "bwipe!" b
     68  bwipe!
     69 endfunc
     70 
     71 func Test_setbufline_getbufline_fold_tab()
     72  split Xtest
     73  setlocal foldmethod=expr foldexpr=0
     74  let b = bufnr('%')
     75  tab new
     76  call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
     77  call assert_equal(['foo'], getbufline(b, 1))
     78  call assert_equal(['bar'], getbufline(b, 2))
     79  call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
     80  exe "bwipe!" b
     81  bwipe!
     82 endfunc
     83 
     84 func Test_setline_startup()
     85  let cmd = GetVimCommand('Xscript')
     86  if cmd == ''
     87    return
     88  endif
     89  call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript', 'D')
     90  call system(cmd)
     91  sleep 50m
     92  call assert_equal(['Hello'], readfile('Xtest'))
     93 
     94  call assert_equal(0, setline(1, []))
     95  call assert_equal(0, setline(1, v:_null_list))
     96  call assert_equal(0, setline(5, []))
     97  call assert_equal(0, setline(6, v:_null_list))
     98 
     99  call delete('Xtest')
    100 endfunc
    101 
    102 func Test_appendbufline()
    103  new
    104  let b = bufnr('%')
    105  hide
    106 
    107  new
    108  call setline(1, ['line1', 'line2', 'line3'])
    109  normal! 2gggg
    110  call assert_equal(2, line("''"))
    111 
    112  call assert_equal(0, appendbufline(b, 0, ['foo', 'bar']))
    113  call assert_equal(['foo'], getbufline(b, 1))
    114  call assert_equal(['bar'], getbufline(b, 2))
    115  call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
    116  call assert_equal(0, appendbufline(b, 0, 'baz'))
    117  call assert_equal(['baz', 'foo', 'bar'], getbufline(b, 1, 3))
    118 
    119  " appendbufline() in a hidden buffer shouldn't move marks in current window.
    120  call assert_equal(2, line("''"))
    121  bwipe!
    122 
    123  exe "bd!" b
    124  call assert_equal([], getbufline(b, 1, 3))
    125 
    126  split Xtest
    127  call setline(1, ['a', 'b', 'c'])
    128  let b = bufnr('%')
    129  wincmd w
    130 
    131  call assert_equal(1, appendbufline(b, -1, 'x'))
    132  call assert_equal(1, appendbufline(b, -1, ['x']))
    133  call assert_equal(1, appendbufline(b, -1, []))
    134  call assert_equal(1, appendbufline(b, -1, v:_null_list))
    135 
    136  call assert_equal(1, appendbufline(b, 4, 'x'))
    137  call assert_equal(1, appendbufline(b, 4, ['x']))
    138  call assert_equal(0, appendbufline(b, 4, []))
    139  call assert_equal(0, appendbufline(b, 4, v:_null_list))
    140 
    141  call assert_equal(1, appendbufline(1234, 1, 'x'))
    142  call assert_equal(1, appendbufline(1234, 1, ['x']))
    143  call assert_equal(1, appendbufline(1234, 1, []))
    144  call assert_equal(1, appendbufline(1234, 1, v:_null_list))
    145 
    146  call assert_equal(0, appendbufline(b, 1, []))
    147  call assert_equal(0, appendbufline(b, 1, v:_null_list))
    148  call assert_equal(0, appendbufline(b, 3, []))
    149  call assert_equal(0, appendbufline(b, 3, v:_null_list))
    150 
    151  call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
    152 
    153  call assert_equal(0, appendbufline(b, 3, ['d', 'e']))
    154  call assert_equal(['c'], getbufline(b, 3))
    155  call assert_equal(['d'], getbufline(b, 4))
    156  call assert_equal(['e'], getbufline(b, 5))
    157  call assert_equal([], getbufline(b, 6))
    158  exe "bwipe! " . b
    159 endfunc
    160 
    161 func Test_appendbufline_no_E315()
    162  let after = [
    163    \ 'set stl=%f ls=2',
    164    \ 'new',
    165    \ 'let buf = bufnr("%")',
    166    \ 'quit',
    167    \ 'vsp',
    168    \ 'exec "buffer" buf',
    169    \ 'wincmd w',
    170    \ 'call appendbufline(buf, 0, "abc")',
    171    \ 'redraw',
    172    \ 'while getbufline(buf, 1)[0] =~ "^\\s*$"',
    173    \ '  sleep 10m',
    174    \ 'endwhile',
    175    \ 'au VimLeavePre * call writefile([v:errmsg], "Xerror")',
    176    \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
    177    \ 'qall!',
    178    \ ]
    179  if !RunVim([], after, '--clean')
    180    return
    181  endif
    182  call assert_notmatch("^E315:", readfile("Xerror")[0])
    183  call assert_equal("done", readfile("Xdone")[0])
    184  call delete("Xerror")
    185  call delete("Xdone")
    186 endfunc
    187 
    188 func Test_deletebufline()
    189  new
    190  let b = bufnr('%')
    191  call setline(1, ['aaa', 'bbb', 'ccc'])
    192  hide
    193 
    194  new
    195  call setline(1, ['line1', 'line2', 'line3'])
    196  normal! 2gggg
    197  call assert_equal(2, line("''"))
    198 
    199  call assert_equal(0, deletebufline(b, 2))
    200  call assert_equal(['aaa', 'ccc'], getbufline(b, 1, 2))
    201  call assert_equal(0, deletebufline(b, 2, 8))
    202  call assert_equal(['aaa'], getbufline(b, 1, 2))
    203 
    204  " deletebufline() in a hidden buffer shouldn't move marks in current window.
    205  call assert_equal(2, line("''"))
    206  bwipe!
    207 
    208  exe "bd!" b
    209  call assert_equal(1, b->deletebufline(1))
    210 
    211  call assert_equal(1, deletebufline(-1, 1))
    212 
    213  split Xtest
    214  call setline(1, ['a', 'b', 'c'])
    215  call cursor(line('$'), 1)
    216  let b = bufnr('%')
    217  wincmd w
    218  call assert_equal(1, deletebufline(b, 4))
    219  call assert_equal(0, deletebufline(b, 1))
    220  call assert_equal(['b', 'c'], getbufline(b, 1, 2))
    221  exe "bwipe! " . b
    222 
    223  edit XbufOne
    224  let one = bufnr()
    225  call setline(1, ['a', 'b', 'c'])
    226  setlocal nomodifiable
    227  split XbufTwo
    228  let two = bufnr()
    229  call assert_fails('call deletebufline(one, 1)', 'E21:')
    230  call assert_equal(two, bufnr())
    231  bwipe! XbufTwo
    232  bwipe! XbufOne
    233 endfunc
    234 
    235 func Test_appendbufline_redraw()
    236  CheckScreendump
    237 
    238  let lines =<< trim END
    239    new foo
    240    let winnr = 'foo'->bufwinnr()
    241    let buf = bufnr('foo')
    242    wincmd p
    243    call appendbufline(buf, '$', range(1,200))
    244    exe winnr .. 'wincmd w'
    245    norm! G
    246    wincmd p
    247    call deletebufline(buf, 1, '$')
    248    call appendbufline(buf, '$', 'Hello Vim world...')
    249  END
    250  call writefile(lines, 'XscriptMatchCommon', 'D')
    251  let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 10})
    252  call VerifyScreenDump(buf, 'Test_appendbufline_1', {})
    253 
    254  call StopVimInTerminal(buf)
    255 endfunc
    256 
    257 func Test_setbufline_select_mode()
    258  new
    259  call setline(1, ['foo', 'bar'])
    260  call feedkeys("j^v2l\<C-G>", 'nx')
    261 
    262  let bufnr = bufadd('Xdummy')
    263  call bufload(bufnr)
    264  call setbufline(bufnr, 1, ['abc'])
    265 
    266  call feedkeys("x", 'nx')
    267  call assert_equal(['foo', 'x'], getline(1, 2))
    268 
    269  exe "bwipe! " .. bufnr
    270  bwipe!
    271 endfunc
    272 
    273 func Test_deletebufline_select_mode()
    274  new
    275  call setline(1, ['foo', 'bar'])
    276  call feedkeys("j^v2l\<C-G>", 'nx')
    277 
    278  let bufnr = bufadd('Xdummy')
    279  call bufload(bufnr)
    280  call setbufline(bufnr, 1, ['abc', 'def'])
    281  call deletebufline(bufnr, 1)
    282 
    283  call feedkeys("x", 'nx')
    284  call assert_equal(['foo', 'x'], getline(1, 2))
    285 
    286  exe "bwipe! " .. bufnr
    287  bwipe!
    288 endfunc
    289 
    290 func Test_setbufline_startup_nofile()
    291  let before =<< trim [CODE]
    292    set shortmess+=F
    293    file Xresult
    294    set buftype=nofile
    295    call setbufline('', 1, 'success')
    296  [CODE]
    297  let after =<< trim [CODE]
    298    set buftype=
    299    write
    300    quit
    301  [CODE]
    302 
    303  if !RunVim(before, after, '--clean')
    304    return
    305  endif
    306  call assert_equal(['success'], readfile('Xresult'))
    307  call delete('Xresult')
    308 endfunc
    309 
    310 " Test that setbufline(), appendbufline() and deletebufline() should fail and
    311 " return 1 when "textlock" is active.
    312 func Test_change_bufline_with_textlock()
    313  new
    314  inoremap <buffer> <expr> <F2> setbufline('', 1, '')
    315  call assert_fails("normal a\<F2>", 'E565:')
    316  call assert_equal('1', getline(1))
    317  inoremap <buffer> <expr> <F2> appendbufline('', 1, '')
    318  call assert_fails("normal a\<F2>", 'E565:')
    319  call assert_equal('11', getline(1))
    320  inoremap <buffer> <expr> <F2> deletebufline('', 1)
    321  call assert_fails("normal a\<F2>", 'E565:')
    322  call assert_equal('111', getline(1))
    323  bwipe!
    324 endfunc
    325 
    326 " vim: shiftwidth=2 sts=2 expandtab