blob: 199c1c29847fc3e3766feb12058469680f7056d7 [file] [log] [blame]
Bram Moolenaarf2732452018-06-03 14:47:35 +02001" Tests for setting 'buftype' to "prompt"
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature channel
Bram Moolenaarf2732452018-06-03 14:47:35 +02005
6source shared.vim
7source screendump.vim
8
Bram Moolenaar6b810d92018-06-04 17:28:44 +02009func CanTestPromptBuffer()
Bram Moolenaarf2732452018-06-03 14:47:35 +020010 " We need to use a terminal window to be able to feed keys without leaving
11 " Insert mode.
12 if !has('terminal')
Bram Moolenaar6b810d92018-06-04 17:28:44 +020013 return 0
Bram Moolenaar11493822018-06-03 15:08:09 +020014 endif
15 if has('win32')
Bram Moolenaar6b810d92018-06-04 17:28:44 +020016 " TODO: make the tests work on MS-Windows
17 return 0
Bram Moolenaarf2732452018-06-03 14:47:35 +020018 endif
Bram Moolenaar6b810d92018-06-04 17:28:44 +020019 return 1
20endfunc
21
22func WriteScript(name)
Bram Moolenaarf2732452018-06-03 14:47:35 +020023 call writefile([
24 \ 'func TextEntered(text)',
25 \ ' if a:text == "exit"',
Bram Moolenaar71ef1ba2018-06-21 12:07:04 +020026 \ ' " Reset &modified to allow the buffer to be closed.',
27 \ ' set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020028 \ ' 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 . "\"")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020042 \ ' " Reset &modified to allow the buffer to be closed.',
43 \ ' set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020044 \ 'endfunc',
45 \ '',
46 \ 'call setline(1, "other buffer")',
Bram Moolenaar3339d3d2018-06-03 17:10:40 +020047 \ 'set nomodified',
Bram Moolenaarf2732452018-06-03 14:47:35 +020048 \ 'new',
49 \ 'set buftype=prompt',
50 \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020051 \ 'eval bufnr("")->prompt_setprompt("cmd: ")',
Bram Moolenaarf2732452018-06-03 14:47:35 +020052 \ '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 Moolenaar3f4f3d82019-09-04 20:05:59 +020064 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
Bram Moolenaarf2732452018-06-03 14:47:35 +020065
66 call term_sendkeys(buf, "hello\<CR>")
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020067 call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))})
Bram Moolenaarf2732452018-06-03 14:47:35 +020068 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, {})
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020086 call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020087
88 let bs = "\<BS>"
89 call term_sendkeys(buf, "hello" . bs . bs)
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020090 call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020091
92 let left = "\<Left>"
93 call term_sendkeys(buf, left . left . left . bs . '-')
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020094 call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020095
96 let end = "\<End>"
97 call term_sendkeys(buf, end . "x")
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020098 call WaitForAssert({-> assert_equal('cmd: -helx', term_getline(buf, 1))})
Bram Moolenaar6b810d92018-06-04 17:28:44 +020099
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
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200106
107func Test_prompt_garbage_collect()
108 func MyPromptCallback(x, text)
109 " NOP
110 endfunc
111 func MyPromptInterrupt(x)
112 " NOP
113 endfunc
114
115 new
116 set buftype=prompt
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200117 eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
118 eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200119 call test_garbagecollect_now()
120 " Must not crash
121 call feedkeys("\<CR>\<C-C>", 'xt')
122 call assert_true(v:true)
123
124 delfunc MyPromptCallback
125 bwipe!
126endfunc