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  let end = "\<End>"
89  call term_sendkeys(buf, end . "x")
90  call WaitForAssert({-> assert_equal('cmd: -helx', term_getline(buf, 1))})
91
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)
97endfunc
98
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
109  eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
110  eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
111  call test_garbagecollect_now()
112  " Must not crash
113  call feedkeys("\<CR>\<C-C>", 'xt')
114  call assert_true(v:true)
115
116  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
121  delfunc MyPromptCallback
122  bwipe!
123endfunc
124
125func Test_prompt_backspace()
126  new
127  set buftype=prompt
128  call feedkeys("A123456\<Left>\<BS>\<Esc>", 'xt')
129  call assert_equal('% 12346', getline(1))
130  bwipe!
131endfunc
132
133" Test for editing the prompt buffer
134func Test_prompt_buffer_edit()
135  new
136  set buftype=prompt
137  normal! i
138  call assert_beeps('normal! dd')
139  call assert_beeps('normal! ~')
140  call assert_beeps('normal! o')
141  call assert_beeps('normal! O')
142  call assert_beeps('normal! p')
143  call assert_beeps('normal! P')
144  call assert_beeps('normal! u')
145  call assert_beeps('normal! ra')
146  call assert_beeps('normal! s')
147  call assert_beeps('normal! S')
148  call assert_beeps("normal! \<C-A>")
149  call assert_beeps("normal! \<C-X>")
150  " pressing CTRL-W in the prompt buffer should trigger the window commands
151  call assert_equal(1, winnr())
152  exe "normal A\<C-W>\<C-W>"
153  call assert_equal(2, winnr())
154  wincmd w
155  close!
156  call assert_equal(0, prompt_setprompt([], ''))
157endfunc
158
159func Test_prompt_buffer_getbufinfo()
160  new
161  call assert_equal('', prompt_getprompt('%'))
162  call assert_equal('', prompt_getprompt(bufnr('%')))
163  let another_buffer = bufnr('%')
164
165  set buftype=prompt
166  call assert_equal('% ', prompt_getprompt('%'))
167  call prompt_setprompt( bufnr( '%' ), 'This is a test: ' )
168  call assert_equal('This is a test: ', prompt_getprompt('%'))
169
170  call prompt_setprompt( bufnr( '%' ), '' )
171  call assert_equal('', '%'->prompt_getprompt())
172
173  call prompt_setprompt( bufnr( '%' ), 'Another: ' )
174  call assert_equal('Another: ', prompt_getprompt('%'))
175  let another = bufnr('%')
176
177  new
178
179  call assert_equal('', prompt_getprompt('%'))
180  call assert_equal('Another: ', prompt_getprompt(another))
181
182  " Doesn't exist
183  let buffers_before = len( getbufinfo() )
184  call assert_equal('', prompt_getprompt( bufnr('$') + 1))
185  call assert_equal(buffers_before, len( getbufinfo()))
186
187  " invalid type
188  call assert_fails('call prompt_getprompt({})', 'E728:')
189
190  %bwipe!
191endfunc
192
193function! Test_prompt_while_writing_to_hidden_buffer()
194  call CanTestPromptBuffer()
195  CheckUnix
196
197  " Make a job continuously write to a hidden buffer, check that the prompt
198  " buffer is not affected.
199  let scriptName = 'XpromptscriptHiddenBuf'
200  let script =<< trim END
201    set buftype=prompt
202    call prompt_setprompt( bufnr(), 'cmd:' )
203    let job = job_start(['/bin/sh', '-c',
204        \ 'while true;
205        \   do echo line;
206        \   sleep 0.1;
207        \ done'], #{out_io: 'buffer', out_name: ''})
208    startinsert
209  END
210  eval script->writefile(scriptName)
211
212  let buf = RunVimInTerminal('-S ' .. scriptName, {})
213  call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
214
215  call term_sendkeys(buf, 'test')
216  call WaitForAssert({-> assert_equal('cmd:test', term_getline(buf, 1))})
217  call term_sendkeys(buf, 'test')
218  call WaitForAssert({-> assert_equal('cmd:testtest', term_getline(buf, 1))})
219  call term_sendkeys(buf, 'test')
220  call WaitForAssert({-> assert_equal('cmd:testtesttest', term_getline(buf, 1))})
221
222  call StopVimInTerminal(buf)
223  call delete(scriptName)
224endfunc
225
226" vim: shiftwidth=2 sts=2 expandtab
227