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