blob: 32ed21e640cb3219b0ca8c19810396c05dc3484f [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 \ '',
42 \ 'call setline(1, "other buffer")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020043 \ 'set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020044 \ 'new',
45 \ 'set buftype=prompt',
46 \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020047 \ 'eval bufnr("")->prompt_setprompt("cmd: ")',
Bram Moolenaarf2732452018-06-03 14:47:35 +020048 \ 'startinsert',
Bram Moolenaar6b810d92018-06-04 17:28:44 +020049 \ ], a:name)
50endfunc
51
52func Test_prompt_basic()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020053 call CanTestPromptBuffer()
Bram Moolenaar6b810d92018-06-04 17:28:44 +020054 let scriptName = 'XpromptscriptBasic'
55 call WriteScript(scriptName)
56
57 let buf = RunVimInTerminal('-S ' . scriptName, {})
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020058 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
Bram Moolenaarf2732452018-06-03 14:47:35 +020059
60 call term_sendkeys(buf, "hello\<CR>")
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020061 call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))})
Bram Moolenaarf2732452018-06-03 14:47:35 +020062 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 Moolenaar6b810d92018-06-04 17:28:44 +020069 call delete(scriptName)
70endfunc
71
72func Test_prompt_editing()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020073 call CanTestPromptBuffer()
Bram Moolenaar6b810d92018-06-04 17:28:44 +020074 let scriptName = 'XpromptscriptEditing'
75 call WriteScript(scriptName)
76
77 let buf = RunVimInTerminal('-S ' . scriptName, {})
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020078 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020079
80 let bs = "\<BS>"
81 call term_sendkeys(buf, "hello" . bs . bs)
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020082 call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020083
84 let left = "\<Left>"
85 call term_sendkeys(buf, left . left . left . 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
Bram Moolenaaree8b7872020-11-19 18:46:25 +010088 call term_sendkeys(buf, "\<C-O>lz")
89 call WaitForAssert({-> assert_equal('cmd: -hzel', term_getline(buf, 1))})
90
Bram Moolenaar6b810d92018-06-04 17:28:44 +020091 let end = "\<End>"
92 call term_sendkeys(buf, end . "x")
Bram Moolenaaree8b7872020-11-19 18:46:25 +010093 call WaitForAssert({-> assert_equal('cmd: -hzelx', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020094
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)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200100endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200101
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
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200112 eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
113 eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200114 call test_garbagecollect_now()
115 " Must not crash
116 call feedkeys("\<CR>\<C-C>", 'xt')
117 call assert_true(v:true)
118
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200119 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
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200124 delfunc MyPromptCallback
125 bwipe!
126endfunc
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100127
Bram Moolenaar6f624482020-11-11 20:52:40 +0100128func 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
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100136" 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! ~')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100143 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>")
Yegappan Lakshmanan30443242021-06-10 21:52:15 +0200153 call assert_beeps("normal! dp")
154 call assert_beeps("normal! do")
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200155 " pressing CTRL-W in the prompt buffer should trigger the window commands
156 call assert_equal(1, winnr())
157 exe "normal A\<C-W>\<C-W>"
158 call assert_equal(2, winnr())
159 wincmd w
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100160 close!
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200161 call assert_equal(0, prompt_setprompt([], ''))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100162endfunc
163
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200164func Test_prompt_buffer_getbufinfo()
165 new
166 call assert_equal('', prompt_getprompt('%'))
167 call assert_equal('', prompt_getprompt(bufnr('%')))
168 let another_buffer = bufnr('%')
169
170 set buftype=prompt
171 call assert_equal('% ', prompt_getprompt('%'))
172 call prompt_setprompt( bufnr( '%' ), 'This is a test: ' )
173 call assert_equal('This is a test: ', prompt_getprompt('%'))
174
175 call prompt_setprompt( bufnr( '%' ), '' )
176 call assert_equal('', '%'->prompt_getprompt())
177
178 call prompt_setprompt( bufnr( '%' ), 'Another: ' )
179 call assert_equal('Another: ', prompt_getprompt('%'))
180 let another = bufnr('%')
181
182 new
183
184 call assert_equal('', prompt_getprompt('%'))
185 call assert_equal('Another: ', prompt_getprompt(another))
186
187 " Doesn't exist
188 let buffers_before = len( getbufinfo() )
189 call assert_equal('', prompt_getprompt( bufnr('$') + 1))
190 call assert_equal(buffers_before, len( getbufinfo()))
191
192 " invalid type
193 call assert_fails('call prompt_getprompt({})', 'E728:')
194
195 %bwipe!
196endfunc
197
Bram Moolenaar4537bcc2020-10-01 20:03:04 +0200198function! Test_prompt_while_writing_to_hidden_buffer()
199 call CanTestPromptBuffer()
200 CheckUnix
201
202 " Make a job continuously write to a hidden buffer, check that the prompt
203 " buffer is not affected.
204 let scriptName = 'XpromptscriptHiddenBuf'
205 let script =<< trim END
206 set buftype=prompt
207 call prompt_setprompt( bufnr(), 'cmd:' )
208 let job = job_start(['/bin/sh', '-c',
209 \ 'while true;
210 \ do echo line;
211 \ sleep 0.1;
212 \ done'], #{out_io: 'buffer', out_name: ''})
213 startinsert
214 END
215 eval script->writefile(scriptName)
216
217 let buf = RunVimInTerminal('-S ' .. scriptName, {})
218 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
219
220 call term_sendkeys(buf, 'test')
221 call WaitForAssert({-> assert_equal('cmd:test', term_getline(buf, 1))})
222 call term_sendkeys(buf, 'test')
223 call WaitForAssert({-> assert_equal('cmd:testtest', term_getline(buf, 1))})
224 call term_sendkeys(buf, 'test')
225 call WaitForAssert({-> assert_equal('cmd:testtesttest', term_getline(buf, 1))})
226
227 call StopVimInTerminal(buf)
228 call delete(scriptName)
229endfunc
230
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100231" vim: shiftwidth=2 sts=2 expandtab