1" Tests for :messages, :echomsg, :echoerr 2 3source shared.vim 4source term_util.vim 5 6func Test_messages() 7 let oldmore = &more 8 try 9 set nomore 10 11 let arr = map(range(10), '"hello" . v:val') 12 for s in arr 13 echomsg s | redraw 14 endfor 15 16 " get last two messages 17 redir => result 18 2messages | redraw 19 redir END 20 let msg_list = split(result, "\n") 21 call assert_equal(["hello8", "hello9"], msg_list) 22 23 " clear messages without last one 24 1messages clear 25 let msg_list = GetMessages() 26 call assert_equal(['hello9'], msg_list) 27 28 " clear all messages 29 messages clear 30 let msg_list = GetMessages() 31 call assert_equal([], msg_list) 32 finally 33 let &more = oldmore 34 endtry 35 36 call assert_fails('message 1', 'E474:') 37endfunc 38 39" Patch 7.4.1696 defined the "clearmode()" function for clearing the mode 40" indicator (e.g., "-- INSERT --") when ":stopinsert" is invoked. Message 41" output could then be disturbed when 'cmdheight' was greater than one. 42" This test ensures that the bugfix for this issue remains in place. 43func Test_stopinsert_does_not_break_message_output() 44 set cmdheight=2 45 redraw! 46 47 stopinsert | echo 'test echo' 48 call assert_equal(116, screenchar(&lines - 1, 1)) 49 call assert_equal(32, screenchar(&lines, 1)) 50 redraw! 51 52 stopinsert | echomsg 'test echomsg' 53 call assert_equal(116, screenchar(&lines - 1, 1)) 54 call assert_equal(32, screenchar(&lines, 1)) 55 redraw! 56 57 set cmdheight& 58endfunc 59 60func Test_message_completion() 61 call feedkeys(":message \<C-A>\<C-B>\"\<CR>", 'tx') 62 call assert_equal('"message clear', @:) 63endfunc 64 65func Test_echomsg() 66 call assert_equal("\nhello", execute(':echomsg "hello"')) 67 call assert_equal("\n", execute(':echomsg ""')) 68 call assert_equal("\n12345", execute(':echomsg 12345')) 69 call assert_equal("\n[]", execute(':echomsg []')) 70 call assert_equal("\n[1, 2, 3]", execute(':echomsg [1, 2, 3]')) 71 call assert_equal("\n[1, 2, []]", execute(':echomsg [1, 2, test_null_list()]')) 72 call assert_equal("\n{}", execute(':echomsg {}')) 73 call assert_equal("\n{'a': 1, 'b': 2}", execute(':echomsg {"a": 1, "b": 2}')) 74 if has('float') 75 call assert_equal("\n1.23", execute(':echomsg 1.23')) 76 endif 77 call assert_match("function('<lambda>\\d*')", execute(':echomsg {-> 1234}')) 78endfunc 79 80func Test_echoerr() 81 call test_ignore_error('IgNoRe') 82 call assert_equal("\nIgNoRe hello", execute(':echoerr "IgNoRe hello"')) 83 call assert_equal("\n12345 IgNoRe", execute(':echoerr 12345 "IgNoRe"')) 84 call assert_equal("\n[1, 2, 'IgNoRe']", execute(':echoerr [1, 2, "IgNoRe"]')) 85 call assert_equal("\n{'IgNoRe': 2, 'a': 1}", execute(':echoerr {"a": 1, "IgNoRe": 2}')) 86 if has('float') 87 call assert_equal("\n1.23 IgNoRe", execute(':echoerr 1.23 "IgNoRe"')) 88 endif 89 eval '<lambda>'->test_ignore_error() 90 call assert_match("function('<lambda>\\d*')", execute(':echoerr {-> 1234}')) 91 call test_ignore_error('RESET') 92endfunc 93 94func Test_mode_message_at_leaving_insert_by_ctrl_c() 95 if !has('terminal') || has('gui_running') 96 return 97 endif 98 99 " Set custom statusline built by user-defined function. 100 let testfile = 'Xtest.vim' 101 let lines =<< trim END 102 func StatusLine() abort 103 return "" 104 endfunc 105 set statusline=%!StatusLine() 106 set laststatus=2 107 END 108 call writefile(lines, testfile) 109 110 let rows = 10 111 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows}) 112 call TermWait(buf, 100) 113 call assert_equal('run', job_status(term_getjob(buf))) 114 115 call term_sendkeys(buf, "i") 116 call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, rows))}) 117 call term_sendkeys(buf, "\<C-C>") 118 call WaitForAssert({-> assert_match('^\s*$', term_getline(buf, rows))}) 119 120 call term_sendkeys(buf, ":qall!\<CR>") 121 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))}) 122 exe buf . 'bwipe!' 123 call delete(testfile) 124endfunc 125 126func Test_mode_message_at_leaving_insert_with_esc_mapped() 127 if !has('terminal') || has('gui_running') 128 return 129 endif 130 131 " Set custom statusline built by user-defined function. 132 let testfile = 'Xtest.vim' 133 let lines =<< trim END 134 set laststatus=2 135 inoremap <Esc> <Esc>00 136 END 137 call writefile(lines, testfile) 138 139 let rows = 10 140 let buf = term_start([GetVimProg(), '--clean', '-S', testfile], {'term_rows': rows}) 141 call WaitForAssert({-> assert_match('0,0-1\s*All$', term_getline(buf, rows - 1))}) 142 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 154 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 171 172" Test more-prompt (see :help more-prompt). 173func Test_message_more() 174 if !CanRunVimInTerminal() 175 throw 'Skipped: cannot run vim in terminal' 176 endif 177 let buf = RunVimInTerminal('', {'rows': 6}) 178 call term_sendkeys(buf, ":call setline(1, range(1, 100))\n") 179 180 call term_sendkeys(buf, ":%p#\n") 181 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))}) 182 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))}) 183 184 call term_sendkeys(buf, '?') 185 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))}) 186 call WaitForAssert({-> assert_equal('-- More -- SPACE/d/j: screen/page/line down, b/u/k: up, q: quit ', term_getline(buf, 6))}) 187 188 " Down a line with j, <CR>, <NL> or <Down>. 189 call term_sendkeys(buf, "j") 190 call WaitForAssert({-> assert_equal(' 6 6', term_getline(buf, 5))}) 191 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))}) 192 call term_sendkeys(buf, "\<NL>") 193 call WaitForAssert({-> assert_equal(' 7 7', term_getline(buf, 5))}) 194 call term_sendkeys(buf, "\<CR>") 195 call WaitForAssert({-> assert_equal(' 8 8', term_getline(buf, 5))}) 196 call term_sendkeys(buf, "\<Down>") 197 call WaitForAssert({-> assert_equal(' 9 9', term_getline(buf, 5))}) 198 199 " Down a screen with <Space>, f, or <PageDown>. 200 call term_sendkeys(buf, 'f') 201 call WaitForAssert({-> assert_equal(' 14 14', term_getline(buf, 5))}) 202 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))}) 203 call term_sendkeys(buf, ' ') 204 call WaitForAssert({-> assert_equal(' 19 19', term_getline(buf, 5))}) 205 call term_sendkeys(buf, "\<PageDown>") 206 call WaitForAssert({-> assert_equal(' 24 24', term_getline(buf, 5))}) 207 208 " Down a page (half a screen) with d. 209 call term_sendkeys(buf, 'd') 210 call WaitForAssert({-> assert_equal(' 27 27', term_getline(buf, 5))}) 211 212 " Down all the way with 'G'. 213 call term_sendkeys(buf, 'G') 214 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))}) 215 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))}) 216 217 " Up a line k, <BS> or <Up>. 218 call term_sendkeys(buf, 'k') 219 call WaitForAssert({-> assert_equal(' 99 99', term_getline(buf, 5))}) 220 call term_sendkeys(buf, "\<BS>") 221 call WaitForAssert({-> assert_equal(' 98 98', term_getline(buf, 5))}) 222 call term_sendkeys(buf, "\<Up>") 223 call WaitForAssert({-> assert_equal(' 97 97', term_getline(buf, 5))}) 224 225 " Up a screen with b or <PageUp>. 226 call term_sendkeys(buf, 'b') 227 call WaitForAssert({-> assert_equal(' 92 92', term_getline(buf, 5))}) 228 call term_sendkeys(buf, "\<PageUp>") 229 call WaitForAssert({-> assert_equal(' 87 87', term_getline(buf, 5))}) 230 231 " Up a page (half a screen) with u. 232 call term_sendkeys(buf, 'u') 233 call WaitForAssert({-> assert_equal(' 84 84', term_getline(buf, 5))}) 234 235 " Up all the way with 'g'. 236 call term_sendkeys(buf, 'g') 237 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))}) 238 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))}) 239 240 " All the way down. Pressing f should do nothing but pressing 241 " space should end the more prompt. 242 call term_sendkeys(buf, 'G') 243 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))}) 244 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))}) 245 call term_sendkeys(buf, 'f') 246 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))}) 247 call term_sendkeys(buf, ' ') 248 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))}) 249 250 " Pressing g< shows the previous command output. 251 call term_sendkeys(buf, 'g<') 252 call WaitForAssert({-> assert_equal('100 100', term_getline(buf, 5))}) 253 call WaitForAssert({-> assert_equal('Press ENTER or type command to continue', term_getline(buf, 6))}) 254 255 call term_sendkeys(buf, ":%p#\n") 256 call WaitForAssert({-> assert_equal(' 5 5', term_getline(buf, 5))}) 257 call WaitForAssert({-> assert_equal('-- More --', term_getline(buf, 6))}) 258 259 " Stop command output with q, <Esc> or CTRL-C. 260 call term_sendkeys(buf, 'q') 261 call WaitForAssert({-> assert_equal('100', term_getline(buf, 5))}) 262 263 call StopVimInTerminal(buf) 264endfunc 265 266func Test_ask_yesno() 267 if !CanRunVimInTerminal() 268 throw 'Skipped: cannot run vim in terminal' 269 endif 270 let buf = RunVimInTerminal('', {'rows': 6}) 271 call term_sendkeys(buf, ":call setline(1, range(1, 2))\n") 272 273 call term_sendkeys(buf, ":2,1s/^/n/\n") 274 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))}) 275 call term_sendkeys(buf, "n") 276 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))}) 277 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))}) 278 279 call term_sendkeys(buf, ":2,1s/^/Esc/\n") 280 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))}) 281 call term_sendkeys(buf, "\<Esc>") 282 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?n *1,1 *All$', term_getline(buf, 6))}) 283 call WaitForAssert({-> assert_equal('1', term_getline(buf, 1))}) 284 285 call term_sendkeys(buf, ":2,1s/^/y/\n") 286 call WaitForAssert({-> assert_equal('Backwards range given, OK to swap (y/n)?', term_getline(buf, 6))}) 287 call term_sendkeys(buf, "y") 288 call WaitForAssert({-> assert_match('^Backwards range given, OK to swap (y/n)?y *2,1 *All$', term_getline(buf, 6))}) 289 call WaitForAssert({-> assert_equal('y1', term_getline(buf, 1))}) 290 call WaitForAssert({-> assert_equal('y2', term_getline(buf, 2))}) 291 292 call StopVimInTerminal(buf) 293endfunc 294 295func Test_null() 296 echom test_null_list() 297 echom test_null_dict() 298 echom test_null_blob() 299 echom test_null_string() 300 echom test_null_function() 301 echom test_null_partial() 302 if has('job') 303 echom test_null_job() 304 echom test_null_channel() 305 endif 306endfunc 307 308" vim: shiftwidth=2 sts=2 expandtab 309