blob: 61adcae33fd265f96ea12661fe90fcef74bf26ec [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
4
Bram Moolenaar04f62f82017-07-19 18:18:39 +02005func! s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02006 for bnr in range(1, bufnr('$'))
7 if bufloaded(bnr) && bufnr('%') != bnr
8 execute 'bd! ' . bnr
9 endif
10 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020011endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020012
Bram Moolenaar14735512016-03-26 21:00:08 +010013func Test_vim_did_enter()
14 call assert_false(v:vim_did_enter)
15
16 " This script will never reach the main loop, can't check if v:vim_did_enter
17 " becomes one.
18endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020019
Bram Moolenaarc67e8922016-05-24 16:07:40 +020020if has('timers')
21 func ExitInsertMode(id)
22 call feedkeys("\<Esc>")
23 endfunc
24
25 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020026 " Need to move the cursor.
27 call feedkeys("ggG", "xt")
28
Bram Moolenaarc67e8922016-05-24 16:07:40 +020029 let g:triggered = 0
30 au CursorHoldI * let g:triggered += 1
31 set updatetime=20
32 call timer_start(100, 'ExitInsertMode')
33 call feedkeys('a', 'x!')
34 call assert_equal(1, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020035 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020036 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020037 endfunc
38
39 func Test_cursorhold_insert_ctrl_x()
40 let g:triggered = 0
41 au CursorHoldI * let g:triggered += 1
42 set updatetime=20
43 call timer_start(100, 'ExitInsertMode')
44 " CursorHoldI does not trigger after CTRL-X
45 call feedkeys("a\<C-X>", 'x!')
46 call assert_equal(0, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020047 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020048 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020049 endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020050endif
51
Bram Moolenaar04f62f82017-07-19 18:18:39 +020052func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +020053 augroup test_bufunload_group
54 autocmd!
55 autocmd BufUnload * call add(s:li, "bufunload")
56 autocmd BufDelete * call add(s:li, "bufdelete")
57 autocmd BufWipeout * call add(s:li, "bufwipeout")
58 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +020059
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let s:li=[]
61 new
62 setlocal bufhidden=
63 bunload
64 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +020065
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 let s:li=[]
67 new
68 setlocal bufhidden=delete
69 bunload
70 call assert_equal(["bufunload", "bufdelete"], s:li)
71
72 let s:li=[]
73 new
74 setlocal bufhidden=unload
75 bwipeout
76 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
77
Bram Moolenaare99e8442016-07-26 20:43:40 +020078 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +020079 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +020080endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +020081
82" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +020083func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +020084 tabedit
85 tabfirst
86
87 augroup test_autocmd_bufunload_with_tabnext_group
88 autocmd!
89 autocmd BufUnload <buffer> tabnext
90 augroup END
91
92 quit
93 call assert_equal(2, tabpagenr('$'))
94
Bram Moolenaare0ab94e2016-09-04 19:50:54 +020095 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +020096 augroup! test_autocmd_bufunload_with_tabnext_group
97 tablast
98 quit
99endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200100
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200101func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200102 tabedit
103 augroup sample
104 autocmd!
105 autocmd BufWinLeave <buffer> tabfirst
106 augroup END
107 call setline(1, ['a', 'b', 'c'])
108 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200109 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200110endfunc
111
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200112" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200113func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200114 split aa.txt
115 let lastbuf = bufnr('$')
116
117 augroup test_autocmd_bufunload
118 autocmd!
119 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
120 augroup END
121
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
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200253func Test_BufReadCmdHelp()
254 " This used to cause access to free memory
255 au BufReadCmd * e +h
256 help
257
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200258 au! BufReadCmd
259endfunc
260
261func Test_BufReadCmdHelpJump()
262 " This used to cause access to free memory
263 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200264 " } to fix highlighting
265 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200266
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200267 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()
Bram Moolenaar153b7042018-01-31 15:48:32 +0100817 au! CmdlineChanged : let g:text = getcmdline()
818 let g:text = 0
819 call feedkeys(":echom 'hello'\<CR>", 'xt')
820 call assert_equal("echom 'hello'", g:text)
821 au! CmdlineChanged
822
823 au! CmdlineChanged : let g:entered = expand('<afile>')
824 let g:entered = 0
825 call feedkeys(":echom 'hello'\<CR>", 'xt')
826 call assert_equal(':', g:entered)
827 au! CmdlineChanged
828
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200829 au! CmdlineEnter : let g:entered = expand('<afile>')
830 au! CmdlineLeave : let g:left = expand('<afile>')
831 let g:entered = 0
832 let g:left = 0
833 call feedkeys(":echo 'hello'\<CR>", 'xt')
834 call assert_equal(':', g:entered)
835 call assert_equal(':', g:left)
836 au! CmdlineEnter
837 au! CmdlineLeave
838
839 au! CmdlineEnter / let g:entered = expand('<afile>')
840 au! CmdlineLeave / let g:left = expand('<afile>')
841 let g:entered = 0
842 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200843 new
844 call setline(1, 'hello')
845 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200846 call assert_equal('/', g:entered)
847 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200848 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200849 au! CmdlineEnter
850 au! CmdlineLeave
851endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200852
853" Test for BufWritePre autocommand that deletes or unloads the buffer.
854func Test_BufWritePre()
855 %bwipe
856 au BufWritePre Xxx1 bunload
857 au BufWritePre Xxx2 bwipe
858
859 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
860 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
861
862 edit Xtest
863 e! Xxx2
864 bdel Xtest
865 e Xxx1
866 " write it, will unload it and give an error msg
867 call assert_fails('w', 'E203')
868 call assert_equal('Xxx2', bufname('%'))
869 edit Xtest
870 e! Xxx2
871 bwipe Xtest
872 " write it, will delete the buffer and give an error msg
873 call assert_fails('w', 'E203')
874 call assert_equal('Xxx1', bufname('%'))
875 au! BufWritePre
876 call delete('Xxx1')
877 call delete('Xxx2')
878endfunc
879
880" Test for BufUnload autocommand that unloads all the other buffers
881func Test_bufunload_all()
882 call writefile(['Test file Xxx1'], 'Xxx1')"
883 call writefile(['Test file Xxx2'], 'Xxx2')"
884
885 let content = [
886 \ "func UnloadAllBufs()",
887 \ " let i = 1",
888 \ " while i <= bufnr('$')",
889 \ " if i != bufnr('%') && bufloaded(i)",
890 \ " exe i . 'bunload'",
891 \ " endif",
892 \ " let i += 1",
893 \ " endwhile",
894 \ "endfunc",
895 \ "au BufUnload * call UnloadAllBufs()",
896 \ "au VimLeave * call writefile(['Test Finished'], 'Xout')",
897 \ "edit Xxx1",
898 \ "split Xxx2",
899 \ "q"]
900 call writefile(content, 'Xtest')
901
902 call delete('Xout')
903 call system(v:progpath. ' --clean -N --not-a-term -S Xtest')
904 call assert_true(filereadable('Xout'))
905
906 call delete('Xxx1')
907 call delete('Xxx2')
908 call delete('Xtest')
909 call delete('Xout')
910endfunc
911
912" Some tests for buffer-local autocommands
913func Test_buflocal_autocmd()
914 let g:bname = ''
915 edit xx
916 au BufLeave <buffer> let g:bname = expand("%")
917 " here, autocommand for xx should trigger.
918 " but autocommand shall not apply to buffer named <buffer>.
919 edit somefile
920 call assert_equal('xx', g:bname)
921 let g:bname = ''
922 " here, autocommand shall be auto-deleted
923 bwipe xx
924 " autocmd should not trigger
925 edit xx
926 call assert_equal('', g:bname)
927 " autocmd should not trigger
928 edit somefile
929 call assert_equal('', g:bname)
930 enew
931 unlet g:bname
932endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +0100933
934" Test for "*Cmd" autocommands
935func Test_Cmd_Autocmds()
936 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
937
938 enew!
939 au BufReadCmd XtestA 0r Xxx|$del
940 edit XtestA " will read text of Xxd instead
941 call assert_equal('start of Xxx', getline(1))
942
943 au BufWriteCmd XtestA call append(line("$"), "write")
944 write " will append a line to the file
945 call assert_equal('write', getline('$'))
946 call assert_fails('read XtestA', 'E484') " should not read anything
947 call assert_equal('write', getline(4))
948
949 " now we have:
950 " 1 start of Xxx
951 " 2 abc2
952 " 3 end of Xxx
953 " 4 write
954
955 au FileReadCmd XtestB '[r Xxx
956 2r XtestB " will read Xxx below line 2 instead
957 call assert_equal('start of Xxx', getline(3))
958
959 " now we have:
960 " 1 start of Xxx
961 " 2 abc2
962 " 3 start of Xxx
963 " 4 abc2
964 " 5 end of Xxx
965 " 6 end of Xxx
966 " 7 write
967
968 au FileWriteCmd XtestC '[,']copy $
969 normal 4GA1
970 4,5w XtestC " will copy lines 4 and 5 to the end
971 call assert_equal("\tabc21", getline(8))
972 call assert_fails('r XtestC', 'E484') " should not read anything
973 call assert_equal("end of Xxx", getline(9))
974
975 " now we have:
976 " 1 start of Xxx
977 " 2 abc2
978 " 3 start of Xxx
979 " 4 abc21
980 " 5 end of Xxx
981 " 6 end of Xxx
982 " 7 write
983 " 8 abc21
984 " 9 end of Xxx
985
986 let g:lines = []
987 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
988 w >>XtestD " will add lines to 'lines'
989 call assert_equal(9, len(g:lines))
990 call assert_fails('$r XtestD', 'E484') " should not read anything
991 call assert_equal(9, line('$'))
992 call assert_equal('end of Xxx', getline('$'))
993
994 au BufReadCmd XtestE 0r Xxx|$del
995 sp XtestE " split window with test.out
996 call assert_equal('end of Xxx', getline(3))
997
998 let g:lines = []
999 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1000 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1001 wall " will write other window to 'lines'
1002 call assert_equal(4, len(g:lines), g:lines)
1003 call assert_equal('asdf', g:lines[2])
1004
1005 au! BufReadCmd
1006 au! BufWriteCmd
1007 au! FileReadCmd
1008 au! FileWriteCmd
1009 au! FileAppendCmd
1010 %bwipe!
1011 call delete('Xxx')
1012 enew!
1013endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001014
1015func SetChangeMarks(start, end)
1016 exe a:start. 'mark ['
1017 exe a:end. 'mark ]'
1018endfunc
1019
1020" Verify the effects of autocmds on '[ and ']
1021func Test_change_mark_in_autocmds()
1022 edit! Xtest
1023 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1024
1025 call SetChangeMarks(2, 3)
1026 write
1027 call assert_equal([1, 4], [line("'["), line("']")])
1028
1029 call SetChangeMarks(2, 3)
1030 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1031 write
1032 au! BufWritePre
1033
1034 if executable('cat')
1035 write XtestFilter
1036 write >> XtestFilter
1037
1038 call SetChangeMarks(2, 3)
1039 " Marks are set to the entire range of the write
1040 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1041 " '[ is adjusted to just before the line that will receive the filtered
1042 " data
1043 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1044 " The filtered data is read into the buffer, and the source lines are
1045 " still present, so the range is after the source lines
1046 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1047 %!cat XtestFilter
1048 " After the filtered data is read, the original lines are deleted
1049 call assert_equal([1, 8], [line("'["), line("']")])
1050 au! FilterWritePre,FilterReadPre,FilterReadPost
1051 undo
1052
1053 call SetChangeMarks(1, 4)
1054 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1055 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1056 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1057 2,3!cat XtestFilter
1058 call assert_equal([2, 9], [line("'["), line("']")])
1059 au! FilterWritePre,FilterReadPre,FilterReadPost
1060 undo
1061
1062 call delete('XtestFilter')
1063 endif
1064
1065 call SetChangeMarks(1, 4)
1066 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1067 2,3write Xtest2
1068 au! FileWritePre
1069
1070 call SetChangeMarks(2, 3)
1071 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1072 write >> Xtest2
1073 au! FileAppendPre
1074
1075 call SetChangeMarks(1, 4)
1076 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1077 2,3write >> Xtest2
1078 au! FileAppendPre
1079
1080 call SetChangeMarks(1, 1)
1081 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1082 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1083 3read Xtest2
1084 au! FileReadPre,FileReadPost
1085 undo
1086
1087 call SetChangeMarks(4, 4)
1088 " When the line is 0, it's adjusted to 1
1089 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1090 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1091 0read Xtest2
1092 au! FileReadPre,FileReadPost
1093 undo
1094
1095 call SetChangeMarks(4, 4)
1096 " When the line is 0, it's adjusted to 1
1097 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1098 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1099 1read Xtest2
1100 au! FileReadPre,FileReadPost
1101 undo
1102
1103 bwipe!
1104 call delete('Xtest')
1105 call delete('Xtest2')
1106endfunc
1107
1108func Test_Filter_noshelltemp()
1109 if !executable('cat')
1110 return
1111 endif
1112
1113 enew!
1114 call setline(1, ['a', 'b', 'c', 'd'])
1115
1116 let shelltemp = &shelltemp
1117 set shelltemp
1118
1119 let g:filter_au = 0
1120 au FilterWritePre * let g:filter_au += 1
1121 au FilterReadPre * let g:filter_au += 1
1122 au FilterReadPost * let g:filter_au += 1
1123 %!cat
1124 call assert_equal(3, g:filter_au)
1125
1126 if has('filterpipe')
1127 set noshelltemp
1128
1129 let g:filter_au = 0
1130 au FilterWritePre * let g:filter_au += 1
1131 au FilterReadPre * let g:filter_au += 1
1132 au FilterReadPost * let g:filter_au += 1
1133 %!cat
1134 call assert_equal(0, g:filter_au)
1135 endif
1136
1137 au! FilterWritePre,FilterReadPre,FilterReadPost
1138 let &shelltemp = shelltemp
1139 bwipe!
1140endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001141
1142func Test_TextYankPost()
1143 enew!
1144 call setline(1, ['foo'])
1145
1146 let g:event = []
1147 au TextYankPost * let g:event = copy(v:event)
1148
1149 call assert_equal({}, v:event)
1150 call assert_fails('let v:event = {}', 'E46:')
1151 call assert_fails('let v:event.mykey = 0', 'E742:')
1152
1153 norm "ayiw
1154 call assert_equal(
1155 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'},
1156 \g:event)
1157 norm y_
1158 call assert_equal(
1159 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'},
1160 \g:event)
1161 call feedkeys("\<C-V>y", 'x')
1162 call assert_equal(
1163 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"},
1164 \g:event)
1165 norm "xciwbar
1166 call assert_equal(
1167 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'},
1168 \g:event)
1169 norm "bdiw
1170 call assert_equal(
1171 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'},
1172 \g:event)
1173
1174 call assert_equal({}, v:event)
1175
1176 au! TextYankPost
1177 unlet g:event
1178 bwipe!
1179endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001180
1181func Test_nocatch_wipe_all_buffers()
1182 " Real nasty autocommand: wipe all buffers on any event.
1183 au * * bwipe *
1184 call assert_fails('next x', 'E93')
1185 bwipe
1186 au!
1187endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001188
1189func Test_nocatch_wipe_dummy_buffer()
1190 " Nasty autocommand: wipe buffer on any event.
1191 au * x bwipe
1192 call assert_fails('lv½ /x', 'E480')
1193 au!
1194endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001195
1196function s:Before_test_dirchanged()
1197 augroup test_dirchanged
1198 autocmd!
1199 augroup END
1200 let s:li = []
1201 let s:dir_this = getcwd()
1202 let s:dir_other = s:dir_this . '/foo'
1203 call mkdir(s:dir_other)
1204endfunc
1205
1206function s:After_test_dirchanged()
1207 exe 'cd' s:dir_this
1208 call delete(s:dir_other, 'd')
1209 augroup test_dirchanged
1210 autocmd!
1211 augroup END
1212endfunc
1213
1214function Test_dirchanged_global()
1215 call s:Before_test_dirchanged()
1216 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1217 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
1218 exe 'cd' s:dir_other
1219 call assert_equal(["cd:", s:dir_other], s:li)
1220 exe 'lcd' s:dir_other
1221 call assert_equal(["cd:", s:dir_other], s:li)
1222 call s:After_test_dirchanged()
1223endfunc
1224
1225function Test_dirchanged_local()
1226 call s:Before_test_dirchanged()
1227 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1228 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
1229 exe 'cd' s:dir_other
1230 call assert_equal([], s:li)
1231 exe 'lcd' s:dir_other
1232 call assert_equal(["lcd:", s:dir_other], s:li)
1233 call s:After_test_dirchanged()
1234endfunc
1235
1236function Test_dirchanged_auto()
Bram Moolenaarec48a9c2018-02-03 20:11:40 +01001237 if !exists('+autochdir')
1238 return
1239 endif
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001240 call s:Before_test_dirchanged()
1241 call test_autochdir()
1242 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1243 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1244 set acd
1245 exe 'cd ..'
1246 call assert_equal([], s:li)
1247 exe 'edit ' . s:dir_other . '/Xfile'
1248 call assert_equal(s:dir_other, getcwd())
1249 call assert_equal(["auto:", s:dir_other], s:li)
1250 set noacd
1251 bwipe!
1252 call s:After_test_dirchanged()
1253endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001254
1255" Test TextChangedI and TextChangedP
1256func Test_ChangedP()
1257 new
1258 call setline(1, ['foo', 'bar', 'foobar'])
1259 call test_override("char_avail", 1)
1260 set complete=. completeopt=menuone
1261
1262 func! TextChangedAutocmd(char)
1263 let g:autocmd .= a:char
1264 endfunc
1265
1266 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1267 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1268 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1269
1270 call cursor(3, 1)
1271 let g:autocmd = ''
1272 call feedkeys("o\<esc>", 'tnix')
1273 call assert_equal('I', g:autocmd)
1274
1275 let g:autocmd = ''
1276 call feedkeys("Sf", 'tnix')
1277 call assert_equal('II', g:autocmd)
1278
1279 let g:autocmd = ''
1280 call feedkeys("Sf\<C-N>", 'tnix')
1281 call assert_equal('IIP', g:autocmd)
1282
1283 let g:autocmd = ''
1284 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1285 call assert_equal('IIPP', g:autocmd)
1286
1287 let g:autocmd = ''
1288 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1289 call assert_equal('IIPPP', g:autocmd)
1290
1291 let g:autocmd = ''
1292 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1293 call assert_equal('IIPPPP', g:autocmd)
1294
1295 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1296 " TODO: how should it handle completeopt=noinsert,noselect?
1297
1298 " CleanUp
1299 call test_override("char_avail", 0)
1300 au! TextChanged
1301 au! TextChangedI
1302 au! TextChangedP
1303 delfu TextChangedAutocmd
1304 unlet! g:autocmd
1305 set complete&vim completeopt&vim
1306
1307 bw!
1308endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001309
1310func Test_Changed_FirstTime()
1311 if !has('terminal') || has('gui_running')
1312 return
1313 endif
1314 " Prepare file for TextChanged event.
1315 call writefile([''], 'Xchanged.txt')
1316 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1317 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001318 " Wait for the ruler (in the status line) to be shown.
1319 call WaitFor({-> term_getline(buf, 3) =~# '\<All$'})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001320 " It's only adding autocmd, so that no event occurs.
1321 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1322 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
1323 call WaitFor({-> term_getstatus(buf) == 'finished'})
1324 call assert_equal([''], readfile('Xchanged.txt'))
1325
1326 " clean up
1327 call delete('Xchanged.txt')
1328 bwipe!
1329endfunc