neovim

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

test_prompt_buffer.vim (11633B)


      1 " Tests for setting 'buftype' to "prompt"
      2 
      3 source check.vim
      4 " Nvim's channel implementation differs from Vim's
      5 " CheckFeature channel
      6 
      7 source shared.vim
      8 source screendump.vim
      9 
     10 func CanTestPromptBuffer()
     11  " We need to use a terminal window to be able to feed keys without leaving
     12  " Insert mode.
     13  CheckFeature terminal
     14 
     15  " TODO: make the tests work on MS-Windows
     16  CheckNotMSWindows
     17 endfunc
     18 
     19 func WriteScript(name)
     20  call writefile([
     21 \ 'func TextEntered(text)',
     22 \ '  if a:text == "exit"',
     23 \ '    " Reset &modified to allow the buffer to be closed.',
     24 \ '    set nomodified',
     25 \ '    stopinsert',
     26 \ '    close',
     27 \ '  else',
     28 \ '    " Add the output above the current prompt.',
     29 \ '    call append(line("$") - 1, "Command: \"" . a:text . "\"")',
     30 \ '    " Reset &modified to allow the buffer to be closed.',
     31 \ '    set nomodified',
     32 \ '    call timer_start(20, {id -> TimerFunc(a:text)})',
     33 \ '  endif',
     34 \ 'endfunc',
     35 \ '',
     36 \ 'func TimerFunc(text)',
     37 \ '  " Add the output above the current prompt.',
     38 \ '  call append(line("$") - 1, "Result: \"" . a:text . "\"")',
     39 \ '  " Reset &modified to allow the buffer to be closed.',
     40 \ '  set nomodified',
     41 \ 'endfunc',
     42 \ '',
     43 \ 'func SwitchWindows()',
     44 \ '  call timer_start(0, {-> execute("wincmd p|wincmd p", "")})',
     45 \ 'endfunc',
     46 \ '',
     47 \ 'call setline(1, "other buffer")',
     48 \ 'set nomodified',
     49 \ 'new',
     50 \ 'set buftype=prompt',
     51 \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
     52 \ 'eval bufnr("")->prompt_setprompt("cmd: ")',
     53 \ 'startinsert',
     54 \ ], a:name)
     55 endfunc
     56 
     57 func Test_prompt_basic()
     58  call CanTestPromptBuffer()
     59  let scriptName = 'XpromptscriptBasic'
     60  call WriteScript(scriptName)
     61 
     62  let buf = RunVimInTerminal('-S ' . scriptName, {})
     63  call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
     64 
     65  call term_sendkeys(buf, "hello\<CR>")
     66  call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))})
     67  call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))})
     68  call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))})
     69 
     70  call term_sendkeys(buf, "exit\<CR>")
     71  call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
     72 
     73  call StopVimInTerminal(buf)
     74  call delete(scriptName)
     75 endfunc
     76 
     77 func Test_prompt_editing()
     78  call CanTestPromptBuffer()
     79  let scriptName = 'XpromptscriptEditing'
     80  call WriteScript(scriptName)
     81 
     82  let buf = RunVimInTerminal('-S ' . scriptName, {})
     83  call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
     84 
     85  let bs = "\<BS>"
     86  call term_sendkeys(buf, "hello" . bs . bs)
     87  call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))})
     88 
     89  let left = "\<Left>"
     90  call term_sendkeys(buf, left . left . left . bs . '-')
     91  call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))})
     92 
     93  call term_sendkeys(buf, "\<C-O>lz")
     94  call WaitForAssert({-> assert_equal('cmd: -hzel', term_getline(buf, 1))})
     95 
     96  let end = "\<End>"
     97  call term_sendkeys(buf, end . "x")
     98  call WaitForAssert({-> assert_equal('cmd: -hzelx', term_getline(buf, 1))})
     99 
    100  call term_sendkeys(buf, "\<C-U>exit\<CR>")
    101  call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
    102 
    103  call StopVimInTerminal(buf)
    104  call delete(scriptName)
    105 endfunc
    106 
    107 func Test_prompt_switch_windows()
    108  call CanTestPromptBuffer()
    109  let scriptName = 'XpromptSwitchWindows'
    110  call WriteScript(scriptName)
    111 
    112  let buf = RunVimInTerminal('-S ' . scriptName, {'rows': 12})
    113  call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
    114  call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 12))})
    115 
    116  call term_sendkeys(buf, "\<C-O>:call SwitchWindows()\<CR>")
    117  call term_wait(buf, 50)
    118  call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 12))})
    119 
    120  call term_sendkeys(buf, "\<Esc>")
    121  call term_wait(buf, 50)
    122  call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 12))})
    123 
    124  call StopVimInTerminal(buf)
    125  call delete(scriptName)
    126 endfunc
    127 
    128 func Test_prompt_garbage_collect()
    129  func MyPromptCallback(x, text)
    130    " NOP
    131  endfunc
    132  func MyPromptInterrupt(x)
    133    " NOP
    134  endfunc
    135 
    136  new
    137  set buftype=prompt
    138  eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
    139  eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
    140  call test_garbagecollect_now()
    141  " Must not crash
    142  call feedkeys("\<CR>\<C-C>", 'xt')
    143  call assert_true(v:true)
    144 
    145  call assert_fails("call prompt_setcallback(bufnr(), [])", 'E921:')
    146  call assert_equal(0, prompt_setcallback({}, ''))
    147  call assert_fails("call prompt_setinterrupt(bufnr(), [])", 'E921:')
    148  call assert_equal(0, prompt_setinterrupt({}, ''))
    149 
    150  delfunc MyPromptCallback
    151  bwipe!
    152 endfunc
    153 
    154 func Test_prompt_backspace()
    155  new
    156  set buftype=prompt
    157  call feedkeys("A123456\<Left>\<BS>\<Esc>", 'xt')
    158  call assert_equal('% 12346', getline(1))
    159  bwipe!
    160 endfunc
    161 
    162 " Test for editing the prompt buffer
    163 func Test_prompt_buffer_edit()
    164  new
    165  set buftype=prompt
    166  normal! i
    167  call assert_beeps('normal! dd')
    168  call assert_beeps('normal! ~')
    169  " Nvim: these operations are supported
    170  " call assert_beeps('normal! o')
    171  " call assert_beeps('normal! O')
    172  " call assert_beeps('normal! p')
    173  " call assert_beeps('normal! P')
    174  " call assert_beeps('normal! u')
    175  call assert_beeps('normal! ra')
    176  call assert_beeps('normal! s')
    177  call assert_beeps('normal! S')
    178  call assert_beeps("normal! \<C-A>")
    179  call assert_beeps("normal! \<C-X>")
    180  call assert_beeps("normal! dp")
    181  call assert_beeps("normal! do")
    182  " pressing CTRL-W in the prompt buffer should trigger the window commands
    183  call assert_equal(1, winnr())
    184  exe "normal A\<C-W>\<C-W>"
    185  call assert_equal(2, winnr())
    186  wincmd w
    187  close!
    188  call assert_equal(0, prompt_setprompt([], ''))
    189 endfunc
    190 
    191 func Test_prompt_buffer_getbufinfo()
    192  new
    193  call assert_equal('', prompt_getprompt('%'))
    194  call assert_equal('', prompt_getprompt(bufnr('%')))
    195  let another_buffer = bufnr('%')
    196 
    197  set buftype=prompt
    198  call assert_equal('% ', prompt_getprompt('%'))
    199  call prompt_setprompt( bufnr( '%' ), 'This is a test: ' )
    200  call assert_equal('This is a test: ', prompt_getprompt('%'))
    201 
    202  call prompt_setprompt( bufnr( '%' ), '' )
    203  call assert_equal('', '%'->prompt_getprompt())
    204 
    205  call prompt_setprompt( bufnr( '%' ), 'Another: ' )
    206  call assert_equal('Another: ', prompt_getprompt('%'))
    207  let another = bufnr('%')
    208 
    209  new
    210 
    211  call assert_equal('', prompt_getprompt('%'))
    212  call assert_equal('Another: ', prompt_getprompt(another))
    213 
    214  " Doesn't exist
    215  let buffers_before = len( getbufinfo() )
    216  call assert_equal('', prompt_getprompt( bufnr('$') + 1))
    217  call assert_equal(buffers_before, len( getbufinfo()))
    218 
    219  " invalid type
    220  call assert_fails('call prompt_getprompt({})', 'E728:')
    221 
    222  %bwipe!
    223 endfunc
    224 
    225 func Test_prompt_while_writing_to_hidden_buffer()
    226  call CanTestPromptBuffer()
    227  CheckUnix
    228 
    229  " Make a job continuously write to a hidden buffer, check that the prompt
    230  " buffer is not affected.
    231  let scriptName = 'XpromptscriptHiddenBuf'
    232  let script =<< trim END
    233    set buftype=prompt
    234    call prompt_setprompt( bufnr(), 'cmd:' )
    235    let job = job_start(['/bin/sh', '-c',
    236        \ 'while true;
    237        \   do echo line;
    238        \   sleep 0.1;
    239        \ done'], #{out_io: 'buffer', out_name: ''})
    240    startinsert
    241  END
    242  eval script->writefile(scriptName, 'D')
    243 
    244  let buf = RunVimInTerminal('-S ' .. scriptName, {})
    245  call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
    246 
    247  call term_sendkeys(buf, 'test')
    248  call WaitForAssert({-> assert_equal('cmd:test', term_getline(buf, 1))})
    249  call term_sendkeys(buf, 'test')
    250  call WaitForAssert({-> assert_equal('cmd:testtest', term_getline(buf, 1))})
    251  call term_sendkeys(buf, 'test')
    252  call WaitForAssert({-> assert_equal('cmd:testtesttest', term_getline(buf, 1))})
    253 
    254  call StopVimInTerminal(buf)
    255 endfunc
    256 
    257 func Test_prompt_appending_while_hidden()
    258  call CanTestPromptBuffer()
    259 
    260  let script =<< trim END
    261      new prompt
    262      set buftype=prompt
    263      set bufhidden=hide
    264 
    265      func s:TextEntered(text)
    266          if a:text == 'exit'
    267              close
    268          endif
    269          echowin 'Entered:' a:text
    270      endfunc
    271      call prompt_setcallback(bufnr(), function('s:TextEntered'))
    272 
    273      func DoAppend(cmd_before = '')
    274        exe a:cmd_before
    275        call appendbufline('prompt', '$', 'Test')
    276        return ''
    277      endfunc
    278 
    279      autocmd User SwitchTabPages tabprevious | tabnext
    280      func DoAutoAll(cmd_before = '')
    281        exe a:cmd_before
    282        doautoall User SwitchTabPages
    283        return ''
    284      endfunc
    285  END
    286  call writefile(script, 'XpromptBuffer', 'D')
    287 
    288  let buf = RunVimInTerminal('-S XpromptBuffer', {'rows': 10})
    289  call TermWait(buf)
    290 
    291  call term_sendkeys(buf, "asomething\<CR>")
    292  call TermWait(buf)
    293 
    294  call term_sendkeys(buf, "exit\<CR>")
    295  call WaitForAssert({-> assert_notmatch('-- .* --', term_getline(buf, 10))})
    296 
    297  call term_sendkeys(buf, ":call DoAppend()\<CR>")
    298  call WaitForAssert({-> assert_notmatch('-- .* --', term_getline(buf, 10))})
    299 
    300  call term_sendkeys(buf, "i")
    301  call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
    302 
    303  call term_sendkeys(buf, "\<C-R>=DoAppend()\<CR>")
    304  call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
    305 
    306  call term_sendkeys(buf, "\<C-R>=DoAppend('stopinsert')\<CR>")
    307  call WaitForAssert({-> assert_notmatch('-- .* --', term_getline(buf, 10))})
    308 
    309  call term_sendkeys(buf, ":call DoAppend('startreplace')\<CR>")
    310  call WaitForAssert({-> assert_match('^-- REPLACE --', term_getline(buf, 10))})
    311 
    312  call term_sendkeys(buf, "\<Esc>:tabnew\<CR>")
    313  call WaitForAssert({-> assert_notmatch('-- .* --', term_getline(buf, 10))})
    314 
    315  call term_sendkeys(buf, ":call DoAutoAll('startinsert')\<CR>")
    316  call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
    317 
    318  call term_sendkeys(buf, "\<C-R>=DoAutoAll('stopinsert')\<CR>")
    319  call WaitForAssert({-> assert_notmatch('-- .* --', term_getline(buf, 10))})
    320 
    321  call StopVimInTerminal(buf)
    322 endfunc
    323 
    324 " Modifying a hidden buffer while leaving a prompt buffer should not prevent
    325 " stopping of Insert mode, and returning to the prompt buffer later should
    326 " restore Insert mode.
    327 func Test_prompt_leave_modify_hidden()
    328  call CanTestPromptBuffer()
    329 
    330  let script =<< trim END
    331      file hidden
    332      set bufhidden=hide
    333      enew
    334      new prompt
    335      set buftype=prompt
    336 
    337      inoremap <buffer> w <Cmd>wincmd w<CR>
    338      inoremap <buffer> q <Cmd>bwipe!<CR>
    339      autocmd BufLeave prompt call appendbufline('hidden', '$', 'Leave')
    340      autocmd BufEnter prompt call appendbufline('hidden', '$', 'Enter')
    341      autocmd BufWinLeave prompt call appendbufline('hidden', '$', 'Close')
    342  END
    343  call writefile(script, 'XpromptLeaveModifyHidden', 'D')
    344 
    345  let buf = RunVimInTerminal('-S XpromptLeaveModifyHidden', {'rows': 10})
    346  call TermWait(buf)
    347 
    348  call term_sendkeys(buf, "a")
    349  call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
    350 
    351  call term_sendkeys(buf, "w")
    352  call WaitForAssert({-> assert_notmatch('-- .* --', term_getline(buf, 10))})
    353 
    354  call term_sendkeys(buf, "\<C-W>w")
    355  call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
    356 
    357  call term_sendkeys(buf, "q")
    358  call WaitForAssert({-> assert_notmatch('-- .* --', term_getline(buf, 10))})
    359 
    360  call term_sendkeys(buf, ":bwipe!\<CR>")
    361  call WaitForAssert({-> assert_equal('Leave', term_getline(buf, 2))})
    362  call WaitForAssert({-> assert_equal('Enter', term_getline(buf, 3))})
    363  call WaitForAssert({-> assert_equal('Leave', term_getline(buf, 4))})
    364  call WaitForAssert({-> assert_equal('Close', term_getline(buf, 5))})
    365 
    366  call StopVimInTerminal(buf)
    367 endfunc
    368 
    369 " vim: shiftwidth=2 sts=2 expandtab