blob: aa7d15779914909406e2bc093c86df0548e78569 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar4520d442017-03-19 16:09:46 +01003
Bram Moolenaar04f62f82017-07-19 18:18:39 +02004func! s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02005 for bnr in range(1, bufnr('$'))
6 if bufloaded(bnr) && bufnr('%') != bnr
7 execute 'bd! ' . bnr
8 endif
9 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020010endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020011
Bram Moolenaar14735512016-03-26 21:00:08 +010012func Test_vim_did_enter()
13 call assert_false(v:vim_did_enter)
14
15 " This script will never reach the main loop, can't check if v:vim_did_enter
16 " becomes one.
17endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020018
Bram Moolenaarc67e8922016-05-24 16:07:40 +020019if has('timers')
20 func ExitInsertMode(id)
21 call feedkeys("\<Esc>")
22 endfunc
23
24 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020025 " Need to move the cursor.
26 call feedkeys("ggG", "xt")
27
Bram Moolenaarc67e8922016-05-24 16:07:40 +020028 let g:triggered = 0
29 au CursorHoldI * let g:triggered += 1
30 set updatetime=20
31 call timer_start(100, 'ExitInsertMode')
32 call feedkeys('a', 'x!')
33 call assert_equal(1, 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
37
38 func Test_cursorhold_insert_ctrl_x()
39 let g:triggered = 0
40 au CursorHoldI * let g:triggered += 1
41 set updatetime=20
42 call timer_start(100, 'ExitInsertMode')
43 " CursorHoldI does not trigger after CTRL-X
44 call feedkeys("a\<C-X>", 'x!')
45 call assert_equal(0, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020046 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020047 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020048 endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020049endif
50
Bram Moolenaar04f62f82017-07-19 18:18:39 +020051func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 augroup test_bufunload_group
53 autocmd!
54 autocmd BufUnload * call add(s:li, "bufunload")
55 autocmd BufDelete * call add(s:li, "bufdelete")
56 autocmd BufWipeout * call add(s:li, "bufwipeout")
57 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +020058
Bram Moolenaarc67e8922016-05-24 16:07:40 +020059 let s:li=[]
60 new
61 setlocal bufhidden=
62 bunload
63 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +020064
Bram Moolenaarc67e8922016-05-24 16:07:40 +020065 let s:li=[]
66 new
67 setlocal bufhidden=delete
68 bunload
69 call assert_equal(["bufunload", "bufdelete"], s:li)
70
71 let s:li=[]
72 new
73 setlocal bufhidden=unload
74 bwipeout
75 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
76
Bram Moolenaare99e8442016-07-26 20:43:40 +020077 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +020078 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +020079endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +020080
81" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +020082func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +020083 tabedit
84 tabfirst
85
86 augroup test_autocmd_bufunload_with_tabnext_group
87 autocmd!
88 autocmd BufUnload <buffer> tabnext
89 augroup END
90
91 quit
92 call assert_equal(2, tabpagenr('$'))
93
Bram Moolenaare0ab94e2016-09-04 19:50:54 +020094 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +020095 augroup! test_autocmd_bufunload_with_tabnext_group
96 tablast
97 quit
98endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +020099
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200100func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200101 tabedit
102 augroup sample
103 autocmd!
104 autocmd BufWinLeave <buffer> tabfirst
105 augroup END
106 call setline(1, ['a', 'b', 'c'])
107 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200108 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200109endfunc
110
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200111" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200112func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200113 split aa.txt
114 let lastbuf = bufnr('$')
115
116 augroup test_autocmd_bufunload
117 autocmd!
118 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
119 augroup END
120
121 call assert_fails('edit bb.txt', 'E937:')
122
123 autocmd! test_autocmd_bufunload
124 augroup! test_autocmd_bufunload
125 bwipe! aa.txt
126 bwipe! bb.txt
127endfunc
128
129" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200130func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200131 setlocal buftype=nowrite
132 let lastbuf = bufnr('$')
133
134 augroup test_autocmd_bufunload
135 autocmd!
136 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
137 augroup END
138
139 normal! i1
140 call assert_fails('edit a.txt', 'E517:')
141 call feedkeys("\<CR>")
142
143 autocmd! test_autocmd_bufunload
144 augroup! test_autocmd_bufunload
145 bwipe! a.txt
146endfunc
147
Bram Moolenaarc917da42016-07-19 22:31:36 +0200148func Test_win_tab_autocmd()
149 let g:record = []
150
151 augroup testing
152 au WinNew * call add(g:record, 'WinNew')
153 au WinEnter * call add(g:record, 'WinEnter')
154 au WinLeave * call add(g:record, 'WinLeave')
155 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200156 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200157 au TabEnter * call add(g:record, 'TabEnter')
158 au TabLeave * call add(g:record, 'TabLeave')
159 augroup END
160
161 split
162 tabnew
163 close
164 close
165
166 call assert_equal([
167 \ 'WinLeave', 'WinNew', 'WinEnter',
168 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200169 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200170 \ 'WinLeave', 'WinEnter'
171 \ ], g:record)
172
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200173 let g:record = []
174 tabnew somefile
175 tabnext
176 bwipe somefile
177
178 call assert_equal([
179 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
180 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
181 \ 'TabClosed'
182 \ ], g:record)
183
Bram Moolenaarc917da42016-07-19 22:31:36 +0200184 augroup testing
185 au!
186 augroup END
187 unlet g:record
188endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200189
190func s:AddAnAutocmd()
191 augroup vimBarTest
192 au BufReadCmd * echo 'hello'
193 augroup END
194 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
195endfunc
196
197func Test_early_bar()
198 " test that a bar is recognized before the {event}
199 call s:AddAnAutocmd()
200 augroup vimBarTest | au! | augroup END
201 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
202
203 call s:AddAnAutocmd()
204 augroup vimBarTest| au!| augroup END
205 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
206
207 " test that a bar is recognized after the {event}
208 call s:AddAnAutocmd()
209 augroup vimBarTest| au!BufReadCmd| augroup END
210 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
211
212 " test that a bar is recognized after the {group}
213 call s:AddAnAutocmd()
214 au! vimBarTest|echo 'hello'
215 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
216endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200217
Bram Moolenaar5c809082016-09-01 16:21:48 +0200218func RemoveGroup()
219 autocmd! StartOK
220 augroup! StartOK
221endfunc
222
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200223func Test_augroup_warning()
224 augroup TheWarning
225 au VimEnter * echo 'entering'
226 augroup END
227 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
228 redir => res
229 augroup! TheWarning
230 redir END
231 call assert_true(match(res, "W19:") >= 0)
232 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
233
234 " check "Another" does not take the pace of the deleted entry
235 augroup Another
236 augroup END
237 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200238 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200239
240 " no warning for postpone aucmd delete
241 augroup StartOK
242 au VimEnter * call RemoveGroup()
243 augroup END
244 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
245 redir => res
246 doautocmd VimEnter
247 redir END
248 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200249 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200250endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200251
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200252func Test_BufReadCmdHelp()
253 " This used to cause access to free memory
254 au BufReadCmd * e +h
255 help
256
257 helpclose
258 au! BufReadCmd
259endfunc
260
261func Test_BufReadCmdHelpJump()
262 " This used to cause access to free memory
263 au BufReadCmd * e +h{
264 help
265
266 helpclose
267 au! BufReadCmd
268endfunc
269
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200270func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200271 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200272 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200273 call assert_fails('augroup! x', 'E936:')
274 au VimEnter * echo
275 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200276 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200277 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
278 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200279endfunc
280
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200281" Tests for autocommands on :close command.
282" This used to be in test13.
283func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200284 " Clean up buffers, because in some cases this function fails.
285 call s:cleanup_buffers()
286
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200287 " Write three files and open them, each in a window.
288 " Then go to next window, with autocommand that deletes the previous one.
289 " Do this twice, writing the file.
290 e! Xtestje1
291 call setline(1, 'testje1')
292 w
293 sp Xtestje2
294 call setline(1, 'testje2')
295 w
296 sp Xtestje3
297 call setline(1, 'testje3')
298 w
299 wincmd w
300 au WinLeave Xtestje2 bwipe
301 wincmd w
302 call assert_equal('Xtestje1', expand('%'))
303
304 au WinLeave Xtestje1 bwipe Xtestje3
305 close
306 call assert_equal('Xtestje1', expand('%'))
307
308 " Test deleting the buffer on a Unload event. If this goes wrong there
309 " will be the ATTENTION prompt.
310 e Xtestje1
311 au!
312 au! BufUnload Xtestje1 bwipe
313 call assert_fails('e Xtestje3', 'E937:')
314 call assert_equal('Xtestje3', expand('%'))
315
316 e Xtestje2
317 sp Xtestje1
318 call assert_fails('e', 'E937:')
319 call assert_equal('Xtestje2', expand('%'))
320
321 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
322 " there are ml_line errors and/or a Crash.
323 au!
324 only
325 e Xanother
326 e Xtestje1
327 bwipe Xtestje2
328 bwipe Xtestje3
329 au BufWipeout Xtestje1 buf Xtestje1
330 bwipe
331 call assert_equal('Xanother', expand('%'))
332
333 only
334 help
335 wincmd w
336 1quit
337 call assert_equal('Xanother', expand('%'))
338
339 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100340 enew
341 bwipe! Xtestje1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200342 call delete('Xtestje1')
343 call delete('Xtestje2')
344 call delete('Xtestje3')
345endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100346
347func Test_BufEnter()
348 au! BufEnter
349 au Bufenter * let val = val . '+'
350 let g:val = ''
351 split NewFile
352 call assert_equal('+', g:val)
353 bwipe!
354 call assert_equal('++', g:val)
355
356 " Also get BufEnter when editing a directory
357 call mkdir('Xdir')
358 split Xdir
359 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100360
361 " On MS-Windows we can't edit the directory, make sure we wipe the right
362 " buffer.
363 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100364
365 call delete('Xdir', 'd')
366 au! BufEnter
367endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100368
369" Closing a window might cause an endless loop
370" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200371func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200372 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100373 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200374 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100375 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100376 mksession!
377
Bram Moolenaare94260f2017-03-21 15:50:12 +0100378 let content = ['set nocp noswapfile',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100379 \ 'let v:swapchoice="e"',
380 \ 'augroup test_autocmd_sessionload',
381 \ 'autocmd!',
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200382 \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100383 \ 'augroup END',
384 \ '',
385 \ 'func WriteErrors()',
386 \ ' call writefile([execute("messages")], "Xerrors")',
387 \ 'endfunc',
388 \ 'au VimLeave * call WriteErrors()',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100389 \ ]
390 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100391 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
392 let errors = join(readfile('Xerrors'))
393 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100394
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100395 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100396 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100397 call delete(file)
398 endfor
399endfunc
400
401" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200402func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100403 tabnew
404 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100405 mksession!
406
407 let content = ['set nocp noswapfile',
408 \ 'function! DeleteInactiveBufs()',
409 \ ' tabfirst',
410 \ ' let tabblist = []',
411 \ ' for i in range(1, tabpagenr(''$''))',
412 \ ' call extend(tabblist, tabpagebuflist(i))',
413 \ ' endfor',
414 \ ' for b in range(1, bufnr(''$''))',
415 \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')',
416 \ ' exec ''bwipeout '' . b',
417 \ ' endif',
418 \ ' endfor',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100419 \ ' echomsg "SessionLoadPost DONE"',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100420 \ 'endfunction',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100421 \ 'au SessionLoadPost * call DeleteInactiveBufs()',
422 \ '',
423 \ 'func WriteErrors()',
424 \ ' call writefile([execute("messages")], "Xerrors")',
425 \ 'endfunc',
426 \ 'au VimLeave * call WriteErrors()',
427 \ ]
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100428 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100429 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
430 let errors = join(readfile('Xerrors'))
431 " This probably only ever matches on unix.
432 call assert_notmatch('Caught deadly signal SEGV', errors)
433 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100434
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100435 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100436 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100437 call delete(file)
438 endfor
439endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200440
441func Test_empty_doau()
442 doau \|
443endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200444
445func s:AutoCommandOptionSet(match)
446 let item = remove(g:options, 0)
447 let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3])
448 let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type)
449 let g:opt = [expected, actual]
450 "call assert_equal(expected, actual)
451endfunc
452
453func Test_OptionSet()
454 if !has("eval") || !has("autocmd") || !exists("+autochdir")
455 return
456 endif
457
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200458 badd test_autocmd.vim
459
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200460 call test_override('starting', 1)
461 set nocp
462 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
463
464 " 1: Setting number option"
465 let g:options=[['number', 0, 1, 'global']]
466 set nu
467 call assert_equal([], g:options)
468 call assert_equal(g:opt[0], g:opt[1])
469
470 " 2: Setting local number option"
471 let g:options=[['number', 1, 0, 'local']]
472 setlocal nonu
473 call assert_equal([], g:options)
474 call assert_equal(g:opt[0], g:opt[1])
475
476 " 3: Setting global number option"
477 let g:options=[['number', 1, 0, 'global']]
478 setglobal nonu
479 call assert_equal([], g:options)
480 call assert_equal(g:opt[0], g:opt[1])
481
482 " 4: Setting local autoindent option"
483 let g:options=[['autoindent', 0, 1, 'local']]
484 setlocal ai
485 call assert_equal([], g:options)
486 call assert_equal(g:opt[0], g:opt[1])
487
488 " 5: Setting global autoindent option"
489 let g:options=[['autoindent', 0, 1, 'global']]
490 setglobal ai
491 call assert_equal([], g:options)
492 call assert_equal(g:opt[0], g:opt[1])
493
494 " 6: Setting global autoindent option"
495 let g:options=[['autoindent', 1, 0, 'global']]
496 set ai!
497 call assert_equal([], g:options)
498 call assert_equal(g:opt[0], g:opt[1])
499
500 " Should not print anything, use :noa
501 " 7: don't trigger OptionSet"
502 let g:options=[['invalid', 1, 1, 'invalid']]
503 noa set nonu
504 call assert_equal([['invalid', 1, 1, 'invalid']], g:options)
505 call assert_equal(g:opt[0], g:opt[1])
506
507 " 8: Setting several global list and number option"
508 let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']]
509 set list nu
510 call assert_equal([], g:options)
511 call assert_equal(g:opt[0], g:opt[1])
512
513 " 9: don't trigger OptionSet"
514 let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']]
515 noa set nolist nonu
516 call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options)
517 call assert_equal(g:opt[0], g:opt[1])
518
519 " 10: Setting global acd"
520 let g:options=[['autochdir', 0, 1, 'local']]
521 setlocal acd
522 call assert_equal([], g:options)
523 call assert_equal(g:opt[0], g:opt[1])
524
525 " 11: Setting global autoread (also sets local value)"
526 let g:options=[['autoread', 0, 1, 'global']]
527 set ar
528 call assert_equal([], g:options)
529 call assert_equal(g:opt[0], g:opt[1])
530
531 " 12: Setting local autoread"
532 let g:options=[['autoread', 1, 1, 'local']]
533 setlocal ar
534 call assert_equal([], g:options)
535 call assert_equal(g:opt[0], g:opt[1])
536
537 " 13: Setting global autoread"
538 let g:options=[['autoread', 1, 0, 'global']]
539 setglobal invar
540 call assert_equal([], g:options)
541 call assert_equal(g:opt[0], g:opt[1])
542
543 " 14: Setting option backspace through :let"
544 let g:options=[['backspace', '', 'eol,indent,start', 'global']]
545 let &bs="eol,indent,start"
546 call assert_equal([], g:options)
547 call assert_equal(g:opt[0], g:opt[1])
548
549 " 15: Setting option backspace through setbufvar()"
550 let g:options=[['backup', 0, 1, 'local']]
551 " try twice, first time, shouldn't trigger because option name is invalid,
552 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200553 let bnum = bufnr('%')
554 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200555 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200556 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200557 call assert_equal([], g:options)
558 call assert_equal(g:opt[0], g:opt[1])
559
560 " 16: Setting number option using setwinvar"
561 let g:options=[['number', 0, 1, 'local']]
562 call setwinvar(0, '&number', 1)
563 call assert_equal([], g:options)
564 call assert_equal(g:opt[0], g:opt[1])
565
566 " 17: Setting key option, shouldn't trigger"
567 let g:options=[['key', 'invalid', 'invalid1', 'invalid']]
568 setlocal key=blah
569 setlocal key=
570 call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options)
571 call assert_equal(g:opt[0], g:opt[1])
572
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200573 " 18: Setting string option"
574 let oldval = &tags
575 let g:options=[['tags', oldval, 'tagpath', 'global']]
576 set tags=tagpath
577 call assert_equal([], g:options)
578 call assert_equal(g:opt[0], g:opt[1])
579
580 " 1l: Resetting string option"
581 let g:options=[['tags', 'tagpath', oldval, 'global']]
582 set tags&
583 call assert_equal([], g:options)
584 call assert_equal(g:opt[0], g:opt[1])
585
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200586 " Cleanup
587 au! OptionSet
588 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp']
589 exe printf(":set %s&vi", opt)
590 endfor
591 call test_override('starting', 0)
592 delfunc! AutoCommandOptionSet
593endfunc
594
595func Test_OptionSet_diffmode()
596 call test_override('starting', 1)
597 " 18: Changing an option when enetering diff mode
598 new
599 au OptionSet diff :let &l:cul=v:option_new
600
601 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
602 call assert_equal(0, &l:cul)
603 diffthis
604 call assert_equal(1, &l:cul)
605
606 vnew
607 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
608 call assert_equal(0, &l:cul)
609 diffthis
610 call assert_equal(1, &l:cul)
611
612 diffoff
613 call assert_equal(0, &l:cul)
614 call assert_equal(1, getwinvar(2, '&l:cul'))
615 bw!
616
617 call assert_equal(1, &l:cul)
618 diffoff!
619 call assert_equal(0, &l:cul)
620 call assert_equal(0, getwinvar(1, '&l:cul'))
621 bw!
622
623 " Cleanup
624 au! OptionSet
625 call test_override('starting', 0)
626endfunc
627
628func Test_OptionSet_diffmode_close()
629 call test_override('starting', 1)
630 " 19: Try to close the current window when entering diff mode
631 " should not segfault
632 new
633 au OptionSet diff close
634
635 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
636 call assert_fails(':diffthis', 'E788')
637 call assert_equal(1, &diff)
638 vnew
639 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
640 call assert_fails(':diffthis', 'E788')
641 call assert_equal(1, &diff)
642 bw!
643 call assert_fails(':diffoff!', 'E788')
644 bw!
645
646 " Cleanup
647 au! OptionSet
648 call test_override('starting', 0)
649 "delfunc! AutoCommandOptionSet
650endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200651
652" Test for Bufleave autocommand that deletes the buffer we are about to edit.
653func Test_BufleaveWithDelete()
654 new | edit Xfile1
655
656 augroup test_bufleavewithdelete
657 autocmd!
658 autocmd BufLeave Xfile1 bwipe Xfile2
659 augroup END
660
661 call assert_fails('edit Xfile2', 'E143:')
662 call assert_equal('Xfile1', bufname('%'))
663
664 autocmd! test_bufleavewithdelete BufLeave Xfile1
665 augroup! test_bufleavewithdelete
666
667 new
668 bwipe! Xfile1
669endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200670
671" Test for autocommand that changes the buffer list, when doing ":ball".
672func Test_Acmd_BufAll()
673 enew!
674 %bwipe!
675 call writefile(['Test file Xxx1'], 'Xxx1')
676 call writefile(['Test file Xxx2'], 'Xxx2')
677 call writefile(['Test file Xxx3'], 'Xxx3')
678
679 " Add three files to the buffer list
680 split Xxx1
681 close
682 split Xxx2
683 close
684 split Xxx3
685 close
686
687 " Wipe the buffer when the buffer is opened
688 au BufReadPost Xxx2 bwipe
689
690 call append(0, 'Test file Xxx4')
691 ball
692
693 call assert_equal(2, winnr('$'))
694 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
695 wincmd t
696
697 au! BufReadPost
698 %bwipe!
699 call delete('Xxx1')
700 call delete('Xxx2')
701 call delete('Xxx3')
702 enew! | only
703endfunc
704
705" Test for autocommand that changes current buffer on BufEnter event.
706" Check if modelines are interpreted for the correct buffer.
707func Test_Acmd_BufEnter()
708 %bwipe!
709 call writefile(['start of test file Xxx1',
710 \ "\<Tab>this is a test",
711 \ 'end of test file Xxx1'], 'Xxx1')
712 call writefile(['start of test file Xxx2',
713 \ 'vim: set noai :',
714 \ "\<Tab>this is a test",
715 \ 'end of test file Xxx2'], 'Xxx2')
716
717 au BufEnter Xxx2 brew
718 set ai modeline modelines=3
719 edit Xxx1
720 " edit Xxx2, autocmd will do :brew
721 edit Xxx2
722 exe "normal G?this is a\<CR>"
723 " Append text with autoindent to this file
724 normal othis should be auto-indented
725 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
726 call assert_equal(3, line('.'))
727 " Remove autocmd and edit Xxx2 again
728 au! BufEnter Xxx2
729 buf! Xxx2
730 exe "normal G?this is a\<CR>"
731 " append text without autoindent to Xxx
732 normal othis should be in column 1
733 call assert_equal("this should be in column 1", getline('.'))
734 call assert_equal(4, line('.'))
735
736 %bwipe!
737 call delete('Xxx1')
738 call delete('Xxx2')
739 set ai&vim modeline&vim modelines&vim
740endfunc
741
742" Test for issue #57
743" do not move cursor on <c-o> when autoindent is set
744func Test_ai_CTRL_O()
745 enew!
746 set ai
747 let save_fo = &fo
748 set fo+=r
749 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
750 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
751 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
752
753 set ai&vim
754 let &fo = save_fo
755 enew!
756endfunc
757
758" Test for autocommand that deletes the current buffer on BufLeave event.
759" Also test deleting the last buffer, should give a new, empty buffer.
760func Test_BufLeave_Wipe()
761 %bwipe!
762 let content = ['start of test file Xxx',
763 \ 'this is a test',
764 \ 'end of test file Xxx']
765 call writefile(content, 'Xxx1')
766 call writefile(content, 'Xxx2')
767
768 au BufLeave Xxx2 bwipe
769 edit Xxx1
770 split Xxx2
771 " delete buffer Xxx2, we should be back to Xxx1
772 bwipe
773 call assert_equal('Xxx1', bufname('%'))
774 call assert_equal(1, winnr('$'))
775
776 " Create an alternate buffer
777 %write! test.out
778 call assert_equal('test.out', bufname('#'))
779 " delete alternate buffer
780 bwipe test.out
781 call assert_equal('Xxx1', bufname('%'))
782 call assert_equal('', bufname('#'))
783
784 au BufLeave Xxx1 bwipe
785 " delete current buffer, get an empty one
786 bwipe!
787 call assert_equal(1, line('$'))
788 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200789 let g:bufinfo = getbufinfo()
790 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200791
792 call delete('Xxx1')
793 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200794 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200795 %bwipe
796 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200797
798 " check that bufinfo doesn't contain a pointer to freed memory
799 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200800endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +0200801
802func Test_QuitPre()
803 edit Xfoo
804 let winid = win_getid(winnr())
805 split Xbar
806 au! QuitPre * let g:afile = expand('<afile>')
807 " Close the other window, <afile> should be correct.
808 exe win_id2win(winid) . 'q'
809 call assert_equal('Xfoo', g:afile)
810
811 unlet g:afile
812 bwipe Xfoo
813 bwipe Xbar
814endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200815
816func Test_Cmdline()
817 au! CmdlineEnter : let g:entered = expand('<afile>')
818 au! CmdlineLeave : let g:left = expand('<afile>')
819 let g:entered = 0
820 let g:left = 0
821 call feedkeys(":echo 'hello'\<CR>", 'xt')
822 call assert_equal(':', g:entered)
823 call assert_equal(':', g:left)
824 au! CmdlineEnter
825 au! CmdlineLeave
826
827 au! CmdlineEnter / let g:entered = expand('<afile>')
828 au! CmdlineLeave / let g:left = expand('<afile>')
829 let g:entered = 0
830 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200831 new
832 call setline(1, 'hello')
833 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200834 call assert_equal('/', g:entered)
835 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200836 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200837 au! CmdlineEnter
838 au! CmdlineLeave
839endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200840
841" Test for BufWritePre autocommand that deletes or unloads the buffer.
842func Test_BufWritePre()
843 %bwipe
844 au BufWritePre Xxx1 bunload
845 au BufWritePre Xxx2 bwipe
846
847 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
848 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
849
850 edit Xtest
851 e! Xxx2
852 bdel Xtest
853 e Xxx1
854 " write it, will unload it and give an error msg
855 call assert_fails('w', 'E203')
856 call assert_equal('Xxx2', bufname('%'))
857 edit Xtest
858 e! Xxx2
859 bwipe Xtest
860 " write it, will delete the buffer and give an error msg
861 call assert_fails('w', 'E203')
862 call assert_equal('Xxx1', bufname('%'))
863 au! BufWritePre
864 call delete('Xxx1')
865 call delete('Xxx2')
866endfunc
867
868" Test for BufUnload autocommand that unloads all the other buffers
869func Test_bufunload_all()
870 call writefile(['Test file Xxx1'], 'Xxx1')"
871 call writefile(['Test file Xxx2'], 'Xxx2')"
872
873 let content = [
874 \ "func UnloadAllBufs()",
875 \ " let i = 1",
876 \ " while i <= bufnr('$')",
877 \ " if i != bufnr('%') && bufloaded(i)",
878 \ " exe i . 'bunload'",
879 \ " endif",
880 \ " let i += 1",
881 \ " endwhile",
882 \ "endfunc",
883 \ "au BufUnload * call UnloadAllBufs()",
884 \ "au VimLeave * call writefile(['Test Finished'], 'Xout')",
885 \ "edit Xxx1",
886 \ "split Xxx2",
887 \ "q"]
888 call writefile(content, 'Xtest')
889
890 call delete('Xout')
891 call system(v:progpath. ' --clean -N --not-a-term -S Xtest')
892 call assert_true(filereadable('Xout'))
893
894 call delete('Xxx1')
895 call delete('Xxx2')
896 call delete('Xtest')
897 call delete('Xout')
898endfunc
899
900" Some tests for buffer-local autocommands
901func Test_buflocal_autocmd()
902 let g:bname = ''
903 edit xx
904 au BufLeave <buffer> let g:bname = expand("%")
905 " here, autocommand for xx should trigger.
906 " but autocommand shall not apply to buffer named <buffer>.
907 edit somefile
908 call assert_equal('xx', g:bname)
909 let g:bname = ''
910 " here, autocommand shall be auto-deleted
911 bwipe xx
912 " autocmd should not trigger
913 edit xx
914 call assert_equal('', g:bname)
915 " autocmd should not trigger
916 edit somefile
917 call assert_equal('', g:bname)
918 enew
919 unlet g:bname
920endfunc