blob: 32ffb48a8d571a77f498bc0874900afcfc245cd4 [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 Moolenaar451f8492016-04-14 17:16:22 +020012
13 let arr = map(range(10), '"hello" . v:val')
14 for s in arr
15 echomsg s | redraw
16 endfor
17 let result = ''
18
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
28 redir => result
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020029 redraw | messages
Bram Moolenaar451f8492016-04-14 17:16:22 +020030 redir END
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020031 let msg_list = split(result, "\n")
32 call assert_equal(['hello9'], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020033
34 " clear all messages
35 messages clear
36 redir => result
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020037 redraw | messages
Bram Moolenaar451f8492016-04-14 17:16:22 +020038 redir END
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020039 call assert_equal('', result)
Bram Moolenaar451f8492016-04-14 17:16:22 +020040 finally
41 let &more = oldmore
42 endtry
Bram Moolenaar067297e2020-04-13 19:55:50 +020043
44 call assert_fails('message 1', 'E474:')
Bram Moolenaar451f8492016-04-14 17:16:22 +020045endfunction
Bram Moolenaar2abad542018-05-19 14:43:45 +020046
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020047" Patch 7.4.1696 defined the "clearmode()" function for clearing the mode
Bram Moolenaar2abad542018-05-19 14:43:45 +020048" indicator (e.g., "-- INSERT --") when ":stopinsert" is invoked. Message
49" output could then be disturbed when 'cmdheight' was greater than one.
50" This test ensures that the bugfix for this issue remains in place.
Bram Moolenaar1e115362019-01-09 23:01:02 +010051func Test_stopinsert_does_not_break_message_output()
Bram Moolenaar2abad542018-05-19 14:43:45 +020052 set cmdheight=2
53 redraw!
54
55 stopinsert | echo 'test echo'
56 call assert_equal(116, screenchar(&lines - 1, 1))
57 call assert_equal(32, screenchar(&lines, 1))
58 redraw!
59
60 stopinsert | echomsg 'test echomsg'
61 call assert_equal(116, screenchar(&lines - 1, 1))
62 call assert_equal(32, screenchar(&lines, 1))
63 redraw!
64
65 set cmdheight&
Bram Moolenaar1e115362019-01-09 23:01:02 +010066endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +010067
68func Test_message_completion()
69 call feedkeys(":message \<C-A>\<C-B>\"\<CR>", 'tx')
70 call assert_equal('"message clear', @:)
71endfunc
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010072
73func Test_echomsg()
74 call assert_equal("\nhello", execute(':echomsg "hello"'))
75 call assert_equal("\n", execute(':echomsg ""'))
76 call assert_equal("\n12345", execute(':echomsg 12345'))
77 call assert_equal("\n[]", execute(':echomsg []'))
78 call assert_equal("\n[1, 2, 3]", execute(':echomsg [1, 2, 3]'))
79 call assert_equal("\n{}", execute(':echomsg {}'))
80 call assert_equal("\n{'a': 1, 'b': 2}", execute(':echomsg {"a": 1, "b": 2}'))
81 if has('float')
82 call assert_equal("\n1.23", execute(':echomsg 1.23'))
83 endif
84 call assert_match("function('<lambda>\\d*')", execute(':echomsg {-> 1234}'))
85endfunc
86
87func Test_echoerr()
88 call test_ignore_error('IgNoRe')
89 call assert_equal("\nIgNoRe hello", execute(':echoerr "IgNoRe hello"'))
90 call assert_equal("\n12345 IgNoRe", execute(':echoerr 12345 "IgNoRe"'))
91 call assert_equal("\n[1, 2, 'IgNoRe']", execute(':echoerr [1, 2, "IgNoRe"]'))
92 call assert_equal("\n{'IgNoRe': 2, 'a': 1}", execute(':echoerr {"a": 1, "IgNoRe": 2}'))
93 if has('float')
94 call assert_equal("\n1.23 IgNoRe", execute(':echoerr 1.23 "IgNoRe"'))
95 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +020096 eval '<lambda>'->test_ignore_error()
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010097 call assert_match("function('<lambda>\\d*')", execute(':echoerr {-> 1234}'))
98 call test_ignore_error('RESET')
99endfunc
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200100
101func Test_mode_message_at_leaving_insert_by_ctrl_c()
102 if !has('terminal') || has('gui_running')
103 return
104 endif
105
106 " Set custom statusline built by user-defined function.
107 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200108 let lines =<< trim END
109 func StatusLine() abort
110 return ""
111 endfunc
112 set statusline=%!StatusLine()
113 set laststatus=2
114 END
115 call writefile(lines, testfile)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200116
117 let rows = 10
118 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200119 call TermWait(buf, 100)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200120 call assert_equal('run', job_status(term_getjob(buf)))
121
122 call term_sendkeys(buf, "i")
123 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
124 call term_sendkeys(buf, "\<C-C>")
125 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
126
127 call term_sendkeys(buf, ":qall!\<CR>")
128 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
129 exe buf . 'bwipe!'
130 call delete(testfile)
131endfunc
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200132
133func Test_mode_message_at_leaving_insert_with_esc_mapped()
134 if !has('terminal') || has('gui_running')
135 return
136 endif
137
138 " Set custom statusline built by user-defined function.
139 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200140 let lines =<< trim END
141 set laststatus=2
142 inoremap <Esc> <Esc>00
143 END
144 call writefile(lines, testfile)
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200145
146 let rows = 10
147 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200148 call WaitForAssert({-> assert_match('0,0-1\s*All$', term_getline(buf, rows - 1))})
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200149 call assert_equal('run', job_status(term_getjob(buf)))
150
151 call term_sendkeys(buf, "i")
152 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
153 call term_sendkeys(buf, "\<Esc>")
154 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
155
156 call term_sendkeys(buf, ":qall!\<CR>")
157 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
158 exe buf . 'bwipe!'
159 call delete(testfile)
160endfunc
Bram Moolenaar37f4cbd2019-08-23 20:58:45 +0200161
162func Test_echospace()
163 set noruler noshowcmd laststatus=1
164 call assert_equal(&columns - 1, v:echospace)
165 split
166 call assert_equal(&columns - 1, v:echospace)
167 set ruler
168 call assert_equal(&columns - 1, v:echospace)
169 close
170 call assert_equal(&columns - 19, v:echospace)
171 set showcmd noruler
172 call assert_equal(&columns - 12, v:echospace)
173 set showcmd ruler
174 call assert_equal(&columns - 29, v:echospace)
175
176 set ruler& showcmd&
177endfunc
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100178
179" Test more-prompt (see :help more-prompt).
180func Test_message_more()
181 if !CanRunVimInTerminal()
182 throw 'Skipped: cannot run vim in terminal'
183 endif
184 let buf = RunVimInTerminal('', {'rows': 6})
185 call term_sendkeys(buf, ":call setline(1, range(1, 100))\n")
186
187 call term_sendkeys(buf, ":%p#\n")
188 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
189 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
190
191 call term_sendkeys(buf, '?')
192 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
193 call WaitForAssert({-> assert_equal('-- More -- SPACE/d/j: screen/page/line down, b/u/k: up, q: quit ', term_getline(buf, 6))})
194
195 " Down a line with j, <CR>, <NL> or <Down>.
196 call term_sendkeys(buf, "j")
197 call WaitForAssert({-> assert_equal(' 6 6', term_getline(buf, 5))})
198 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
199 call term_sendkeys(buf, "\<NL>")
200 call WaitForAssert({-> assert_equal(' 7 7', term_getline(buf, 5))})
201 call term_sendkeys(buf, "\<CR>")
202 call WaitForAssert({-> assert_equal(' 8 8', term_getline(buf, 5))})
203 call term_sendkeys(buf, "\<Down>")
204 call WaitForAssert({-> assert_equal(' 9 9', term_getline(buf, 5))})
205
206 " Down a screen with <Space>, f, or <PageDown>.
207 call term_sendkeys(buf, 'f')
208 call WaitForAssert({-> assert_equal(' 14 14', term_getline(buf, 5))})
209 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
210 call term_sendkeys(buf, ' ')
211 call WaitForAssert({-> assert_equal(' 19 19', term_getline(buf, 5))})
212 call term_sendkeys(buf, "\<PageDown>")
213 call WaitForAssert({-> assert_equal(' 24 24', term_getline(buf, 5))})
214
215 " Down a page (half a screen) with d.
216 call term_sendkeys(buf, 'd')
217 call WaitForAssert({-> assert_equal(' 27 27', term_getline(buf, 5))})
218
219 " Down all the way with 'G'.
220 call term_sendkeys(buf, 'G')
221 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
222 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
223
224 " Up a line k, <BS> or <Up>.
225 call term_sendkeys(buf, 'k')
226 call WaitForAssert({-> assert_equal(' 99 99', term_getline(buf, 5))})
227 call term_sendkeys(buf, "\<BS>")
228 call WaitForAssert({-> assert_equal(' 98 98', term_getline(buf, 5))})
229 call term_sendkeys(buf, "\<Up>")
230 call WaitForAssert({-> assert_equal(' 97 97', term_getline(buf, 5))})
231
232 " Up a screen with b or <PageUp>.
233 call term_sendkeys(buf, 'b')
234 call WaitForAssert({-> assert_equal(' 92 92', term_getline(buf, 5))})
235 call term_sendkeys(buf, "\<PageUp>")
236 call WaitForAssert({-> assert_equal(' 87 87', term_getline(buf, 5))})
237
238 " Up a page (half a screen) with u.
239 call term_sendkeys(buf, 'u')
240 call WaitForAssert({-> assert_equal(' 84 84', term_getline(buf, 5))})
241
242 " Up all the way with 'g'.
243 call term_sendkeys(buf, 'g')
244 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
245 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
246
247 " All the way down. Pressing f should do nothing but pressing
248 " space should end the more prompt.
249 call term_sendkeys(buf, 'G')
250 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
251 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
252 call term_sendkeys(buf, 'f')
253 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
254 call term_sendkeys(buf, ' ')
255 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
256
257 " Pressing g< shows the previous command output.
258 call term_sendkeys(buf, 'g<')
259 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
260 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
261
262 call term_sendkeys(buf, ":%p#\n")
263 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
264 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
265
266 " Stop command output with q, <Esc> or CTRL-C.
267 call term_sendkeys(buf, 'q')
268 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
269
Bram Moolenaar43c60ed2020-02-02 15:55:19 +0100270 call StopVimInTerminal(buf)
271endfunc
272
273func Test_ask_yesno()
274 if !CanRunVimInTerminal()
275 throw 'Skipped: cannot run vim in terminal'
276 endif
277 let buf = RunVimInTerminal('', {'rows': 6})
278 call term_sendkeys(buf, ":call setline(1, range(1, 2))\n")
279
280 call term_sendkeys(buf, ":2,1s/^/n/\n")
281 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
282 call term_sendkeys(buf, "n")
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/^/Esc/\n")
287 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
288 call term_sendkeys(buf, "\<Esc>")
289 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
290 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
291
292 call term_sendkeys(buf, ":2,1s/^/y/\n")
293 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
294 call term_sendkeys(buf, "y")
295 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?y *2,1 *All$', term_getline(buf, 6))})
296 call WaitForAssert({-> assert_equal('y1', term_getline(buf, 1))})
297 call WaitForAssert({-> assert_equal('y2', term_getline(buf, 2))})
298
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100299 call StopVimInTerminal(buf)
300endfunc
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100301
302func Test_null()
303 echom test_null_list()
304 echom test_null_dict()
305 echom test_null_blob()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100306 echom test_null_string()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100307 echom test_null_partial()
Bram Moolenaarda292b02020-01-08 19:27:40 +0100308 if has('job')
309 echom test_null_job()
310 echom test_null_channel()
311 endif
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100312endfunc