1" Tests for setting 'buftype' to "prompt" 2 3source check.vim 4CheckFeature channel 5 6source shared.vim 7source screendump.vim 8 9func CanTestPromptBuffer() 10 " We need to use a terminal window to be able to feed keys without leaving 11 " Insert mode. 12 CheckFeature terminal 13 14 " TODO: make the tests work on MS-Windows 15 CheckNotMSWindows 16endfunc 17 18func WriteScript(name) 19 call writefile([ 20 \ 'func TextEntered(text)', 21 \ ' if a:text == "exit"', 22 \ ' " Reset &modified to allow the buffer to be closed.', 23 \ ' set nomodified', 24 \ ' stopinsert', 25 \ ' close', 26 \ ' else', 27 \ ' " Add the output above the current prompt.', 28 \ ' call append(line("$") - 1, "Command: \"" . a:text . "\"")', 29 \ ' " Reset &modified to allow the buffer to be closed.', 30 \ ' set nomodified', 31 \ ' call timer_start(20, {id -> TimerFunc(a:text)})', 32 \ ' endif', 33 \ 'endfunc', 34 \ '', 35 \ 'func TimerFunc(text)', 36 \ ' " Add the output above the current prompt.', 37 \ ' call append(line("$") - 1, "Result: \"" . a:text . "\"")', 38 \ ' " Reset &modified to allow the buffer to be closed.', 39 \ ' set nomodified', 40 \ 'endfunc', 41 \ '', 42 \ 'call setline(1, "other buffer")', 43 \ 'set nomodified', 44 \ 'new', 45 \ 'set buftype=prompt', 46 \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))', 47 \ 'eval bufnr("")->prompt_setprompt("cmd: ")', 48 \ 'startinsert', 49 \ ], a:name) 50endfunc 51 52func Test_prompt_basic() 53 call CanTestPromptBuffer() 54 let scriptName = 'XpromptscriptBasic' 55 call WriteScript(scriptName) 56 57 let buf = RunVimInTerminal('-S ' . scriptName, {}) 58 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))}) 59 60 call term_sendkeys(buf, "hello\<CR>") 61 call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))}) 62 call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))}) 63 call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))}) 64 65 call term_sendkeys(buf, "exit\<CR>") 66 call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))}) 67 68 call StopVimInTerminal(buf) 69 call delete(scriptName) 70endfunc 71 72func Test_prompt_editing() 73 call CanTestPromptBuffer() 74 let scriptName = 'XpromptscriptEditing' 75 call WriteScript(scriptName) 76 77 let buf = RunVimInTerminal('-S ' . scriptName, {}) 78 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))}) 79 80 let bs = "\<BS>" 81 call term_sendkeys(buf, "hello" . bs . bs) 82 call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))}) 83 84 let left = "\<Left>" 85 call term_sendkeys(buf, left . left . left . bs . '-') 86 call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))}) 87 88 call term_sendkeys(buf, "\<C-O>lz") 89 call WaitForAssert({-> assert_equal('cmd: -hzel', term_getline(buf, 1))}) 90 91 let end = "\<End>" 92 call term_sendkeys(buf, end . "x") 93 call WaitForAssert({-> assert_equal('cmd: -hzelx', term_getline(buf, 1))}) 94 95 call term_sendkeys(buf, "\<C-U>exit\<CR>") 96 call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))}) 97 98 call StopVimInTerminal(buf) 99 call delete(scriptName) 100endfunc 101 102func Test_prompt_garbage_collect() 103 func MyPromptCallback(x, text) 104 " NOP 105 endfunc 106 func MyPromptInterrupt(x) 107 " NOP 108 endfunc 109 110 new 111 set buftype=prompt 112 eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}])) 113 eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}])) 114 call test_garbagecollect_now() 115 " Must not crash 116 call feedkeys("\<CR>\<C-C>", 'xt') 117 call assert_true(v:true) 118 119 call assert_fails("call prompt_setcallback(bufnr(), [])", 'E921:') 120 call assert_equal(0, prompt_setcallback({}, '')) 121 call assert_fails("call prompt_setinterrupt(bufnr(), [])", 'E921:') 122 call assert_equal(0, prompt_setinterrupt({}, '')) 123 124 delfunc MyPromptCallback 125 bwipe! 126endfunc 127 128func Test_prompt_backspace() 129 new 130 set buftype=prompt 131 call feedkeys("A123456\<Left>\<BS>\<Esc>", 'xt') 132 call assert_equal('% 12346', getline(1)) 133 bwipe! 134endfunc 135 136" Test for editing the prompt buffer 137func Test_prompt_buffer_edit() 138 new 139 set buftype=prompt 140 normal! i 141 call assert_beeps('normal! dd') 142 call assert_beeps('normal! ~') 143 call assert_beeps('normal! o') 144 call assert_beeps('normal! O') 145 call assert_beeps('normal! p') 146 call assert_beeps('normal! P') 147 call assert_beeps('normal! u') 148 call assert_beeps('normal! ra') 149 call assert_beeps('normal! s') 150 call assert_beeps('normal! S') 151 call assert_beeps("normal! \<C-A>") 152 call assert_beeps("normal! \<C-X>") 153 " pressing CTRL-W in the prompt buffer should trigger the window commands 154 call assert_equal(1, winnr()) 155 exe "normal A\<C-W>\<C-W>" 156 call assert_equal(2, winnr()) 157 wincmd w 158 close! 159 call assert_equal(0, prompt_setprompt([], '')) 160endfunc 161 162func Test_prompt_buffer_getbufinfo() 163 new 164 call assert_equal('', prompt_getprompt('%')) 165 call assert_equal('', prompt_getprompt(bufnr('%'))) 166 let another_buffer = bufnr('%') 167 168 set buftype=prompt 169 call assert_equal('% ', prompt_getprompt('%')) 170 call prompt_setprompt( bufnr( '%' ), 'This is a test: ' ) 171 call assert_equal('This is a test: ', prompt_getprompt('%')) 172 173 call prompt_setprompt( bufnr( '%' ), '' ) 174 call assert_equal('', '%'->prompt_getprompt()) 175 176 call prompt_setprompt( bufnr( '%' ), 'Another: ' ) 177 call assert_equal('Another: ', prompt_getprompt('%')) 178 let another = bufnr('%') 179 180 new 181 182 call assert_equal('', prompt_getprompt('%')) 183 call assert_equal('Another: ', prompt_getprompt(another)) 184 185 " Doesn't exist 186 let buffers_before = len( getbufinfo() ) 187 call assert_equal('', prompt_getprompt( bufnr('$') + 1)) 188 call assert_equal(buffers_before, len( getbufinfo())) 189 190 " invalid type 191 call assert_fails('call prompt_getprompt({})', 'E728:') 192 193 %bwipe! 194endfunc 195 196function! Test_prompt_while_writing_to_hidden_buffer() 197 call CanTestPromptBuffer() 198 CheckUnix 199 200 " Make a job continuously write to a hidden buffer, check that the prompt 201 " buffer is not affected. 202 let scriptName = 'XpromptscriptHiddenBuf' 203 let script =<< trim END 204 set buftype=prompt 205 call prompt_setprompt( bufnr(), 'cmd:' ) 206 let job = job_start(['/bin/sh', '-c', 207 \ 'while true; 208 \ do echo line; 209 \ sleep 0.1; 210 \ done'], #{out_io: 'buffer', out_name: ''}) 211 startinsert 212 END 213 eval script->writefile(scriptName) 214 215 let buf = RunVimInTerminal('-S ' .. scriptName, {}) 216 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))}) 217 218 call term_sendkeys(buf, 'test') 219 call WaitForAssert({-> assert_equal('cmd:test', term_getline(buf, 1))}) 220 call term_sendkeys(buf, 'test') 221 call WaitForAssert({-> assert_equal('cmd:testtest', term_getline(buf, 1))}) 222 call term_sendkeys(buf, 'test') 223 call WaitForAssert({-> assert_equal('cmd:testtesttest', term_getline(buf, 1))}) 224 225 call StopVimInTerminal(buf) 226 call delete(scriptName) 227endfunc 228 229" vim: shiftwidth=2 sts=2 expandtab 230