blob: 2d7956f45d1a3a18b18edc0d215dc2c66b63bbe8 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
4
Bram Moolenaar04f62f82017-07-19 18:18:39 +02005func! s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02006 for bnr in range(1, bufnr('$'))
7 if bufloaded(bnr) && bufnr('%') != bnr
8 execute 'bd! ' . bnr
9 endif
10 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020011endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020012
Bram Moolenaar14735512016-03-26 21:00:08 +010013func Test_vim_did_enter()
14 call assert_false(v:vim_did_enter)
15
16 " This script will never reach the main loop, can't check if v:vim_did_enter
17 " becomes one.
18endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020019
Bram Moolenaarc67e8922016-05-24 16:07:40 +020020if has('timers')
21 func ExitInsertMode(id)
22 call feedkeys("\<Esc>")
23 endfunc
24
25 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020026 " Need to move the cursor.
27 call feedkeys("ggG", "xt")
28
Bram Moolenaarc67e8922016-05-24 16:07:40 +020029 let g:triggered = 0
30 au CursorHoldI * let g:triggered += 1
31 set updatetime=20
32 call timer_start(100, 'ExitInsertMode')
33 call feedkeys('a', 'x!')
34 call assert_equal(1, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020035 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020036 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020037 endfunc
38
39 func Test_cursorhold_insert_ctrl_x()
40 let g:triggered = 0
41 au CursorHoldI * let g:triggered += 1
42 set updatetime=20
43 call timer_start(100, 'ExitInsertMode')
44 " CursorHoldI does not trigger after CTRL-X
45 call feedkeys("a\<C-X>", 'x!')
46 call assert_equal(0, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020047 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020048 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020049 endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020050endif
51
Bram Moolenaar04f62f82017-07-19 18:18:39 +020052func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +020053 augroup test_bufunload_group
54 autocmd!
55 autocmd BufUnload * call add(s:li, "bufunload")
56 autocmd BufDelete * call add(s:li, "bufdelete")
57 autocmd BufWipeout * call add(s:li, "bufwipeout")
58 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +020059
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let s:li=[]
61 new
62 setlocal bufhidden=
63 bunload
64 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +020065
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 let s:li=[]
67 new
68 setlocal bufhidden=delete
69 bunload
70 call assert_equal(["bufunload", "bufdelete"], s:li)
71
72 let s:li=[]
73 new
74 setlocal bufhidden=unload
75 bwipeout
76 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
77
Bram Moolenaare99e8442016-07-26 20:43:40 +020078 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +020079 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +020080endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +020081
82" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +020083func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +020084 tabedit
85 tabfirst
86
87 augroup test_autocmd_bufunload_with_tabnext_group
88 autocmd!
89 autocmd BufUnload <buffer> tabnext
90 augroup END
91
92 quit
93 call assert_equal(2, tabpagenr('$'))
94
Bram Moolenaare0ab94e2016-09-04 19:50:54 +020095 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +020096 augroup! test_autocmd_bufunload_with_tabnext_group
97 tablast
98 quit
99endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200100
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200101func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200102 tabedit
103 augroup sample
104 autocmd!
105 autocmd BufWinLeave <buffer> tabfirst
106 augroup END
107 call setline(1, ['a', 'b', 'c'])
108 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200109 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200110endfunc
111
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200112" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200113func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200114 split aa.txt
115 let lastbuf = bufnr('$')
116
117 augroup test_autocmd_bufunload
118 autocmd!
119 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
120 augroup END
121
Bram Moolenaara997b452018-04-17 23:24:06 +0200122 " Todo: check for E937 generated first
123 " call assert_fails('edit bb.txt', 'E937:')
124 call assert_fails('edit bb.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200125
126 autocmd! test_autocmd_bufunload
127 augroup! test_autocmd_bufunload
128 bwipe! aa.txt
129 bwipe! bb.txt
130endfunc
131
132" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200133func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200134 setlocal buftype=nowrite
135 let lastbuf = bufnr('$')
136
137 augroup test_autocmd_bufunload
138 autocmd!
139 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
140 augroup END
141
142 normal! i1
143 call assert_fails('edit a.txt', 'E517:')
144 call feedkeys("\<CR>")
145
146 autocmd! test_autocmd_bufunload
147 augroup! test_autocmd_bufunload
148 bwipe! a.txt
149endfunc
150
Bram Moolenaarc917da42016-07-19 22:31:36 +0200151func Test_win_tab_autocmd()
152 let g:record = []
153
154 augroup testing
155 au WinNew * call add(g:record, 'WinNew')
156 au WinEnter * call add(g:record, 'WinEnter')
157 au WinLeave * call add(g:record, 'WinLeave')
158 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200159 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200160 au TabEnter * call add(g:record, 'TabEnter')
161 au TabLeave * call add(g:record, 'TabLeave')
162 augroup END
163
164 split
165 tabnew
166 close
167 close
168
169 call assert_equal([
170 \ 'WinLeave', 'WinNew', 'WinEnter',
171 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200172 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200173 \ 'WinLeave', 'WinEnter'
174 \ ], g:record)
175
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200176 let g:record = []
177 tabnew somefile
178 tabnext
179 bwipe somefile
180
181 call assert_equal([
182 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
183 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
184 \ 'TabClosed'
185 \ ], g:record)
186
Bram Moolenaarc917da42016-07-19 22:31:36 +0200187 augroup testing
188 au!
189 augroup END
190 unlet g:record
191endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200192
193func s:AddAnAutocmd()
194 augroup vimBarTest
195 au BufReadCmd * echo 'hello'
196 augroup END
197 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
198endfunc
199
200func Test_early_bar()
201 " test that a bar is recognized before the {event}
202 call s:AddAnAutocmd()
203 augroup vimBarTest | au! | augroup END
204 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
205
206 call s:AddAnAutocmd()
207 augroup vimBarTest| au!| augroup END
208 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
209
210 " test that a bar is recognized after the {event}
211 call s:AddAnAutocmd()
212 augroup vimBarTest| au!BufReadCmd| augroup END
213 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
214
215 " test that a bar is recognized after the {group}
216 call s:AddAnAutocmd()
217 au! vimBarTest|echo 'hello'
218 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
219endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200220
Bram Moolenaar5c809082016-09-01 16:21:48 +0200221func RemoveGroup()
222 autocmd! StartOK
223 augroup! StartOK
224endfunc
225
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200226func Test_augroup_warning()
227 augroup TheWarning
228 au VimEnter * echo 'entering'
229 augroup END
230 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
231 redir => res
232 augroup! TheWarning
233 redir END
234 call assert_true(match(res, "W19:") >= 0)
235 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
236
237 " check "Another" does not take the pace of the deleted entry
238 augroup Another
239 augroup END
240 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200241 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200242
243 " no warning for postpone aucmd delete
244 augroup StartOK
245 au VimEnter * call RemoveGroup()
246 augroup END
247 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
248 redir => res
249 doautocmd VimEnter
250 redir END
251 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200252 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200253endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200254
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200255func Test_BufReadCmdHelp()
256 " This used to cause access to free memory
257 au BufReadCmd * e +h
258 help
259
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200260 au! BufReadCmd
261endfunc
262
263func Test_BufReadCmdHelpJump()
264 " This used to cause access to free memory
265 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200266 " } to fix highlighting
267 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200268
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200269 au! BufReadCmd
270endfunc
271
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200272func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200273 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200274 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200275 call assert_fails('augroup! x', 'E936:')
276 au VimEnter * echo
277 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200278 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200279 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
280 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200281endfunc
282
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200283" Tests for autocommands on :close command.
284" This used to be in test13.
285func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200286 " Clean up buffers, because in some cases this function fails.
287 call s:cleanup_buffers()
288
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200289 " Write three files and open them, each in a window.
290 " Then go to next window, with autocommand that deletes the previous one.
291 " Do this twice, writing the file.
292 e! Xtestje1
293 call setline(1, 'testje1')
294 w
295 sp Xtestje2
296 call setline(1, 'testje2')
297 w
298 sp Xtestje3
299 call setline(1, 'testje3')
300 w
301 wincmd w
302 au WinLeave Xtestje2 bwipe
303 wincmd w
304 call assert_equal('Xtestje1', expand('%'))
305
306 au WinLeave Xtestje1 bwipe Xtestje3
307 close
308 call assert_equal('Xtestje1', expand('%'))
309
310 " Test deleting the buffer on a Unload event. If this goes wrong there
311 " will be the ATTENTION prompt.
312 e Xtestje1
313 au!
314 au! BufUnload Xtestje1 bwipe
315 call assert_fails('e Xtestje3', 'E937:')
316 call assert_equal('Xtestje3', expand('%'))
317
318 e Xtestje2
319 sp Xtestje1
320 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200321 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200322
323 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
324 " there are ml_line errors and/or a Crash.
325 au!
326 only
327 e Xanother
328 e Xtestje1
329 bwipe Xtestje2
330 bwipe Xtestje3
331 au BufWipeout Xtestje1 buf Xtestje1
332 bwipe
333 call assert_equal('Xanother', expand('%'))
334
335 only
336 help
337 wincmd w
338 1quit
339 call assert_equal('Xanother', expand('%'))
340
341 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100342 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200343 call delete('Xtestje1')
344 call delete('Xtestje2')
345 call delete('Xtestje3')
346endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100347
348func Test_BufEnter()
349 au! BufEnter
350 au Bufenter * let val = val . '+'
351 let g:val = ''
352 split NewFile
353 call assert_equal('+', g:val)
354 bwipe!
355 call assert_equal('++', g:val)
356
357 " Also get BufEnter when editing a directory
358 call mkdir('Xdir')
359 split Xdir
360 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100361
362 " On MS-Windows we can't edit the directory, make sure we wipe the right
363 " buffer.
364 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100365
366 call delete('Xdir', 'd')
367 au! BufEnter
368endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100369
370" Closing a window might cause an endless loop
371" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200372func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200373 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100374 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200375 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100376 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100377 mksession!
378
Bram Moolenaare94260f2017-03-21 15:50:12 +0100379 let content = ['set nocp noswapfile',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100380 \ 'let v:swapchoice="e"',
381 \ 'augroup test_autocmd_sessionload',
382 \ 'autocmd!',
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200383 \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100384 \ 'augroup END',
385 \ '',
386 \ 'func WriteErrors()',
387 \ ' call writefile([execute("messages")], "Xerrors")',
388 \ 'endfunc',
389 \ 'au VimLeave * call WriteErrors()',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100390 \ ]
391 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100392 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
393 let errors = join(readfile('Xerrors'))
394 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100395
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100396 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100397 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100398 call delete(file)
399 endfor
400endfunc
401
402" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200403func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100404 tabnew
405 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100406 mksession!
407
408 let content = ['set nocp noswapfile',
409 \ 'function! DeleteInactiveBufs()',
410 \ ' tabfirst',
411 \ ' let tabblist = []',
412 \ ' for i in range(1, tabpagenr(''$''))',
413 \ ' call extend(tabblist, tabpagebuflist(i))',
414 \ ' endfor',
415 \ ' for b in range(1, bufnr(''$''))',
416 \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')',
417 \ ' exec ''bwipeout '' . b',
418 \ ' endif',
419 \ ' endfor',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100420 \ ' echomsg "SessionLoadPost DONE"',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100421 \ 'endfunction',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100422 \ 'au SessionLoadPost * call DeleteInactiveBufs()',
423 \ '',
424 \ 'func WriteErrors()',
425 \ ' call writefile([execute("messages")], "Xerrors")',
426 \ 'endfunc',
427 \ 'au VimLeave * call WriteErrors()',
428 \ ]
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100429 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100430 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
431 let errors = join(readfile('Xerrors'))
432 " This probably only ever matches on unix.
433 call assert_notmatch('Caught deadly signal SEGV', errors)
434 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100435
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100436 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100437 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100438 call delete(file)
439 endfor
440endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200441
442func Test_empty_doau()
443 doau \|
444endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200445
446func s:AutoCommandOptionSet(match)
447 let item = remove(g:options, 0)
448 let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3])
449 let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type)
450 let g:opt = [expected, actual]
451 "call assert_equal(expected, actual)
452endfunc
453
454func Test_OptionSet()
455 if !has("eval") || !has("autocmd") || !exists("+autochdir")
456 return
457 endif
458
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200459 badd test_autocmd.vim
460
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200461 call test_override('starting', 1)
462 set nocp
463 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
464
465 " 1: Setting number option"
466 let g:options=[['number', 0, 1, 'global']]
467 set nu
468 call assert_equal([], g:options)
469 call assert_equal(g:opt[0], g:opt[1])
470
471 " 2: Setting local number option"
472 let g:options=[['number', 1, 0, 'local']]
473 setlocal nonu
474 call assert_equal([], g:options)
475 call assert_equal(g:opt[0], g:opt[1])
476
477 " 3: Setting global number option"
478 let g:options=[['number', 1, 0, 'global']]
479 setglobal nonu
480 call assert_equal([], g:options)
481 call assert_equal(g:opt[0], g:opt[1])
482
483 " 4: Setting local autoindent option"
484 let g:options=[['autoindent', 0, 1, 'local']]
485 setlocal ai
486 call assert_equal([], g:options)
487 call assert_equal(g:opt[0], g:opt[1])
488
489 " 5: Setting global autoindent option"
490 let g:options=[['autoindent', 0, 1, 'global']]
491 setglobal ai
492 call assert_equal([], g:options)
493 call assert_equal(g:opt[0], g:opt[1])
494
495 " 6: Setting global autoindent option"
496 let g:options=[['autoindent', 1, 0, 'global']]
497 set ai!
498 call assert_equal([], g:options)
499 call assert_equal(g:opt[0], g:opt[1])
500
501 " Should not print anything, use :noa
502 " 7: don't trigger OptionSet"
503 let g:options=[['invalid', 1, 1, 'invalid']]
504 noa set nonu
505 call assert_equal([['invalid', 1, 1, 'invalid']], g:options)
506 call assert_equal(g:opt[0], g:opt[1])
507
508 " 8: Setting several global list and number option"
509 let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']]
510 set list nu
511 call assert_equal([], g:options)
512 call assert_equal(g:opt[0], g:opt[1])
513
514 " 9: don't trigger OptionSet"
515 let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']]
516 noa set nolist nonu
517 call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options)
518 call assert_equal(g:opt[0], g:opt[1])
519
520 " 10: Setting global acd"
521 let g:options=[['autochdir', 0, 1, 'local']]
522 setlocal acd
523 call assert_equal([], g:options)
524 call assert_equal(g:opt[0], g:opt[1])
525
526 " 11: Setting global autoread (also sets local value)"
527 let g:options=[['autoread', 0, 1, 'global']]
528 set ar
529 call assert_equal([], g:options)
530 call assert_equal(g:opt[0], g:opt[1])
531
532 " 12: Setting local autoread"
533 let g:options=[['autoread', 1, 1, 'local']]
534 setlocal ar
535 call assert_equal([], g:options)
536 call assert_equal(g:opt[0], g:opt[1])
537
538 " 13: Setting global autoread"
539 let g:options=[['autoread', 1, 0, 'global']]
540 setglobal invar
541 call assert_equal([], g:options)
542 call assert_equal(g:opt[0], g:opt[1])
543
544 " 14: Setting option backspace through :let"
545 let g:options=[['backspace', '', 'eol,indent,start', 'global']]
546 let &bs="eol,indent,start"
547 call assert_equal([], g:options)
548 call assert_equal(g:opt[0], g:opt[1])
549
550 " 15: Setting option backspace through setbufvar()"
551 let g:options=[['backup', 0, 1, 'local']]
552 " try twice, first time, shouldn't trigger because option name is invalid,
553 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200554 let bnum = bufnr('%')
555 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200556 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200557 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200558 call assert_equal([], g:options)
559 call assert_equal(g:opt[0], g:opt[1])
560
561 " 16: Setting number option using setwinvar"
562 let g:options=[['number', 0, 1, 'local']]
563 call setwinvar(0, '&number', 1)
564 call assert_equal([], g:options)
565 call assert_equal(g:opt[0], g:opt[1])
566
567 " 17: Setting key option, shouldn't trigger"
568 let g:options=[['key', 'invalid', 'invalid1', 'invalid']]
569 setlocal key=blah
570 setlocal key=
571 call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options)
572 call assert_equal(g:opt[0], g:opt[1])
573
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200574 " 18: Setting string option"
575 let oldval = &tags
576 let g:options=[['tags', oldval, 'tagpath', 'global']]
577 set tags=tagpath
578 call assert_equal([], g:options)
579 call assert_equal(g:opt[0], g:opt[1])
580
581 " 1l: Resetting string option"
582 let g:options=[['tags', 'tagpath', oldval, 'global']]
583 set tags&
584 call assert_equal([], g:options)
585 call assert_equal(g:opt[0], g:opt[1])
586
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200587 " Cleanup
588 au! OptionSet
589 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp']
590 exe printf(":set %s&vi", opt)
591 endfor
592 call test_override('starting', 0)
593 delfunc! AutoCommandOptionSet
594endfunc
595
596func Test_OptionSet_diffmode()
597 call test_override('starting', 1)
598 " 18: Changing an option when enetering diff mode
599 new
600 au OptionSet diff :let &l:cul=v:option_new
601
602 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
603 call assert_equal(0, &l:cul)
604 diffthis
605 call assert_equal(1, &l:cul)
606
607 vnew
608 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
609 call assert_equal(0, &l:cul)
610 diffthis
611 call assert_equal(1, &l:cul)
612
613 diffoff
614 call assert_equal(0, &l:cul)
615 call assert_equal(1, getwinvar(2, '&l:cul'))
616 bw!
617
618 call assert_equal(1, &l:cul)
619 diffoff!
620 call assert_equal(0, &l:cul)
621 call assert_equal(0, getwinvar(1, '&l:cul'))
622 bw!
623
624 " Cleanup
625 au! OptionSet
626 call test_override('starting', 0)
627endfunc
628
629func Test_OptionSet_diffmode_close()
630 call test_override('starting', 1)
631 " 19: Try to close the current window when entering diff mode
632 " should not segfault
633 new
634 au OptionSet diff close
635
636 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
637 call assert_fails(':diffthis', 'E788')
638 call assert_equal(1, &diff)
639 vnew
640 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
641 call assert_fails(':diffthis', 'E788')
642 call assert_equal(1, &diff)
643 bw!
644 call assert_fails(':diffoff!', 'E788')
645 bw!
646
647 " Cleanup
648 au! OptionSet
649 call test_override('starting', 0)
650 "delfunc! AutoCommandOptionSet
651endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200652
653" Test for Bufleave autocommand that deletes the buffer we are about to edit.
654func Test_BufleaveWithDelete()
655 new | edit Xfile1
656
657 augroup test_bufleavewithdelete
658 autocmd!
659 autocmd BufLeave Xfile1 bwipe Xfile2
660 augroup END
661
662 call assert_fails('edit Xfile2', 'E143:')
663 call assert_equal('Xfile1', bufname('%'))
664
665 autocmd! test_bufleavewithdelete BufLeave Xfile1
666 augroup! test_bufleavewithdelete
667
668 new
669 bwipe! Xfile1
670endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200671
672" Test for autocommand that changes the buffer list, when doing ":ball".
673func Test_Acmd_BufAll()
674 enew!
675 %bwipe!
676 call writefile(['Test file Xxx1'], 'Xxx1')
677 call writefile(['Test file Xxx2'], 'Xxx2')
678 call writefile(['Test file Xxx3'], 'Xxx3')
679
680 " Add three files to the buffer list
681 split Xxx1
682 close
683 split Xxx2
684 close
685 split Xxx3
686 close
687
688 " Wipe the buffer when the buffer is opened
689 au BufReadPost Xxx2 bwipe
690
691 call append(0, 'Test file Xxx4')
692 ball
693
694 call assert_equal(2, winnr('$'))
695 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
696 wincmd t
697
698 au! BufReadPost
699 %bwipe!
700 call delete('Xxx1')
701 call delete('Xxx2')
702 call delete('Xxx3')
703 enew! | only
704endfunc
705
706" Test for autocommand that changes current buffer on BufEnter event.
707" Check if modelines are interpreted for the correct buffer.
708func Test_Acmd_BufEnter()
709 %bwipe!
710 call writefile(['start of test file Xxx1',
711 \ "\<Tab>this is a test",
712 \ 'end of test file Xxx1'], 'Xxx1')
713 call writefile(['start of test file Xxx2',
714 \ 'vim: set noai :',
715 \ "\<Tab>this is a test",
716 \ 'end of test file Xxx2'], 'Xxx2')
717
718 au BufEnter Xxx2 brew
719 set ai modeline modelines=3
720 edit Xxx1
721 " edit Xxx2, autocmd will do :brew
722 edit Xxx2
723 exe "normal G?this is a\<CR>"
724 " Append text with autoindent to this file
725 normal othis should be auto-indented
726 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
727 call assert_equal(3, line('.'))
728 " Remove autocmd and edit Xxx2 again
729 au! BufEnter Xxx2
730 buf! Xxx2
731 exe "normal G?this is a\<CR>"
732 " append text without autoindent to Xxx
733 normal othis should be in column 1
734 call assert_equal("this should be in column 1", getline('.'))
735 call assert_equal(4, line('.'))
736
737 %bwipe!
738 call delete('Xxx1')
739 call delete('Xxx2')
740 set ai&vim modeline&vim modelines&vim
741endfunc
742
743" Test for issue #57
744" do not move cursor on <c-o> when autoindent is set
745func Test_ai_CTRL_O()
746 enew!
747 set ai
748 let save_fo = &fo
749 set fo+=r
750 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
751 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
752 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
753
754 set ai&vim
755 let &fo = save_fo
756 enew!
757endfunc
758
759" Test for autocommand that deletes the current buffer on BufLeave event.
760" Also test deleting the last buffer, should give a new, empty buffer.
761func Test_BufLeave_Wipe()
762 %bwipe!
763 let content = ['start of test file Xxx',
764 \ 'this is a test',
765 \ 'end of test file Xxx']
766 call writefile(content, 'Xxx1')
767 call writefile(content, 'Xxx2')
768
769 au BufLeave Xxx2 bwipe
770 edit Xxx1
771 split Xxx2
772 " delete buffer Xxx2, we should be back to Xxx1
773 bwipe
774 call assert_equal('Xxx1', bufname('%'))
775 call assert_equal(1, winnr('$'))
776
777 " Create an alternate buffer
778 %write! test.out
779 call assert_equal('test.out', bufname('#'))
780 " delete alternate buffer
781 bwipe test.out
782 call assert_equal('Xxx1', bufname('%'))
783 call assert_equal('', bufname('#'))
784
785 au BufLeave Xxx1 bwipe
786 " delete current buffer, get an empty one
787 bwipe!
788 call assert_equal(1, line('$'))
789 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200790 let g:bufinfo = getbufinfo()
791 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200792
793 call delete('Xxx1')
794 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200795 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200796 %bwipe
797 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200798
799 " check that bufinfo doesn't contain a pointer to freed memory
800 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200801endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +0200802
803func Test_QuitPre()
804 edit Xfoo
805 let winid = win_getid(winnr())
806 split Xbar
807 au! QuitPre * let g:afile = expand('<afile>')
808 " Close the other window, <afile> should be correct.
809 exe win_id2win(winid) . 'q'
810 call assert_equal('Xfoo', g:afile)
811
812 unlet g:afile
813 bwipe Xfoo
814 bwipe Xbar
815endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200816
817func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +0100818 au! CmdlineChanged : let g:text = getcmdline()
819 let g:text = 0
820 call feedkeys(":echom 'hello'\<CR>", 'xt')
821 call assert_equal("echom 'hello'", g:text)
822 au! CmdlineChanged
823
824 au! CmdlineChanged : let g:entered = expand('<afile>')
825 let g:entered = 0
826 call feedkeys(":echom 'hello'\<CR>", 'xt')
827 call assert_equal(':', g:entered)
828 au! CmdlineChanged
829
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200830 au! CmdlineEnter : let g:entered = expand('<afile>')
831 au! CmdlineLeave : let g:left = expand('<afile>')
832 let g:entered = 0
833 let g:left = 0
834 call feedkeys(":echo 'hello'\<CR>", 'xt')
835 call assert_equal(':', g:entered)
836 call assert_equal(':', g:left)
837 au! CmdlineEnter
838 au! CmdlineLeave
839
840 au! CmdlineEnter / let g:entered = expand('<afile>')
841 au! CmdlineLeave / let g:left = expand('<afile>')
842 let g:entered = 0
843 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200844 new
845 call setline(1, 'hello')
846 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200847 call assert_equal('/', g:entered)
848 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200849 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200850 au! CmdlineEnter
851 au! CmdlineLeave
852endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200853
854" Test for BufWritePre autocommand that deletes or unloads the buffer.
855func Test_BufWritePre()
856 %bwipe
857 au BufWritePre Xxx1 bunload
858 au BufWritePre Xxx2 bwipe
859
860 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
861 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
862
863 edit Xtest
864 e! Xxx2
865 bdel Xtest
866 e Xxx1
867 " write it, will unload it and give an error msg
868 call assert_fails('w', 'E203')
869 call assert_equal('Xxx2', bufname('%'))
870 edit Xtest
871 e! Xxx2
872 bwipe Xtest
873 " write it, will delete the buffer and give an error msg
874 call assert_fails('w', 'E203')
875 call assert_equal('Xxx1', bufname('%'))
876 au! BufWritePre
877 call delete('Xxx1')
878 call delete('Xxx2')
879endfunc
880
881" Test for BufUnload autocommand that unloads all the other buffers
882func Test_bufunload_all()
883 call writefile(['Test file Xxx1'], 'Xxx1')"
884 call writefile(['Test file Xxx2'], 'Xxx2')"
885
886 let content = [
887 \ "func UnloadAllBufs()",
888 \ " let i = 1",
889 \ " while i <= bufnr('$')",
890 \ " if i != bufnr('%') && bufloaded(i)",
891 \ " exe i . 'bunload'",
892 \ " endif",
893 \ " let i += 1",
894 \ " endwhile",
895 \ "endfunc",
896 \ "au BufUnload * call UnloadAllBufs()",
897 \ "au VimLeave * call writefile(['Test Finished'], 'Xout')",
898 \ "edit Xxx1",
899 \ "split Xxx2",
900 \ "q"]
901 call writefile(content, 'Xtest')
902
903 call delete('Xout')
904 call system(v:progpath. ' --clean -N --not-a-term -S Xtest')
905 call assert_true(filereadable('Xout'))
906
907 call delete('Xxx1')
908 call delete('Xxx2')
909 call delete('Xtest')
910 call delete('Xout')
911endfunc
912
913" Some tests for buffer-local autocommands
914func Test_buflocal_autocmd()
915 let g:bname = ''
916 edit xx
917 au BufLeave <buffer> let g:bname = expand("%")
918 " here, autocommand for xx should trigger.
919 " but autocommand shall not apply to buffer named <buffer>.
920 edit somefile
921 call assert_equal('xx', g:bname)
922 let g:bname = ''
923 " here, autocommand shall be auto-deleted
924 bwipe xx
925 " autocmd should not trigger
926 edit xx
927 call assert_equal('', g:bname)
928 " autocmd should not trigger
929 edit somefile
930 call assert_equal('', g:bname)
931 enew
932 unlet g:bname
933endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +0100934
935" Test for "*Cmd" autocommands
936func Test_Cmd_Autocmds()
937 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
938
939 enew!
940 au BufReadCmd XtestA 0r Xxx|$del
941 edit XtestA " will read text of Xxd instead
942 call assert_equal('start of Xxx', getline(1))
943
944 au BufWriteCmd XtestA call append(line("$"), "write")
945 write " will append a line to the file
946 call assert_equal('write', getline('$'))
947 call assert_fails('read XtestA', 'E484') " should not read anything
948 call assert_equal('write', getline(4))
949
950 " now we have:
951 " 1 start of Xxx
952 " 2 abc2
953 " 3 end of Xxx
954 " 4 write
955
956 au FileReadCmd XtestB '[r Xxx
957 2r XtestB " will read Xxx below line 2 instead
958 call assert_equal('start of Xxx', getline(3))
959
960 " now we have:
961 " 1 start of Xxx
962 " 2 abc2
963 " 3 start of Xxx
964 " 4 abc2
965 " 5 end of Xxx
966 " 6 end of Xxx
967 " 7 write
968
969 au FileWriteCmd XtestC '[,']copy $
970 normal 4GA1
971 4,5w XtestC " will copy lines 4 and 5 to the end
972 call assert_equal("\tabc21", getline(8))
973 call assert_fails('r XtestC', 'E484') " should not read anything
974 call assert_equal("end of Xxx", getline(9))
975
976 " now we have:
977 " 1 start of Xxx
978 " 2 abc2
979 " 3 start of Xxx
980 " 4 abc21
981 " 5 end of Xxx
982 " 6 end of Xxx
983 " 7 write
984 " 8 abc21
985 " 9 end of Xxx
986
987 let g:lines = []
988 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
989 w >>XtestD " will add lines to 'lines'
990 call assert_equal(9, len(g:lines))
991 call assert_fails('$r XtestD', 'E484') " should not read anything
992 call assert_equal(9, line('$'))
993 call assert_equal('end of Xxx', getline('$'))
994
995 au BufReadCmd XtestE 0r Xxx|$del
996 sp XtestE " split window with test.out
997 call assert_equal('end of Xxx', getline(3))
998
999 let g:lines = []
1000 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1001 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1002 wall " will write other window to 'lines'
1003 call assert_equal(4, len(g:lines), g:lines)
1004 call assert_equal('asdf', g:lines[2])
1005
1006 au! BufReadCmd
1007 au! BufWriteCmd
1008 au! FileReadCmd
1009 au! FileWriteCmd
1010 au! FileAppendCmd
1011 %bwipe!
1012 call delete('Xxx')
1013 enew!
1014endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001015
1016func SetChangeMarks(start, end)
1017 exe a:start. 'mark ['
1018 exe a:end. 'mark ]'
1019endfunc
1020
1021" Verify the effects of autocmds on '[ and ']
1022func Test_change_mark_in_autocmds()
1023 edit! Xtest
1024 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1025
1026 call SetChangeMarks(2, 3)
1027 write
1028 call assert_equal([1, 4], [line("'["), line("']")])
1029
1030 call SetChangeMarks(2, 3)
1031 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1032 write
1033 au! BufWritePre
1034
1035 if executable('cat')
1036 write XtestFilter
1037 write >> XtestFilter
1038
1039 call SetChangeMarks(2, 3)
1040 " Marks are set to the entire range of the write
1041 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1042 " '[ is adjusted to just before the line that will receive the filtered
1043 " data
1044 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1045 " The filtered data is read into the buffer, and the source lines are
1046 " still present, so the range is after the source lines
1047 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1048 %!cat XtestFilter
1049 " After the filtered data is read, the original lines are deleted
1050 call assert_equal([1, 8], [line("'["), line("']")])
1051 au! FilterWritePre,FilterReadPre,FilterReadPost
1052 undo
1053
1054 call SetChangeMarks(1, 4)
1055 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1056 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1057 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1058 2,3!cat XtestFilter
1059 call assert_equal([2, 9], [line("'["), line("']")])
1060 au! FilterWritePre,FilterReadPre,FilterReadPost
1061 undo
1062
1063 call delete('XtestFilter')
1064 endif
1065
1066 call SetChangeMarks(1, 4)
1067 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1068 2,3write Xtest2
1069 au! FileWritePre
1070
1071 call SetChangeMarks(2, 3)
1072 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1073 write >> Xtest2
1074 au! FileAppendPre
1075
1076 call SetChangeMarks(1, 4)
1077 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1078 2,3write >> Xtest2
1079 au! FileAppendPre
1080
1081 call SetChangeMarks(1, 1)
1082 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1083 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1084 3read Xtest2
1085 au! FileReadPre,FileReadPost
1086 undo
1087
1088 call SetChangeMarks(4, 4)
1089 " When the line is 0, it's adjusted to 1
1090 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1091 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1092 0read Xtest2
1093 au! FileReadPre,FileReadPost
1094 undo
1095
1096 call SetChangeMarks(4, 4)
1097 " When the line is 0, it's adjusted to 1
1098 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1099 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1100 1read Xtest2
1101 au! FileReadPre,FileReadPost
1102 undo
1103
1104 bwipe!
1105 call delete('Xtest')
1106 call delete('Xtest2')
1107endfunc
1108
1109func Test_Filter_noshelltemp()
1110 if !executable('cat')
1111 return
1112 endif
1113
1114 enew!
1115 call setline(1, ['a', 'b', 'c', 'd'])
1116
1117 let shelltemp = &shelltemp
1118 set shelltemp
1119
1120 let g:filter_au = 0
1121 au FilterWritePre * let g:filter_au += 1
1122 au FilterReadPre * let g:filter_au += 1
1123 au FilterReadPost * let g:filter_au += 1
1124 %!cat
1125 call assert_equal(3, g:filter_au)
1126
1127 if has('filterpipe')
1128 set noshelltemp
1129
1130 let g:filter_au = 0
1131 au FilterWritePre * let g:filter_au += 1
1132 au FilterReadPre * let g:filter_au += 1
1133 au FilterReadPost * let g:filter_au += 1
1134 %!cat
1135 call assert_equal(0, g:filter_au)
1136 endif
1137
1138 au! FilterWritePre,FilterReadPre,FilterReadPost
1139 let &shelltemp = shelltemp
1140 bwipe!
1141endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001142
1143func Test_TextYankPost()
1144 enew!
1145 call setline(1, ['foo'])
1146
1147 let g:event = []
1148 au TextYankPost * let g:event = copy(v:event)
1149
1150 call assert_equal({}, v:event)
1151 call assert_fails('let v:event = {}', 'E46:')
1152 call assert_fails('let v:event.mykey = 0', 'E742:')
1153
1154 norm "ayiw
1155 call assert_equal(
1156 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'},
1157 \g:event)
1158 norm y_
1159 call assert_equal(
1160 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'},
1161 \g:event)
1162 call feedkeys("\<C-V>y", 'x')
1163 call assert_equal(
1164 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"},
1165 \g:event)
1166 norm "xciwbar
1167 call assert_equal(
1168 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'},
1169 \g:event)
1170 norm "bdiw
1171 call assert_equal(
1172 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'},
1173 \g:event)
1174
1175 call assert_equal({}, v:event)
1176
1177 au! TextYankPost
1178 unlet g:event
1179 bwipe!
1180endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001181
1182func Test_nocatch_wipe_all_buffers()
1183 " Real nasty autocommand: wipe all buffers on any event.
1184 au * * bwipe *
Bram Moolenaara997b452018-04-17 23:24:06 +02001185 " Get E93 first?
1186 " call assert_fails('next x', 'E93:')
1187 call assert_fails('next x', 'E517:')
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001188 bwipe
1189 au!
1190endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001191
1192func Test_nocatch_wipe_dummy_buffer()
1193 " Nasty autocommand: wipe buffer on any event.
1194 au * x bwipe
1195 call assert_fails('lv½ /x', 'E480')
1196 au!
1197endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001198
1199function s:Before_test_dirchanged()
1200 augroup test_dirchanged
1201 autocmd!
1202 augroup END
1203 let s:li = []
1204 let s:dir_this = getcwd()
1205 let s:dir_other = s:dir_this . '/foo'
1206 call mkdir(s:dir_other)
1207endfunc
1208
1209function s:After_test_dirchanged()
1210 exe 'cd' s:dir_this
1211 call delete(s:dir_other, 'd')
1212 augroup test_dirchanged
1213 autocmd!
1214 augroup END
1215endfunc
1216
1217function Test_dirchanged_global()
1218 call s:Before_test_dirchanged()
1219 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1220 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
1221 exe 'cd' s:dir_other
1222 call assert_equal(["cd:", s:dir_other], s:li)
1223 exe 'lcd' s:dir_other
1224 call assert_equal(["cd:", s:dir_other], s:li)
1225 call s:After_test_dirchanged()
1226endfunc
1227
1228function Test_dirchanged_local()
1229 call s:Before_test_dirchanged()
1230 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1231 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
1232 exe 'cd' s:dir_other
1233 call assert_equal([], s:li)
1234 exe 'lcd' s:dir_other
1235 call assert_equal(["lcd:", s:dir_other], s:li)
1236 call s:After_test_dirchanged()
1237endfunc
1238
1239function Test_dirchanged_auto()
Bram Moolenaarec48a9c2018-02-03 20:11:40 +01001240 if !exists('+autochdir')
1241 return
1242 endif
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001243 call s:Before_test_dirchanged()
1244 call test_autochdir()
1245 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1246 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1247 set acd
1248 exe 'cd ..'
1249 call assert_equal([], s:li)
1250 exe 'edit ' . s:dir_other . '/Xfile'
1251 call assert_equal(s:dir_other, getcwd())
1252 call assert_equal(["auto:", s:dir_other], s:li)
1253 set noacd
1254 bwipe!
1255 call s:After_test_dirchanged()
1256endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001257
1258" Test TextChangedI and TextChangedP
1259func Test_ChangedP()
1260 new
1261 call setline(1, ['foo', 'bar', 'foobar'])
1262 call test_override("char_avail", 1)
1263 set complete=. completeopt=menuone
1264
1265 func! TextChangedAutocmd(char)
1266 let g:autocmd .= a:char
1267 endfunc
1268
1269 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1270 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1271 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1272
1273 call cursor(3, 1)
1274 let g:autocmd = ''
1275 call feedkeys("o\<esc>", 'tnix')
1276 call assert_equal('I', g:autocmd)
1277
1278 let g:autocmd = ''
1279 call feedkeys("Sf", 'tnix')
1280 call assert_equal('II', g:autocmd)
1281
1282 let g:autocmd = ''
1283 call feedkeys("Sf\<C-N>", 'tnix')
1284 call assert_equal('IIP', g:autocmd)
1285
1286 let g:autocmd = ''
1287 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1288 call assert_equal('IIPP', g:autocmd)
1289
1290 let g:autocmd = ''
1291 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1292 call assert_equal('IIPPP', g:autocmd)
1293
1294 let g:autocmd = ''
1295 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1296 call assert_equal('IIPPPP', g:autocmd)
1297
1298 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1299 " TODO: how should it handle completeopt=noinsert,noselect?
1300
1301 " CleanUp
1302 call test_override("char_avail", 0)
1303 au! TextChanged
1304 au! TextChangedI
1305 au! TextChangedP
1306 delfu TextChangedAutocmd
1307 unlet! g:autocmd
1308 set complete&vim completeopt&vim
1309
1310 bw!
1311endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001312
1313func Test_Changed_FirstTime()
1314 if !has('terminal') || has('gui_running')
1315 return
1316 endif
1317 " Prepare file for TextChanged event.
1318 call writefile([''], 'Xchanged.txt')
1319 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1320 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001321 " Wait for the ruler (in the status line) to be shown.
1322 call WaitFor({-> term_getline(buf, 3) =~# '\<All$'})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001323 " It's only adding autocmd, so that no event occurs.
1324 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1325 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
1326 call WaitFor({-> term_getstatus(buf) == 'finished'})
1327 call assert_equal([''], readfile('Xchanged.txt'))
1328
1329 " clean up
1330 call delete('Xchanged.txt')
1331 bwipe!
1332endfunc