blob: ac91aa097b202af42fbbc9bc75670cf3d1dcd41b [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]'))
Bram Moolenaardb950e42020-04-22 19:13:19 +020079 call assert_equal("\n[1, 2, []]", execute(':echomsg [1, 2, test_null_list()]'))
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010080 call assert_equal("\n{}", execute(':echomsg {}'))
81 call assert_equal("\n{'a': 1, 'b': 2}", execute(':echomsg {"a": 1, "b": 2}'))
82 if has('float')
83 call assert_equal("\n1.23", execute(':echomsg 1.23'))
84 endif
85 call assert_match("function('<lambda>\\d*')", execute(':echomsg {-> 1234}'))
86endfunc
87
88func Test_echoerr()
89 call test_ignore_error('IgNoRe')
90 call assert_equal("\nIgNoRe hello", execute(':echoerr "IgNoRe hello"'))
91 call assert_equal("\n12345 IgNoRe", execute(':echoerr 12345 "IgNoRe"'))
92 call assert_equal("\n[1, 2, 'IgNoRe']", execute(':echoerr [1, 2, "IgNoRe"]'))
93 call assert_equal("\n{'IgNoRe': 2, 'a': 1}", execute(':echoerr {"a": 1, "IgNoRe": 2}'))
94 if has('float')
95 call assert_equal("\n1.23 IgNoRe", execute(':echoerr 1.23 "IgNoRe"'))
96 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +020097 eval '<lambda>'->test_ignore_error()
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010098 call assert_match("function('<lambda>\\d*')", execute(':echoerr {-> 1234}'))
99 call test_ignore_error('RESET')
100endfunc
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200101
102func Test_mode_message_at_leaving_insert_by_ctrl_c()
103 if !has('terminal') || has('gui_running')
104 return
105 endif
106
107 " Set custom statusline built by user-defined function.
108 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200109 let lines =<< trim END
110 func StatusLine() abort
111 return ""
112 endfunc
113 set statusline=%!StatusLine()
114 set laststatus=2
115 END
116 call writefile(lines, testfile)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200117
118 let rows = 10
119 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200120 call TermWait(buf, 100)
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +0200121 call assert_equal('run', job_status(term_getjob(buf)))
122
123 call term_sendkeys(buf, "i")
124 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
125 call term_sendkeys(buf, "\<C-C>")
126 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
127
128 call term_sendkeys(buf, ":qall!\<CR>")
129 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
130 exe buf . 'bwipe!'
131 call delete(testfile)
132endfunc
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200133
134func Test_mode_message_at_leaving_insert_with_esc_mapped()
135 if !has('terminal') || has('gui_running')
136 return
137 endif
138
139 " Set custom statusline built by user-defined function.
140 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200141 let lines =<< trim END
142 set laststatus=2
143 inoremap <Esc> <Esc>00
144 END
145 call writefile(lines, testfile)
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200146
147 let rows = 10
148 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200149 call WaitForAssert({-> assert_match('0,0-1\s*All$', term_getline(buf, rows - 1))})
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200150 call assert_equal('run', job_status(term_getjob(buf)))
151
152 call term_sendkeys(buf, "i")
153 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
154 call term_sendkeys(buf, "\<Esc>")
155 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
156
157 call term_sendkeys(buf, ":qall!\<CR>")
158 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
159 exe buf . 'bwipe!'
160 call delete(testfile)
161endfunc
Bram Moolenaar37f4cbd2019-08-23 20:58:45 +0200162
163func Test_echospace()
164 set noruler noshowcmd laststatus=1
165 call assert_equal(&columns - 1, v:echospace)
166 split
167 call assert_equal(&columns - 1, v:echospace)
168 set ruler
169 call assert_equal(&columns - 1, v:echospace)
170 close
171 call assert_equal(&columns - 19, v:echospace)
172 set showcmd noruler
173 call assert_equal(&columns - 12, v:echospace)
174 set showcmd ruler
175 call assert_equal(&columns - 29, v:echospace)
176
177 set ruler& showcmd&
178endfunc
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100179
180" Test more-prompt (see :help more-prompt).
181func Test_message_more()
182 if !CanRunVimInTerminal()
183 throw 'Skipped: cannot run vim in terminal'
184 endif
185 let buf = RunVimInTerminal('', {'rows': 6})
186 call term_sendkeys(buf, ":call setline(1, range(1, 100))\n")
187
188 call term_sendkeys(buf, ":%p#\n")
189 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
190 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
191
192 call term_sendkeys(buf, '?')
193 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
194 call WaitForAssert({-> assert_equal('-- More -- SPACE/d/j: screen/page/line down, b/u/k: up, q: quit ', term_getline(buf, 6))})
195
196 " Down a line with j, <CR>, <NL> or <Down>.
197 call term_sendkeys(buf, "j")
198 call WaitForAssert({-> assert_equal(' 6 6', term_getline(buf, 5))})
199 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
200 call term_sendkeys(buf, "\<NL>")
201 call WaitForAssert({-> assert_equal(' 7 7', term_getline(buf, 5))})
202 call term_sendkeys(buf, "\<CR>")
203 call WaitForAssert({-> assert_equal(' 8 8', term_getline(buf, 5))})
204 call term_sendkeys(buf, "\<Down>")
205 call WaitForAssert({-> assert_equal(' 9 9', term_getline(buf, 5))})
206
207 " Down a screen with <Space>, f, or <PageDown>.
208 call term_sendkeys(buf, 'f')
209 call WaitForAssert({-> assert_equal(' 14 14', term_getline(buf, 5))})
210 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
211 call term_sendkeys(buf, ' ')
212 call WaitForAssert({-> assert_equal(' 19 19', term_getline(buf, 5))})
213 call term_sendkeys(buf, "\<PageDown>")
214 call WaitForAssert({-> assert_equal(' 24 24', term_getline(buf, 5))})
215
216 " Down a page (half a screen) with d.
217 call term_sendkeys(buf, 'd')
218 call WaitForAssert({-> assert_equal(' 27 27', term_getline(buf, 5))})
219
220 " Down all the way with 'G'.
221 call term_sendkeys(buf, 'G')
222 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
223 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
224
225 " Up a line k, <BS> or <Up>.
226 call term_sendkeys(buf, 'k')
227 call WaitForAssert({-> assert_equal(' 99 99', term_getline(buf, 5))})
228 call term_sendkeys(buf, "\<BS>")
229 call WaitForAssert({-> assert_equal(' 98 98', term_getline(buf, 5))})
230 call term_sendkeys(buf, "\<Up>")
231 call WaitForAssert({-> assert_equal(' 97 97', term_getline(buf, 5))})
232
233 " Up a screen with b or <PageUp>.
234 call term_sendkeys(buf, 'b')
235 call WaitForAssert({-> assert_equal(' 92 92', term_getline(buf, 5))})
236 call term_sendkeys(buf, "\<PageUp>")
237 call WaitForAssert({-> assert_equal(' 87 87', term_getline(buf, 5))})
238
239 " Up a page (half a screen) with u.
240 call term_sendkeys(buf, 'u')
241 call WaitForAssert({-> assert_equal(' 84 84', term_getline(buf, 5))})
242
243 " Up all the way with 'g'.
244 call term_sendkeys(buf, 'g')
245 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
246 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
247
248 " All the way down. Pressing f should do nothing but pressing
249 " space should end the more prompt.
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 call term_sendkeys(buf, 'f')
254 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
255 call term_sendkeys(buf, ' ')
256 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
257
258 " Pressing g< shows the previous command output.
259 call term_sendkeys(buf, 'g<')
260 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
261 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
262
263 call term_sendkeys(buf, ":%p#\n")
264 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
265 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
266
267 " Stop command output with q, <Esc> or CTRL-C.
268 call term_sendkeys(buf, 'q')
269 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
270
Bram Moolenaar43c60ed2020-02-02 15:55:19 +0100271 call StopVimInTerminal(buf)
272endfunc
273
274func Test_ask_yesno()
275 if !CanRunVimInTerminal()
276 throw 'Skipped: cannot run vim in terminal'
277 endif
278 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 Moolenaar9db2afe2020-01-08 18:56:20 +0100308 echom test_null_partial()
Bram Moolenaarda292b02020-01-08 19:27:40 +0100309 if has('job')
310 echom test_null_job()
311 echom test_null_channel()
312 endif
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100313endfunc