blob: a6269dec49a9ded6710b31b72510bb81712074a7 [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"',
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 . "\"")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020041 \ ' " Reset &modified to allow the buffer to be closed.',
42 \ ' set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020043 \ 'endfunc',
44 \ '',
45 \ 'call setline(1, "other buffer")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020046 \ 'set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020047 \ 'new',
48 \ 'set buftype=prompt',
49 \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
50 \ 'startinsert',
Bram Moolenaar6b810d92018-06-04 17:28:44 +020051 \ ], 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, {})
Bram Moolenaarf2732452018-06-03 14:47:35 +020062 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)
Bram Moolenaar6b810d92018-06-04 17:28:44 +020073 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)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200103endfunc