1" Tests for setting 'buftype' to "prompt"
2
3source check.vim
4CheckFeature channel
5
6source shared.vim
7source screendump.vim
8
9func CanTestPromptBuffer()
10  " We need to use a terminal window to be able to feed keys without leaving
11  " Insert mode.
12  CheckFeature terminal
13
14  " TODO: make the tests work on MS-Windows
15  CheckNotMSWindows
16endfunc
17
18func WriteScript(name)
19  call writefile([
20	\ 'func TextEntered(text)',
21	\ '  if a:text == "exit"',
22	\ '    " Reset &modified to allow the buffer to be closed.',
23	\ '    set nomodified',
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 . "\"")',
38	\ '  " Reset &modified to allow the buffer to be closed.',
39	\ '  set nomodified',
40	\ 'endfunc',
41	\ '',
42	\ 'call setline(1, "other buffer")',
43	\ 'set nomodified',
44	\ 'new',
45	\ 'set buftype=prompt',
46	\ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
47	\ 'eval bufnr("")->prompt_setprompt("cmd: ")',
48	\ 'startinsert',
49	\ ], a:name)
50endfunc
51
52func Test_prompt_basic()
53  call CanTestPromptBuffer()
54  let scriptName = 'XpromptscriptBasic'
55  call WriteScript(scriptName)
56
57  let buf = RunVimInTerminal('-S ' . scriptName, {})
58  call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
59
60  call term_sendkeys(buf, "hello\<CR>")
61  call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))})
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)
69  call delete(scriptName)
70endfunc
71
72func Test_prompt_editing()
73  call CanTestPromptBuffer()
74  let scriptName = 'XpromptscriptEditing'
75  call WriteScript(scriptName)
76
77  let buf = RunVimInTerminal('-S ' . scriptName, {})
78  call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
79
80  let bs = "\<BS>"
81  call term_sendkeys(buf, "hello" . bs . bs)
82  call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))})
83
84  let left = "\<Left>"
85  call term_sendkeys(buf, left . left . left . bs . '-')
86  call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))})
87
88  call term_sendkeys(buf, "\<C-O>lz")
89  call WaitForAssert({-> assert_equal('cmd: -hzel', term_getline(buf, 1))})
90
91  let end = "\<End>"
92  call term_sendkeys(buf, end . "x")
93  call WaitForAssert({-> assert_equal('cmd: -hzelx', term_getline(buf, 1))})
94
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)
100endfunc
101
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
112  eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
113  eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
114  call test_garbagecollect_now()
115  " Must not crash
116  call feedkeys("\<CR>\<C-C>", 'xt')
117  call assert_true(v:true)
118
119  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
124  delfunc MyPromptCallback
125  bwipe!
126endfunc
127
128func 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
136" 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! ~')
143  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>")
153  call assert_beeps("normal! dp")
154  call assert_beeps("normal! do")
155  " 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
160  close!
161  call assert_equal(0, prompt_setprompt([], ''))
162endfunc
163
164func 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
198function! 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
231" vim: shiftwidth=2 sts=2 expandtab
232