blob: 8c266ebe6f228b94c128cd4aaee32e431967ba76 [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
88 let end = "\<End>"
89 call term_sendkeys(buf, end . "x")
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020090 call WaitForAssert({-> assert_equal('cmd: -helx', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020091
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 Moolenaarf2732452018-06-03 14:47:35 +020097endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +020098
99func 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 Moolenaar3f4f3d82019-09-04 20:05:59 +0200109 eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
110 eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200111 call test_garbagecollect_now()
112 " Must not crash
113 call feedkeys("\<CR>\<C-C>", 'xt')
114 call assert_true(v:true)
115
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200116 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 Moolenaar75a1a942019-06-20 03:45:36 +0200121 delfunc MyPromptCallback
122 bwipe!
123endfunc
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100124
125" Test for editing the prompt buffer
126func 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 Moolenaar1671f442020-03-10 07:48:13 +0100132 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 Moolenaar845e0ee2020-06-20 16:05:32 +0200142 " 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 Moolenaarf5f1e102020-03-08 05:13:15 +0100147 close!
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200148 call assert_equal(0, prompt_setprompt([], ''))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100149endfunc
150
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200151func 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!
183endfunc
184
Bram Moolenaar4537bcc2020-10-01 20:03:04 +0200185function! 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)
216endfunc
217
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100218" vim: shiftwidth=2 sts=2 expandtab