blob: 5402fc665e44e354792919791623099816735d72 [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()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020016 " Need to move the cursor.
17 call feedkeys("ggG", "xt")
18
Bram Moolenaarc67e8922016-05-24 16:07:40 +020019 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)
Bram Moolenaare99e8442016-07-26 20:43:40 +020025 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020026 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020027 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)
Bram Moolenaare99e8442016-07-26 20:43:40 +020037 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020038 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020039 endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020040endif
41
Bram Moolenaarc67e8922016-05-24 16:07:40 +020042function 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
Bram Moolenaar40b1b542016-04-20 20:18:23 +020049
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050 let s:li=[]
51 new
52 setlocal bufhidden=
53 bunload
54 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +020055
Bram Moolenaarc67e8922016-05-24 16:07:40 +020056 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
Bram Moolenaare99e8442016-07-26 20:43:40 +020068 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +020069 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +020070endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +020071
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
Bram Moolenaare0ab94e2016-09-04 19:50:54 +020085 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +020086 augroup! test_autocmd_bufunload_with_tabnext_group
87 tablast
88 quit
89endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +020090
Bram Moolenaarf9e687e2016-09-04 21:33:09 +020091function 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
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020099 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200100endfunc
101
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200102" 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
Bram Moolenaarc917da42016-07-19 22:31:36 +0200139func 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')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200147 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200148 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',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200160 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200161 \ 'WinLeave', 'WinEnter'
162 \ ], g:record)
163
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200164 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
Bram Moolenaarc917da42016-07-19 22:31:36 +0200175 augroup testing
176 au!
177 augroup END
178 unlet g:record
179endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200180
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
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200208
Bram Moolenaar5c809082016-09-01 16:21:48 +0200209func RemoveGroup()
210 autocmd! StartOK
211 augroup! StartOK
212endfunc
213
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200214func 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)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200229 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200230
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)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200240 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200241endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200242
243func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200244 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200245 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200246 call assert_fails('augroup! x', 'E936:')
247 au VimEnter * echo
248 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200249 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200250 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
251 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200252endfunc
253
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200254" 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