blob: 028f3371e3ad4d99139146eab234addbf7fdc64a [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"))',
51 \ 'startinsert',
Bram Moolenaar6b810d92018-06-04 17:28:44 +020052 \ ], a:name)
53endfunc
54
55func Test_prompt_basic()
56 if !CanTestPromptBuffer()
57 return
58 endif
59 let scriptName = 'XpromptscriptBasic'
60 call WriteScript(scriptName)
61
62 let buf = RunVimInTerminal('-S ' . scriptName, {})
Bram Moolenaarf2732452018-06-03 14:47:35 +020063 call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))})
64
65 call term_sendkeys(buf, "hello\<CR>")
66 call WaitForAssert({-> assert_equal('% hello', term_getline(buf, 1))})
67 call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))})
68 call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))})
69
70 call term_sendkeys(buf, "exit\<CR>")
71 call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
72
73 call StopVimInTerminal(buf)
Bram Moolenaar6b810d92018-06-04 17:28:44 +020074 call delete(scriptName)
75endfunc
76
77func Test_prompt_editing()
78 if !CanTestPromptBuffer()
79 return
80 endif
81 let scriptName = 'XpromptscriptEditing'
82 call WriteScript(scriptName)
83
84 let buf = RunVimInTerminal('-S ' . scriptName, {})
85 call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))})
86
87 let bs = "\<BS>"
88 call term_sendkeys(buf, "hello" . bs . bs)
89 call WaitForAssert({-> assert_equal('% hel', term_getline(buf, 1))})
90
91 let left = "\<Left>"
92 call term_sendkeys(buf, left . left . left . bs . '-')
93 call WaitForAssert({-> assert_equal('% -hel', term_getline(buf, 1))})
94
95 let end = "\<End>"
96 call term_sendkeys(buf, end . "x")
97 call WaitForAssert({-> assert_equal('% -helx', term_getline(buf, 1))})
98
99 call term_sendkeys(buf, "\<C-U>exit\<CR>")
100 call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
101
102 call StopVimInTerminal(buf)
103 call delete(scriptName)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200104endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +0200105
106func Test_prompt_garbage_collect()
107 func MyPromptCallback(x, text)
108 " NOP
109 endfunc
110 func MyPromptInterrupt(x)
111 " NOP
112 endfunc
113
114 new
115 set buftype=prompt
116 call prompt_setcallback(bufnr(''), function('MyPromptCallback', [{}]))
117 call prompt_setinterrupt(bufnr(''), function('MyPromptInterrupt', [{}]))
118 call test_garbagecollect_now()
119 " Must not crash
120 call feedkeys("\<CR>\<C-C>", 'xt')
121 call assert_true(v:true)
122
123 delfunc MyPromptCallback
124 bwipe!
125endfunc