blob: 566a07c6f5aa66d0b6d7c5a32fb4732eaae4e358 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaarb3435b02016-09-29 20:54:59 +02003function! s:cleanup_buffers() abort
4 for bnr in range(1, bufnr('$'))
5 if bufloaded(bnr) && bufnr('%') != bnr
6 execute 'bd! ' . bnr
7 endif
8 endfor
9endfunction
10
Bram Moolenaar14735512016-03-26 21:00:08 +010011func Test_vim_did_enter()
12 call assert_false(v:vim_did_enter)
13
14 " This script will never reach the main loop, can't check if v:vim_did_enter
15 " becomes one.
16endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020017
Bram Moolenaarc67e8922016-05-24 16:07:40 +020018if has('timers')
19 func ExitInsertMode(id)
20 call feedkeys("\<Esc>")
21 endfunc
22
23 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020024 " Need to move the cursor.
25 call feedkeys("ggG", "xt")
26
Bram Moolenaarc67e8922016-05-24 16:07:40 +020027 let g:triggered = 0
28 au CursorHoldI * let g:triggered += 1
29 set updatetime=20
30 call timer_start(100, 'ExitInsertMode')
31 call feedkeys('a', 'x!')
32 call assert_equal(1, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020033 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020034 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020035 endfunc
36
37 func Test_cursorhold_insert_ctrl_x()
38 let g:triggered = 0
39 au CursorHoldI * let g:triggered += 1
40 set updatetime=20
41 call timer_start(100, 'ExitInsertMode')
42 " CursorHoldI does not trigger after CTRL-X
43 call feedkeys("a\<C-X>", 'x!')
44 call assert_equal(0, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020045 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020046 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020047 endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020048endif
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050function Test_bufunload()
51 augroup test_bufunload_group
52 autocmd!
53 autocmd BufUnload * call add(s:li, "bufunload")
54 autocmd BufDelete * call add(s:li, "bufdelete")
55 autocmd BufWipeout * call add(s:li, "bufwipeout")
56 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +020057
Bram Moolenaarc67e8922016-05-24 16:07:40 +020058 let s:li=[]
59 new
60 setlocal bufhidden=
61 bunload
62 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +020063
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 let s:li=[]
65 new
66 setlocal bufhidden=delete
67 bunload
68 call assert_equal(["bufunload", "bufdelete"], s:li)
69
70 let s:li=[]
71 new
72 setlocal bufhidden=unload
73 bwipeout
74 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
75
Bram Moolenaare99e8442016-07-26 20:43:40 +020076 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +020077 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +020078endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +020079
80" SEGV occurs in older versions. (At least 7.4.2005 or older)
81function Test_autocmd_bufunload_with_tabnext()
82 tabedit
83 tabfirst
84
85 augroup test_autocmd_bufunload_with_tabnext_group
86 autocmd!
87 autocmd BufUnload <buffer> tabnext
88 augroup END
89
90 quit
91 call assert_equal(2, tabpagenr('$'))
92
Bram Moolenaare0ab94e2016-09-04 19:50:54 +020093 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +020094 augroup! test_autocmd_bufunload_with_tabnext_group
95 tablast
96 quit
97endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +020098
Bram Moolenaarf9e687e2016-09-04 21:33:09 +020099function Test_autocmd_bufwinleave_with_tabfirst()
100 tabedit
101 augroup sample
102 autocmd!
103 autocmd BufWinLeave <buffer> tabfirst
104 augroup END
105 call setline(1, ['a', 'b', 'c'])
106 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200107 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200108endfunc
109
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200110" SEGV occurs in older versions. (At least 7.4.2321 or older)
111function Test_autocmd_bufunload_avoiding_SEGV_01()
112 split aa.txt
113 let lastbuf = bufnr('$')
114
115 augroup test_autocmd_bufunload
116 autocmd!
117 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
118 augroup END
119
120 call assert_fails('edit bb.txt', 'E937:')
121
122 autocmd! test_autocmd_bufunload
123 augroup! test_autocmd_bufunload
124 bwipe! aa.txt
125 bwipe! bb.txt
126endfunc
127
128" SEGV occurs in older versions. (At least 7.4.2321 or older)
129function Test_autocmd_bufunload_avoiding_SEGV_02()
130 setlocal buftype=nowrite
131 let lastbuf = bufnr('$')
132
133 augroup test_autocmd_bufunload
134 autocmd!
135 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
136 augroup END
137
138 normal! i1
139 call assert_fails('edit a.txt', 'E517:')
140 call feedkeys("\<CR>")
141
142 autocmd! test_autocmd_bufunload
143 augroup! test_autocmd_bufunload
144 bwipe! a.txt
145endfunc
146
Bram Moolenaarc917da42016-07-19 22:31:36 +0200147func Test_win_tab_autocmd()
148 let g:record = []
149
150 augroup testing
151 au WinNew * call add(g:record, 'WinNew')
152 au WinEnter * call add(g:record, 'WinEnter')
153 au WinLeave * call add(g:record, 'WinLeave')
154 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200155 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200156 au TabEnter * call add(g:record, 'TabEnter')
157 au TabLeave * call add(g:record, 'TabLeave')
158 augroup END
159
160 split
161 tabnew
162 close
163 close
164
165 call assert_equal([
166 \ 'WinLeave', 'WinNew', 'WinEnter',
167 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200168 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200169 \ 'WinLeave', 'WinEnter'
170 \ ], g:record)
171
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200172 let g:record = []
173 tabnew somefile
174 tabnext
175 bwipe somefile
176
177 call assert_equal([
178 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
179 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
180 \ 'TabClosed'
181 \ ], g:record)
182
Bram Moolenaarc917da42016-07-19 22:31:36 +0200183 augroup testing
184 au!
185 augroup END
186 unlet g:record
187endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200188
189func s:AddAnAutocmd()
190 augroup vimBarTest
191 au BufReadCmd * echo 'hello'
192 augroup END
193 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
194endfunc
195
196func Test_early_bar()
197 " test that a bar is recognized before the {event}
198 call s:AddAnAutocmd()
199 augroup vimBarTest | au! | augroup END
200 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
201
202 call s:AddAnAutocmd()
203 augroup vimBarTest| au!| augroup END
204 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
205
206 " test that a bar is recognized after the {event}
207 call s:AddAnAutocmd()
208 augroup vimBarTest| au!BufReadCmd| augroup END
209 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
210
211 " test that a bar is recognized after the {group}
212 call s:AddAnAutocmd()
213 au! vimBarTest|echo 'hello'
214 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
215endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200216
Bram Moolenaar5c809082016-09-01 16:21:48 +0200217func RemoveGroup()
218 autocmd! StartOK
219 augroup! StartOK
220endfunc
221
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200222func Test_augroup_warning()
223 augroup TheWarning
224 au VimEnter * echo 'entering'
225 augroup END
226 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
227 redir => res
228 augroup! TheWarning
229 redir END
230 call assert_true(match(res, "W19:") >= 0)
231 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
232
233 " check "Another" does not take the pace of the deleted entry
234 augroup Another
235 augroup END
236 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200237 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200238
239 " no warning for postpone aucmd delete
240 augroup StartOK
241 au VimEnter * call RemoveGroup()
242 augroup END
243 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
244 redir => res
245 doautocmd VimEnter
246 redir END
247 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200248 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200249endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200250
251func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200252 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200253 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200254 call assert_fails('augroup! x', 'E936:')
255 au VimEnter * echo
256 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200257 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200258 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
259 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200260endfunc
261
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200262" Tests for autocommands on :close command.
263" This used to be in test13.
264func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200265 " Clean up buffers, because in some cases this function fails.
266 call s:cleanup_buffers()
267
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200268 " Write three files and open them, each in a window.
269 " Then go to next window, with autocommand that deletes the previous one.
270 " Do this twice, writing the file.
271 e! Xtestje1
272 call setline(1, 'testje1')
273 w
274 sp Xtestje2
275 call setline(1, 'testje2')
276 w
277 sp Xtestje3
278 call setline(1, 'testje3')
279 w
280 wincmd w
281 au WinLeave Xtestje2 bwipe
282 wincmd w
283 call assert_equal('Xtestje1', expand('%'))
284
285 au WinLeave Xtestje1 bwipe Xtestje3
286 close
287 call assert_equal('Xtestje1', expand('%'))
288
289 " Test deleting the buffer on a Unload event. If this goes wrong there
290 " will be the ATTENTION prompt.
291 e Xtestje1
292 au!
293 au! BufUnload Xtestje1 bwipe
294 call assert_fails('e Xtestje3', 'E937:')
295 call assert_equal('Xtestje3', expand('%'))
296
297 e Xtestje2
298 sp Xtestje1
299 call assert_fails('e', 'E937:')
300 call assert_equal('Xtestje2', expand('%'))
301
302 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
303 " there are ml_line errors and/or a Crash.
304 au!
305 only
306 e Xanother
307 e Xtestje1
308 bwipe Xtestje2
309 bwipe Xtestje3
310 au BufWipeout Xtestje1 buf Xtestje1
311 bwipe
312 call assert_equal('Xanother', expand('%'))
313
314 only
315 help
316 wincmd w
317 1quit
318 call assert_equal('Xanother', expand('%'))
319
320 au!
321 call delete('Xtestje1')
322 call delete('Xtestje2')
323 call delete('Xtestje3')
324endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100325
326func Test_BufEnter()
327 au! BufEnter
328 au Bufenter * let val = val . '+'
329 let g:val = ''
330 split NewFile
331 call assert_equal('+', g:val)
332 bwipe!
333 call assert_equal('++', g:val)
334
335 " Also get BufEnter when editing a directory
336 call mkdir('Xdir')
337 split Xdir
338 call assert_equal('+++', g:val)
339 bwipe!
340
341 call delete('Xdir', 'd')
342 au! BufEnter
343endfunc