1" Tests for autocommands 2 3func Test_vim_did_enter() 4 call assert_false(v:vim_did_enter) 5 6 " This script will never reach the main loop, can't check if v:vim_did_enter 7 " becomes one. 8endfunc 9 10if has('timers') 11 func ExitInsertMode(id) 12 call feedkeys("\<Esc>") 13 endfunc 14 15 func Test_cursorhold_insert() 16 " Need to move the cursor. 17 call feedkeys("ggG", "xt") 18 19 let g:triggered = 0 20 au CursorHoldI * let g:triggered += 1 21 set updatetime=20 22 call timer_start(100, 'ExitInsertMode') 23 call feedkeys('a', 'x!') 24 call assert_equal(1, g:triggered) 25 au! CursorHoldI 26 set updatetime& 27 endfunc 28 29 func Test_cursorhold_insert_ctrl_x() 30 let g:triggered = 0 31 au CursorHoldI * let g:triggered += 1 32 set updatetime=20 33 call timer_start(100, 'ExitInsertMode') 34 " CursorHoldI does not trigger after CTRL-X 35 call feedkeys("a\<C-X>", 'x!') 36 call assert_equal(0, g:triggered) 37 au! CursorHoldI 38 set updatetime& 39 endfunc 40endif 41 42function Test_bufunload() 43 augroup test_bufunload_group 44 autocmd! 45 autocmd BufUnload * call add(s:li, "bufunload") 46 autocmd BufDelete * call add(s:li, "bufdelete") 47 autocmd BufWipeout * call add(s:li, "bufwipeout") 48 augroup END 49 50 let s:li=[] 51 new 52 setlocal bufhidden= 53 bunload 54 call assert_equal(["bufunload", "bufdelete"], s:li) 55 56 let s:li=[] 57 new 58 setlocal bufhidden=delete 59 bunload 60 call assert_equal(["bufunload", "bufdelete"], s:li) 61 62 let s:li=[] 63 new 64 setlocal bufhidden=unload 65 bwipeout 66 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li) 67 68 au! test_bufunload_group 69 augroup! test_bufunload_group 70endfunc 71 72" SEGV occurs in older versions. (At least 7.4.2005 or older) 73function Test_autocmd_bufunload_with_tabnext() 74 tabedit 75 tabfirst 76 77 augroup test_autocmd_bufunload_with_tabnext_group 78 autocmd! 79 autocmd BufUnload <buffer> tabnext 80 augroup END 81 82 quit 83 call assert_equal(2, tabpagenr('$')) 84 85 autocmd! test_autocmd_bufunload_with_tabnext_group 86 augroup! test_autocmd_bufunload_with_tabnext_group 87 tablast 88 quit 89endfunc 90 91function Test_autocmd_bufwinleave_with_tabfirst() 92 tabedit 93 augroup sample 94 autocmd! 95 autocmd BufWinLeave <buffer> tabfirst 96 augroup END 97 call setline(1, ['a', 'b', 'c']) 98 edit! a.txt 99 tabclose 100endfunc 101 102" SEGV occurs in older versions. (At least 7.4.2321 or older) 103function Test_autocmd_bufunload_avoiding_SEGV_01() 104 split aa.txt 105 let lastbuf = bufnr('$') 106 107 augroup test_autocmd_bufunload 108 autocmd! 109 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' 110 augroup END 111 112 call assert_fails('edit bb.txt', 'E937:') 113 114 autocmd! test_autocmd_bufunload 115 augroup! test_autocmd_bufunload 116 bwipe! aa.txt 117 bwipe! bb.txt 118endfunc 119 120" SEGV occurs in older versions. (At least 7.4.2321 or older) 121function Test_autocmd_bufunload_avoiding_SEGV_02() 122 setlocal buftype=nowrite 123 let lastbuf = bufnr('$') 124 125 augroup test_autocmd_bufunload 126 autocmd! 127 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' 128 augroup END 129 130 normal! i1 131 call assert_fails('edit a.txt', 'E517:') 132 call feedkeys("\<CR>") 133 134 autocmd! test_autocmd_bufunload 135 augroup! test_autocmd_bufunload 136 bwipe! a.txt 137endfunc 138 139func Test_win_tab_autocmd() 140 let g:record = [] 141 142 augroup testing 143 au WinNew * call add(g:record, 'WinNew') 144 au WinEnter * call add(g:record, 'WinEnter') 145 au WinLeave * call add(g:record, 'WinLeave') 146 au TabNew * call add(g:record, 'TabNew') 147 au TabClosed * call add(g:record, 'TabClosed') 148 au TabEnter * call add(g:record, 'TabEnter') 149 au TabLeave * call add(g:record, 'TabLeave') 150 augroup END 151 152 split 153 tabnew 154 close 155 close 156 157 call assert_equal([ 158 \ 'WinLeave', 'WinNew', 'WinEnter', 159 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', 160 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter', 161 \ 'WinLeave', 'WinEnter' 162 \ ], g:record) 163 164 let g:record = [] 165 tabnew somefile 166 tabnext 167 bwipe somefile 168 169 call assert_equal([ 170 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', 171 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 172 \ 'TabClosed' 173 \ ], g:record) 174 175 augroup testing 176 au! 177 augroup END 178 unlet g:record 179endfunc 180 181func s:AddAnAutocmd() 182 augroup vimBarTest 183 au BufReadCmd * echo 'hello' 184 augroup END 185 call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) 186endfunc 187 188func Test_early_bar() 189 " test that a bar is recognized before the {event} 190 call s:AddAnAutocmd() 191 augroup vimBarTest | au! | augroup END 192 call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) 193 194 call s:AddAnAutocmd() 195 augroup vimBarTest| au!| augroup END 196 call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) 197 198 " test that a bar is recognized after the {event} 199 call s:AddAnAutocmd() 200 augroup vimBarTest| au!BufReadCmd| augroup END 201 call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) 202 203 " test that a bar is recognized after the {group} 204 call s:AddAnAutocmd() 205 au! vimBarTest|echo 'hello' 206 call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) 207endfunc 208 209func RemoveGroup() 210 autocmd! StartOK 211 augroup! StartOK 212endfunc 213 214func Test_augroup_warning() 215 augroup TheWarning 216 au VimEnter * echo 'entering' 217 augroup END 218 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0) 219 redir => res 220 augroup! TheWarning 221 redir END 222 call assert_true(match(res, "W19:") >= 0) 223 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) 224 225 " check "Another" does not take the pace of the deleted entry 226 augroup Another 227 augroup END 228 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) 229 augroup! Another 230 231 " no warning for postpone aucmd delete 232 augroup StartOK 233 au VimEnter * call RemoveGroup() 234 augroup END 235 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0) 236 redir => res 237 doautocmd VimEnter 238 redir END 239 call assert_true(match(res, "W19:") < 0) 240 au! VimEnter 241endfunc 242 243func Test_augroup_deleted() 244 " This caused a crash before E936 was introduced 245 augroup x 246 call assert_fails('augroup! x', 'E936:') 247 au VimEnter * echo 248 augroup end 249 augroup! x 250 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) 251 au! VimEnter 252endfunc 253 254" Tests for autocommands on :close command. 255" This used to be in test13. 256func Test_three_windows() 257 " Write three files and open them, each in a window. 258 " Then go to next window, with autocommand that deletes the previous one. 259 " Do this twice, writing the file. 260 e! Xtestje1 261 call setline(1, 'testje1') 262 w 263 sp Xtestje2 264 call setline(1, 'testje2') 265 w 266 sp Xtestje3 267 call setline(1, 'testje3') 268 w 269 wincmd w 270 au WinLeave Xtestje2 bwipe 271 wincmd w 272 call assert_equal('Xtestje1', expand('%')) 273 274 au WinLeave Xtestje1 bwipe Xtestje3 275 close 276 call assert_equal('Xtestje1', expand('%')) 277 278 " Test deleting the buffer on a Unload event. If this goes wrong there 279 " will be the ATTENTION prompt. 280 e Xtestje1 281 au! 282 au! BufUnload Xtestje1 bwipe 283 call assert_fails('e Xtestje3', 'E937:') 284 call assert_equal('Xtestje3', expand('%')) 285 286 e Xtestje2 287 sp Xtestje1 288 call assert_fails('e', 'E937:') 289 call assert_equal('Xtestje2', expand('%')) 290 291 " Test changing buffers in a BufWipeout autocommand. If this goes wrong 292 " there are ml_line errors and/or a Crash. 293 au! 294 only 295 e Xanother 296 e Xtestje1 297 bwipe Xtestje2 298 bwipe Xtestje3 299 au BufWipeout Xtestje1 buf Xtestje1 300 bwipe 301 call assert_equal('Xanother', expand('%')) 302 303 only 304 help 305 wincmd w 306 1quit 307 call assert_equal('Xanother', expand('%')) 308 309 au! 310 call delete('Xtestje1') 311 call delete('Xtestje2') 312 call delete('Xtestje3') 313endfunc 314