blob: 1b8a1ec7497c6be468230e61ff3b9bc450ba03f2 [file] [log] [blame]
Bram Moolenaarf2732452018-06-03 14:47:35 +02001" Tests for setting 'buftype' to "prompt"
2
3if !has('channel')
4 finish
5endif
6
7source shared.vim
8source screendump.vim
9
Bram Moolenaar6b810d92018-06-04 17:28:44 +020010func CanTestPromptBuffer()
Bram Moolenaarf2732452018-06-03 14:47:35 +020011 " We need to use a terminal window to be able to feed keys without leaving
12 " Insert mode.
13 if !has('terminal')
Bram Moolenaar6b810d92018-06-04 17:28:44 +020014 return 0
Bram Moolenaar11493822018-06-03 15:08:09 +020015 endif
16 if has('win32')
Bram Moolenaar6b810d92018-06-04 17:28:44 +020017 " TODO: make the tests work on MS-Windows
18 return 0
Bram Moolenaarf2732452018-06-03 14:47:35 +020019 endif
Bram Moolenaar6b810d92018-06-04 17:28:44 +020020 return 1
21endfunc
22
23func WriteScript(name)
Bram Moolenaarf2732452018-06-03 14:47:35 +020024 call writefile([
25 \ 'func TextEntered(text)',
26 \ ' if a:text == "exit"',
Bram Moolenaar71ef1ba2018-06-21 12:07:04 +020027 \ ' " Reset &modified to allow the buffer to be closed.',
28 \ ' set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020029 \ ' 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 . "\"")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020043 \ ' " Reset &modified to allow the buffer to be closed.',
44 \ ' set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020045 \ 'endfunc',
46 \ '',
47 \ 'call setline(1, "other buffer")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020048 \ 'set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020049 \ 'new',
50 \ 'set buftype=prompt',
51 \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
52 \ 'startinsert',
Bram Moolenaar6b810d92018-06-04 17:28:44 +020053 \ ], 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, {})
Bram Moolenaarf2732452018-06-03 14:47:35 +020064 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)
Bram Moolenaar6b810d92018-06-04 17:28:44 +020075 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)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200105endfunc