blob: 832762a5c8f05a64c6bcfa95e02ea3bb24474d73 [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 Moolenaarb42c0d52020-05-29 22:41:41 +02005source view_util.vim
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02006
Bram Moolenaar49b2fb32020-04-25 17:13:56 +02007func Test_messages()
Bram Moolenaar451f8492016-04-14 17:16:22 +02008 let oldmore = &more
9 try
10 set nomore
11
12 let arr = map(range(10), '"hello" . v:val')
13 for s in arr
14 echomsg s | redraw
15 endfor
Bram Moolenaar451f8492016-04-14 17:16:22 +020016
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020017 " get last two messages
Bram Moolenaar451f8492016-04-14 17:16:22 +020018 redir => result
19 2messages | redraw
20 redir END
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020021 let msg_list = split(result, "\n")
22 call assert_equal(["hello8", "hello9"], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020023
24 " clear messages without last one
25 1messages clear
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020026 let msg_list = GetMessages()
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020027 call assert_equal(['hello9'], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020028
29 " clear all messages
30 messages clear
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020031 let msg_list = GetMessages()
32 call assert_equal([], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020033 finally
34 let &more = oldmore
35 endtry
Bram Moolenaar067297e2020-04-13 19:55:50 +020036
37 call assert_fails('message 1', 'E474:')
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020038endfunc
Bram Moolenaar2abad542018-05-19 14:43:45 +020039
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020040" Patch 7.4.1696 defined the "clearmode()" function for clearing the mode
Bram Moolenaar2abad542018-05-19 14:43:45 +020041" indicator (e.g., "-- INSERT --") when ":stopinsert" is invoked. Message
42" output could then be disturbed when 'cmdheight' was greater than one.
43" This test ensures that the bugfix for this issue remains in place.
Bram Moolenaar1e115362019-01-09 23:01:02 +010044func Test_stopinsert_does_not_break_message_output()
Bram Moolenaar2abad542018-05-19 14:43:45 +020045 set cmdheight=2
46 redraw!
47
48 stopinsert | echo 'test echo'
49 call assert_equal(116, screenchar(&lines - 1, 1))
50 call assert_equal(32, screenchar(&lines, 1))
51 redraw!
52
53 stopinsert | echomsg 'test echomsg'
54 call assert_equal(116, screenchar(&lines - 1, 1))
55 call assert_equal(32, screenchar(&lines, 1))
56 redraw!
57
58 set cmdheight&
Bram Moolenaar1e115362019-01-09 23:01:02 +010059endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +010060
61func Test_message_completion()
62 call feedkeys(":message \<C-A>\<C-B>\"\<CR>", 'tx')
63 call assert_equal('"message clear', @:)
64endfunc
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010065
66func Test_echomsg()
67 call assert_equal("\nhello", execute(':echomsg "hello"'))
68 call assert_equal("\n", execute(':echomsg ""'))
69 call assert_equal("\n12345", execute(':echomsg 12345'))
70 call assert_equal("\n[]", execute(':echomsg []'))
71 call assert_equal("\n[1, 2, 3]", execute(':echomsg [1, 2, 3]'))
Bram Moolenaardb950e42020-04-22 19:13:19 +020072 call assert_equal("\n[1, 2, []]", execute(':echomsg [1, 2, test_null_list()]'))
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010073 call assert_equal("\n{}", execute(':echomsg {}'))
74 call assert_equal("\n{'a': 1, 'b': 2}", execute(':echomsg {"a": 1, "b": 2}'))
75 if has('float')
76 call assert_equal("\n1.23", execute(':echomsg 1.23'))
77 endif
78 call assert_match("function('<lambda>\\d*')", execute(':echomsg {-> 1234}'))
79endfunc
80
81func Test_echoerr()
82 call test_ignore_error('IgNoRe')
83 call assert_equal("\nIgNoRe hello", execute(':echoerr "IgNoRe hello"'))
84 call assert_equal("\n12345 IgNoRe", execute(':echoerr 12345 "IgNoRe"'))
85 call assert_equal("\n[1, 2, 'IgNoRe']", execute(':echoerr [1, 2, "IgNoRe"]'))
86 call assert_equal("\n{'IgNoRe': 2, 'a': 1}", execute(':echoerr {"a": 1, "IgNoRe": 2}'))
87 if has('float')
88 call assert_equal("\n1.23 IgNoRe", execute(':echoerr 1.23 "IgNoRe"'))
89 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +020090 eval '<lambda>'->test_ignore_error()
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010091 call assert_match("function('<lambda>\\d*')", execute(':echoerr {-> 1234}'))
92 call test_ignore_error('RESET')
93endfunc
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +020094
95func Test_mode_message_at_leaving_insert_by_ctrl_c()
96 if !has('terminal') || has('gui_running')
97 return
98 endif
99
100 " Set custom statusline built by user-defined function.
101 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200102 let lines =<< trim END
103 func StatusLine() abort
104 return ""
105 endfunc
106 set statusline=%!StatusLine()
107 set laststatus=2
108 END
109 call writefile(lines, testfile)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200110
111 let rows = 10
112 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200113 call TermWait(buf, 100)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200114 call assert_equal('run', job_status(term_getjob(buf)))
115
116 call term_sendkeys(buf, "i")
117 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
118 call term_sendkeys(buf, "\<C-C>")
119 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
120
121 call term_sendkeys(buf, ":qall!\<CR>")
122 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
123 exe buf . 'bwipe!'
124 call delete(testfile)
125endfunc
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200126
127func Test_mode_message_at_leaving_insert_with_esc_mapped()
128 if !has('terminal') || has('gui_running')
129 return
130 endif
131
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()
175 if !CanRunVimInTerminal()
176 throw 'Skipped: cannot run vim in terminal'
177 endif
178 let buf = RunVimInTerminal('', {'rows': 6})
179 call term_sendkeys(buf, ":call setline(1, range(1, 100))\n")
180
181 call term_sendkeys(buf, ":%p#\n")
182 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
183 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
184
185 call term_sendkeys(buf, '?')
186 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
187 call WaitForAssert({-> assert_equal('-- More -- SPACE/d/j: screen/page/line down, b/u/k: up, q: quit ', term_getline(buf, 6))})
188
189 " Down a line with j, <CR>, <NL> or <Down>.
190 call term_sendkeys(buf, "j")
191 call WaitForAssert({-> assert_equal(' 6 6', term_getline(buf, 5))})
192 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
193 call term_sendkeys(buf, "\<NL>")
194 call WaitForAssert({-> assert_equal(' 7 7', term_getline(buf, 5))})
195 call term_sendkeys(buf, "\<CR>")
196 call WaitForAssert({-> assert_equal(' 8 8', term_getline(buf, 5))})
197 call term_sendkeys(buf, "\<Down>")
198 call WaitForAssert({-> assert_equal(' 9 9', term_getline(buf, 5))})
199
200 " Down a screen with <Space>, f, or <PageDown>.
201 call term_sendkeys(buf, 'f')
202 call WaitForAssert({-> assert_equal(' 14 14', term_getline(buf, 5))})
203 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
204 call term_sendkeys(buf, ' ')
205 call WaitForAssert({-> assert_equal(' 19 19', term_getline(buf, 5))})
206 call term_sendkeys(buf, "\<PageDown>")
207 call WaitForAssert({-> assert_equal(' 24 24', term_getline(buf, 5))})
208
209 " Down a page (half a screen) with d.
210 call term_sendkeys(buf, 'd')
211 call WaitForAssert({-> assert_equal(' 27 27', term_getline(buf, 5))})
212
213 " Down all the way with 'G'.
214 call term_sendkeys(buf, 'G')
215 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
216 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
217
218 " Up a line k, <BS> or <Up>.
219 call term_sendkeys(buf, 'k')
220 call WaitForAssert({-> assert_equal(' 99 99', term_getline(buf, 5))})
221 call term_sendkeys(buf, "\<BS>")
222 call WaitForAssert({-> assert_equal(' 98 98', term_getline(buf, 5))})
223 call term_sendkeys(buf, "\<Up>")
224 call WaitForAssert({-> assert_equal(' 97 97', term_getline(buf, 5))})
225
226 " Up a screen with b or <PageUp>.
227 call term_sendkeys(buf, 'b')
228 call WaitForAssert({-> assert_equal(' 92 92', term_getline(buf, 5))})
229 call term_sendkeys(buf, "\<PageUp>")
230 call WaitForAssert({-> assert_equal(' 87 87', term_getline(buf, 5))})
231
232 " Up a page (half a screen) with u.
233 call term_sendkeys(buf, 'u')
234 call WaitForAssert({-> assert_equal(' 84 84', term_getline(buf, 5))})
235
236 " Up all the way with 'g'.
237 call term_sendkeys(buf, 'g')
238 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
239 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
240
241 " All the way down. Pressing f should do nothing but pressing
242 " space should end the more prompt.
243 call term_sendkeys(buf, 'G')
244 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
245 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
246 call term_sendkeys(buf, 'f')
247 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
248 call term_sendkeys(buf, ' ')
249 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
250
251 " Pressing g< shows the previous command output.
252 call term_sendkeys(buf, 'g<')
253 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
254 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
255
256 call term_sendkeys(buf, ":%p#\n")
257 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
258 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
259
260 " Stop command output with q, <Esc> or CTRL-C.
261 call term_sendkeys(buf, 'q')
262 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
263
Bram Moolenaar43c60ed2020-02-02 15:55:19 +0100264 call StopVimInTerminal(buf)
265endfunc
266
267func Test_ask_yesno()
268 if !CanRunVimInTerminal()
269 throw 'Skipped: cannot run vim in terminal'
270 endif
271 let buf = RunVimInTerminal('', {'rows': 6})
272 call term_sendkeys(buf, ":call setline(1, range(1, 2))\n")
273
274 call term_sendkeys(buf, ":2,1s/^/n/\n")
275 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
276 call term_sendkeys(buf, "n")
277 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
278 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
279
280 call term_sendkeys(buf, ":2,1s/^/Esc/\n")
281 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
282 call term_sendkeys(buf, "\<Esc>")
283 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
284 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
285
286 call term_sendkeys(buf, ":2,1s/^/y/\n")
287 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
288 call term_sendkeys(buf, "y")
289 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?y *2,1 *All$', term_getline(buf, 6))})
290 call WaitForAssert({-> assert_equal('y1', term_getline(buf, 1))})
291 call WaitForAssert({-> assert_equal('y2', term_getline(buf, 2))})
292
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100293 call StopVimInTerminal(buf)
294endfunc
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100295
296func Test_null()
297 echom test_null_list()
298 echom test_null_dict()
299 echom test_null_blob()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100300 echom test_null_string()
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200301 echom test_null_function()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100302 echom test_null_partial()
Bram Moolenaarda292b02020-01-08 19:27:40 +0100303 if has('job')
304 echom test_null_job()
305 echom test_null_channel()
306 endif
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100307endfunc
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200308
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200309func Test_mapping_at_hit_return_prompt()
310 nnoremap <C-B> :echo "hit ctrl-b"<CR>
311 call feedkeys(":ls\<CR>", "xt")
Bram Moolenaarebe9d342020-05-30 21:52:54 +0200312 call feedkeys("\{C-B}", "xt")
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200313 call assert_match('hit ctrl-b', Screenline(&lines - 1))
314 nunmap <C-B>
315endfunc
316
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200317" vim: shiftwidth=2 sts=2 expandtab