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	\ '    stopinsert',
28	\ '    close',
29	\ '  else',
30	\ '    " Add the output above the current prompt.',
31	\ '    call append(line("$") - 1, "Command: \"" . a:text . "\"")',
32	\ '    " Reset &modified to allow the buffer to be closed.',
33	\ '    set nomodified',
34	\ '    call timer_start(20, {id -> TimerFunc(a:text)})',
35	\ '  endif',
36	\ 'endfunc',
37	\ '',
38	\ 'func TimerFunc(text)',
39	\ '  " Add the output above the current prompt.',
40	\ '  call append(line("$") - 1, "Result: \"" . a:text . "\"")',
41	\ '  " Reset &modified to allow the buffer to be closed.',
42	\ '  set nomodified',
43	\ 'endfunc',
44	\ '',
45	\ 'call setline(1, "other buffer")',
46	\ 'set nomodified',
47	\ 'new',
48	\ 'set buftype=prompt',
49	\ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
50	\ 'startinsert',
51	\ ], a:name)
52endfunc
53
54func Test_prompt_basic()
55  if !CanTestPromptBuffer()
56    return
57  endif
58  let scriptName = 'XpromptscriptBasic'
59  call WriteScript(scriptName)
60
61  let buf = RunVimInTerminal('-S ' . scriptName, {})
62  call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))})
63
64  call term_sendkeys(buf, "hello\<CR>")
65  call WaitForAssert({-> assert_equal('% hello', term_getline(buf, 1))})
66  call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))})
67  call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))})
68
69  call term_sendkeys(buf, "exit\<CR>")
70  call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
71
72  call StopVimInTerminal(buf)
73  call delete(scriptName)
74endfunc
75
76func Test_prompt_editing()
77  if !CanTestPromptBuffer()
78    return
79  endif
80  let scriptName = 'XpromptscriptEditing'
81  call WriteScript(scriptName)
82
83  let buf = RunVimInTerminal('-S ' . scriptName, {})
84  call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))})
85
86  let bs = "\<BS>"
87  call term_sendkeys(buf, "hello" . bs . bs)
88  call WaitForAssert({-> assert_equal('% hel', term_getline(buf, 1))})
89
90  let left = "\<Left>"
91  call term_sendkeys(buf, left . left . left . bs . '-')
92  call WaitForAssert({-> assert_equal('% -hel', term_getline(buf, 1))})
93
94  let end = "\<End>"
95  call term_sendkeys(buf, end . "x")
96  call WaitForAssert({-> assert_equal('% -helx', term_getline(buf, 1))})
97
98  call term_sendkeys(buf, "\<C-U>exit\<CR>")
99  call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
100
101  call StopVimInTerminal(buf)
102  call delete(scriptName)
103endfunc
104