blob: 414ed6e6cbb426a0ac7f8645bbf13554a94610a8 [file] [log] [blame]
Bram Moolenaarf2732452018-06-03 14:47:35 +02001" Tests for setting 'buftype' to "prompt"
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003CheckFeature channel
Bram Moolenaarf2732452018-06-03 14:47:35 +02004
Bram Moolenaarf2732452018-06-03 14:47:35 +02005source screendump.vim
6
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007func CanTestPromptBuffer()
Bram Moolenaarf2732452018-06-03 14:47:35 +02008 " We need to use a terminal window to be able to feed keys without leaving
9 " Insert mode.
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020010 CheckFeature terminal
11
12 " TODO: make the tests work on MS-Windows
13 CheckNotMSWindows
Bram Moolenaar6b810d92018-06-04 17:28:44 +020014endfunc
15
16func WriteScript(name)
Bram Moolenaarf2732452018-06-03 14:47:35 +020017 call writefile([
18 \ 'func TextEntered(text)',
19 \ ' if a:text == "exit"',
Bram Moolenaar71ef1ba2018-06-21 12:07:04 +020020 \ ' " Reset &modified to allow the buffer to be closed.',
21 \ ' set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020022 \ ' stopinsert',
23 \ ' close',
24 \ ' else',
25 \ ' " Add the output above the current prompt.',
26 \ ' call append(line("$") - 1, "Command: \"" . a:text . "\"")',
27 \ ' " Reset &modified to allow the buffer to be closed.',
28 \ ' set nomodified',
29 \ ' call timer_start(20, {id -> TimerFunc(a:text)})',
30 \ ' endif',
31 \ 'endfunc',
32 \ '',
33 \ 'func TimerFunc(text)',
34 \ ' " Add the output above the current prompt.',
35 \ ' call append(line("$") - 1, "Result: \"" . a:text . "\"")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020036 \ ' " Reset &modified to allow the buffer to be closed.',
37 \ ' set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020038 \ 'endfunc',
39 \ '',
Bram Moolenaar34c20ff2021-11-25 13:04:48 +000040 \ 'func SwitchWindows()',
41 \ ' call timer_start(0, {-> execute("wincmd p|wincmd p", "")})',
42 \ 'endfunc',
43 \ '',
Bram Moolenaarf2732452018-06-03 14:47:35 +020044 \ 'call setline(1, "other buffer")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020045 \ 'set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020046 \ 'new',
47 \ 'set buftype=prompt',
48 \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020049 \ 'eval bufnr("")->prompt_setprompt("cmd: ")',
Bram Moolenaarf2732452018-06-03 14:47:35 +020050 \ 'startinsert',
Bram Moolenaar6b810d92018-06-04 17:28:44 +020051 \ ], a:name)
52endfunc
53
54func Test_prompt_basic()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020055 call CanTestPromptBuffer()
Bram Moolenaar6b810d92018-06-04 17:28:44 +020056 let scriptName = 'XpromptscriptBasic'
57 call WriteScript(scriptName)
58
59 let buf = RunVimInTerminal('-S ' . scriptName, {})
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020060 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
Bram Moolenaarf2732452018-06-03 14:47:35 +020061
62 call term_sendkeys(buf, "hello\<CR>")
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020063 call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))})
Bram Moolenaarf2732452018-06-03 14:47:35 +020064 call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))})
65 call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))})
66
67 call term_sendkeys(buf, "exit\<CR>")
68 call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
69
70 call StopVimInTerminal(buf)
Bram Moolenaar6b810d92018-06-04 17:28:44 +020071 call delete(scriptName)
72endfunc
73
74func Test_prompt_editing()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020075 call CanTestPromptBuffer()
Bram Moolenaar6b810d92018-06-04 17:28:44 +020076 let scriptName = 'XpromptscriptEditing'
77 call WriteScript(scriptName)
78
79 let buf = RunVimInTerminal('-S ' . scriptName, {})
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020080 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020081
82 let bs = "\<BS>"
83 call term_sendkeys(buf, "hello" . bs . bs)
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020084 call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020085
86 let left = "\<Left>"
87 call term_sendkeys(buf, left . left . left . bs . '-')
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020088 call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020089
Bram Moolenaaree8b7872020-11-19 18:46:25 +010090 call term_sendkeys(buf, "\<C-O>lz")
91 call WaitForAssert({-> assert_equal('cmd: -hzel', term_getline(buf, 1))})
92
Bram Moolenaar6b810d92018-06-04 17:28:44 +020093 let end = "\<End>"
94 call term_sendkeys(buf, end . "x")
Bram Moolenaaree8b7872020-11-19 18:46:25 +010095 call WaitForAssert({-> assert_equal('cmd: -hzelx', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020096
97 call term_sendkeys(buf, "\<C-U>exit\<CR>")
98 call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
99
100 call StopVimInTerminal(buf)
101 call delete(scriptName)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200102endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200103
Bram Moolenaar34c20ff2021-11-25 13:04:48 +0000104func Test_prompt_switch_windows()
105 call CanTestPromptBuffer()
106 let scriptName = 'XpromptSwitchWindows'
107 call WriteScript(scriptName)
108
109 let buf = RunVimInTerminal('-S ' . scriptName, {'rows': 12})
110 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
111 call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 12))})
112
113 call term_sendkeys(buf, "\<C-O>:call SwitchWindows()\<CR>")
114 call term_wait(buf, 50)
115 call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 12))})
116
117 call term_sendkeys(buf, "\<Esc>")
118 call term_wait(buf, 50)
119 call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 12))})
120
121 call StopVimInTerminal(buf)
122 call delete(scriptName)
123endfunc
124
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200125func Test_prompt_garbage_collect()
126 func MyPromptCallback(x, text)
127 " NOP
128 endfunc
129 func MyPromptInterrupt(x)
130 " NOP
131 endfunc
132
133 new
134 set buftype=prompt
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200135 eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
136 eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200137 call test_garbagecollect_now()
138 " Must not crash
139 call feedkeys("\<CR>\<C-C>", 'xt')
140 call assert_true(v:true)
141
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200142 call assert_fails("call prompt_setcallback(bufnr(), [])", 'E921:')
143 call assert_equal(0, prompt_setcallback({}, ''))
144 call assert_fails("call prompt_setinterrupt(bufnr(), [])", 'E921:')
145 call assert_equal(0, prompt_setinterrupt({}, ''))
146
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200147 delfunc MyPromptCallback
148 bwipe!
149endfunc
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100150
Bram Moolenaar6f624482020-11-11 20:52:40 +0100151func Test_prompt_backspace()
152 new
153 set buftype=prompt
154 call feedkeys("A123456\<Left>\<BS>\<Esc>", 'xt')
155 call assert_equal('% 12346', getline(1))
156 bwipe!
157endfunc
158
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100159" Test for editing the prompt buffer
160func Test_prompt_buffer_edit()
161 new
162 set buftype=prompt
163 normal! i
164 call assert_beeps('normal! dd')
165 call assert_beeps('normal! ~')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100166 call assert_beeps('normal! o')
167 call assert_beeps('normal! O')
168 call assert_beeps('normal! p')
169 call assert_beeps('normal! P')
170 call assert_beeps('normal! u')
171 call assert_beeps('normal! ra')
172 call assert_beeps('normal! s')
173 call assert_beeps('normal! S')
174 call assert_beeps("normal! \<C-A>")
175 call assert_beeps("normal! \<C-X>")
Yegappan Lakshmanan30443242021-06-10 21:52:15 +0200176 call assert_beeps("normal! dp")
177 call assert_beeps("normal! do")
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200178 " pressing CTRL-W in the prompt buffer should trigger the window commands
179 call assert_equal(1, winnr())
180 exe "normal A\<C-W>\<C-W>"
181 call assert_equal(2, winnr())
182 wincmd w
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100183 close!
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200184 call assert_equal(0, prompt_setprompt([], ''))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100185endfunc
186
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200187func Test_prompt_buffer_getbufinfo()
188 new
189 call assert_equal('', prompt_getprompt('%'))
190 call assert_equal('', prompt_getprompt(bufnr('%')))
191 let another_buffer = bufnr('%')
192
193 set buftype=prompt
194 call assert_equal('% ', prompt_getprompt('%'))
195 call prompt_setprompt( bufnr( '%' ), 'This is a test: ' )
196 call assert_equal('This is a test: ', prompt_getprompt('%'))
197
198 call prompt_setprompt( bufnr( '%' ), '' )
199 call assert_equal('', '%'->prompt_getprompt())
200
201 call prompt_setprompt( bufnr( '%' ), 'Another: ' )
202 call assert_equal('Another: ', prompt_getprompt('%'))
203 let another = bufnr('%')
204
205 new
206
207 call assert_equal('', prompt_getprompt('%'))
208 call assert_equal('Another: ', prompt_getprompt(another))
209
210 " Doesn't exist
211 let buffers_before = len( getbufinfo() )
212 call assert_equal('', prompt_getprompt( bufnr('$') + 1))
213 call assert_equal(buffers_before, len( getbufinfo()))
214
215 " invalid type
216 call assert_fails('call prompt_getprompt({})', 'E728:')
217
218 %bwipe!
219endfunc
220
Bram Moolenaar97f0eb12022-10-06 19:49:13 +0100221func Test_prompt_while_writing_to_hidden_buffer()
Bram Moolenaar4537bcc2020-10-01 20:03:04 +0200222 call CanTestPromptBuffer()
223 CheckUnix
224
225 " Make a job continuously write to a hidden buffer, check that the prompt
226 " buffer is not affected.
227 let scriptName = 'XpromptscriptHiddenBuf'
228 let script =<< trim END
229 set buftype=prompt
230 call prompt_setprompt( bufnr(), 'cmd:' )
231 let job = job_start(['/bin/sh', '-c',
232 \ 'while true;
233 \ do echo line;
234 \ sleep 0.1;
235 \ done'], #{out_io: 'buffer', out_name: ''})
236 startinsert
237 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100238 eval script->writefile(scriptName, 'D')
Bram Moolenaar4537bcc2020-10-01 20:03:04 +0200239
240 let buf = RunVimInTerminal('-S ' .. scriptName, {})
241 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
242
243 call term_sendkeys(buf, 'test')
244 call WaitForAssert({-> assert_equal('cmd:test', term_getline(buf, 1))})
245 call term_sendkeys(buf, 'test')
246 call WaitForAssert({-> assert_equal('cmd:testtest', term_getline(buf, 1))})
247 call term_sendkeys(buf, 'test')
248 call WaitForAssert({-> assert_equal('cmd:testtesttest', term_getline(buf, 1))})
249
250 call StopVimInTerminal(buf)
Bram Moolenaar4537bcc2020-10-01 20:03:04 +0200251endfunc
252
orbitalcde8de02023-04-02 22:05:13 +0100253func Test_prompt_appending_while_hidden()
254 call CanTestPromptBuffer()
255
256 let script =<< trim END
257 new prompt
258 set buftype=prompt
259 set bufhidden=hide
260
261 func s:TextEntered(text)
262 if a:text == 'exit'
263 close
264 endif
265 echowin 'Entered:' a:text
266 endfunc
267 call prompt_setcallback(bufnr(), function('s:TextEntered'))
268
269 func DoAppend()
270 call appendbufline('prompt', '$', 'Test')
Bram Moolenaar05a627c2023-04-09 22:01:31 +0100271 return ''
orbitalcde8de02023-04-02 22:05:13 +0100272 endfunc
273 END
274 call writefile(script, 'XpromptBuffer', 'D')
275
276 let buf = RunVimInTerminal('-S XpromptBuffer', {'rows': 10})
277 call TermWait(buf)
278
279 call term_sendkeys(buf, "asomething\<CR>")
280 call TermWait(buf)
281
282 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaarff6c2302023-04-13 17:32:23 +0100283 call WaitForAssert({-> assert_notmatch('-- INSERT --', term_getline(buf, 10))})
orbitalcde8de02023-04-02 22:05:13 +0100284
285 call term_sendkeys(buf, ":call DoAppend()\<CR>")
Bram Moolenaarff6c2302023-04-13 17:32:23 +0100286 call WaitForAssert({-> assert_notmatch('-- INSERT --', term_getline(buf, 10))})
orbitalcde8de02023-04-02 22:05:13 +0100287
Bram Moolenaar05a627c2023-04-09 22:01:31 +0100288 call term_sendkeys(buf, "i")
Bram Moolenaarff6c2302023-04-13 17:32:23 +0100289 call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 10))})
Bram Moolenaar05a627c2023-04-09 22:01:31 +0100290
291 call term_sendkeys(buf, "\<C-R>=DoAppend()\<CR>")
Bram Moolenaarff6c2302023-04-13 17:32:23 +0100292 call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 10))})
Bram Moolenaar05a627c2023-04-09 22:01:31 +0100293
294 call term_sendkeys(buf, "\<Esc>")
orbitalcde8de02023-04-02 22:05:13 +0100295 call StopVimInTerminal(buf)
296endfunc
297
zeertzjqf2678472024-01-17 21:22:59 +0100298" Modifying a hidden buffer while leaving a prompt buffer should not prevent
299" stopping of Insert mode, and returning to the prompt buffer later should
300" restore Insert mode.
301func Test_prompt_leave_modify_hidden()
zeertzjq96958362024-01-16 17:19:59 +0100302 call CanTestPromptBuffer()
303
304 let script =<< trim END
305 file hidden
306 set bufhidden=hide
307 enew
308 new prompt
309 set buftype=prompt
310
zeertzjqf2678472024-01-17 21:22:59 +0100311 inoremap <buffer> w <Cmd>wincmd w<CR>
zeertzjq96958362024-01-16 17:19:59 +0100312 inoremap <buffer> q <Cmd>bwipe!<CR>
zeertzjqf2678472024-01-17 21:22:59 +0100313 autocmd BufLeave prompt call appendbufline('hidden', '$', 'Leave')
314 autocmd BufEnter prompt call appendbufline('hidden', '$', 'Enter')
315 autocmd BufWinLeave prompt call appendbufline('hidden', '$', 'Close')
zeertzjq96958362024-01-16 17:19:59 +0100316 END
zeertzjqf2678472024-01-17 21:22:59 +0100317 call writefile(script, 'XpromptLeaveModifyHidden', 'D')
zeertzjq96958362024-01-16 17:19:59 +0100318
zeertzjqf2678472024-01-17 21:22:59 +0100319 let buf = RunVimInTerminal('-S XpromptLeaveModifyHidden', {'rows': 10})
zeertzjq96958362024-01-16 17:19:59 +0100320 call TermWait(buf)
321
322 call term_sendkeys(buf, "a")
323 call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 10))})
324
zeertzjqf2678472024-01-17 21:22:59 +0100325 call term_sendkeys(buf, "w")
326 call WaitForAssert({-> assert_notmatch('-- INSERT --', term_getline(buf, 10))})
327
328 call term_sendkeys(buf, "\<C-W>w")
329 call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 10))})
330
zeertzjq96958362024-01-16 17:19:59 +0100331 call term_sendkeys(buf, "q")
332 call WaitForAssert({-> assert_notmatch('-- INSERT --', term_getline(buf, 10))})
333
334 call term_sendkeys(buf, ":bwipe!\<CR>")
zeertzjqf2678472024-01-17 21:22:59 +0100335 call WaitForAssert({-> assert_equal('Leave', term_getline(buf, 2))})
336 call WaitForAssert({-> assert_equal('Enter', term_getline(buf, 3))})
337 call WaitForAssert({-> assert_equal('Leave', term_getline(buf, 4))})
338 call WaitForAssert({-> assert_equal('Close', term_getline(buf, 5))})
zeertzjq96958362024-01-16 17:19:59 +0100339
340 call StopVimInTerminal(buf)
341endfunc
342
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100343" vim: shiftwidth=2 sts=2 expandtab