blob: dbf7d20b558fea4a62836c870874a937149d8df6 [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
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200257 au! BufReadCmd
258endfunc
259
260func Test_BufReadCmdHelpJump()
261 " This used to cause access to free memory
262 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200263 " } to fix highlighting
264 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200265
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200266 au! BufReadCmd
267endfunc
268
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200269func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200270 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200271 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200272 call assert_fails('augroup! x', 'E936:')
273 au VimEnter * echo
274 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200275 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200276 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
277 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200278endfunc
279
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200280" Tests for autocommands on :close command.
281" This used to be in test13.
282func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200283 " Clean up buffers, because in some cases this function fails.
284 call s:cleanup_buffers()
285
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200286 " Write three files and open them, each in a window.
287 " Then go to next window, with autocommand that deletes the previous one.
288 " Do this twice, writing the file.
289 e! Xtestje1
290 call setline(1, 'testje1')
291 w
292 sp Xtestje2
293 call setline(1, 'testje2')
294 w
295 sp Xtestje3
296 call setline(1, 'testje3')
297 w
298 wincmd w
299 au WinLeave Xtestje2 bwipe
300 wincmd w
301 call assert_equal('Xtestje1', expand('%'))
302
303 au WinLeave Xtestje1 bwipe Xtestje3
304 close
305 call assert_equal('Xtestje1', expand('%'))
306
307 " Test deleting the buffer on a Unload event. If this goes wrong there
308 " will be the ATTENTION prompt.
309 e Xtestje1
310 au!
311 au! BufUnload Xtestje1 bwipe
312 call assert_fails('e Xtestje3', 'E937:')
313 call assert_equal('Xtestje3', expand('%'))
314
315 e Xtestje2
316 sp Xtestje1
317 call assert_fails('e', 'E937:')
318 call assert_equal('Xtestje2', expand('%'))
319
320 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
321 " there are ml_line errors and/or a Crash.
322 au!
323 only
324 e Xanother
325 e Xtestje1
326 bwipe Xtestje2
327 bwipe Xtestje3
328 au BufWipeout Xtestje1 buf Xtestje1
329 bwipe
330 call assert_equal('Xanother', expand('%'))
331
332 only
333 help
334 wincmd w
335 1quit
336 call assert_equal('Xanother', expand('%'))
337
338 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100339 enew
340 bwipe! Xtestje1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200341 call delete('Xtestje1')
342 call delete('Xtestje2')
343 call delete('Xtestje3')
344endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100345
346func Test_BufEnter()
347 au! BufEnter
348 au Bufenter * let val = val . '+'
349 let g:val = ''
350 split NewFile
351 call assert_equal('+', g:val)
352 bwipe!
353 call assert_equal('++', g:val)
354
355 " Also get BufEnter when editing a directory
356 call mkdir('Xdir')
357 split Xdir
358 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100359
360 " On MS-Windows we can't edit the directory, make sure we wipe the right
361 " buffer.
362 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100363
364 call delete('Xdir', 'd')
365 au! BufEnter
366endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100367
368" Closing a window might cause an endless loop
369" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200370func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200371 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100372 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200373 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100374 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100375 mksession!
376
Bram Moolenaare94260f2017-03-21 15:50:12 +0100377 let content = ['set nocp noswapfile',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100378 \ 'let v:swapchoice="e"',
379 \ 'augroup test_autocmd_sessionload',
380 \ 'autocmd!',
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200381 \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100382 \ 'augroup END',
383 \ '',
384 \ 'func WriteErrors()',
385 \ ' call writefile([execute("messages")], "Xerrors")',
386 \ 'endfunc',
387 \ 'au VimLeave * call WriteErrors()',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100388 \ ]
389 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100390 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
391 let errors = join(readfile('Xerrors'))
392 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100393
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100394 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100395 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100396 call delete(file)
397 endfor
398endfunc
399
400" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200401func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100402 tabnew
403 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100404 mksession!
405
406 let content = ['set nocp noswapfile',
407 \ 'function! DeleteInactiveBufs()',
408 \ ' tabfirst',
409 \ ' let tabblist = []',
410 \ ' for i in range(1, tabpagenr(''$''))',
411 \ ' call extend(tabblist, tabpagebuflist(i))',
412 \ ' endfor',
413 \ ' for b in range(1, bufnr(''$''))',
414 \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')',
415 \ ' exec ''bwipeout '' . b',
416 \ ' endif',
417 \ ' endfor',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100418 \ ' echomsg "SessionLoadPost DONE"',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100419 \ 'endfunction',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100420 \ 'au SessionLoadPost * call DeleteInactiveBufs()',
421 \ '',
422 \ 'func WriteErrors()',
423 \ ' call writefile([execute("messages")], "Xerrors")',
424 \ 'endfunc',
425 \ 'au VimLeave * call WriteErrors()',
426 \ ]
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100427 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100428 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
429 let errors = join(readfile('Xerrors'))
430 " This probably only ever matches on unix.
431 call assert_notmatch('Caught deadly signal SEGV', errors)
432 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100433
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100434 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100435 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100436 call delete(file)
437 endfor
438endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200439
440func Test_empty_doau()
441 doau \|
442endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200443
444func s:AutoCommandOptionSet(match)
445 let item = remove(g:options, 0)
446 let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3])
447 let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type)
448 let g:opt = [expected, actual]
449 "call assert_equal(expected, actual)
450endfunc
451
452func Test_OptionSet()
453 if !has("eval") || !has("autocmd") || !exists("+autochdir")
454 return
455 endif
456
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200457 badd test_autocmd.vim
458
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200459 call test_override('starting', 1)
460 set nocp
461 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
462
463 " 1: Setting number option"
464 let g:options=[['number', 0, 1, 'global']]
465 set nu
466 call assert_equal([], g:options)
467 call assert_equal(g:opt[0], g:opt[1])
468
469 " 2: Setting local number option"
470 let g:options=[['number', 1, 0, 'local']]
471 setlocal nonu
472 call assert_equal([], g:options)
473 call assert_equal(g:opt[0], g:opt[1])
474
475 " 3: Setting global number option"
476 let g:options=[['number', 1, 0, 'global']]
477 setglobal nonu
478 call assert_equal([], g:options)
479 call assert_equal(g:opt[0], g:opt[1])
480
481 " 4: Setting local autoindent option"
482 let g:options=[['autoindent', 0, 1, 'local']]
483 setlocal ai
484 call assert_equal([], g:options)
485 call assert_equal(g:opt[0], g:opt[1])
486
487 " 5: Setting global autoindent option"
488 let g:options=[['autoindent', 0, 1, 'global']]
489 setglobal ai
490 call assert_equal([], g:options)
491 call assert_equal(g:opt[0], g:opt[1])
492
493 " 6: Setting global autoindent option"
494 let g:options=[['autoindent', 1, 0, 'global']]
495 set ai!
496 call assert_equal([], g:options)
497 call assert_equal(g:opt[0], g:opt[1])
498
499 " Should not print anything, use :noa
500 " 7: don't trigger OptionSet"
501 let g:options=[['invalid', 1, 1, 'invalid']]
502 noa set nonu
503 call assert_equal([['invalid', 1, 1, 'invalid']], g:options)
504 call assert_equal(g:opt[0], g:opt[1])
505
506 " 8: Setting several global list and number option"
507 let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']]
508 set list nu
509 call assert_equal([], g:options)
510 call assert_equal(g:opt[0], g:opt[1])
511
512 " 9: don't trigger OptionSet"
513 let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']]
514 noa set nolist nonu
515 call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options)
516 call assert_equal(g:opt[0], g:opt[1])
517
518 " 10: Setting global acd"
519 let g:options=[['autochdir', 0, 1, 'local']]
520 setlocal acd
521 call assert_equal([], g:options)
522 call assert_equal(g:opt[0], g:opt[1])
523
524 " 11: Setting global autoread (also sets local value)"
525 let g:options=[['autoread', 0, 1, 'global']]
526 set ar
527 call assert_equal([], g:options)
528 call assert_equal(g:opt[0], g:opt[1])
529
530 " 12: Setting local autoread"
531 let g:options=[['autoread', 1, 1, 'local']]
532 setlocal ar
533 call assert_equal([], g:options)
534 call assert_equal(g:opt[0], g:opt[1])
535
536 " 13: Setting global autoread"
537 let g:options=[['autoread', 1, 0, 'global']]
538 setglobal invar
539 call assert_equal([], g:options)
540 call assert_equal(g:opt[0], g:opt[1])
541
542 " 14: Setting option backspace through :let"
543 let g:options=[['backspace', '', 'eol,indent,start', 'global']]
544 let &bs="eol,indent,start"
545 call assert_equal([], g:options)
546 call assert_equal(g:opt[0], g:opt[1])
547
548 " 15: Setting option backspace through setbufvar()"
549 let g:options=[['backup', 0, 1, 'local']]
550 " try twice, first time, shouldn't trigger because option name is invalid,
551 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200552 let bnum = bufnr('%')
553 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200554 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200555 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200556 call assert_equal([], g:options)
557 call assert_equal(g:opt[0], g:opt[1])
558
559 " 16: Setting number option using setwinvar"
560 let g:options=[['number', 0, 1, 'local']]
561 call setwinvar(0, '&number', 1)
562 call assert_equal([], g:options)
563 call assert_equal(g:opt[0], g:opt[1])
564
565 " 17: Setting key option, shouldn't trigger"
566 let g:options=[['key', 'invalid', 'invalid1', 'invalid']]
567 setlocal key=blah
568 setlocal key=
569 call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options)
570 call assert_equal(g:opt[0], g:opt[1])
571
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200572 " 18: Setting string option"
573 let oldval = &tags
574 let g:options=[['tags', oldval, 'tagpath', 'global']]
575 set tags=tagpath
576 call assert_equal([], g:options)
577 call assert_equal(g:opt[0], g:opt[1])
578
579 " 1l: Resetting string option"
580 let g:options=[['tags', 'tagpath', oldval, 'global']]
581 set tags&
582 call assert_equal([], g:options)
583 call assert_equal(g:opt[0], g:opt[1])
584
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200585 " Cleanup
586 au! OptionSet
587 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp']
588 exe printf(":set %s&vi", opt)
589 endfor
590 call test_override('starting', 0)
591 delfunc! AutoCommandOptionSet
592endfunc
593
594func Test_OptionSet_diffmode()
595 call test_override('starting', 1)
596 " 18: Changing an option when enetering diff mode
597 new
598 au OptionSet diff :let &l:cul=v:option_new
599
600 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
601 call assert_equal(0, &l:cul)
602 diffthis
603 call assert_equal(1, &l:cul)
604
605 vnew
606 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
607 call assert_equal(0, &l:cul)
608 diffthis
609 call assert_equal(1, &l:cul)
610
611 diffoff
612 call assert_equal(0, &l:cul)
613 call assert_equal(1, getwinvar(2, '&l:cul'))
614 bw!
615
616 call assert_equal(1, &l:cul)
617 diffoff!
618 call assert_equal(0, &l:cul)
619 call assert_equal(0, getwinvar(1, '&l:cul'))
620 bw!
621
622 " Cleanup
623 au! OptionSet
624 call test_override('starting', 0)
625endfunc
626
627func Test_OptionSet_diffmode_close()
628 call test_override('starting', 1)
629 " 19: Try to close the current window when entering diff mode
630 " should not segfault
631 new
632 au OptionSet diff close
633
634 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
635 call assert_fails(':diffthis', 'E788')
636 call assert_equal(1, &diff)
637 vnew
638 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
639 call assert_fails(':diffthis', 'E788')
640 call assert_equal(1, &diff)
641 bw!
642 call assert_fails(':diffoff!', 'E788')
643 bw!
644
645 " Cleanup
646 au! OptionSet
647 call test_override('starting', 0)
648 "delfunc! AutoCommandOptionSet
649endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200650
651" Test for Bufleave autocommand that deletes the buffer we are about to edit.
652func Test_BufleaveWithDelete()
653 new | edit Xfile1
654
655 augroup test_bufleavewithdelete
656 autocmd!
657 autocmd BufLeave Xfile1 bwipe Xfile2
658 augroup END
659
660 call assert_fails('edit Xfile2', 'E143:')
661 call assert_equal('Xfile1', bufname('%'))
662
663 autocmd! test_bufleavewithdelete BufLeave Xfile1
664 augroup! test_bufleavewithdelete
665
666 new
667 bwipe! Xfile1
668endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200669
670" Test for autocommand that changes the buffer list, when doing ":ball".
671func Test_Acmd_BufAll()
672 enew!
673 %bwipe!
674 call writefile(['Test file Xxx1'], 'Xxx1')
675 call writefile(['Test file Xxx2'], 'Xxx2')
676 call writefile(['Test file Xxx3'], 'Xxx3')
677
678 " Add three files to the buffer list
679 split Xxx1
680 close
681 split Xxx2
682 close
683 split Xxx3
684 close
685
686 " Wipe the buffer when the buffer is opened
687 au BufReadPost Xxx2 bwipe
688
689 call append(0, 'Test file Xxx4')
690 ball
691
692 call assert_equal(2, winnr('$'))
693 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
694 wincmd t
695
696 au! BufReadPost
697 %bwipe!
698 call delete('Xxx1')
699 call delete('Xxx2')
700 call delete('Xxx3')
701 enew! | only
702endfunc
703
704" Test for autocommand that changes current buffer on BufEnter event.
705" Check if modelines are interpreted for the correct buffer.
706func Test_Acmd_BufEnter()
707 %bwipe!
708 call writefile(['start of test file Xxx1',
709 \ "\<Tab>this is a test",
710 \ 'end of test file Xxx1'], 'Xxx1')
711 call writefile(['start of test file Xxx2',
712 \ 'vim: set noai :',
713 \ "\<Tab>this is a test",
714 \ 'end of test file Xxx2'], 'Xxx2')
715
716 au BufEnter Xxx2 brew
717 set ai modeline modelines=3
718 edit Xxx1
719 " edit Xxx2, autocmd will do :brew
720 edit Xxx2
721 exe "normal G?this is a\<CR>"
722 " Append text with autoindent to this file
723 normal othis should be auto-indented
724 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
725 call assert_equal(3, line('.'))
726 " Remove autocmd and edit Xxx2 again
727 au! BufEnter Xxx2
728 buf! Xxx2
729 exe "normal G?this is a\<CR>"
730 " append text without autoindent to Xxx
731 normal othis should be in column 1
732 call assert_equal("this should be in column 1", getline('.'))
733 call assert_equal(4, line('.'))
734
735 %bwipe!
736 call delete('Xxx1')
737 call delete('Xxx2')
738 set ai&vim modeline&vim modelines&vim
739endfunc
740
741" Test for issue #57
742" do not move cursor on <c-o> when autoindent is set
743func Test_ai_CTRL_O()
744 enew!
745 set ai
746 let save_fo = &fo
747 set fo+=r
748 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
749 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
750 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
751
752 set ai&vim
753 let &fo = save_fo
754 enew!
755endfunc
756
757" Test for autocommand that deletes the current buffer on BufLeave event.
758" Also test deleting the last buffer, should give a new, empty buffer.
759func Test_BufLeave_Wipe()
760 %bwipe!
761 let content = ['start of test file Xxx',
762 \ 'this is a test',
763 \ 'end of test file Xxx']
764 call writefile(content, 'Xxx1')
765 call writefile(content, 'Xxx2')
766
767 au BufLeave Xxx2 bwipe
768 edit Xxx1
769 split Xxx2
770 " delete buffer Xxx2, we should be back to Xxx1
771 bwipe
772 call assert_equal('Xxx1', bufname('%'))
773 call assert_equal(1, winnr('$'))
774
775 " Create an alternate buffer
776 %write! test.out
777 call assert_equal('test.out', bufname('#'))
778 " delete alternate buffer
779 bwipe test.out
780 call assert_equal('Xxx1', bufname('%'))
781 call assert_equal('', bufname('#'))
782
783 au BufLeave Xxx1 bwipe
784 " delete current buffer, get an empty one
785 bwipe!
786 call assert_equal(1, line('$'))
787 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200788 let g:bufinfo = getbufinfo()
789 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200790
791 call delete('Xxx1')
792 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200793 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200794 %bwipe
795 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200796
797 " check that bufinfo doesn't contain a pointer to freed memory
798 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200799endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +0200800
801func Test_QuitPre()
802 edit Xfoo
803 let winid = win_getid(winnr())
804 split Xbar
805 au! QuitPre * let g:afile = expand('<afile>')
806 " Close the other window, <afile> should be correct.
807 exe win_id2win(winid) . 'q'
808 call assert_equal('Xfoo', g:afile)
809
810 unlet g:afile
811 bwipe Xfoo
812 bwipe Xbar
813endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200814
815func Test_Cmdline()
816 au! CmdlineEnter : let g:entered = expand('<afile>')
817 au! CmdlineLeave : let g:left = expand('<afile>')
818 let g:entered = 0
819 let g:left = 0
820 call feedkeys(":echo 'hello'\<CR>", 'xt')
821 call assert_equal(':', g:entered)
822 call assert_equal(':', g:left)
823 au! CmdlineEnter
824 au! CmdlineLeave
825
826 au! CmdlineEnter / let g:entered = expand('<afile>')
827 au! CmdlineLeave / let g:left = expand('<afile>')
828 let g:entered = 0
829 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200830 new
831 call setline(1, 'hello')
832 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200833 call assert_equal('/', g:entered)
834 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200835 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200836 au! CmdlineEnter
837 au! CmdlineLeave
838endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200839
840" Test for BufWritePre autocommand that deletes or unloads the buffer.
841func Test_BufWritePre()
842 %bwipe
843 au BufWritePre Xxx1 bunload
844 au BufWritePre Xxx2 bwipe
845
846 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
847 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
848
849 edit Xtest
850 e! Xxx2
851 bdel Xtest
852 e Xxx1
853 " write it, will unload it and give an error msg
854 call assert_fails('w', 'E203')
855 call assert_equal('Xxx2', bufname('%'))
856 edit Xtest
857 e! Xxx2
858 bwipe Xtest
859 " write it, will delete the buffer and give an error msg
860 call assert_fails('w', 'E203')
861 call assert_equal('Xxx1', bufname('%'))
862 au! BufWritePre
863 call delete('Xxx1')
864 call delete('Xxx2')
865endfunc
866
867" Test for BufUnload autocommand that unloads all the other buffers
868func Test_bufunload_all()
869 call writefile(['Test file Xxx1'], 'Xxx1')"
870 call writefile(['Test file Xxx2'], 'Xxx2')"
871
872 let content = [
873 \ "func UnloadAllBufs()",
874 \ " let i = 1",
875 \ " while i <= bufnr('$')",
876 \ " if i != bufnr('%') && bufloaded(i)",
877 \ " exe i . 'bunload'",
878 \ " endif",
879 \ " let i += 1",
880 \ " endwhile",
881 \ "endfunc",
882 \ "au BufUnload * call UnloadAllBufs()",
883 \ "au VimLeave * call writefile(['Test Finished'], 'Xout')",
884 \ "edit Xxx1",
885 \ "split Xxx2",
886 \ "q"]
887 call writefile(content, 'Xtest')
888
889 call delete('Xout')
890 call system(v:progpath. ' --clean -N --not-a-term -S Xtest')
891 call assert_true(filereadable('Xout'))
892
893 call delete('Xxx1')
894 call delete('Xxx2')
895 call delete('Xtest')
896 call delete('Xout')
897endfunc
898
899" Some tests for buffer-local autocommands
900func Test_buflocal_autocmd()
901 let g:bname = ''
902 edit xx
903 au BufLeave <buffer> let g:bname = expand("%")
904 " here, autocommand for xx should trigger.
905 " but autocommand shall not apply to buffer named <buffer>.
906 edit somefile
907 call assert_equal('xx', g:bname)
908 let g:bname = ''
909 " here, autocommand shall be auto-deleted
910 bwipe xx
911 " autocmd should not trigger
912 edit xx
913 call assert_equal('', g:bname)
914 " autocmd should not trigger
915 edit somefile
916 call assert_equal('', g:bname)
917 enew
918 unlet g:bname
919endfunc