blob: 32689b09eaf2d09aba8590e31d587450cf9602a8 [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 Moolenaar494e9062020-05-31 21:28:02 +02003source check.vim
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004source shared.vim
Bram Moolenaarc6d539b2019-12-28 17:10:46 +01005source term_util.vim
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02006source view_util.vim
Bram Moolenaar3d30af82020-10-13 22:15:56 +02007source screendump.vim
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02008
Bram Moolenaar49b2fb32020-04-25 17:13:56 +02009func Test_messages()
Bram Moolenaar451f8492016-04-14 17:16:22 +020010 let oldmore = &more
11 try
12 set nomore
13
14 let arr = map(range(10), '"hello" . v:val')
15 for s in arr
16 echomsg s | redraw
17 endfor
Bram Moolenaar451f8492016-04-14 17:16:22 +020018
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020019 " get last two messages
Bram Moolenaar451f8492016-04-14 17:16:22 +020020 redir => result
21 2messages | redraw
22 redir END
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020023 let msg_list = split(result, "\n")
24 call assert_equal(["hello8", "hello9"], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020025
26 " clear messages without last one
27 1messages clear
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020028 let msg_list = GetMessages()
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020029 call assert_equal(['hello9'], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020030
31 " clear all messages
32 messages clear
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020033 let msg_list = GetMessages()
34 call assert_equal([], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020035 finally
36 let &more = oldmore
37 endtry
Bram Moolenaar067297e2020-04-13 19:55:50 +020038
39 call assert_fails('message 1', 'E474:')
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020040endfunc
Bram Moolenaar2abad542018-05-19 14:43:45 +020041
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020042" Patch 7.4.1696 defined the "clearmode()" function for clearing the mode
Bram Moolenaar2abad542018-05-19 14:43:45 +020043" indicator (e.g., "-- INSERT --") when ":stopinsert" is invoked. Message
44" output could then be disturbed when 'cmdheight' was greater than one.
45" This test ensures that the bugfix for this issue remains in place.
Bram Moolenaar1e115362019-01-09 23:01:02 +010046func Test_stopinsert_does_not_break_message_output()
Bram Moolenaar2abad542018-05-19 14:43:45 +020047 set cmdheight=2
48 redraw!
49
50 stopinsert | echo 'test echo'
51 call assert_equal(116, screenchar(&lines - 1, 1))
52 call assert_equal(32, screenchar(&lines, 1))
53 redraw!
54
55 stopinsert | echomsg 'test echomsg'
56 call assert_equal(116, screenchar(&lines - 1, 1))
57 call assert_equal(32, screenchar(&lines, 1))
58 redraw!
59
60 set cmdheight&
Bram Moolenaar1e115362019-01-09 23:01:02 +010061endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +010062
63func Test_message_completion()
64 call feedkeys(":message \<C-A>\<C-B>\"\<CR>", 'tx')
65 call assert_equal('"message clear', @:)
66endfunc
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010067
68func Test_echomsg()
69 call assert_equal("\nhello", execute(':echomsg "hello"'))
70 call assert_equal("\n", execute(':echomsg ""'))
71 call assert_equal("\n12345", execute(':echomsg 12345'))
72 call assert_equal("\n[]", execute(':echomsg []'))
73 call assert_equal("\n[1, 2, 3]", execute(':echomsg [1, 2, 3]'))
Bram Moolenaardb950e42020-04-22 19:13:19 +020074 call assert_equal("\n[1, 2, []]", execute(':echomsg [1, 2, test_null_list()]'))
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010075 call assert_equal("\n{}", execute(':echomsg {}'))
76 call assert_equal("\n{'a': 1, 'b': 2}", execute(':echomsg {"a": 1, "b": 2}'))
77 if has('float')
78 call assert_equal("\n1.23", execute(':echomsg 1.23'))
79 endif
80 call assert_match("function('<lambda>\\d*')", execute(':echomsg {-> 1234}'))
81endfunc
82
83func Test_echoerr()
84 call test_ignore_error('IgNoRe')
85 call assert_equal("\nIgNoRe hello", execute(':echoerr "IgNoRe hello"'))
86 call assert_equal("\n12345 IgNoRe", execute(':echoerr 12345 "IgNoRe"'))
87 call assert_equal("\n[1, 2, 'IgNoRe']", execute(':echoerr [1, 2, "IgNoRe"]'))
88 call assert_equal("\n{'IgNoRe': 2, 'a': 1}", execute(':echoerr {"a": 1, "IgNoRe": 2}'))
89 if has('float')
90 call assert_equal("\n1.23 IgNoRe", execute(':echoerr 1.23 "IgNoRe"'))
91 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +020092 eval '<lambda>'->test_ignore_error()
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010093 call assert_match("function('<lambda>\\d*')", execute(':echoerr {-> 1234}'))
94 call test_ignore_error('RESET')
95endfunc
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +020096
97func Test_mode_message_at_leaving_insert_by_ctrl_c()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020098 CheckFeature terminal
99 CheckNotGui
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200100
101 " Set custom statusline built by user-defined function.
102 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200103 let lines =<< trim END
104 func StatusLine() abort
105 return ""
106 endfunc
107 set statusline=%!StatusLine()
108 set laststatus=2
109 END
110 call writefile(lines, testfile)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200111
112 let rows = 10
113 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200114 call TermWait(buf, 100)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200115 call assert_equal('run', job_status(term_getjob(buf)))
116
117 call term_sendkeys(buf, "i")
118 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
119 call term_sendkeys(buf, "\<C-C>")
120 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
121
122 call term_sendkeys(buf, ":qall!\<CR>")
123 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
124 exe buf . 'bwipe!'
125 call delete(testfile)
126endfunc
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200127
128func Test_mode_message_at_leaving_insert_with_esc_mapped()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200129 CheckFeature terminal
130 CheckNotGui
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200131
132 " Set custom statusline built by user-defined function.
133 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200134 let lines =<< trim END
135 set laststatus=2
136 inoremap <Esc> <Esc>00
137 END
138 call writefile(lines, testfile)
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200139
140 let rows = 10
141 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200142 call WaitForAssert({-> assert_match('0,0-1\s*All$', term_getline(buf, rows - 1))})
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200143 call assert_equal('run', job_status(term_getjob(buf)))
144
145 call term_sendkeys(buf, "i")
146 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
147 call term_sendkeys(buf, "\<Esc>")
148 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
149
150 call term_sendkeys(buf, ":qall!\<CR>")
151 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
152 exe buf . 'bwipe!'
153 call delete(testfile)
154endfunc
Bram Moolenaar37f4cbd2019-08-23 20:58:45 +0200155
156func Test_echospace()
157 set noruler noshowcmd laststatus=1
158 call assert_equal(&columns - 1, v:echospace)
159 split
160 call assert_equal(&columns - 1, v:echospace)
161 set ruler
162 call assert_equal(&columns - 1, v:echospace)
163 close
164 call assert_equal(&columns - 19, v:echospace)
165 set showcmd noruler
166 call assert_equal(&columns - 12, v:echospace)
167 set showcmd ruler
168 call assert_equal(&columns - 29, v:echospace)
169
170 set ruler& showcmd&
171endfunc
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100172
173" Test more-prompt (see :help more-prompt).
174func Test_message_more()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200175 CheckRunVimInTerminal
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100176 let buf = RunVimInTerminal('', {'rows': 6})
177 call term_sendkeys(buf, ":call setline(1, range(1, 100))\n")
178
179 call term_sendkeys(buf, ":%p#\n")
180 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
181 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
182
183 call term_sendkeys(buf, '?')
184 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
185 call WaitForAssert({-> assert_equal('-- More -- SPACE/d/j: screen/page/line down, b/u/k: up, q: quit ', term_getline(buf, 6))})
186
187 " Down a line with j, <CR>, <NL> or <Down>.
188 call term_sendkeys(buf, "j")
189 call WaitForAssert({-> assert_equal(' 6 6', term_getline(buf, 5))})
190 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
191 call term_sendkeys(buf, "\<NL>")
192 call WaitForAssert({-> assert_equal(' 7 7', term_getline(buf, 5))})
193 call term_sendkeys(buf, "\<CR>")
194 call WaitForAssert({-> assert_equal(' 8 8', term_getline(buf, 5))})
195 call term_sendkeys(buf, "\<Down>")
196 call WaitForAssert({-> assert_equal(' 9 9', term_getline(buf, 5))})
197
198 " Down a screen with <Space>, f, or <PageDown>.
199 call term_sendkeys(buf, 'f')
200 call WaitForAssert({-> assert_equal(' 14 14', term_getline(buf, 5))})
201 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
202 call term_sendkeys(buf, ' ')
203 call WaitForAssert({-> assert_equal(' 19 19', term_getline(buf, 5))})
204 call term_sendkeys(buf, "\<PageDown>")
205 call WaitForAssert({-> assert_equal(' 24 24', term_getline(buf, 5))})
206
207 " Down a page (half a screen) with d.
208 call term_sendkeys(buf, 'd')
209 call WaitForAssert({-> assert_equal(' 27 27', term_getline(buf, 5))})
210
211 " Down all the way with 'G'.
212 call term_sendkeys(buf, 'G')
213 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
214 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
215
216 " Up a line k, <BS> or <Up>.
217 call term_sendkeys(buf, 'k')
218 call WaitForAssert({-> assert_equal(' 99 99', term_getline(buf, 5))})
219 call term_sendkeys(buf, "\<BS>")
220 call WaitForAssert({-> assert_equal(' 98 98', term_getline(buf, 5))})
221 call term_sendkeys(buf, "\<Up>")
222 call WaitForAssert({-> assert_equal(' 97 97', term_getline(buf, 5))})
223
224 " Up a screen with b or <PageUp>.
225 call term_sendkeys(buf, 'b')
226 call WaitForAssert({-> assert_equal(' 92 92', term_getline(buf, 5))})
227 call term_sendkeys(buf, "\<PageUp>")
228 call WaitForAssert({-> assert_equal(' 87 87', term_getline(buf, 5))})
229
230 " Up a page (half a screen) with u.
231 call term_sendkeys(buf, 'u')
232 call WaitForAssert({-> assert_equal(' 84 84', term_getline(buf, 5))})
233
234 " Up all the way with 'g'.
235 call term_sendkeys(buf, 'g')
236 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
237 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
238
239 " All the way down. Pressing f should do nothing but pressing
240 " space should end the more prompt.
241 call term_sendkeys(buf, 'G')
242 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
243 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
244 call term_sendkeys(buf, 'f')
245 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
246 call term_sendkeys(buf, ' ')
247 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
248
249 " Pressing g< shows the previous command output.
250 call term_sendkeys(buf, 'g<')
251 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
252 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
253
254 call term_sendkeys(buf, ":%p#\n")
255 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
256 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
257
258 " Stop command output with q, <Esc> or CTRL-C.
259 call term_sendkeys(buf, 'q')
260 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
261
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +0100262 " Execute a : command from the more prompt
263 call term_sendkeys(buf, ":%p#\n")
264 call term_wait(buf)
265 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
266 call term_sendkeys(buf, ":")
267 call term_wait(buf)
268 call WaitForAssert({-> assert_equal(':', term_getline(buf, 6))})
269 call term_sendkeys(buf, "echo 'Hello'\n")
270 call term_wait(buf)
271 call WaitForAssert({-> assert_equal('Hello ', term_getline(buf, 5))})
272
Bram Moolenaar43c60ed2020-02-02 15:55:19 +0100273 call StopVimInTerminal(buf)
274endfunc
275
276func Test_ask_yesno()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200277 CheckRunVimInTerminal
Bram Moolenaar43c60ed2020-02-02 15:55:19 +0100278 let buf = RunVimInTerminal('', {'rows': 6})
279 call term_sendkeys(buf, ":call setline(1, range(1, 2))\n")
280
281 call term_sendkeys(buf, ":2,1s/^/n/\n")
282 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
283 call term_sendkeys(buf, "n")
284 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
285 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
286
287 call term_sendkeys(buf, ":2,1s/^/Esc/\n")
288 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
289 call term_sendkeys(buf, "\<Esc>")
290 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
291 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
292
293 call term_sendkeys(buf, ":2,1s/^/y/\n")
294 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
295 call term_sendkeys(buf, "y")
296 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?y *2,1 *All$', term_getline(buf, 6))})
297 call WaitForAssert({-> assert_equal('y1', term_getline(buf, 1))})
298 call WaitForAssert({-> assert_equal('y2', term_getline(buf, 2))})
299
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100300 call StopVimInTerminal(buf)
301endfunc
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100302
303func Test_null()
304 echom test_null_list()
305 echom test_null_dict()
306 echom test_null_blob()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100307 echom test_null_string()
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200308 echom test_null_function()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100309 echom test_null_partial()
Bram Moolenaarda292b02020-01-08 19:27:40 +0100310 if has('job')
311 echom test_null_job()
312 echom test_null_channel()
313 endif
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100314endfunc
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200315
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200316func Test_mapping_at_hit_return_prompt()
317 nnoremap <C-B> :echo "hit ctrl-b"<CR>
318 call feedkeys(":ls\<CR>", "xt")
Bram Moolenaarfccd93f2020-05-31 22:06:51 +0200319 call feedkeys("\<*C-B>", "xt")
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200320 call assert_match('hit ctrl-b', Screenline(&lines - 1))
321 nunmap <C-B>
322endfunc
323
Bram Moolenaar3d30af82020-10-13 22:15:56 +0200324func Test_quit_long_message()
325 CheckScreendump
326
327 let content =<< trim END
328 echom range(9999)->join("\x01")
329 END
330 call writefile(content, 'Xtest_quit_message')
331 let buf = RunVimInTerminal('-S Xtest_quit_message', #{rows: 6})
332 call term_sendkeys(buf, "q")
333 call VerifyScreenDump(buf, 'Test_quit_long_message', {})
334
335 " clean up
336 call StopVimInTerminal(buf)
Bram Moolenaarac665c22020-12-08 20:39:15 +0100337 call delete('Xtest_quit_message')
Bram Moolenaar3d30af82020-10-13 22:15:56 +0200338endfunc
339
Bram Moolenaar2de53712021-12-19 11:06:35 +0000340" this was missing a terminating NUL
341func Test_echo_string_partial()
342 function CountSpaces()
343 endfunction
Yegappan Lakshmananbc404bf2021-12-19 19:19:31 +0000344 call assert_equal("function('CountSpaces', [{'ccccccccccc': ['ab', 'cd'], 'aaaaaaaaaaa': v:false, 'bbbbbbbbbbbb': ''}])", string(function('CountSpaces', [#{aaaaaaaaaaa: v:false, bbbbbbbbbbbb: '', ccccccccccc: ['ab', 'cd']}])))
Bram Moolenaar2de53712021-12-19 11:06:35 +0000345endfunc
346
Rob Pilling726f7f92022-01-20 14:44:38 +0000347" Message output was previously overwritten by the fileinfo display, shown
348" when switching buffers. If a buffer is switched to, then a message if
349" echoed, we should show the message, rather than overwriting it with
350" fileinfo.
351func Test_fileinfo_after_echo()
352 CheckScreendump
353
354 let content =<< trim END
355 file a.txt
356
357 hide edit b.txt
358 call setline(1, "hi")
359 setlocal modified
360
361 hide buffer a.txt
362
Bram Moolenaar9323ca52022-03-16 11:14:57 +0000363 autocmd CursorHold * buf b.txt | w | echo "'b' written"
Rob Pilling726f7f92022-01-20 14:44:38 +0000364 END
365
366 call writefile(content, 'Xtest_fileinfo_after_echo')
367 let buf = RunVimInTerminal('-S Xtest_fileinfo_after_echo', #{rows: 6})
Bram Moolenaar9323ca52022-03-16 11:14:57 +0000368 call term_sendkeys(buf, ":set updatetime=50\<CR>")
369 call term_sendkeys(buf, "0$")
Rob Pilling726f7f92022-01-20 14:44:38 +0000370 call VerifyScreenDump(buf, 'Test_fileinfo_after_echo', {})
371
372 call term_sendkeys(buf, ":q\<CR>")
373
374 " clean up
375 call StopVimInTerminal(buf)
376 call delete('Xtest_fileinfo_after_echo')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +0000377 call delete('b.txt')
Rob Pilling726f7f92022-01-20 14:44:38 +0000378endfunc
379
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200380" vim: shiftwidth=2 sts=2 expandtab