blob: 85233797d39ae435ca9edec520d69eaa412d94a1 [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 Moolenaaraeac9002016-09-06 22:15:08 +020023 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020024 endfunc
25
26 func Test_cursorhold_insert_ctrl_x()
27 let g:triggered = 0
28 au CursorHoldI * let g:triggered += 1
29 set updatetime=20
30 call timer_start(100, 'ExitInsertMode')
31 " CursorHoldI does not trigger after CTRL-X
32 call feedkeys("a\<C-X>", 'x!')
33 call assert_equal(0, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020034 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020035 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020036 endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020037endif
38
Bram Moolenaarc67e8922016-05-24 16:07:40 +020039function Test_bufunload()
40 augroup test_bufunload_group
41 autocmd!
42 autocmd BufUnload * call add(s:li, "bufunload")
43 autocmd BufDelete * call add(s:li, "bufdelete")
44 autocmd BufWipeout * call add(s:li, "bufwipeout")
45 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +020046
Bram Moolenaarc67e8922016-05-24 16:07:40 +020047 let s:li=[]
48 new
49 setlocal bufhidden=
50 bunload
51 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +020052
Bram Moolenaarc67e8922016-05-24 16:07:40 +020053 let s:li=[]
54 new
55 setlocal bufhidden=delete
56 bunload
57 call assert_equal(["bufunload", "bufdelete"], s:li)
58
59 let s:li=[]
60 new
61 setlocal bufhidden=unload
62 bwipeout
63 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
64
Bram Moolenaare99e8442016-07-26 20:43:40 +020065 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +020067endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +020068
69" SEGV occurs in older versions. (At least 7.4.2005 or older)
70function Test_autocmd_bufunload_with_tabnext()
71 tabedit
72 tabfirst
73
74 augroup test_autocmd_bufunload_with_tabnext_group
75 autocmd!
76 autocmd BufUnload <buffer> tabnext
77 augroup END
78
79 quit
80 call assert_equal(2, tabpagenr('$'))
81
Bram Moolenaare0ab94e2016-09-04 19:50:54 +020082 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +020083 augroup! test_autocmd_bufunload_with_tabnext_group
84 tablast
85 quit
86endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +020087
Bram Moolenaarf9e687e2016-09-04 21:33:09 +020088function Test_autocmd_bufwinleave_with_tabfirst()
89 tabedit
90 augroup sample
91 autocmd!
92 autocmd BufWinLeave <buffer> tabfirst
93 augroup END
94 call setline(1, ['a', 'b', 'c'])
95 edit! a.txt
96endfunc
97
Bram Moolenaare0ab94e2016-09-04 19:50:54 +020098" SEGV occurs in older versions. (At least 7.4.2321 or older)
99function Test_autocmd_bufunload_avoiding_SEGV_01()
100 split aa.txt
101 let lastbuf = bufnr('$')
102
103 augroup test_autocmd_bufunload
104 autocmd!
105 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
106 augroup END
107
108 call assert_fails('edit bb.txt', 'E937:')
109
110 autocmd! test_autocmd_bufunload
111 augroup! test_autocmd_bufunload
112 bwipe! aa.txt
113 bwipe! bb.txt
114endfunc
115
116" SEGV occurs in older versions. (At least 7.4.2321 or older)
117function Test_autocmd_bufunload_avoiding_SEGV_02()
118 setlocal buftype=nowrite
119 let lastbuf = bufnr('$')
120
121 augroup test_autocmd_bufunload
122 autocmd!
123 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
124 augroup END
125
126 normal! i1
127 call assert_fails('edit a.txt', 'E517:')
128 call feedkeys("\<CR>")
129
130 autocmd! test_autocmd_bufunload
131 augroup! test_autocmd_bufunload
132 bwipe! a.txt
133endfunc
134
Bram Moolenaarc917da42016-07-19 22:31:36 +0200135func Test_win_tab_autocmd()
136 let g:record = []
137
138 augroup testing
139 au WinNew * call add(g:record, 'WinNew')
140 au WinEnter * call add(g:record, 'WinEnter')
141 au WinLeave * call add(g:record, 'WinLeave')
142 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200143 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200144 au TabEnter * call add(g:record, 'TabEnter')
145 au TabLeave * call add(g:record, 'TabLeave')
146 augroup END
147
148 split
149 tabnew
150 close
151 close
152
153 call assert_equal([
154 \ 'WinLeave', 'WinNew', 'WinEnter',
155 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200156 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200157 \ 'WinLeave', 'WinEnter'
158 \ ], g:record)
159
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200160 let g:record = []
161 tabnew somefile
162 tabnext
163 bwipe somefile
164
165 call assert_equal([
166 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
167 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
168 \ 'TabClosed'
169 \ ], g:record)
170
Bram Moolenaarc917da42016-07-19 22:31:36 +0200171 augroup testing
172 au!
173 augroup END
174 unlet g:record
175endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200176
177func s:AddAnAutocmd()
178 augroup vimBarTest
179 au BufReadCmd * echo 'hello'
180 augroup END
181 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
182endfunc
183
184func Test_early_bar()
185 " test that a bar is recognized before the {event}
186 call s:AddAnAutocmd()
187 augroup vimBarTest | au! | augroup END
188 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
189
190 call s:AddAnAutocmd()
191 augroup vimBarTest| au!| augroup END
192 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
193
194 " test that a bar is recognized after the {event}
195 call s:AddAnAutocmd()
196 augroup vimBarTest| au!BufReadCmd| augroup END
197 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
198
199 " test that a bar is recognized after the {group}
200 call s:AddAnAutocmd()
201 au! vimBarTest|echo 'hello'
202 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
203endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200204
Bram Moolenaar5c809082016-09-01 16:21:48 +0200205func RemoveGroup()
206 autocmd! StartOK
207 augroup! StartOK
208endfunc
209
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200210func Test_augroup_warning()
211 augroup TheWarning
212 au VimEnter * echo 'entering'
213 augroup END
214 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
215 redir => res
216 augroup! TheWarning
217 redir END
218 call assert_true(match(res, "W19:") >= 0)
219 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
220
221 " check "Another" does not take the pace of the deleted entry
222 augroup Another
223 augroup END
224 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200225 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200226
227 " no warning for postpone aucmd delete
228 augroup StartOK
229 au VimEnter * call RemoveGroup()
230 augroup END
231 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
232 redir => res
233 doautocmd VimEnter
234 redir END
235 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200236 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200237endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200238
239func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200240 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200241 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200242 call assert_fails('augroup! x', 'E936:')
243 au VimEnter * echo
244 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200245 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200246 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
247 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200248endfunc
249
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200250" Tests for autocommands on :close command.
251" This used to be in test13.
252func Test_three_windows()
253 " Write three files and open them, each in a window.
254 " Then go to next window, with autocommand that deletes the previous one.
255 " Do this twice, writing the file.
256 e! Xtestje1
257 call setline(1, 'testje1')
258 w
259 sp Xtestje2
260 call setline(1, 'testje2')
261 w
262 sp Xtestje3
263 call setline(1, 'testje3')
264 w
265 wincmd w
266 au WinLeave Xtestje2 bwipe
267 wincmd w
268 call assert_equal('Xtestje1', expand('%'))
269
270 au WinLeave Xtestje1 bwipe Xtestje3
271 close
272 call assert_equal('Xtestje1', expand('%'))
273
274 " Test deleting the buffer on a Unload event. If this goes wrong there
275 " will be the ATTENTION prompt.
276 e Xtestje1
277 au!
278 au! BufUnload Xtestje1 bwipe
279 call assert_fails('e Xtestje3', 'E937:')
280 call assert_equal('Xtestje3', expand('%'))
281
282 e Xtestje2
283 sp Xtestje1
284 call assert_fails('e', 'E937:')
285 call assert_equal('Xtestje2', expand('%'))
286
287 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
288 " there are ml_line errors and/or a Crash.
289 au!
290 only
291 e Xanother
292 e Xtestje1
293 bwipe Xtestje2
294 bwipe Xtestje3
295 au BufWipeout Xtestje1 buf Xtestje1
296 bwipe
297 call assert_equal('Xanother', expand('%'))
298
299 only
300 help
301 wincmd w
302 1quit
303 call assert_equal('Xanother', expand('%'))
304
305 au!
306 call delete('Xtestje1')
307 call delete('Xtestje2')
308 call delete('Xtestje3')
309endfunc