blob: 593d1cc4098864cb5eb7064bf3d5f2605a47b59b [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
252func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200253 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200254 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200255 call assert_fails('augroup! x', 'E936:')
256 au VimEnter * echo
257 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200258 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200259 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
260 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200261endfunc
262
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200263" Tests for autocommands on :close command.
264" This used to be in test13.
265func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200266 " Clean up buffers, because in some cases this function fails.
267 call s:cleanup_buffers()
268
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200269 " Write three files and open them, each in a window.
270 " Then go to next window, with autocommand that deletes the previous one.
271 " Do this twice, writing the file.
272 e! Xtestje1
273 call setline(1, 'testje1')
274 w
275 sp Xtestje2
276 call setline(1, 'testje2')
277 w
278 sp Xtestje3
279 call setline(1, 'testje3')
280 w
281 wincmd w
282 au WinLeave Xtestje2 bwipe
283 wincmd w
284 call assert_equal('Xtestje1', expand('%'))
285
286 au WinLeave Xtestje1 bwipe Xtestje3
287 close
288 call assert_equal('Xtestje1', expand('%'))
289
290 " Test deleting the buffer on a Unload event. If this goes wrong there
291 " will be the ATTENTION prompt.
292 e Xtestje1
293 au!
294 au! BufUnload Xtestje1 bwipe
295 call assert_fails('e Xtestje3', 'E937:')
296 call assert_equal('Xtestje3', expand('%'))
297
298 e Xtestje2
299 sp Xtestje1
300 call assert_fails('e', 'E937:')
301 call assert_equal('Xtestje2', expand('%'))
302
303 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
304 " there are ml_line errors and/or a Crash.
305 au!
306 only
307 e Xanother
308 e Xtestje1
309 bwipe Xtestje2
310 bwipe Xtestje3
311 au BufWipeout Xtestje1 buf Xtestje1
312 bwipe
313 call assert_equal('Xanother', expand('%'))
314
315 only
316 help
317 wincmd w
318 1quit
319 call assert_equal('Xanother', expand('%'))
320
321 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100322 enew
323 bwipe! Xtestje1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200324 call delete('Xtestje1')
325 call delete('Xtestje2')
326 call delete('Xtestje3')
327endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100328
329func Test_BufEnter()
330 au! BufEnter
331 au Bufenter * let val = val . '+'
332 let g:val = ''
333 split NewFile
334 call assert_equal('+', g:val)
335 bwipe!
336 call assert_equal('++', g:val)
337
338 " Also get BufEnter when editing a directory
339 call mkdir('Xdir')
340 split Xdir
341 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100342
343 " On MS-Windows we can't edit the directory, make sure we wipe the right
344 " buffer.
345 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100346
347 call delete('Xdir', 'd')
348 au! BufEnter
349endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100350
351" Closing a window might cause an endless loop
352" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200353func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200354 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100355 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200356 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100357 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100358 mksession!
359
Bram Moolenaare94260f2017-03-21 15:50:12 +0100360 let content = ['set nocp noswapfile',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100361 \ 'let v:swapchoice="e"',
362 \ 'augroup test_autocmd_sessionload',
363 \ 'autocmd!',
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200364 \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100365 \ 'augroup END',
366 \ '',
367 \ 'func WriteErrors()',
368 \ ' call writefile([execute("messages")], "Xerrors")',
369 \ 'endfunc',
370 \ 'au VimLeave * call WriteErrors()',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100371 \ ]
372 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100373 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
374 let errors = join(readfile('Xerrors'))
375 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100376
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100377 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100378 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100379 call delete(file)
380 endfor
381endfunc
382
383" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200384func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100385 tabnew
386 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100387 mksession!
388
389 let content = ['set nocp noswapfile',
390 \ 'function! DeleteInactiveBufs()',
391 \ ' tabfirst',
392 \ ' let tabblist = []',
393 \ ' for i in range(1, tabpagenr(''$''))',
394 \ ' call extend(tabblist, tabpagebuflist(i))',
395 \ ' endfor',
396 \ ' for b in range(1, bufnr(''$''))',
397 \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')',
398 \ ' exec ''bwipeout '' . b',
399 \ ' endif',
400 \ ' endfor',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100401 \ ' echomsg "SessionLoadPost DONE"',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100402 \ 'endfunction',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100403 \ 'au SessionLoadPost * call DeleteInactiveBufs()',
404 \ '',
405 \ 'func WriteErrors()',
406 \ ' call writefile([execute("messages")], "Xerrors")',
407 \ 'endfunc',
408 \ 'au VimLeave * call WriteErrors()',
409 \ ]
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100410 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100411 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
412 let errors = join(readfile('Xerrors'))
413 " This probably only ever matches on unix.
414 call assert_notmatch('Caught deadly signal SEGV', errors)
415 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100416
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100417 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100418 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100419 call delete(file)
420 endfor
421endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200422
423func Test_empty_doau()
424 doau \|
425endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200426
427func s:AutoCommandOptionSet(match)
428 let item = remove(g:options, 0)
429 let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3])
430 let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type)
431 let g:opt = [expected, actual]
432 "call assert_equal(expected, actual)
433endfunc
434
435func Test_OptionSet()
436 if !has("eval") || !has("autocmd") || !exists("+autochdir")
437 return
438 endif
439
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200440 badd test_autocmd.vim
441
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200442 call test_override('starting', 1)
443 set nocp
444 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
445
446 " 1: Setting number option"
447 let g:options=[['number', 0, 1, 'global']]
448 set nu
449 call assert_equal([], g:options)
450 call assert_equal(g:opt[0], g:opt[1])
451
452 " 2: Setting local number option"
453 let g:options=[['number', 1, 0, 'local']]
454 setlocal nonu
455 call assert_equal([], g:options)
456 call assert_equal(g:opt[0], g:opt[1])
457
458 " 3: Setting global number option"
459 let g:options=[['number', 1, 0, 'global']]
460 setglobal nonu
461 call assert_equal([], g:options)
462 call assert_equal(g:opt[0], g:opt[1])
463
464 " 4: Setting local autoindent option"
465 let g:options=[['autoindent', 0, 1, 'local']]
466 setlocal ai
467 call assert_equal([], g:options)
468 call assert_equal(g:opt[0], g:opt[1])
469
470 " 5: Setting global autoindent option"
471 let g:options=[['autoindent', 0, 1, 'global']]
472 setglobal ai
473 call assert_equal([], g:options)
474 call assert_equal(g:opt[0], g:opt[1])
475
476 " 6: Setting global autoindent option"
477 let g:options=[['autoindent', 1, 0, 'global']]
478 set ai!
479 call assert_equal([], g:options)
480 call assert_equal(g:opt[0], g:opt[1])
481
482 " Should not print anything, use :noa
483 " 7: don't trigger OptionSet"
484 let g:options=[['invalid', 1, 1, 'invalid']]
485 noa set nonu
486 call assert_equal([['invalid', 1, 1, 'invalid']], g:options)
487 call assert_equal(g:opt[0], g:opt[1])
488
489 " 8: Setting several global list and number option"
490 let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']]
491 set list nu
492 call assert_equal([], g:options)
493 call assert_equal(g:opt[0], g:opt[1])
494
495 " 9: don't trigger OptionSet"
496 let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']]
497 noa set nolist nonu
498 call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options)
499 call assert_equal(g:opt[0], g:opt[1])
500
501 " 10: Setting global acd"
502 let g:options=[['autochdir', 0, 1, 'local']]
503 setlocal acd
504 call assert_equal([], g:options)
505 call assert_equal(g:opt[0], g:opt[1])
506
507 " 11: Setting global autoread (also sets local value)"
508 let g:options=[['autoread', 0, 1, 'global']]
509 set ar
510 call assert_equal([], g:options)
511 call assert_equal(g:opt[0], g:opt[1])
512
513 " 12: Setting local autoread"
514 let g:options=[['autoread', 1, 1, 'local']]
515 setlocal ar
516 call assert_equal([], g:options)
517 call assert_equal(g:opt[0], g:opt[1])
518
519 " 13: Setting global autoread"
520 let g:options=[['autoread', 1, 0, 'global']]
521 setglobal invar
522 call assert_equal([], g:options)
523 call assert_equal(g:opt[0], g:opt[1])
524
525 " 14: Setting option backspace through :let"
526 let g:options=[['backspace', '', 'eol,indent,start', 'global']]
527 let &bs="eol,indent,start"
528 call assert_equal([], g:options)
529 call assert_equal(g:opt[0], g:opt[1])
530
531 " 15: Setting option backspace through setbufvar()"
532 let g:options=[['backup', 0, 1, 'local']]
533 " try twice, first time, shouldn't trigger because option name is invalid,
534 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200535 let bnum = bufnr('%')
536 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200537 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200538 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200539 call assert_equal([], g:options)
540 call assert_equal(g:opt[0], g:opt[1])
541
542 " 16: Setting number option using setwinvar"
543 let g:options=[['number', 0, 1, 'local']]
544 call setwinvar(0, '&number', 1)
545 call assert_equal([], g:options)
546 call assert_equal(g:opt[0], g:opt[1])
547
548 " 17: Setting key option, shouldn't trigger"
549 let g:options=[['key', 'invalid', 'invalid1', 'invalid']]
550 setlocal key=blah
551 setlocal key=
552 call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options)
553 call assert_equal(g:opt[0], g:opt[1])
554
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200555 " 18: Setting string option"
556 let oldval = &tags
557 let g:options=[['tags', oldval, 'tagpath', 'global']]
558 set tags=tagpath
559 call assert_equal([], g:options)
560 call assert_equal(g:opt[0], g:opt[1])
561
562 " 1l: Resetting string option"
563 let g:options=[['tags', 'tagpath', oldval, 'global']]
564 set tags&
565 call assert_equal([], g:options)
566 call assert_equal(g:opt[0], g:opt[1])
567
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200568 " Cleanup
569 au! OptionSet
570 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp']
571 exe printf(":set %s&vi", opt)
572 endfor
573 call test_override('starting', 0)
574 delfunc! AutoCommandOptionSet
575endfunc
576
577func Test_OptionSet_diffmode()
578 call test_override('starting', 1)
579 " 18: Changing an option when enetering diff mode
580 new
581 au OptionSet diff :let &l:cul=v:option_new
582
583 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
584 call assert_equal(0, &l:cul)
585 diffthis
586 call assert_equal(1, &l:cul)
587
588 vnew
589 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
590 call assert_equal(0, &l:cul)
591 diffthis
592 call assert_equal(1, &l:cul)
593
594 diffoff
595 call assert_equal(0, &l:cul)
596 call assert_equal(1, getwinvar(2, '&l:cul'))
597 bw!
598
599 call assert_equal(1, &l:cul)
600 diffoff!
601 call assert_equal(0, &l:cul)
602 call assert_equal(0, getwinvar(1, '&l:cul'))
603 bw!
604
605 " Cleanup
606 au! OptionSet
607 call test_override('starting', 0)
608endfunc
609
610func Test_OptionSet_diffmode_close()
611 call test_override('starting', 1)
612 " 19: Try to close the current window when entering diff mode
613 " should not segfault
614 new
615 au OptionSet diff close
616
617 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
618 call assert_fails(':diffthis', 'E788')
619 call assert_equal(1, &diff)
620 vnew
621 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
622 call assert_fails(':diffthis', 'E788')
623 call assert_equal(1, &diff)
624 bw!
625 call assert_fails(':diffoff!', 'E788')
626 bw!
627
628 " Cleanup
629 au! OptionSet
630 call test_override('starting', 0)
631 "delfunc! AutoCommandOptionSet
632endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200633
634" Test for Bufleave autocommand that deletes the buffer we are about to edit.
635func Test_BufleaveWithDelete()
636 new | edit Xfile1
637
638 augroup test_bufleavewithdelete
639 autocmd!
640 autocmd BufLeave Xfile1 bwipe Xfile2
641 augroup END
642
643 call assert_fails('edit Xfile2', 'E143:')
644 call assert_equal('Xfile1', bufname('%'))
645
646 autocmd! test_bufleavewithdelete BufLeave Xfile1
647 augroup! test_bufleavewithdelete
648
649 new
650 bwipe! Xfile1
651endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200652
653" Test for autocommand that changes the buffer list, when doing ":ball".
654func Test_Acmd_BufAll()
655 enew!
656 %bwipe!
657 call writefile(['Test file Xxx1'], 'Xxx1')
658 call writefile(['Test file Xxx2'], 'Xxx2')
659 call writefile(['Test file Xxx3'], 'Xxx3')
660
661 " Add three files to the buffer list
662 split Xxx1
663 close
664 split Xxx2
665 close
666 split Xxx3
667 close
668
669 " Wipe the buffer when the buffer is opened
670 au BufReadPost Xxx2 bwipe
671
672 call append(0, 'Test file Xxx4')
673 ball
674
675 call assert_equal(2, winnr('$'))
676 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
677 wincmd t
678
679 au! BufReadPost
680 %bwipe!
681 call delete('Xxx1')
682 call delete('Xxx2')
683 call delete('Xxx3')
684 enew! | only
685endfunc
686
687" Test for autocommand that changes current buffer on BufEnter event.
688" Check if modelines are interpreted for the correct buffer.
689func Test_Acmd_BufEnter()
690 %bwipe!
691 call writefile(['start of test file Xxx1',
692 \ "\<Tab>this is a test",
693 \ 'end of test file Xxx1'], 'Xxx1')
694 call writefile(['start of test file Xxx2',
695 \ 'vim: set noai :',
696 \ "\<Tab>this is a test",
697 \ 'end of test file Xxx2'], 'Xxx2')
698
699 au BufEnter Xxx2 brew
700 set ai modeline modelines=3
701 edit Xxx1
702 " edit Xxx2, autocmd will do :brew
703 edit Xxx2
704 exe "normal G?this is a\<CR>"
705 " Append text with autoindent to this file
706 normal othis should be auto-indented
707 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
708 call assert_equal(3, line('.'))
709 " Remove autocmd and edit Xxx2 again
710 au! BufEnter Xxx2
711 buf! Xxx2
712 exe "normal G?this is a\<CR>"
713 " append text without autoindent to Xxx
714 normal othis should be in column 1
715 call assert_equal("this should be in column 1", getline('.'))
716 call assert_equal(4, line('.'))
717
718 %bwipe!
719 call delete('Xxx1')
720 call delete('Xxx2')
721 set ai&vim modeline&vim modelines&vim
722endfunc
723
724" Test for issue #57
725" do not move cursor on <c-o> when autoindent is set
726func Test_ai_CTRL_O()
727 enew!
728 set ai
729 let save_fo = &fo
730 set fo+=r
731 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
732 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
733 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
734
735 set ai&vim
736 let &fo = save_fo
737 enew!
738endfunc
739
740" Test for autocommand that deletes the current buffer on BufLeave event.
741" Also test deleting the last buffer, should give a new, empty buffer.
742func Test_BufLeave_Wipe()
743 %bwipe!
744 let content = ['start of test file Xxx',
745 \ 'this is a test',
746 \ 'end of test file Xxx']
747 call writefile(content, 'Xxx1')
748 call writefile(content, 'Xxx2')
749
750 au BufLeave Xxx2 bwipe
751 edit Xxx1
752 split Xxx2
753 " delete buffer Xxx2, we should be back to Xxx1
754 bwipe
755 call assert_equal('Xxx1', bufname('%'))
756 call assert_equal(1, winnr('$'))
757
758 " Create an alternate buffer
759 %write! test.out
760 call assert_equal('test.out', bufname('#'))
761 " delete alternate buffer
762 bwipe test.out
763 call assert_equal('Xxx1', bufname('%'))
764 call assert_equal('', bufname('#'))
765
766 au BufLeave Xxx1 bwipe
767 " delete current buffer, get an empty one
768 bwipe!
769 call assert_equal(1, line('$'))
770 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200771 let g:bufinfo = getbufinfo()
772 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200773
774 call delete('Xxx1')
775 call delete('Xxx2')
776 %bwipe
777 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200778
779 " check that bufinfo doesn't contain a pointer to freed memory
780 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200781endfunc