neovim

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

test_cmdwin.vim (6558B)


      1 " Tests for editing the command line.
      2 
      3 source check.vim
      4 source screendump.vim
      5 
      6 
      7 func Test_cant_open_cmdwin_in_cmdwin()
      8  try
      9    call feedkeys("q:q::q\<CR>", "x!")
     10  catch
     11    let caught = v:exception
     12  endtry
     13  call assert_match('E1292:', caught)
     14 endfunc
     15 
     16 func Test_cmdwin_virtual_edit()
     17  enew!
     18  set ve=all cpo+=$
     19  silent normal q/s
     20 
     21  set ve= cpo-=$
     22 endfunc
     23 
     24 " Check that a :normal command can be used to stop Visual mode without side
     25 " effects.
     26 func Test_normal_escape()
     27  call feedkeys("q:i\" foo\<Esc>:normal! \<C-V>\<Esc>\<CR>:\" bar\<CR>", 'ntx')
     28  call assert_equal('" bar', @:)
     29 endfunc
     30 
     31 " This was using a pointer to a freed buffer
     32 func Test_cmdwin_freed_buffer_ptr()
     33  " this does not work on MS-Windows because renaming an open file fails
     34  CheckNotMSWindows
     35 
     36  au BufEnter * next 0| file 
     37  edit 0
     38  silent! norm q/
     39 
     40  au! BufEnter
     41  bwipe!
     42 endfunc
     43 
     44 " This was resulting in a window with negative width.
     45 " The test doesn't reproduce the illegal memory access though...
     46 func Test_cmdwin_split_often()
     47  let lines = &lines
     48  let columns = &columns
     49  set t_WS=
     50 
     51  try
     52    " set encoding=iso8859
     53    set ruler
     54    winsize 0 0
     55    noremap 0 H
     56    sil norm 0000000q:
     57  catch /E36:/
     58  endtry
     59 
     60  bwipe!
     61  set encoding=utf8
     62  let &lines = lines
     63  let &columns = columns
     64 endfunc
     65 
     66 func Test_cmdwin_restore_heights()
     67  set showtabline=0 cmdheight=2 laststatus=0
     68  call feedkeys("q::set cmdheight=1\<CR>:q\<CR>", 'ntx')
     69  call assert_equal(&lines - 1, winheight(0))
     70 
     71  set showtabline=2 cmdheight=3
     72  call feedkeys("q::set showtabline=0\<CR>:q\<CR>", 'ntx')
     73  call assert_equal(&lines - 3, winheight(0))
     74 
     75  set cmdheight=1 laststatus=2
     76  call feedkeys("q::set laststatus=0\<CR>:q\<CR>", 'ntx')
     77  call assert_equal(&lines - 1, winheight(0))
     78 
     79  set laststatus=2
     80  call feedkeys("q::set laststatus=1\<CR>:q\<CR>", 'ntx')
     81  call assert_equal(&lines - 1, winheight(0))
     82 
     83  set laststatus=2
     84  belowright vsplit
     85  wincmd _
     86  let restcmds = winrestcmd()
     87  call feedkeys("q::set laststatus=1\<CR>:q\<CR>", 'ntx')
     88  " As we have 2 windows, &ls = 1 should still have a statusline on the last
     89  " window. As such, the number of available rows hasn't changed and the window
     90  " sizes should be restored.
     91  call assert_equal(restcmds, winrestcmd())
     92 
     93  set cmdheight& showtabline& laststatus&
     94 endfunc
     95 
     96 func Test_cmdwin_temp_curwin()
     97  func CheckWraps(expect_wrap)
     98    setlocal textwidth=0 wrapmargin=1
     99 
    100    call deletebufline('', 1, '$')
    101    let as = repeat('a', winwidth(0) - 2 - &wrapmargin)
    102    call setline(1, as .. ' b')
    103    normal! gww
    104 
    105    setlocal textwidth& wrapmargin&
    106    call assert_equal(a:expect_wrap ? [as, 'b'] : [as .. ' b'], getline(1, '$'))
    107  endfunc
    108 
    109  func CheckCmdWin()
    110    call assert_equal('command', win_gettype())
    111    " textoff and &wrapmargin formatting considers the cmdwin_type char.
    112    call assert_equal(1, getwininfo(win_getid())[0].textoff)
    113    call CheckWraps(1)
    114  endfunc
    115 
    116  func CheckOtherWin()
    117    call assert_equal('', win_gettype())
    118    call assert_equal(0, getwininfo(win_getid())[0].textoff)
    119    call CheckWraps(0)
    120  endfunc
    121 
    122  call feedkeys("q::call CheckCmdWin()\<CR>:call win_execute(win_getid(winnr('#')), 'call CheckOtherWin()')\<CR>:q<CR>", 'ntx')
    123 
    124  %bwipe!
    125  delfunc CheckWraps
    126  delfunc CheckCmdWin
    127  delfunc CheckOtherWin
    128 endfunc
    129 
    130 func Test_cmdwin_interrupted()
    131  func CheckInterrupted()
    132    call feedkeys("q::call assert_equal('', getcmdwintype())\<CR>:call assert_equal('', getcmdtype())\<CR>:q<CR>", 'ntx')
    133  endfunc
    134 
    135  augroup CmdWin
    136 
    137  " While opening the cmdwin's split:
    138  " Close the cmdwin's window.
    139  au WinEnter * ++once quit
    140  call CheckInterrupted()
    141 
    142  " Close the old window.
    143  au WinEnter * ++once execute winnr('#') 'quit'
    144  call CheckInterrupted()
    145 
    146  " Switch back to the old window.
    147  au WinEnter * ++once wincmd p
    148  call CheckInterrupted()
    149 
    150  " Change the old window's buffer.
    151  au WinEnter * ++once call win_execute(win_getid(winnr('#')), 'enew')
    152  call CheckInterrupted()
    153 
    154  " Using BufLeave autocmds as cmdwin restrictions do not apply to them when
    155  " fired from opening the cmdwin...
    156  " After opening the cmdwin's split, while creating the cmdwin's buffer:
    157  " Delete the cmdwin's buffer.
    158  au BufLeave * ++once bwipe
    159  call CheckInterrupted()
    160 
    161  " Close the cmdwin's window.
    162  au BufLeave * ++once quit
    163  call CheckInterrupted()
    164 
    165  " Close the old window.
    166  au BufLeave * ++once execute winnr('#') 'quit'
    167  call CheckInterrupted()
    168 
    169  " Switch to a different window.
    170  au BufLeave * ++once split
    171  call CheckInterrupted()
    172 
    173  " Change the old window's buffer.
    174  au BufLeave * ++once call win_execute(win_getid(winnr('#')), 'enew')
    175  call CheckInterrupted()
    176 
    177  " However, changing the current buffer is OK and does not interrupt.
    178  au BufLeave * ++once edit other
    179  call feedkeys("q::let t=getcmdwintype()\<CR>:let b=bufnr()\<CR>:clo<CR>", 'ntx')
    180  call assert_equal(':', t)
    181  call assert_equal(1, bufloaded('other'))
    182  call assert_notequal(b, bufnr('other'))
    183 
    184  augroup END
    185 
    186  " No autocmds should remain, but clear the augroup to be sure.
    187  augroup CmdWin
    188    au!
    189  augroup END
    190 
    191  %bwipe!
    192  delfunc CheckInterrupted
    193 endfunc
    194 
    195 func Test_cmdwin_existing_bufname()
    196  func CheckName()
    197    call assert_equal(1, getbufinfo('')[0].command)
    198    call assert_equal(0, getbufinfo('[Command Line]')[0].command)
    199    call assert_match('#a\s*"\[Command Line\]"', execute('ls'))
    200    call assert_match('%a\s*"\[Command Line\]"', execute('ls'))
    201  endfunc
    202  file [Command Line]
    203  call feedkeys("q::call CheckName()\<CR>:q\<CR>", 'ntx')
    204  0file
    205  delfunc CheckName
    206 endfunc
    207 
    208 func Test_cmdwin_showcmd()
    209  CheckScreendump
    210 
    211  let lines =<< trim [SCRIPT]
    212    augroup vimHints | au! | augroup END
    213    set showcmd
    214  [SCRIPT]
    215  call writefile(lines, 'XTest_cmdwin_showcmd', 'D')
    216  let buf = RunVimInTerminal('-S XTest_cmdwin_showcmd', {'rows': 18})
    217 
    218  for keys in ['q:', ":\<C-F>"]
    219    call term_sendkeys(buf, keys)
    220    call VerifyScreenDump(buf, 'Test_cmdwin_showcmd_1', {})
    221    call term_sendkeys(buf, '"')
    222    call WaitForAssert({-> assert_match('^: \+" *$', term_getline(buf, 18))})
    223    call term_sendkeys(buf, 'x')
    224    call WaitForAssert({-> assert_match('^: \+"x *$', term_getline(buf, 18))})
    225    call term_sendkeys(buf, 'y')
    226    call WaitForAssert({-> assert_match('^: \+"xy *$', term_getline(buf, 18))})
    227    call term_sendkeys(buf, 'y')
    228    call WaitForAssert({-> assert_match('^: \+$', term_getline(buf, 18))})
    229    call term_sendkeys(buf, "\<C-C>\<C-C>")
    230    call VerifyScreenDump(buf, 'Test_cmdwin_showcmd_2', {})
    231  endfor
    232 
    233  " clean up
    234  call StopVimInTerminal(buf)
    235 endfunc
    236 
    237 " vim: shiftwidth=2 sts=2 expandtab