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