blob: c575672073b91ad457ffc23095eab4df2a723e69 [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 Moolenaar494e9062020-05-31 21:28:02 +02003source check.vim
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004source shared.vim
Bram Moolenaarc6d539b2019-12-28 17:10:46 +01005source term_util.vim
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02006source view_util.vim
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02007
Bram Moolenaar49b2fb32020-04-25 17:13:56 +02008func Test_messages()
Bram Moolenaar451f8492016-04-14 17:16:22 +02009 let oldmore = &more
10 try
11 set nomore
12
13 let arr = map(range(10), '"hello" . v:val')
14 for s in arr
15 echomsg s | redraw
16 endfor
Bram Moolenaar451f8492016-04-14 17:16:22 +020017
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020018 " get last two messages
Bram Moolenaar451f8492016-04-14 17:16:22 +020019 redir => result
20 2messages | redraw
21 redir END
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020022 let msg_list = split(result, "\n")
23 call assert_equal(["hello8", "hello9"], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020024
25 " clear messages without last one
26 1messages clear
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020027 let msg_list = GetMessages()
Bram Moolenaarbea1ede2016-04-14 19:44:36 +020028 call assert_equal(['hello9'], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020029
30 " clear all messages
31 messages clear
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020032 let msg_list = GetMessages()
33 call assert_equal([], msg_list)
Bram Moolenaar451f8492016-04-14 17:16:22 +020034 finally
35 let &more = oldmore
36 endtry
Bram Moolenaar067297e2020-04-13 19:55:50 +020037
38 call assert_fails('message 1', 'E474:')
Bram Moolenaar49b2fb32020-04-25 17:13:56 +020039endfunc
Bram Moolenaar2abad542018-05-19 14:43:45 +020040
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020041" Patch 7.4.1696 defined the "clearmode()" function for clearing the mode
Bram Moolenaar2abad542018-05-19 14:43:45 +020042" indicator (e.g., "-- INSERT --") when ":stopinsert" is invoked. Message
43" output could then be disturbed when 'cmdheight' was greater than one.
44" This test ensures that the bugfix for this issue remains in place.
Bram Moolenaar1e115362019-01-09 23:01:02 +010045func Test_stopinsert_does_not_break_message_output()
Bram Moolenaar2abad542018-05-19 14:43:45 +020046 set cmdheight=2
47 redraw!
48
49 stopinsert | echo 'test echo'
50 call assert_equal(116, screenchar(&lines - 1, 1))
51 call assert_equal(32, screenchar(&lines, 1))
52 redraw!
53
54 stopinsert | echomsg 'test echomsg'
55 call assert_equal(116, screenchar(&lines - 1, 1))
56 call assert_equal(32, screenchar(&lines, 1))
57 redraw!
58
59 set cmdheight&
Bram Moolenaar1e115362019-01-09 23:01:02 +010060endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +010061
62func Test_message_completion()
63 call feedkeys(":message \<C-A>\<C-B>\"\<CR>", 'tx')
64 call assert_equal('"message clear', @:)
65endfunc
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010066
67func Test_echomsg()
68 call assert_equal("\nhello", execute(':echomsg "hello"'))
69 call assert_equal("\n", execute(':echomsg ""'))
70 call assert_equal("\n12345", execute(':echomsg 12345'))
71 call assert_equal("\n[]", execute(':echomsg []'))
72 call assert_equal("\n[1, 2, 3]", execute(':echomsg [1, 2, 3]'))
Bram Moolenaardb950e42020-04-22 19:13:19 +020073 call assert_equal("\n[1, 2, []]", execute(':echomsg [1, 2, test_null_list()]'))
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010074 call assert_equal("\n{}", execute(':echomsg {}'))
75 call assert_equal("\n{'a': 1, 'b': 2}", execute(':echomsg {"a": 1, "b": 2}'))
76 if has('float')
77 call assert_equal("\n1.23", execute(':echomsg 1.23'))
78 endif
79 call assert_match("function('<lambda>\\d*')", execute(':echomsg {-> 1234}'))
80endfunc
81
82func Test_echoerr()
83 call test_ignore_error('IgNoRe')
84 call assert_equal("\nIgNoRe hello", execute(':echoerr "IgNoRe hello"'))
85 call assert_equal("\n12345 IgNoRe", execute(':echoerr 12345 "IgNoRe"'))
86 call assert_equal("\n[1, 2, 'IgNoRe']", execute(':echoerr [1, 2, "IgNoRe"]'))
87 call assert_equal("\n{'IgNoRe': 2, 'a': 1}", execute(':echoerr {"a": 1, "IgNoRe": 2}'))
88 if has('float')
89 call assert_equal("\n1.23 IgNoRe", execute(':echoerr 1.23 "IgNoRe"'))
90 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +020091 eval '<lambda>'->test_ignore_error()
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010092 call assert_match("function('<lambda>\\d*')", execute(':echoerr {-> 1234}'))
93 call test_ignore_error('RESET')
94endfunc
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +020095
96func Test_mode_message_at_leaving_insert_by_ctrl_c()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020097 CheckFeature terminal
98 CheckNotGui
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +020099
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()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200128 CheckFeature terminal
129 CheckNotGui
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200130
131 " Set custom statusline built by user-defined function.
132 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200133 let lines =<< trim END
134 set laststatus=2
135 inoremap <Esc> <Esc>00
136 END
137 call writefile(lines, testfile)
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200138
139 let rows = 10
140 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200141 call WaitForAssert({-> assert_match('0,0-1\s*All$', term_getline(buf, rows - 1))})
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200142 call assert_equal('run', job_status(term_getjob(buf)))
143
144 call term_sendkeys(buf, "i")
145 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))})
146 call term_sendkeys(buf, "\<Esc>")
147 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))})
148
149 call term_sendkeys(buf, ":qall!\<CR>")
150 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
151 exe buf . 'bwipe!'
152 call delete(testfile)
153endfunc
Bram Moolenaar37f4cbd2019-08-23 20:58:45 +0200154
155func Test_echospace()
156 set noruler noshowcmd laststatus=1
157 call assert_equal(&columns - 1, v:echospace)
158 split
159 call assert_equal(&columns - 1, v:echospace)
160 set ruler
161 call assert_equal(&columns - 1, v:echospace)
162 close
163 call assert_equal(&columns - 19, v:echospace)
164 set showcmd noruler
165 call assert_equal(&columns - 12, v:echospace)
166 set showcmd ruler
167 call assert_equal(&columns - 29, v:echospace)
168
169 set ruler& showcmd&
170endfunc
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100171
172" Test more-prompt (see :help more-prompt).
173func Test_message_more()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200174 CheckRunVimInTerminal
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100175 let buf = RunVimInTerminal('', {'rows': 6})
176 call term_sendkeys(buf, ":call setline(1, range(1, 100))\n")
177
178 call term_sendkeys(buf, ":%p#\n")
179 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
180 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
181
182 call term_sendkeys(buf, '?')
183 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
184 call WaitForAssert({-> assert_equal('-- More -- SPACE/d/j: screen/page/line down, b/u/k: up, q: quit ', term_getline(buf, 6))})
185
186 " Down a line with j, <CR>, <NL> or <Down>.
187 call term_sendkeys(buf, "j")
188 call WaitForAssert({-> assert_equal(' 6 6', term_getline(buf, 5))})
189 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
190 call term_sendkeys(buf, "\<NL>")
191 call WaitForAssert({-> assert_equal(' 7 7', term_getline(buf, 5))})
192 call term_sendkeys(buf, "\<CR>")
193 call WaitForAssert({-> assert_equal(' 8 8', term_getline(buf, 5))})
194 call term_sendkeys(buf, "\<Down>")
195 call WaitForAssert({-> assert_equal(' 9 9', term_getline(buf, 5))})
196
197 " Down a screen with <Space>, f, or <PageDown>.
198 call term_sendkeys(buf, 'f')
199 call WaitForAssert({-> assert_equal(' 14 14', term_getline(buf, 5))})
200 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
201 call term_sendkeys(buf, ' ')
202 call WaitForAssert({-> assert_equal(' 19 19', term_getline(buf, 5))})
203 call term_sendkeys(buf, "\<PageDown>")
204 call WaitForAssert({-> assert_equal(' 24 24', term_getline(buf, 5))})
205
206 " Down a page (half a screen) with d.
207 call term_sendkeys(buf, 'd')
208 call WaitForAssert({-> assert_equal(' 27 27', term_getline(buf, 5))})
209
210 " Down all the way with 'G'.
211 call term_sendkeys(buf, 'G')
212 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
213 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
214
215 " Up a line k, <BS> or <Up>.
216 call term_sendkeys(buf, 'k')
217 call WaitForAssert({-> assert_equal(' 99 99', term_getline(buf, 5))})
218 call term_sendkeys(buf, "\<BS>")
219 call WaitForAssert({-> assert_equal(' 98 98', term_getline(buf, 5))})
220 call term_sendkeys(buf, "\<Up>")
221 call WaitForAssert({-> assert_equal(' 97 97', term_getline(buf, 5))})
222
223 " Up a screen with b or <PageUp>.
224 call term_sendkeys(buf, 'b')
225 call WaitForAssert({-> assert_equal(' 92 92', term_getline(buf, 5))})
226 call term_sendkeys(buf, "\<PageUp>")
227 call WaitForAssert({-> assert_equal(' 87 87', term_getline(buf, 5))})
228
229 " Up a page (half a screen) with u.
230 call term_sendkeys(buf, 'u')
231 call WaitForAssert({-> assert_equal(' 84 84', term_getline(buf, 5))})
232
233 " Up all the way with 'g'.
234 call term_sendkeys(buf, 'g')
235 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
236 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
237
238 " All the way down. Pressing f should do nothing but pressing
239 " space should end the more prompt.
240 call term_sendkeys(buf, 'G')
241 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
242 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))})
243 call term_sendkeys(buf, 'f')
244 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))})
245 call term_sendkeys(buf, ' ')
246 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
247
248 " Pressing g< shows the previous command output.
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
253 call term_sendkeys(buf, ":%p#\n")
254 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))})
255 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))})
256
257 " Stop command output with q, <Esc> or CTRL-C.
258 call term_sendkeys(buf, 'q')
259 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))})
260
Bram Moolenaar43c60ed2020-02-02 15:55:19 +0100261 call StopVimInTerminal(buf)
262endfunc
263
264func Test_ask_yesno()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200265 CheckRunVimInTerminal
Bram Moolenaar43c60ed2020-02-02 15:55:19 +0100266 let buf = RunVimInTerminal('', {'rows': 6})
267 call term_sendkeys(buf, ":call setline(1, range(1, 2))\n")
268
269 call term_sendkeys(buf, ":2,1s/^/n/\n")
270 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
271 call term_sendkeys(buf, "n")
272 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
273 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
274
275 call term_sendkeys(buf, ":2,1s/^/Esc/\n")
276 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
277 call term_sendkeys(buf, "\<Esc>")
278 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))})
279 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))})
280
281 call term_sendkeys(buf, ":2,1s/^/y/\n")
282 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))})
283 call term_sendkeys(buf, "y")
284 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?y *2,1 *All$', term_getline(buf, 6))})
285 call WaitForAssert({-> assert_equal('y1', term_getline(buf, 1))})
286 call WaitForAssert({-> assert_equal('y2', term_getline(buf, 2))})
287
Bram Moolenaarc6d539b2019-12-28 17:10:46 +0100288 call StopVimInTerminal(buf)
289endfunc
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100290
291func Test_null()
292 echom test_null_list()
293 echom test_null_dict()
294 echom test_null_blob()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100295 echom test_null_string()
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200296 echom test_null_function()
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100297 echom test_null_partial()
Bram Moolenaarda292b02020-01-08 19:27:40 +0100298 if has('job')
299 echom test_null_job()
300 echom test_null_channel()
301 endif
Bram Moolenaar9db2afe2020-01-08 18:56:20 +0100302endfunc
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200303
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200304func Test_mapping_at_hit_return_prompt()
305 nnoremap <C-B> :echo "hit ctrl-b"<CR>
306 call feedkeys(":ls\<CR>", "xt")
Bram Moolenaarfccd93f2020-05-31 22:06:51 +0200307 call feedkeys("\<*C-B>", "xt")
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200308 call assert_match('hit ctrl-b', Screenline(&lines - 1))
309 nunmap <C-B>
310endfunc
311
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200312" vim: shiftwidth=2 sts=2 expandtab