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	\ 'startinsert',
52	\ ], a:name)
53endfunc
54
55func Test_prompt_basic()
56  if !CanTestPromptBuffer()
57    return
58  endif
59  let scriptName = 'XpromptscriptBasic'
60  call WriteScript(scriptName)
61
62  let buf = RunVimInTerminal('-S ' . scriptName, {})
63  call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))})
64
65  call term_sendkeys(buf, "hello\<CR>")
66  call WaitForAssert({-> assert_equal('% hello', term_getline(buf, 1))})
67  call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))})
68  call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))})
69
70  call term_sendkeys(buf, "exit\<CR>")
71  call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
72
73  call StopVimInTerminal(buf)
74  call delete(scriptName)
75endfunc
76
77func Test_prompt_editing()
78  if !CanTestPromptBuffer()
79    return
80  endif
81  let scriptName = 'XpromptscriptEditing'
82  call WriteScript(scriptName)
83
84  let buf = RunVimInTerminal('-S ' . scriptName, {})
85  call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))})
86
87  let bs = "\<BS>"
88  call term_sendkeys(buf, "hello" . bs . bs)
89  call WaitForAssert({-> assert_equal('% hel', term_getline(buf, 1))})
90
91  let left = "\<Left>"
92  call term_sendkeys(buf, left . left . left . bs . '-')
93  call WaitForAssert({-> assert_equal('% -hel', term_getline(buf, 1))})
94
95  let end = "\<End>"
96  call term_sendkeys(buf, end . "x")
97  call WaitForAssert({-> assert_equal('% -helx', term_getline(buf, 1))})
98
99  call term_sendkeys(buf, "\<C-U>exit\<CR>")
100  call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
101
102  call StopVimInTerminal(buf)
103  call delete(scriptName)
104endfunc
105
106func Test_prompt_garbage_collect()
107  func MyPromptCallback(x, text)
108    " NOP
109  endfunc
110  func MyPromptInterrupt(x)
111    " NOP
112  endfunc
113
114  new
115  set buftype=prompt
116  call prompt_setcallback(bufnr(''), function('MyPromptCallback', [{}]))
117  call prompt_setinterrupt(bufnr(''), function('MyPromptInterrupt', [{}]))
118  call test_garbagecollect_now()
119  " Must not crash
120  call feedkeys("\<CR>\<C-C>", 'xt')
121  call assert_true(v:true)
122
123  delfunc MyPromptCallback
124  bwipe!
125endfunc
126