blob: c029ca1bd6a5306fad6db05454494cd83bc703ee [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" 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
Bram Moolenaar40b1b542016-04-20 20:18:23 +02009
Bram Moolenaarc67e8922016-05-24 16:07:40 +020010if has('timers')
11 func ExitInsertMode(id)
12 call feedkeys("\<Esc>")
13 endfunc
14
15 func Test_cursorhold_insert()
16 let g:triggered = 0
17 au CursorHoldI * let g:triggered += 1
18 set updatetime=20
19 call timer_start(100, 'ExitInsertMode')
20 call feedkeys('a', 'x!')
21 call assert_equal(1, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020022 au! CursorHoldI
Bram Moolenaarc67e8922016-05-24 16:07:40 +020023 endfunc
24
25 func Test_cursorhold_insert_ctrl_x()
26 let g:triggered = 0
27 au CursorHoldI * let g:triggered += 1
28 set updatetime=20
29 call timer_start(100, 'ExitInsertMode')
30 " CursorHoldI does not trigger after CTRL-X
31 call feedkeys("a\<C-X>", 'x!')
32 call assert_equal(0, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020033 au! CursorHoldI
Bram Moolenaarc67e8922016-05-24 16:07:40 +020034 endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020035endif
36
Bram Moolenaarc67e8922016-05-24 16:07:40 +020037function Test_bufunload()
38 augroup test_bufunload_group
39 autocmd!
40 autocmd BufUnload * call add(s:li, "bufunload")
41 autocmd BufDelete * call add(s:li, "bufdelete")
42 autocmd BufWipeout * call add(s:li, "bufwipeout")
43 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +020044
Bram Moolenaarc67e8922016-05-24 16:07:40 +020045 let s:li=[]
46 new
47 setlocal bufhidden=
48 bunload
49 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +020050
Bram Moolenaarc67e8922016-05-24 16:07:40 +020051 let s:li=[]
52 new
53 setlocal bufhidden=delete
54 bunload
55 call assert_equal(["bufunload", "bufdelete"], s:li)
56
57 let s:li=[]
58 new
59 setlocal bufhidden=unload
60 bwipeout
61 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
62
Bram Moolenaare99e8442016-07-26 20:43:40 +020063 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +020065endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +020066
67" SEGV occurs in older versions. (At least 7.4.2005 or older)
68function Test_autocmd_bufunload_with_tabnext()
69 tabedit
70 tabfirst
71
72 augroup test_autocmd_bufunload_with_tabnext_group
73 autocmd!
74 autocmd BufUnload <buffer> tabnext
75 augroup END
76
77 quit
78 call assert_equal(2, tabpagenr('$'))
79
Bram Moolenaare0ab94e2016-09-04 19:50:54 +020080 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +020081 augroup! test_autocmd_bufunload_with_tabnext_group
82 tablast
83 quit
84endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +020085
Bram Moolenaarf9e687e2016-09-04 21:33:09 +020086function Test_autocmd_bufwinleave_with_tabfirst()
87 tabedit
88 augroup sample
89 autocmd!
90 autocmd BufWinLeave <buffer> tabfirst
91 augroup END
92 call setline(1, ['a', 'b', 'c'])
93 edit! a.txt
94endfunc
95
Bram Moolenaare0ab94e2016-09-04 19:50:54 +020096" SEGV occurs in older versions. (At least 7.4.2321 or older)
97function Test_autocmd_bufunload_avoiding_SEGV_01()
98 split aa.txt
99 let lastbuf = bufnr('$')
100
101 augroup test_autocmd_bufunload
102 autocmd!
103 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
104 augroup END
105
106 call assert_fails('edit bb.txt', 'E937:')
107
108 autocmd! test_autocmd_bufunload
109 augroup! test_autocmd_bufunload
110 bwipe! aa.txt
111 bwipe! bb.txt
112endfunc
113
114" SEGV occurs in older versions. (At least 7.4.2321 or older)
115function Test_autocmd_bufunload_avoiding_SEGV_02()
116 setlocal buftype=nowrite
117 let lastbuf = bufnr('$')
118
119 augroup test_autocmd_bufunload
120 autocmd!
121 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
122 augroup END
123
124 normal! i1
125 call assert_fails('edit a.txt', 'E517:')
126 call feedkeys("\<CR>")
127
128 autocmd! test_autocmd_bufunload
129 augroup! test_autocmd_bufunload
130 bwipe! a.txt
131endfunc
132
Bram Moolenaarc917da42016-07-19 22:31:36 +0200133func Test_win_tab_autocmd()
134 let g:record = []
135
136 augroup testing
137 au WinNew * call add(g:record, 'WinNew')
138 au WinEnter * call add(g:record, 'WinEnter')
139 au WinLeave * call add(g:record, 'WinLeave')
140 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200141 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200142 au TabEnter * call add(g:record, 'TabEnter')
143 au TabLeave * call add(g:record, 'TabLeave')
144 augroup END
145
146 split
147 tabnew
148 close
149 close
150
151 call assert_equal([
152 \ 'WinLeave', 'WinNew', 'WinEnter',
153 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200154 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200155 \ 'WinLeave', 'WinEnter'
156 \ ], g:record)
157
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200158 let g:record = []
159 tabnew somefile
160 tabnext
161 bwipe somefile
162
163 call assert_equal([
164 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
165 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
166 \ 'TabClosed'
167 \ ], g:record)
168
Bram Moolenaarc917da42016-07-19 22:31:36 +0200169 augroup testing
170 au!
171 augroup END
172 unlet g:record
173endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200174
175func s:AddAnAutocmd()
176 augroup vimBarTest
177 au BufReadCmd * echo 'hello'
178 augroup END
179 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
180endfunc
181
182func Test_early_bar()
183 " test that a bar is recognized before the {event}
184 call s:AddAnAutocmd()
185 augroup vimBarTest | au! | augroup END
186 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
187
188 call s:AddAnAutocmd()
189 augroup vimBarTest| au!| augroup END
190 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
191
192 " test that a bar is recognized after the {event}
193 call s:AddAnAutocmd()
194 augroup vimBarTest| au!BufReadCmd| augroup END
195 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
196
197 " test that a bar is recognized after the {group}
198 call s:AddAnAutocmd()
199 au! vimBarTest|echo 'hello'
200 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
201endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200202
Bram Moolenaar5c809082016-09-01 16:21:48 +0200203func RemoveGroup()
204 autocmd! StartOK
205 augroup! StartOK
206endfunc
207
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200208func Test_augroup_warning()
209 augroup TheWarning
210 au VimEnter * echo 'entering'
211 augroup END
212 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
213 redir => res
214 augroup! TheWarning
215 redir END
216 call assert_true(match(res, "W19:") >= 0)
217 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
218
219 " check "Another" does not take the pace of the deleted entry
220 augroup Another
221 augroup END
222 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaar5c809082016-09-01 16:21:48 +0200223
224 " no warning for postpone aucmd delete
225 augroup StartOK
226 au VimEnter * call RemoveGroup()
227 augroup END
228 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
229 redir => res
230 doautocmd VimEnter
231 redir END
232 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200233 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200234endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200235
236func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200237 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200238 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200239 call assert_fails('augroup! x', 'E936:')
240 au VimEnter * echo
241 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200242 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200243 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
244 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200245endfunc
246
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200247" Tests for autocommands on :close command.
248" This used to be in test13.
249func Test_three_windows()
250 " Write three files and open them, each in a window.
251 " Then go to next window, with autocommand that deletes the previous one.
252 " Do this twice, writing the file.
253 e! Xtestje1
254 call setline(1, 'testje1')
255 w
256 sp Xtestje2
257 call setline(1, 'testje2')
258 w
259 sp Xtestje3
260 call setline(1, 'testje3')
261 w
262 wincmd w
263 au WinLeave Xtestje2 bwipe
264 wincmd w
265 call assert_equal('Xtestje1', expand('%'))
266
267 au WinLeave Xtestje1 bwipe Xtestje3
268 close
269 call assert_equal('Xtestje1', expand('%'))
270
271 " Test deleting the buffer on a Unload event. If this goes wrong there
272 " will be the ATTENTION prompt.
273 e Xtestje1
274 au!
275 au! BufUnload Xtestje1 bwipe
276 call assert_fails('e Xtestje3', 'E937:')
277 call assert_equal('Xtestje3', expand('%'))
278
279 e Xtestje2
280 sp Xtestje1
281 call assert_fails('e', 'E937:')
282 call assert_equal('Xtestje2', expand('%'))
283
284 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
285 " there are ml_line errors and/or a Crash.
286 au!
287 only
288 e Xanother
289 e Xtestje1
290 bwipe Xtestje2
291 bwipe Xtestje3
292 au BufWipeout Xtestje1 buf Xtestje1
293 bwipe
294 call assert_equal('Xanother', expand('%'))
295
296 only
297 help
298 wincmd w
299 1quit
300 call assert_equal('Xanother', expand('%'))
301
302 au!
303 call delete('Xtestje1')
304 call delete('Xtestje2')
305 call delete('Xtestje3')
306endfunc