blob: eb13563e0aa0b90f00bdb970b91d6a719423d784 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar4520d442017-03-19 16:09:46 +01003set belloff=all
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
122 call assert_fails('edit bb.txt', 'E937:')
123
124 autocmd! test_autocmd_bufunload
125 augroup! test_autocmd_bufunload
126 bwipe! aa.txt
127 bwipe! bb.txt
128endfunc
129
130" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200131func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200132 setlocal buftype=nowrite
133 let lastbuf = bufnr('$')
134
135 augroup test_autocmd_bufunload
136 autocmd!
137 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
138 augroup END
139
140 normal! i1
141 call assert_fails('edit a.txt', 'E517:')
142 call feedkeys("\<CR>")
143
144 autocmd! test_autocmd_bufunload
145 augroup! test_autocmd_bufunload
146 bwipe! a.txt
147endfunc
148
Bram Moolenaarc917da42016-07-19 22:31:36 +0200149func Test_win_tab_autocmd()
150 let g:record = []
151
152 augroup testing
153 au WinNew * call add(g:record, 'WinNew')
154 au WinEnter * call add(g:record, 'WinEnter')
155 au WinLeave * call add(g:record, 'WinLeave')
156 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200157 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200158 au TabEnter * call add(g:record, 'TabEnter')
159 au TabLeave * call add(g:record, 'TabLeave')
160 augroup END
161
162 split
163 tabnew
164 close
165 close
166
167 call assert_equal([
168 \ 'WinLeave', 'WinNew', 'WinEnter',
169 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200170 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200171 \ 'WinLeave', 'WinEnter'
172 \ ], g:record)
173
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200174 let g:record = []
175 tabnew somefile
176 tabnext
177 bwipe somefile
178
179 call assert_equal([
180 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
181 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
182 \ 'TabClosed'
183 \ ], g:record)
184
Bram Moolenaarc917da42016-07-19 22:31:36 +0200185 augroup testing
186 au!
187 augroup END
188 unlet g:record
189endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200190
191func s:AddAnAutocmd()
192 augroup vimBarTest
193 au BufReadCmd * echo 'hello'
194 augroup END
195 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
196endfunc
197
198func Test_early_bar()
199 " test that a bar is recognized before the {event}
200 call s:AddAnAutocmd()
201 augroup vimBarTest | au! | augroup END
202 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
203
204 call s:AddAnAutocmd()
205 augroup vimBarTest| au!| augroup END
206 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
207
208 " test that a bar is recognized after the {event}
209 call s:AddAnAutocmd()
210 augroup vimBarTest| au!BufReadCmd| augroup END
211 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
212
213 " test that a bar is recognized after the {group}
214 call s:AddAnAutocmd()
215 au! vimBarTest|echo 'hello'
216 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
217endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200218
Bram Moolenaar5c809082016-09-01 16:21:48 +0200219func RemoveGroup()
220 autocmd! StartOK
221 augroup! StartOK
222endfunc
223
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200224func Test_augroup_warning()
225 augroup TheWarning
226 au VimEnter * echo 'entering'
227 augroup END
228 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
229 redir => res
230 augroup! TheWarning
231 redir END
232 call assert_true(match(res, "W19:") >= 0)
233 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
234
235 " check "Another" does not take the pace of the deleted entry
236 augroup Another
237 augroup END
238 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200239 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200240
241 " no warning for postpone aucmd delete
242 augroup StartOK
243 au VimEnter * call RemoveGroup()
244 augroup END
245 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
246 redir => res
247 doautocmd VimEnter
248 redir END
249 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200250 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200251endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200252
253func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200254 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200255 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200256 call assert_fails('augroup! x', 'E936:')
257 au VimEnter * echo
258 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200259 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200260 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
261 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200262endfunc
263
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200264" Tests for autocommands on :close command.
265" This used to be in test13.
266func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200267 " Clean up buffers, because in some cases this function fails.
268 call s:cleanup_buffers()
269
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200270 " Write three files and open them, each in a window.
271 " Then go to next window, with autocommand that deletes the previous one.
272 " Do this twice, writing the file.
273 e! Xtestje1
274 call setline(1, 'testje1')
275 w
276 sp Xtestje2
277 call setline(1, 'testje2')
278 w
279 sp Xtestje3
280 call setline(1, 'testje3')
281 w
282 wincmd w
283 au WinLeave Xtestje2 bwipe
284 wincmd w
285 call assert_equal('Xtestje1', expand('%'))
286
287 au WinLeave Xtestje1 bwipe Xtestje3
288 close
289 call assert_equal('Xtestje1', expand('%'))
290
291 " Test deleting the buffer on a Unload event. If this goes wrong there
292 " will be the ATTENTION prompt.
293 e Xtestje1
294 au!
295 au! BufUnload Xtestje1 bwipe
296 call assert_fails('e Xtestje3', 'E937:')
297 call assert_equal('Xtestje3', expand('%'))
298
299 e Xtestje2
300 sp Xtestje1
301 call assert_fails('e', 'E937:')
302 call assert_equal('Xtestje2', expand('%'))
303
304 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
305 " there are ml_line errors and/or a Crash.
306 au!
307 only
308 e Xanother
309 e Xtestje1
310 bwipe Xtestje2
311 bwipe Xtestje3
312 au BufWipeout Xtestje1 buf Xtestje1
313 bwipe
314 call assert_equal('Xanother', expand('%'))
315
316 only
317 help
318 wincmd w
319 1quit
320 call assert_equal('Xanother', expand('%'))
321
322 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100323 enew
324 bwipe! Xtestje1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200325 call delete('Xtestje1')
326 call delete('Xtestje2')
327 call delete('Xtestje3')
328endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100329
330func Test_BufEnter()
331 au! BufEnter
332 au Bufenter * let val = val . '+'
333 let g:val = ''
334 split NewFile
335 call assert_equal('+', g:val)
336 bwipe!
337 call assert_equal('++', g:val)
338
339 " Also get BufEnter when editing a directory
340 call mkdir('Xdir')
341 split Xdir
342 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100343
344 " On MS-Windows we can't edit the directory, make sure we wipe the right
345 " buffer.
346 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100347
348 call delete('Xdir', 'd')
349 au! BufEnter
350endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100351
352" Closing a window might cause an endless loop
353" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200354func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100355 tabnew
356 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100357 mksession!
358
Bram Moolenaare94260f2017-03-21 15:50:12 +0100359 let content = ['set nocp noswapfile',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100360 \ 'let v:swapchoice="e"',
361 \ 'augroup test_autocmd_sessionload',
362 \ 'autocmd!',
363 \ 'autocmd SessionLoadPost * 4bw!',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100364 \ 'augroup END',
365 \ '',
366 \ 'func WriteErrors()',
367 \ ' call writefile([execute("messages")], "Xerrors")',
368 \ 'endfunc',
369 \ 'au VimLeave * call WriteErrors()',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100370 \ ]
371 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100372 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
373 let errors = join(readfile('Xerrors'))
374 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100375
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100376 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100377 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100378 call delete(file)
379 endfor
380endfunc
381
382" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200383func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100384 tabnew
385 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100386 mksession!
387
388 let content = ['set nocp noswapfile',
389 \ 'function! DeleteInactiveBufs()',
390 \ ' tabfirst',
391 \ ' let tabblist = []',
392 \ ' for i in range(1, tabpagenr(''$''))',
393 \ ' call extend(tabblist, tabpagebuflist(i))',
394 \ ' endfor',
395 \ ' for b in range(1, bufnr(''$''))',
396 \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')',
397 \ ' exec ''bwipeout '' . b',
398 \ ' endif',
399 \ ' endfor',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100400 \ ' echomsg "SessionLoadPost DONE"',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100401 \ 'endfunction',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100402 \ 'au SessionLoadPost * call DeleteInactiveBufs()',
403 \ '',
404 \ 'func WriteErrors()',
405 \ ' call writefile([execute("messages")], "Xerrors")',
406 \ 'endfunc',
407 \ 'au VimLeave * call WriteErrors()',
408 \ ]
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100409 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100410 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
411 let errors = join(readfile('Xerrors'))
412 " This probably only ever matches on unix.
413 call assert_notmatch('Caught deadly signal SEGV', errors)
414 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100415
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100416 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100417 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100418 call delete(file)
419 endfor
420endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200421
422func Test_empty_doau()
423 doau \|
424endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200425
426func s:AutoCommandOptionSet(match)
427 let item = remove(g:options, 0)
428 let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3])
429 let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type)
430 let g:opt = [expected, actual]
431 "call assert_equal(expected, actual)
432endfunc
433
434func Test_OptionSet()
435 if !has("eval") || !has("autocmd") || !exists("+autochdir")
436 return
437 endif
438
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200439 badd test_autocmd.vim
440
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200441 call test_override('starting', 1)
442 set nocp
443 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
444
445 " 1: Setting number option"
446 let g:options=[['number', 0, 1, 'global']]
447 set nu
448 call assert_equal([], g:options)
449 call assert_equal(g:opt[0], g:opt[1])
450
451 " 2: Setting local number option"
452 let g:options=[['number', 1, 0, 'local']]
453 setlocal nonu
454 call assert_equal([], g:options)
455 call assert_equal(g:opt[0], g:opt[1])
456
457 " 3: Setting global number option"
458 let g:options=[['number', 1, 0, 'global']]
459 setglobal nonu
460 call assert_equal([], g:options)
461 call assert_equal(g:opt[0], g:opt[1])
462
463 " 4: Setting local autoindent option"
464 let g:options=[['autoindent', 0, 1, 'local']]
465 setlocal ai
466 call assert_equal([], g:options)
467 call assert_equal(g:opt[0], g:opt[1])
468
469 " 5: Setting global autoindent option"
470 let g:options=[['autoindent', 0, 1, 'global']]
471 setglobal ai
472 call assert_equal([], g:options)
473 call assert_equal(g:opt[0], g:opt[1])
474
475 " 6: Setting global autoindent option"
476 let g:options=[['autoindent', 1, 0, 'global']]
477 set ai!
478 call assert_equal([], g:options)
479 call assert_equal(g:opt[0], g:opt[1])
480
481 " Should not print anything, use :noa
482 " 7: don't trigger OptionSet"
483 let g:options=[['invalid', 1, 1, 'invalid']]
484 noa set nonu
485 call assert_equal([['invalid', 1, 1, 'invalid']], g:options)
486 call assert_equal(g:opt[0], g:opt[1])
487
488 " 8: Setting several global list and number option"
489 let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']]
490 set list nu
491 call assert_equal([], g:options)
492 call assert_equal(g:opt[0], g:opt[1])
493
494 " 9: don't trigger OptionSet"
495 let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']]
496 noa set nolist nonu
497 call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options)
498 call assert_equal(g:opt[0], g:opt[1])
499
500 " 10: Setting global acd"
501 let g:options=[['autochdir', 0, 1, 'local']]
502 setlocal acd
503 call assert_equal([], g:options)
504 call assert_equal(g:opt[0], g:opt[1])
505
506 " 11: Setting global autoread (also sets local value)"
507 let g:options=[['autoread', 0, 1, 'global']]
508 set ar
509 call assert_equal([], g:options)
510 call assert_equal(g:opt[0], g:opt[1])
511
512 " 12: Setting local autoread"
513 let g:options=[['autoread', 1, 1, 'local']]
514 setlocal ar
515 call assert_equal([], g:options)
516 call assert_equal(g:opt[0], g:opt[1])
517
518 " 13: Setting global autoread"
519 let g:options=[['autoread', 1, 0, 'global']]
520 setglobal invar
521 call assert_equal([], g:options)
522 call assert_equal(g:opt[0], g:opt[1])
523
524 " 14: Setting option backspace through :let"
525 let g:options=[['backspace', '', 'eol,indent,start', 'global']]
526 let &bs="eol,indent,start"
527 call assert_equal([], g:options)
528 call assert_equal(g:opt[0], g:opt[1])
529
530 " 15: Setting option backspace through setbufvar()"
531 let g:options=[['backup', 0, 1, 'local']]
532 " try twice, first time, shouldn't trigger because option name is invalid,
533 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200534 let bnum = bufnr('%')
535 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200536 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200537 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200538 call assert_equal([], g:options)
539 call assert_equal(g:opt[0], g:opt[1])
540
541 " 16: Setting number option using setwinvar"
542 let g:options=[['number', 0, 1, 'local']]
543 call setwinvar(0, '&number', 1)
544 call assert_equal([], g:options)
545 call assert_equal(g:opt[0], g:opt[1])
546
547 " 17: Setting key option, shouldn't trigger"
548 let g:options=[['key', 'invalid', 'invalid1', 'invalid']]
549 setlocal key=blah
550 setlocal key=
551 call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options)
552 call assert_equal(g:opt[0], g:opt[1])
553
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200554 " 18: Setting string option"
555 let oldval = &tags
556 let g:options=[['tags', oldval, 'tagpath', 'global']]
557 set tags=tagpath
558 call assert_equal([], g:options)
559 call assert_equal(g:opt[0], g:opt[1])
560
561 " 1l: Resetting string option"
562 let g:options=[['tags', 'tagpath', oldval, 'global']]
563 set tags&
564 call assert_equal([], g:options)
565 call assert_equal(g:opt[0], g:opt[1])
566
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200567 " Cleanup
568 au! OptionSet
569 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp']
570 exe printf(":set %s&vi", opt)
571 endfor
572 call test_override('starting', 0)
573 delfunc! AutoCommandOptionSet
574endfunc
575
576func Test_OptionSet_diffmode()
577 call test_override('starting', 1)
578 " 18: Changing an option when enetering diff mode
579 new
580 au OptionSet diff :let &l:cul=v:option_new
581
582 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
583 call assert_equal(0, &l:cul)
584 diffthis
585 call assert_equal(1, &l:cul)
586
587 vnew
588 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
589 call assert_equal(0, &l:cul)
590 diffthis
591 call assert_equal(1, &l:cul)
592
593 diffoff
594 call assert_equal(0, &l:cul)
595 call assert_equal(1, getwinvar(2, '&l:cul'))
596 bw!
597
598 call assert_equal(1, &l:cul)
599 diffoff!
600 call assert_equal(0, &l:cul)
601 call assert_equal(0, getwinvar(1, '&l:cul'))
602 bw!
603
604 " Cleanup
605 au! OptionSet
606 call test_override('starting', 0)
607endfunc
608
609func Test_OptionSet_diffmode_close()
610 call test_override('starting', 1)
611 " 19: Try to close the current window when entering diff mode
612 " should not segfault
613 new
614 au OptionSet diff close
615
616 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
617 call assert_fails(':diffthis', 'E788')
618 call assert_equal(1, &diff)
619 vnew
620 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
621 call assert_fails(':diffthis', 'E788')
622 call assert_equal(1, &diff)
623 bw!
624 call assert_fails(':diffoff!', 'E788')
625 bw!
626
627 " Cleanup
628 au! OptionSet
629 call test_override('starting', 0)
630 "delfunc! AutoCommandOptionSet
631endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200632
633" Test for Bufleave autocommand that deletes the buffer we are about to edit.
634func Test_BufleaveWithDelete()
635 new | edit Xfile1
636
637 augroup test_bufleavewithdelete
638 autocmd!
639 autocmd BufLeave Xfile1 bwipe Xfile2
640 augroup END
641
642 call assert_fails('edit Xfile2', 'E143:')
643 call assert_equal('Xfile1', bufname('%'))
644
645 autocmd! test_bufleavewithdelete BufLeave Xfile1
646 augroup! test_bufleavewithdelete
647
648 new
649 bwipe! Xfile1
650endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200651
652" Test for autocommand that changes the buffer list, when doing ":ball".
653func Test_Acmd_BufAll()
654 enew!
655 %bwipe!
656 call writefile(['Test file Xxx1'], 'Xxx1')
657 call writefile(['Test file Xxx2'], 'Xxx2')
658 call writefile(['Test file Xxx3'], 'Xxx3')
659
660 " Add three files to the buffer list
661 split Xxx1
662 close
663 split Xxx2
664 close
665 split Xxx3
666 close
667
668 " Wipe the buffer when the buffer is opened
669 au BufReadPost Xxx2 bwipe
670
671 call append(0, 'Test file Xxx4')
672 ball
673
674 call assert_equal(2, winnr('$'))
675 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
676 wincmd t
677
678 au! BufReadPost
679 %bwipe!
680 call delete('Xxx1')
681 call delete('Xxx2')
682 call delete('Xxx3')
683 enew! | only
684endfunc
685
686" Test for autocommand that changes current buffer on BufEnter event.
687" Check if modelines are interpreted for the correct buffer.
688func Test_Acmd_BufEnter()
689 %bwipe!
690 call writefile(['start of test file Xxx1',
691 \ "\<Tab>this is a test",
692 \ 'end of test file Xxx1'], 'Xxx1')
693 call writefile(['start of test file Xxx2',
694 \ 'vim: set noai :',
695 \ "\<Tab>this is a test",
696 \ 'end of test file Xxx2'], 'Xxx2')
697
698 au BufEnter Xxx2 brew
699 set ai modeline modelines=3
700 edit Xxx1
701 " edit Xxx2, autocmd will do :brew
702 edit Xxx2
703 exe "normal G?this is a\<CR>"
704 " Append text with autoindent to this file
705 normal othis should be auto-indented
706 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
707 call assert_equal(3, line('.'))
708 " Remove autocmd and edit Xxx2 again
709 au! BufEnter Xxx2
710 buf! Xxx2
711 exe "normal G?this is a\<CR>"
712 " append text without autoindent to Xxx
713 normal othis should be in column 1
714 call assert_equal("this should be in column 1", getline('.'))
715 call assert_equal(4, line('.'))
716
717 %bwipe!
718 call delete('Xxx1')
719 call delete('Xxx2')
720 set ai&vim modeline&vim modelines&vim
721endfunc
722
723" Test for issue #57
724" do not move cursor on <c-o> when autoindent is set
725func Test_ai_CTRL_O()
726 enew!
727 set ai
728 let save_fo = &fo
729 set fo+=r
730 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
731 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
732 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
733
734 set ai&vim
735 let &fo = save_fo
736 enew!
737endfunc
738
739" Test for autocommand that deletes the current buffer on BufLeave event.
740" Also test deleting the last buffer, should give a new, empty buffer.
741func Test_BufLeave_Wipe()
742 %bwipe!
743 let content = ['start of test file Xxx',
744 \ 'this is a test',
745 \ 'end of test file Xxx']
746 call writefile(content, 'Xxx1')
747 call writefile(content, 'Xxx2')
748
749 au BufLeave Xxx2 bwipe
750 edit Xxx1
751 split Xxx2
752 " delete buffer Xxx2, we should be back to Xxx1
753 bwipe
754 call assert_equal('Xxx1', bufname('%'))
755 call assert_equal(1, winnr('$'))
756
757 " Create an alternate buffer
758 %write! test.out
759 call assert_equal('test.out', bufname('#'))
760 " delete alternate buffer
761 bwipe test.out
762 call assert_equal('Xxx1', bufname('%'))
763 call assert_equal('', bufname('#'))
764
765 au BufLeave Xxx1 bwipe
766 " delete current buffer, get an empty one
767 bwipe!
768 call assert_equal(1, line('$'))
769 call assert_equal('', bufname('%'))
770 call assert_equal(1, len(getbufinfo()))
771
772 call delete('Xxx1')
773 call delete('Xxx2')
774 %bwipe
775 au! BufLeave
776endfunc