blob: 3d7245dc6347cb953a57f91e7cf6a201d74591c0 [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 Moolenaar451f8492016-04-14 17:16:22 +02006function Test_messages()
7 let oldmore = &more
8 try
9 set nomore
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020010 " Avoid the "message maintainer" line.
11 let $LANG = ''
Bram Moolenaar47a1a8b2020-04-25 16:41:58 +020012 let $LC_ALL = ''
13 let $LC_MESSAGES = ''
14 let $LC_COLLATE = ''
Bram Moolenaar451f8492016-04-14 17:16:22 +020015
16 let arr = map(range(10), '"hello" . v:val')
17 for s in arr
18 echomsg s | redraw
19 endfor
20 let result = ''
21
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020022 " get last two messages
Bram Moolenaar451f8492016-04-14 17:16:22 +020023 redir => result
24 2messages | redraw
25 redir END
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020026 let msg_list = split(result, "\n")
27 call assert_equal(["hello8", "hello9"], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020028
29 " clear messages without last one
30 1messages clear
31 redir => result
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020032 redraw | messages
Bram Moolenaar451f8492016-04-14 17:16:22 +020033 redir END
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020034 let msg_list = split(result, "\n")
35 call assert_equal(['hello9'], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020036
37 " clear all messages
38 messages clear
39 redir => result
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020040 redraw | messages
Bram Moolenaar451f8492016-04-14 17:16:22 +020041 redir END
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020042 call assert_equal('', result)
Bram Moolenaar451f8492016-04-14 17:16:22 +020043 finally
44 let &more = oldmore
45 endtry
Bram Moolenaar067297e2020-04-13 19:55:50 +020046
47 call assert_fails('message 1', 'E474:')
Bram Moolenaar451f8492016-04-14 17:16:22 +020048endfunction
Bram Moolenaar2abad542018-05-19 14:43:45 +020049
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020050" Patch 7.4.1696 defined the "clearmode()" function for clearing the mode
Bram Moolenaar2abad542018-05-19 14:43:45 +020051" indicator (e.g., "-- INSERT --") when ":stopinsert" is invoked. Message
52" output could then be disturbed when 'cmdheight' was greater than one.
53" This test ensures that the bugfix for this issue remains in place.
Bram Moolenaar1e115362019-01-09 23:01:02 +010054func Test_stopinsert_does_not_break_message_output()
Bram Moolenaar2abad542018-05-19 14:43:45 +020055 set cmdheight=2
56 redraw!
57
58 stopinsert | echo 'test echo'
59 call assert_equal(116, screenchar(&lines - 1, 1))
60 call assert_equal(32, screenchar(&lines, 1))
61 redraw!
62
63 stopinsert | echomsg 'test echomsg'
64 call assert_equal(116, screenchar(&lines - 1, 1))
65 call assert_equal(32, screenchar(&lines, 1))
66 redraw!
67
68 set cmdheight&
Bram Moolenaar1e115362019-01-09 23:01:02 +010069endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +010070
71func Test_message_completion()
72 call feedkeys(":message \<C-A>\<C-B>\"\<CR>", 'tx')
73 call assert_equal('"message clear', @:)
74endfunc
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010075
76func Test_echomsg()
77 call assert_equal("\nhello", execute(':echomsg "hello"'))
78 call assert_equal("\n", execute(':echomsg ""'))
79 call assert_equal("\n12345", execute(':echomsg 12345'))
80 call assert_equal("\n[]", execute(':echomsg []'))
81 call assert_equal("\n[1, 2, 3]", execute(':echomsg [1, 2, 3]'))
Bram Moolenaardb950e42020-04-22 19:13:19 +020082 call assert_equal("\n[1, 2, []]", execute(':echomsg [1, 2, test_null_list()]'))
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010083 call assert_equal("\n{}", execute(':echomsg {}'))
84 call assert_equal("\n{'a': 1, 'b': 2}", execute(':echomsg {"a": 1, "b": 2}'))
85 if has('float')
86 call assert_equal("\n1.23", execute(':echomsg 1.23'))
87 endif
88 call assert_match("function('<lambda>\\d*')", execute(':echomsg {-> 1234}'))
89endfunc
90
91func Test_echoerr()
92 call test_ignore_error('IgNoRe')
93 call assert_equal("\nIgNoRe hello", execute(':echoerr "IgNoRe hello"'))
94 call assert_equal("\n12345 IgNoRe", execute(':echoerr 12345 "IgNoRe"'))
95 call assert_equal("\n[1, 2, 'IgNoRe']", execute(':echoerr [1, 2, "IgNoRe"]'))
96 call assert_equal("\n{'IgNoRe': 2, 'a': 1}", execute(':echoerr {"a": 1, "IgNoRe": 2}'))
97 if has('float')
98 call assert_equal("\n1.23 IgNoRe", execute(':echoerr 1.23 "IgNoRe"'))
99 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +0200100 eval '<lambda>'->test_ignore_error()
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100101 call assert_match("function('<lambda>\\d*')", execute(':echoerr {-> 1234}'))
102 call test_ignore_error('RESET')
103endfunc
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200104
105func Test_mode_message_at_leaving_insert_by_ctrl_c()
106 if !has('terminal') || has('gui_running')
107 return
108 endif
109
110 " Set custom statusline built by user-defined function.
111 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200112 let lines =<< trim END
113 func StatusLine() abort
114 return ""
115 endfunc
116 set statusline=%!StatusLine()
117 set laststatus=2
118 END
119 call writefile(lines, testfile)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200120
121 let rows = 10
122 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200123 call TermWait(buf, 100)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200124 call assert_equal('run', job_status(term_getjob(buf)))
125
126 call term_sendkeys(buf, "i")
127 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
128 call term_sendkeys(buf, "\<C-C>")
129 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
130
131 call term_sendkeys(buf, ":qall!\<CR>")
132 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
133 exe buf . 'bwipe!'
134 call delete(testfile)
135endfunc
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200136
137func Test_mode_message_at_leaving_insert_with_esc_mapped()
138 if !has('terminal') || has('gui_running')
139 return
140 endif
141
142 " Set custom statusline built by user-defined function.
143 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200144 let lines =<< trim END
145 set laststatus=2
146 inoremap <Esc> <Esc>00
147 END
148 call writefile(lines, testfile)
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200149
150 let rows = 10
151 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200152 call WaitForAssert({-> assert_match('0,0-1\s*All$', term_getline(buf, rows - 1))})
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200153 call assert_equal('run', job_status(term_getjob(buf)))
154
155 call term_sendkeys(buf, "i")
156 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
157 call term_sendkeys(buf, "\<Esc>")
158 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
159
160 call term_sendkeys(buf, ":qall!\<CR>")
161 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
162 exe buf . 'bwipe!'
163 call delete(testfile)
164endfunc
Bram Moolenaar37f4cbd2019-08-23 20:58:45 +0200165
166func Test_echospace()
167 set noruler noshowcmd laststatus=1
168 call assert_equal(&columns - 1, v:echospace)
169 split
170 call assert_equal(&columns - 1, v:echospace)
171 set ruler
172 call assert_equal(&columns - 1, v:echospace)
173 close
174 call assert_equal(&columns - 19, v:echospace)
175 set showcmd noruler
176 call assert_equal(&columns - 12, v:echospace)
177 set showcmd ruler
178 call assert_equal(&columns - 29, v:echospace)
179
180 set ruler& showcmd&
181endfunc
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100182
183" Test more-prompt (see :help more-prompt).
184func Test_message_more()
185 if !CanRunVimInTerminal()
186 throw 'Skipped: cannot run vim in terminal'
187 endif
188 let buf = RunVimInTerminal('', {'rows': 6})
189 call term_sendkeys(buf, ":call setline(1, range(1, 100))\n")
190
191 call term_sendkeys(buf, ":%p#\n")
192 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
193 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
194
195 call term_sendkeys(buf, '?')
196 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
197 call WaitForAssert({-> assert_equal('-- More -- SPACE/d/j: screen/page/line down, b/u/k: up, q: quit ', term_getline(buf, 6))})
198
199 " Down a line with j, <CR>, <NL> or <Down>.
200 call term_sendkeys(buf, "j")
201 call WaitForAssert({-> assert_equal(' 6 6', term_getline(buf, 5))})
202 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
203 call term_sendkeys(buf, "\<NL>")
204 call WaitForAssert({-> assert_equal(' 7 7', term_getline(buf, 5))})
205 call term_sendkeys(buf, "\<CR>")
206 call WaitForAssert({-> assert_equal(' 8 8', term_getline(buf, 5))})
207 call term_sendkeys(buf, "\<Down>")
208 call WaitForAssert({-> assert_equal(' 9 9', term_getline(buf, 5))})
209
210 " Down a screen with <Space>, f, or <PageDown>.
211 call term_sendkeys(buf, 'f')
212 call WaitForAssert({-> assert_equal(' 14 14', term_getline(buf, 5))})
213 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
214 call term_sendkeys(buf, ' ')
215 call WaitForAssert({-> assert_equal(' 19 19', term_getline(buf, 5))})
216 call term_sendkeys(buf, "\<PageDown>")
217 call WaitForAssert({-> assert_equal(' 24 24', term_getline(buf, 5))})
218
219 " Down a page (half a screen) with d.
220 call term_sendkeys(buf, 'd')
221 call WaitForAssert({-> assert_equal(' 27 27', term_getline(buf, 5))})
222
223 " Down all the way with 'G'.
224 call term_sendkeys(buf, 'G')
225 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
226 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
227
228 " Up a line k, <BS> or <Up>.
229 call term_sendkeys(buf, 'k')
230 call WaitForAssert({-> assert_equal(' 99 99', term_getline(buf, 5))})
231 call term_sendkeys(buf, "\<BS>")
232 call WaitForAssert({-> assert_equal(' 98 98', term_getline(buf, 5))})
233 call term_sendkeys(buf, "\<Up>")
234 call WaitForAssert({-> assert_equal(' 97 97', term_getline(buf, 5))})
235
236 " Up a screen with b or <PageUp>.
237 call term_sendkeys(buf, 'b')
238 call WaitForAssert({-> assert_equal(' 92 92', term_getline(buf, 5))})
239 call term_sendkeys(buf, "\<PageUp>")
240 call WaitForAssert({-> assert_equal(' 87 87', term_getline(buf, 5))})
241
242 " Up a page (half a screen) with u.
243 call term_sendkeys(buf, 'u')
244 call WaitForAssert({-> assert_equal(' 84 84', term_getline(buf, 5))})
245
246 " Up all the way with 'g'.
247 call term_sendkeys(buf, 'g')
248 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
249 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
250
251 " All the way down. Pressing f should do nothing but pressing
252 " space should end the more prompt.
253 call term_sendkeys(buf, 'G')
254 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
255 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
256 call term_sendkeys(buf, 'f')
257 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
258 call term_sendkeys(buf, ' ')
259 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
260
261 " Pressing g< shows the previous command output.
262 call term_sendkeys(buf, 'g<')
263 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
264 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
265
266 call term_sendkeys(buf, ":%p#\n")
267 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
268 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
269
270 " Stop command output with q, <Esc> or CTRL-C.
271 call term_sendkeys(buf, 'q')
272 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
273
Bram Moolenaar43c60ed2020-02-02 15:55:19 +0100274 call StopVimInTerminal(buf)
275endfunc
276
277func Test_ask_yesno()
278 if !CanRunVimInTerminal()
279 throw 'Skipped: cannot run vim in terminal'
280 endif
281 let buf = RunVimInTerminal('', {'rows': 6})
282 call term_sendkeys(buf, ":call setline(1, range(1, 2))\n")
283
284 call term_sendkeys(buf, ":2,1s/^/n/\n")
285 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
286 call term_sendkeys(buf, "n")
287 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
288 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
289
290 call term_sendkeys(buf, ":2,1s/^/Esc/\n")
291 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
292 call term_sendkeys(buf, "\<Esc>")
293 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
294 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
295
296 call term_sendkeys(buf, ":2,1s/^/y/\n")
297 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
298 call term_sendkeys(buf, "y")
299 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?y *2,1 *All$', term_getline(buf, 6))})
300 call WaitForAssert({-> assert_equal('y1', term_getline(buf, 1))})
301 call WaitForAssert({-> assert_equal('y2', term_getline(buf, 2))})
302
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100303 call StopVimInTerminal(buf)
304endfunc
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100305
306func Test_null()
307 echom test_null_list()
308 echom test_null_dict()
309 echom test_null_blob()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100310 echom test_null_string()
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200311 echom test_null_function()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100312 echom test_null_partial()
Bram Moolenaarda292b02020-01-08 19:27:40 +0100313 if has('job')
314 echom test_null_job()
315 echom test_null_channel()
316 endif
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100317endfunc
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200318
319" vim: shiftwidth=2 sts=2 expandtab