blob: fb4b94511a09c0dddd5bf7d40b21ec0917e1bf89 [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 +02003source check.vim
4CheckFeature channel
Bram Moolenaarf2732452018-06-03 14:47:35 +02005
6source shared.vim
7source screendump.vim
8
Bram Moolenaar6b810d92018-06-04 17:28:44 +02009func CanTestPromptBuffer()
Bram Moolenaarf2732452018-06-03 14:47:35 +020010 " We need to use a terminal window to be able to feed keys without leaving
11 " Insert mode.
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020012 CheckFeature terminal
13
14 " TODO: make the tests work on MS-Windows
15 CheckNotMSWindows
Bram Moolenaar6b810d92018-06-04 17:28:44 +020016endfunc
17
18func WriteScript(name)
Bram Moolenaarf2732452018-06-03 14:47:35 +020019 call writefile([
20 \ 'func TextEntered(text)',
21 \ ' if a:text == "exit"',
Bram Moolenaar71ef1ba2018-06-21 12:07:04 +020022 \ ' " Reset &modified to allow the buffer to be closed.',
23 \ ' set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020024 \ ' 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 Moolenaar3339d3d2018-06-03 17:10:40 +020038 \ ' " Reset &modified to allow the buffer to be closed.',
39 \ ' set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020040 \ 'endfunc',
41 \ '',
Bram Moolenaar34c20ff2021-11-25 13:04:48 +000042 \ 'func SwitchWindows()',
43 \ ' call timer_start(0, {-> execute("wincmd p|wincmd p", "")})',
44 \ 'endfunc',
45 \ '',
Bram Moolenaarf2732452018-06-03 14:47:35 +020046 \ 'call setline(1, "other buffer")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020047 \ 'set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020048 \ 'new',
49 \ 'set buftype=prompt',
50 \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020051 \ 'eval bufnr("")->prompt_setprompt("cmd: ")',
Bram Moolenaarf2732452018-06-03 14:47:35 +020052 \ 'startinsert',
Bram Moolenaar6b810d92018-06-04 17:28:44 +020053 \ ], a:name)
54endfunc
55
56func Test_prompt_basic()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020057 call CanTestPromptBuffer()
Bram Moolenaar6b810d92018-06-04 17:28:44 +020058 let scriptName = 'XpromptscriptBasic'
59 call WriteScript(scriptName)
60
61 let buf = RunVimInTerminal('-S ' . scriptName, {})
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020062 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
Bram Moolenaarf2732452018-06-03 14:47:35 +020063
64 call term_sendkeys(buf, "hello\<CR>")
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020065 call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))})
Bram Moolenaarf2732452018-06-03 14:47:35 +020066 call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))})
67 call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))})
68
69 call term_sendkeys(buf, "exit\<CR>")
70 call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
71
72 call StopVimInTerminal(buf)
Bram Moolenaar6b810d92018-06-04 17:28:44 +020073 call delete(scriptName)
74endfunc
75
76func Test_prompt_editing()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020077 call CanTestPromptBuffer()
Bram Moolenaar6b810d92018-06-04 17:28:44 +020078 let scriptName = 'XpromptscriptEditing'
79 call WriteScript(scriptName)
80
81 let buf = RunVimInTerminal('-S ' . scriptName, {})
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020082 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020083
84 let bs = "\<BS>"
85 call term_sendkeys(buf, "hello" . bs . bs)
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020086 call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020087
88 let left = "\<Left>"
89 call term_sendkeys(buf, left . left . left . bs . '-')
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020090 call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020091
Bram Moolenaaree8b7872020-11-19 18:46:25 +010092 call term_sendkeys(buf, "\<C-O>lz")
93 call WaitForAssert({-> assert_equal('cmd: -hzel', term_getline(buf, 1))})
94
Bram Moolenaar6b810d92018-06-04 17:28:44 +020095 let end = "\<End>"
96 call term_sendkeys(buf, end . "x")
Bram Moolenaaree8b7872020-11-19 18:46:25 +010097 call WaitForAssert({-> assert_equal('cmd: -hzelx', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020098
99 call term_sendkeys(buf, "\<C-U>exit\<CR>")
100 call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
101
102 call StopVimInTerminal(buf)
103 call delete(scriptName)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200104endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200105
Bram Moolenaar34c20ff2021-11-25 13:04:48 +0000106func Test_prompt_switch_windows()
107 call CanTestPromptBuffer()
108 let scriptName = 'XpromptSwitchWindows'
109 call WriteScript(scriptName)
110
111 let buf = RunVimInTerminal('-S ' . scriptName, {'rows': 12})
112 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
113 call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 12))})
114
115 call term_sendkeys(buf, "\<C-O>:call SwitchWindows()\<CR>")
116 call term_wait(buf, 50)
117 call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 12))})
118
119 call term_sendkeys(buf, "\<Esc>")
120 call term_wait(buf, 50)
121 call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 12))})
122
123 call StopVimInTerminal(buf)
124 call delete(scriptName)
125endfunc
126
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200127func Test_prompt_garbage_collect()
128 func MyPromptCallback(x, text)
129 " NOP
130 endfunc
131 func MyPromptInterrupt(x)
132 " NOP
133 endfunc
134
135 new
136 set buftype=prompt
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200137 eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
138 eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200139 call test_garbagecollect_now()
140 " Must not crash
141 call feedkeys("\<CR>\<C-C>", 'xt')
142 call assert_true(v:true)
143
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200144 call assert_fails("call prompt_setcallback(bufnr(), [])", 'E921:')
145 call assert_equal(0, prompt_setcallback({}, ''))
146 call assert_fails("call prompt_setinterrupt(bufnr(), [])", 'E921:')
147 call assert_equal(0, prompt_setinterrupt({}, ''))
148
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200149 delfunc MyPromptCallback
150 bwipe!
151endfunc
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100152
Bram Moolenaar6f624482020-11-11 20:52:40 +0100153func Test_prompt_backspace()
154 new
155 set buftype=prompt
156 call feedkeys("A123456\<Left>\<BS>\<Esc>", 'xt')
157 call assert_equal('% 12346', getline(1))
158 bwipe!
159endfunc
160
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100161" Test for editing the prompt buffer
162func Test_prompt_buffer_edit()
163 new
164 set buftype=prompt
165 normal! i
166 call assert_beeps('normal! dd')
167 call assert_beeps('normal! ~')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100168 call assert_beeps('normal! o')
169 call assert_beeps('normal! O')
170 call assert_beeps('normal! p')
171 call assert_beeps('normal! P')
172 call assert_beeps('normal! u')
173 call assert_beeps('normal! ra')
174 call assert_beeps('normal! s')
175 call assert_beeps('normal! S')
176 call assert_beeps("normal! \<C-A>")
177 call assert_beeps("normal! \<C-X>")
Yegappan Lakshmanan30443242021-06-10 21:52:15 +0200178 call assert_beeps("normal! dp")
179 call assert_beeps("normal! do")
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200180 " pressing CTRL-W in the prompt buffer should trigger the window commands
181 call assert_equal(1, winnr())
182 exe "normal A\<C-W>\<C-W>"
183 call assert_equal(2, winnr())
184 wincmd w
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100185 close!
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200186 call assert_equal(0, prompt_setprompt([], ''))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100187endfunc
188
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200189func Test_prompt_buffer_getbufinfo()
190 new
191 call assert_equal('', prompt_getprompt('%'))
192 call assert_equal('', prompt_getprompt(bufnr('%')))
193 let another_buffer = bufnr('%')
194
195 set buftype=prompt
196 call assert_equal('% ', prompt_getprompt('%'))
197 call prompt_setprompt( bufnr( '%' ), 'This is a test: ' )
198 call assert_equal('This is a test: ', prompt_getprompt('%'))
199
200 call prompt_setprompt( bufnr( '%' ), '' )
201 call assert_equal('', '%'->prompt_getprompt())
202
203 call prompt_setprompt( bufnr( '%' ), 'Another: ' )
204 call assert_equal('Another: ', prompt_getprompt('%'))
205 let another = bufnr('%')
206
207 new
208
209 call assert_equal('', prompt_getprompt('%'))
210 call assert_equal('Another: ', prompt_getprompt(another))
211
212 " Doesn't exist
213 let buffers_before = len( getbufinfo() )
214 call assert_equal('', prompt_getprompt( bufnr('$') + 1))
215 call assert_equal(buffers_before, len( getbufinfo()))
216
217 " invalid type
218 call assert_fails('call prompt_getprompt({})', 'E728:')
219
220 %bwipe!
221endfunc
222
Bram Moolenaar4537bcc2020-10-01 20:03:04 +0200223function! Test_prompt_while_writing_to_hidden_buffer()
224 call CanTestPromptBuffer()
225 CheckUnix
226
227 " Make a job continuously write to a hidden buffer, check that the prompt
228 " buffer is not affected.
229 let scriptName = 'XpromptscriptHiddenBuf'
230 let script =<< trim END
231 set buftype=prompt
232 call prompt_setprompt( bufnr(), 'cmd:' )
233 let job = job_start(['/bin/sh', '-c',
234 \ 'while true;
235 \ do echo line;
236 \ sleep 0.1;
237 \ done'], #{out_io: 'buffer', out_name: ''})
238 startinsert
239 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100240 eval script->writefile(scriptName, 'D')
Bram Moolenaar4537bcc2020-10-01 20:03:04 +0200241
242 let buf = RunVimInTerminal('-S ' .. scriptName, {})
243 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
244
245 call term_sendkeys(buf, 'test')
246 call WaitForAssert({-> assert_equal('cmd:test', term_getline(buf, 1))})
247 call term_sendkeys(buf, 'test')
248 call WaitForAssert({-> assert_equal('cmd:testtest', term_getline(buf, 1))})
249 call term_sendkeys(buf, 'test')
250 call WaitForAssert({-> assert_equal('cmd:testtesttest', term_getline(buf, 1))})
251
252 call StopVimInTerminal(buf)
Bram Moolenaar4537bcc2020-10-01 20:03:04 +0200253endfunc
254
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100255" vim: shiftwidth=2 sts=2 expandtab