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