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
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! ~')
132  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>")
142  " 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
147  close!
148  call assert_equal(0, prompt_setprompt([], ''))
149endfunc
150
151func 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
185" vim: shiftwidth=2 sts=2 expandtab
186