1" Tests for setting 'buftype' to "prompt"
2
3if !has('channel')
4  finish
5endif
6
7source shared.vim
8source screendump.vim
9
10func CanTestPromptBuffer()
11  " We need to use a terminal window to be able to feed keys without leaving
12  " Insert mode.
13  if !has('terminal')
14    return 0
15  endif
16  if has('win32')
17    " TODO: make the tests work on MS-Windows
18    return 0
19  endif
20  return 1
21endfunc
22
23func WriteScript(name)
24  call writefile([
25	\ 'func TextEntered(text)',
26	\ '  if a:text == "exit"',
27	\ '    " Reset &modified to allow the buffer to be closed.',
28	\ '    set nomodified',
29	\ '    stopinsert',
30	\ '    close',
31	\ '  else',
32	\ '    " Add the output above the current prompt.',
33	\ '    call append(line("$") - 1, "Command: \"" . a:text . "\"")',
34	\ '    " Reset &modified to allow the buffer to be closed.',
35	\ '    set nomodified',
36	\ '    call timer_start(20, {id -> TimerFunc(a:text)})',
37	\ '  endif',
38	\ 'endfunc',
39	\ '',
40	\ 'func TimerFunc(text)',
41	\ '  " Add the output above the current prompt.',
42	\ '  call append(line("$") - 1, "Result: \"" . a:text . "\"")',
43	\ '  " Reset &modified to allow the buffer to be closed.',
44	\ '  set nomodified',
45	\ 'endfunc',
46	\ '',
47	\ 'call setline(1, "other buffer")',
48	\ 'set nomodified',
49	\ 'new',
50	\ 'set buftype=prompt',
51	\ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
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('%', term_getline(buf, 1))})
65
66  call term_sendkeys(buf, "hello\<CR>")
67  call WaitForAssert({-> assert_equal('% 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('%', term_getline(buf, 1))})
87
88  let bs = "\<BS>"
89  call term_sendkeys(buf, "hello" . bs . bs)
90  call WaitForAssert({-> assert_equal('% hel', term_getline(buf, 1))})
91
92  let left = "\<Left>"
93  call term_sendkeys(buf, left . left . left . bs . '-')
94  call WaitForAssert({-> assert_equal('% -hel', term_getline(buf, 1))})
95
96  let end = "\<End>"
97  call term_sendkeys(buf, end . "x")
98  call WaitForAssert({-> assert_equal('% -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