blob: 43aa8d48dd8d8189755f7112a0d2f1eec83e276d [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 Moolenaare0ab94e2016-09-04 19:50:54 +020086" SEGV occurs in older versions. (At least 7.4.2321 or older)
87function Test_autocmd_bufunload_avoiding_SEGV_01()
88 split aa.txt
89 let lastbuf = bufnr('$')
90
91 augroup test_autocmd_bufunload
92 autocmd!
93 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
94 augroup END
95
96 call assert_fails('edit bb.txt', 'E937:')
97
98 autocmd! test_autocmd_bufunload
99 augroup! test_autocmd_bufunload
100 bwipe! aa.txt
101 bwipe! bb.txt
102endfunc
103
104" SEGV occurs in older versions. (At least 7.4.2321 or older)
105function Test_autocmd_bufunload_avoiding_SEGV_02()
106 setlocal buftype=nowrite
107 let lastbuf = bufnr('$')
108
109 augroup test_autocmd_bufunload
110 autocmd!
111 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
112 augroup END
113
114 normal! i1
115 call assert_fails('edit a.txt', 'E517:')
116 call feedkeys("\<CR>")
117
118 autocmd! test_autocmd_bufunload
119 augroup! test_autocmd_bufunload
120 bwipe! a.txt
121endfunc
122
Bram Moolenaarc917da42016-07-19 22:31:36 +0200123func Test_win_tab_autocmd()
124 let g:record = []
125
126 augroup testing
127 au WinNew * call add(g:record, 'WinNew')
128 au WinEnter * call add(g:record, 'WinEnter')
129 au WinLeave * call add(g:record, 'WinLeave')
130 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200131 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200132 au TabEnter * call add(g:record, 'TabEnter')
133 au TabLeave * call add(g:record, 'TabLeave')
134 augroup END
135
136 split
137 tabnew
138 close
139 close
140
141 call assert_equal([
142 \ 'WinLeave', 'WinNew', 'WinEnter',
143 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200144 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200145 \ 'WinLeave', 'WinEnter'
146 \ ], g:record)
147
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200148 let g:record = []
149 tabnew somefile
150 tabnext
151 bwipe somefile
152
153 call assert_equal([
154 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
155 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
156 \ 'TabClosed'
157 \ ], g:record)
158
Bram Moolenaarc917da42016-07-19 22:31:36 +0200159 augroup testing
160 au!
161 augroup END
162 unlet g:record
163endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200164
165func s:AddAnAutocmd()
166 augroup vimBarTest
167 au BufReadCmd * echo 'hello'
168 augroup END
169 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
170endfunc
171
172func Test_early_bar()
173 " test that a bar is recognized before the {event}
174 call s:AddAnAutocmd()
175 augroup vimBarTest | au! | augroup END
176 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
177
178 call s:AddAnAutocmd()
179 augroup vimBarTest| au!| augroup END
180 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
181
182 " test that a bar is recognized after the {event}
183 call s:AddAnAutocmd()
184 augroup vimBarTest| au!BufReadCmd| augroup END
185 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
186
187 " test that a bar is recognized after the {group}
188 call s:AddAnAutocmd()
189 au! vimBarTest|echo 'hello'
190 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
191endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200192
Bram Moolenaar5c809082016-09-01 16:21:48 +0200193func RemoveGroup()
194 autocmd! StartOK
195 augroup! StartOK
196endfunc
197
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200198func Test_augroup_warning()
199 augroup TheWarning
200 au VimEnter * echo 'entering'
201 augroup END
202 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
203 redir => res
204 augroup! TheWarning
205 redir END
206 call assert_true(match(res, "W19:") >= 0)
207 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
208
209 " check "Another" does not take the pace of the deleted entry
210 augroup Another
211 augroup END
212 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaar5c809082016-09-01 16:21:48 +0200213
214 " no warning for postpone aucmd delete
215 augroup StartOK
216 au VimEnter * call RemoveGroup()
217 augroup END
218 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
219 redir => res
220 doautocmd VimEnter
221 redir END
222 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200223 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200224endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200225
226func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200227 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200228 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200229 call assert_fails('augroup! x', 'E936:')
230 au VimEnter * echo
231 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200232 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200233 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
234 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200235endfunc
236
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200237" Tests for autocommands on :close command.
238" This used to be in test13.
239func Test_three_windows()
240 " Write three files and open them, each in a window.
241 " Then go to next window, with autocommand that deletes the previous one.
242 " Do this twice, writing the file.
243 e! Xtestje1
244 call setline(1, 'testje1')
245 w
246 sp Xtestje2
247 call setline(1, 'testje2')
248 w
249 sp Xtestje3
250 call setline(1, 'testje3')
251 w
252 wincmd w
253 au WinLeave Xtestje2 bwipe
254 wincmd w
255 call assert_equal('Xtestje1', expand('%'))
256
257 au WinLeave Xtestje1 bwipe Xtestje3
258 close
259 call assert_equal('Xtestje1', expand('%'))
260
261 " Test deleting the buffer on a Unload event. If this goes wrong there
262 " will be the ATTENTION prompt.
263 e Xtestje1
264 au!
265 au! BufUnload Xtestje1 bwipe
266 call assert_fails('e Xtestje3', 'E937:')
267 call assert_equal('Xtestje3', expand('%'))
268
269 e Xtestje2
270 sp Xtestje1
271 call assert_fails('e', 'E937:')
272 call assert_equal('Xtestje2', expand('%'))
273
274 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
275 " there are ml_line errors and/or a Crash.
276 au!
277 only
278 e Xanother
279 e Xtestje1
280 bwipe Xtestje2
281 bwipe Xtestje3
282 au BufWipeout Xtestje1 buf Xtestje1
283 bwipe
284 call assert_equal('Xanother', expand('%'))
285
286 only
287 help
288 wincmd w
289 1quit
290 call assert_equal('Xanother', expand('%'))
291
292 au!
293 call delete('Xtestje1')
294 call delete('Xtestje2')
295 call delete('Xtestje3')
296endfunc