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  if !has('terminal')
13    return 0
14  endif
15  if has('win32')
16    " TODO: make the tests work on MS-Windows
17    return 0
18  endif
19  return 1
20endfunc
21
22func WriteScript(name)
23  call writefile([
24	\ 'func TextEntered(text)',
25	\ '  if a:text == "exit"',
26	\ '    " Reset &modified to allow the buffer to be closed.',
27	\ '    set nomodified',
28	\ '    stopinsert',
29	\ '    close',
30	\ '  else',
31	\ '    " Add the output above the current prompt.',
32	\ '    call append(line("$") - 1, "Command: \"" . a:text . "\"")',
33	\ '    " Reset &modified to allow the buffer to be closed.',
34	\ '    set nomodified',
35	\ '    call timer_start(20, {id -> TimerFunc(a:text)})',
36	\ '  endif',
37	\ 'endfunc',
38	\ '',
39	\ 'func TimerFunc(text)',
40	\ '  " Add the output above the current prompt.',
41	\ '  call append(line("$") - 1, "Result: \"" . a:text . "\"")',
42	\ '  " Reset &modified to allow the buffer to be closed.',
43	\ '  set nomodified',
44	\ 'endfunc',
45	\ '',
46	\ 'call setline(1, "other buffer")',
47	\ 'set nomodified',
48	\ 'new',
49	\ 'set buftype=prompt',
50	\ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
51	\ 'eval bufnr("")->prompt_setprompt("cmd: ")',
52	\ 'startinsert',
53	\ ], a:name)
54endfunc
55
56func Test_prompt_basic()
57  if !CanTestPromptBuffer()
58    return
59  endif
60  let scriptName = 'XpromptscriptBasic'
61  call WriteScript(scriptName)
62
63  let buf = RunVimInTerminal('-S ' . scriptName, {})
64  call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
65
66  call term_sendkeys(buf, "hello\<CR>")
67  call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))})
68  call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))})
69  call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))})
70
71  call term_sendkeys(buf, "exit\<CR>")
72  call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
73
74  call StopVimInTerminal(buf)
75  call delete(scriptName)
76endfunc
77
78func Test_prompt_editing()
79  if !CanTestPromptBuffer()
80    return
81  endif
82  let scriptName = 'XpromptscriptEditing'
83  call WriteScript(scriptName)
84
85  let buf = RunVimInTerminal('-S ' . scriptName, {})
86  call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
87
88  let bs = "\<BS>"
89  call term_sendkeys(buf, "hello" . bs . bs)
90  call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))})
91
92  let left = "\<Left>"
93  call term_sendkeys(buf, left . left . left . bs . '-')
94  call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))})
95
96  let end = "\<End>"
97  call term_sendkeys(buf, end . "x")
98  call WaitForAssert({-> assert_equal('cmd: -helx', term_getline(buf, 1))})
99
100  call term_sendkeys(buf, "\<C-U>exit\<CR>")
101  call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
102
103  call StopVimInTerminal(buf)
104  call delete(scriptName)
105endfunc
106
107func Test_prompt_garbage_collect()
108  func MyPromptCallback(x, text)
109    " NOP
110  endfunc
111  func MyPromptInterrupt(x)
112    " NOP
113  endfunc
114
115  new
116  set buftype=prompt
117  eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
118  eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
119  call test_garbagecollect_now()
120  " Must not crash
121  call feedkeys("\<CR>\<C-C>", 'xt')
122  call assert_true(v:true)
123
124  call assert_fails("call prompt_setcallback(bufnr(), [])", 'E921:')
125  call assert_equal(0, prompt_setcallback({}, ''))
126  call assert_fails("call prompt_setinterrupt(bufnr(), [])", 'E921:')
127  call assert_equal(0, prompt_setinterrupt({}, ''))
128
129  delfunc MyPromptCallback
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
159" vim: shiftwidth=2 sts=2 expandtab
160