blob: e076241607560c8272643e40021d7f31930c2a82 [file] [log] [blame]
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01001" Tests for :messages, :echomsg, :echoerr
Bram Moolenaar451f8492016-04-14 17:16:22 +02002
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02003source shared.vim
Bram Moolenaarc6d539b2019-12-28 17:10:46 +01004source term_util.vim
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02005
Bram Moolenaar49b2fb32020-04-25 17:13:56 +02006func Test_messages()
Bram Moolenaar451f8492016-04-14 17:16:22 +02007 let oldmore = &more
8 try
9 set nomore
10
11 let arr = map(range(10), '"hello" . v:val')
12 for s in arr
13 echomsg s | redraw
14 endfor
Bram Moolenaar451f8492016-04-14 17:16:22 +020015
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020016 " get last two messages
Bram Moolenaar451f8492016-04-14 17:16:22 +020017 redir => result
18 2messages | redraw
19 redir END
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020020 let msg_list = split(result, "\n")
21 call assert_equal(["hello8", "hello9"], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020022
23 " clear messages without last one
24 1messages clear
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020025 let msg_list = GetMessages()
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020026 call assert_equal(['hello9'], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020027
28 " clear all messages
29 messages clear
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020030 let msg_list = GetMessages()
31 call assert_equal([], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020032 finally
33 let &more = oldmore
34 endtry
Bram Moolenaar067297e2020-04-13 19:55:50 +020035
36 call assert_fails('message 1', 'E474:')
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020037endfunc
Bram Moolenaar2abad542018-05-19 14:43:45 +020038
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020039" Patch 7.4.1696 defined the "clearmode()" function for clearing the mode
Bram Moolenaar2abad542018-05-19 14:43:45 +020040" indicator (e.g., "-- INSERT --") when ":stopinsert" is invoked. Message
41" output could then be disturbed when 'cmdheight' was greater than one.
42" This test ensures that the bugfix for this issue remains in place.
Bram Moolenaar1e115362019-01-09 23:01:02 +010043func Test_stopinsert_does_not_break_message_output()
Bram Moolenaar2abad542018-05-19 14:43:45 +020044 set cmdheight=2
45 redraw!
46
47 stopinsert | echo 'test echo'
48 call assert_equal(116, screenchar(&lines - 1, 1))
49 call assert_equal(32, screenchar(&lines, 1))
50 redraw!
51
52 stopinsert | echomsg 'test echomsg'
53 call assert_equal(116, screenchar(&lines - 1, 1))
54 call assert_equal(32, screenchar(&lines, 1))
55 redraw!
56
57 set cmdheight&
Bram Moolenaar1e115362019-01-09 23:01:02 +010058endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +010059
60func Test_message_completion()
61 call feedkeys(":message \<C-A>\<C-B>\"\<CR>", 'tx')
62 call assert_equal('"message clear', @:)
63endfunc
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010064
65func Test_echomsg()
66 call assert_equal("\nhello", execute(':echomsg "hello"'))
67 call assert_equal("\n", execute(':echomsg ""'))
68 call assert_equal("\n12345", execute(':echomsg 12345'))
69 call assert_equal("\n[]", execute(':echomsg []'))
70 call assert_equal("\n[1, 2, 3]", execute(':echomsg [1, 2, 3]'))
Bram Moolenaardb950e42020-04-22 19:13:19 +020071 call assert_equal("\n[1, 2, []]", execute(':echomsg [1, 2, test_null_list()]'))
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010072 call assert_equal("\n{}", execute(':echomsg {}'))
73 call assert_equal("\n{'a': 1, 'b': 2}", execute(':echomsg {"a": 1, "b": 2}'))
74 if has('float')
75 call assert_equal("\n1.23", execute(':echomsg 1.23'))
76 endif
77 call assert_match("function('<lambda>\\d*')", execute(':echomsg {-> 1234}'))
78endfunc
79
80func Test_echoerr()
81 call test_ignore_error('IgNoRe')
82 call assert_equal("\nIgNoRe hello", execute(':echoerr "IgNoRe hello"'))
83 call assert_equal("\n12345 IgNoRe", execute(':echoerr 12345 "IgNoRe"'))
84 call assert_equal("\n[1, 2, 'IgNoRe']", execute(':echoerr [1, 2, "IgNoRe"]'))
85 call assert_equal("\n{'IgNoRe': 2, 'a': 1}", execute(':echoerr {"a": 1, "IgNoRe": 2}'))
86 if has('float')
87 call assert_equal("\n1.23 IgNoRe", execute(':echoerr 1.23 "IgNoRe"'))
88 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +020089 eval '<lambda>'->test_ignore_error()
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010090 call assert_match("function('<lambda>\\d*')", execute(':echoerr {-> 1234}'))
91 call test_ignore_error('RESET')
92endfunc
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +020093
94func Test_mode_message_at_leaving_insert_by_ctrl_c()
95 if !has('terminal') || has('gui_running')
96 return
97 endif
98
99 " Set custom statusline built by user-defined function.
100 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200101 let lines =<< trim END
102 func StatusLine() abort
103 return ""
104 endfunc
105 set statusline=%!StatusLine()
106 set laststatus=2
107 END
108 call writefile(lines, testfile)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200109
110 let rows = 10
111 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200112 call TermWait(buf, 100)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200113 call assert_equal('run', job_status(term_getjob(buf)))
114
115 call term_sendkeys(buf, "i")
116 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
117 call term_sendkeys(buf, "\<C-C>")
118 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
119
120 call term_sendkeys(buf, ":qall!\<CR>")
121 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
122 exe buf . 'bwipe!'
123 call delete(testfile)
124endfunc
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200125
126func Test_mode_message_at_leaving_insert_with_esc_mapped()
127 if !has('terminal') || has('gui_running')
128 return
129 endif
130
131 " Set custom statusline built by user-defined function.
132 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200133 let lines =<< trim END
134 set laststatus=2
135 inoremap <Esc> <Esc>00
136 END
137 call writefile(lines, testfile)
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200138
139 let rows = 10
140 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200141 call WaitForAssert({-> assert_match('0,0-1\s*All$', term_getline(buf, rows - 1))})
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200142 call assert_equal('run', job_status(term_getjob(buf)))
143
144 call term_sendkeys(buf, "i")
145 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
146 call term_sendkeys(buf, "\<Esc>")
147 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
148
149 call term_sendkeys(buf, ":qall!\<CR>")
150 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
151 exe buf . 'bwipe!'
152 call delete(testfile)
153endfunc
Bram Moolenaar37f4cbd2019-08-23 20:58:45 +0200154
155func Test_echospace()
156 set noruler noshowcmd laststatus=1
157 call assert_equal(&columns - 1, v:echospace)
158 split
159 call assert_equal(&columns - 1, v:echospace)
160 set ruler
161 call assert_equal(&columns - 1, v:echospace)
162 close
163 call assert_equal(&columns - 19, v:echospace)
164 set showcmd noruler
165 call assert_equal(&columns - 12, v:echospace)
166 set showcmd ruler
167 call assert_equal(&columns - 29, v:echospace)
168
169 set ruler& showcmd&
170endfunc
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100171
172" Test more-prompt (see :help more-prompt).
173func Test_message_more()
174 if !CanRunVimInTerminal()
175 throw 'Skipped: cannot run vim in terminal'
176 endif
177 let buf = RunVimInTerminal('', {'rows': 6})
178 call term_sendkeys(buf, ":call setline(1, range(1, 100))\n")
179
180 call term_sendkeys(buf, ":%p#\n")
181 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
182 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
183
184 call term_sendkeys(buf, '?')
185 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
186 call WaitForAssert({-> assert_equal('-- More -- SPACE/d/j: screen/page/line down, b/u/k: up, q: quit ', term_getline(buf, 6))})
187
188 " Down a line with j, <CR>, <NL> or <Down>.
189 call term_sendkeys(buf, "j")
190 call WaitForAssert({-> assert_equal(' 6 6', term_getline(buf, 5))})
191 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
192 call term_sendkeys(buf, "\<NL>")
193 call WaitForAssert({-> assert_equal(' 7 7', term_getline(buf, 5))})
194 call term_sendkeys(buf, "\<CR>")
195 call WaitForAssert({-> assert_equal(' 8 8', term_getline(buf, 5))})
196 call term_sendkeys(buf, "\<Down>")
197 call WaitForAssert({-> assert_equal(' 9 9', term_getline(buf, 5))})
198
199 " Down a screen with <Space>, f, or <PageDown>.
200 call term_sendkeys(buf, 'f')
201 call WaitForAssert({-> assert_equal(' 14 14', term_getline(buf, 5))})
202 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
203 call term_sendkeys(buf, ' ')
204 call WaitForAssert({-> assert_equal(' 19 19', term_getline(buf, 5))})
205 call term_sendkeys(buf, "\<PageDown>")
206 call WaitForAssert({-> assert_equal(' 24 24', term_getline(buf, 5))})
207
208 " Down a page (half a screen) with d.
209 call term_sendkeys(buf, 'd')
210 call WaitForAssert({-> assert_equal(' 27 27', term_getline(buf, 5))})
211
212 " Down all the way with 'G'.
213 call term_sendkeys(buf, 'G')
214 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
215 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
216
217 " Up a line k, <BS> or <Up>.
218 call term_sendkeys(buf, 'k')
219 call WaitForAssert({-> assert_equal(' 99 99', term_getline(buf, 5))})
220 call term_sendkeys(buf, "\<BS>")
221 call WaitForAssert({-> assert_equal(' 98 98', term_getline(buf, 5))})
222 call term_sendkeys(buf, "\<Up>")
223 call WaitForAssert({-> assert_equal(' 97 97', term_getline(buf, 5))})
224
225 " Up a screen with b or <PageUp>.
226 call term_sendkeys(buf, 'b')
227 call WaitForAssert({-> assert_equal(' 92 92', term_getline(buf, 5))})
228 call term_sendkeys(buf, "\<PageUp>")
229 call WaitForAssert({-> assert_equal(' 87 87', term_getline(buf, 5))})
230
231 " Up a page (half a screen) with u.
232 call term_sendkeys(buf, 'u')
233 call WaitForAssert({-> assert_equal(' 84 84', term_getline(buf, 5))})
234
235 " Up all the way with 'g'.
236 call term_sendkeys(buf, 'g')
237 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
238 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
239
240 " All the way down. Pressing f should do nothing but pressing
241 " space should end the more prompt.
242 call term_sendkeys(buf, 'G')
243 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
244 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
245 call term_sendkeys(buf, 'f')
246 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
247 call term_sendkeys(buf, ' ')
248 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
249
250 " Pressing g< shows the previous command output.
251 call term_sendkeys(buf, 'g<')
252 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
253 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
254
255 call term_sendkeys(buf, ":%p#\n")
256 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
257 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
258
259 " Stop command output with q, <Esc> or CTRL-C.
260 call term_sendkeys(buf, 'q')
261 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
262
Bram Moolenaar43c60ed2020-02-02 15:55:19 +0100263 call StopVimInTerminal(buf)
264endfunc
265
266func Test_ask_yesno()
267 if !CanRunVimInTerminal()
268 throw 'Skipped: cannot run vim in terminal'
269 endif
270 let buf = RunVimInTerminal('', {'rows': 6})
271 call term_sendkeys(buf, ":call setline(1, range(1, 2))\n")
272
273 call term_sendkeys(buf, ":2,1s/^/n/\n")
274 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
275 call term_sendkeys(buf, "n")
276 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
277 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
278
279 call term_sendkeys(buf, ":2,1s/^/Esc/\n")
280 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
281 call term_sendkeys(buf, "\<Esc>")
282 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
283 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
284
285 call term_sendkeys(buf, ":2,1s/^/y/\n")
286 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
287 call term_sendkeys(buf, "y")
288 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?y *2,1 *All$', term_getline(buf, 6))})
289 call WaitForAssert({-> assert_equal('y1', term_getline(buf, 1))})
290 call WaitForAssert({-> assert_equal('y2', term_getline(buf, 2))})
291
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100292 call StopVimInTerminal(buf)
293endfunc
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100294
295func Test_null()
296 echom test_null_list()
297 echom test_null_dict()
298 echom test_null_blob()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100299 echom test_null_string()
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200300 echom test_null_function()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100301 echom test_null_partial()
Bram Moolenaarda292b02020-01-08 19:27:40 +0100302 if has('job')
303 echom test_null_job()
304 echom test_null_channel()
305 endif
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100306endfunc
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200307
308" vim: shiftwidth=2 sts=2 expandtab