blob: f05a55f1aaa6e2659275e551335b71a593530abf [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
3func Test_vim_did_enter()
4 call assert_false(v:vim_did_enter)
5
6 " This script will never reach the main loop, can't check if v:vim_did_enter
7 " becomes one.
8endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +02009
Bram Moolenaarc67e8922016-05-24 16:07:40 +020010if has('timers')
11 func ExitInsertMode(id)
12 call feedkeys("\<Esc>")
13 endfunc
14
15 func Test_cursorhold_insert()
16 let g:triggered = 0
17 au CursorHoldI * let g:triggered += 1
18 set updatetime=20
19 call timer_start(100, 'ExitInsertMode')
20 call feedkeys('a', 'x!')
21 call assert_equal(1, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020022 au! CursorHoldI
Bram Moolenaarc67e8922016-05-24 16:07:40 +020023 endfunc
24
25 func Test_cursorhold_insert_ctrl_x()
26 let g:triggered = 0
27 au CursorHoldI * let g:triggered += 1
28 set updatetime=20
29 call timer_start(100, 'ExitInsertMode')
30 " CursorHoldI does not trigger after CTRL-X
31 call feedkeys("a\<C-X>", 'x!')
32 call assert_equal(0, g:triggered)
Bram Moolenaare99e8442016-07-26 20:43:40 +020033 au! CursorHoldI
Bram Moolenaarc67e8922016-05-24 16:07:40 +020034 endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020035endif
36
Bram Moolenaarc67e8922016-05-24 16:07:40 +020037function Test_bufunload()
38 augroup test_bufunload_group
39 autocmd!
40 autocmd BufUnload * call add(s:li, "bufunload")
41 autocmd BufDelete * call add(s:li, "bufdelete")
42 autocmd BufWipeout * call add(s:li, "bufwipeout")
43 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +020044
Bram Moolenaarc67e8922016-05-24 16:07:40 +020045 let s:li=[]
46 new
47 setlocal bufhidden=
48 bunload
49 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +020050
Bram Moolenaarc67e8922016-05-24 16:07:40 +020051 let s:li=[]
52 new
53 setlocal bufhidden=delete
54 bunload
55 call assert_equal(["bufunload", "bufdelete"], s:li)
56
57 let s:li=[]
58 new
59 setlocal bufhidden=unload
60 bwipeout
61 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
62
Bram Moolenaare99e8442016-07-26 20:43:40 +020063 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +020065endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +020066
67" SEGV occurs in older versions. (At least 7.4.2005 or older)
68function Test_autocmd_bufunload_with_tabnext()
69 tabedit
70 tabfirst
71
72 augroup test_autocmd_bufunload_with_tabnext_group
73 autocmd!
74 autocmd BufUnload <buffer> tabnext
75 augroup END
76
77 quit
78 call assert_equal(2, tabpagenr('$'))
79
80 augroup! test_autocmd_bufunload_with_tabnext_group
81 tablast
82 quit
83endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +020084
85func Test_win_tab_autocmd()
86 let g:record = []
87
88 augroup testing
89 au WinNew * call add(g:record, 'WinNew')
90 au WinEnter * call add(g:record, 'WinEnter')
91 au WinLeave * call add(g:record, 'WinLeave')
92 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +020093 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +020094 au TabEnter * call add(g:record, 'TabEnter')
95 au TabLeave * call add(g:record, 'TabLeave')
96 augroup END
97
98 split
99 tabnew
100 close
101 close
102
103 call assert_equal([
104 \ 'WinLeave', 'WinNew', 'WinEnter',
105 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200106 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200107 \ 'WinLeave', 'WinEnter'
108 \ ], g:record)
109
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200110 let g:record = []
111 tabnew somefile
112 tabnext
113 bwipe somefile
114
115 call assert_equal([
116 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
117 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
118 \ 'TabClosed'
119 \ ], g:record)
120
Bram Moolenaarc917da42016-07-19 22:31:36 +0200121 augroup testing
122 au!
123 augroup END
124 unlet g:record
125endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200126
127func s:AddAnAutocmd()
128 augroup vimBarTest
129 au BufReadCmd * echo 'hello'
130 augroup END
131 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
132endfunc
133
134func Test_early_bar()
135 " test that a bar is recognized before the {event}
136 call s:AddAnAutocmd()
137 augroup vimBarTest | au! | augroup END
138 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
139
140 call s:AddAnAutocmd()
141 augroup vimBarTest| au!| augroup END
142 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
143
144 " test that a bar is recognized after the {event}
145 call s:AddAnAutocmd()
146 augroup vimBarTest| au!BufReadCmd| augroup END
147 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
148
149 " test that a bar is recognized after the {group}
150 call s:AddAnAutocmd()
151 au! vimBarTest|echo 'hello'
152 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
153endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200154
Bram Moolenaar5c809082016-09-01 16:21:48 +0200155func RemoveGroup()
156 autocmd! StartOK
157 augroup! StartOK
158endfunc
159
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200160func Test_augroup_warning()
161 augroup TheWarning
162 au VimEnter * echo 'entering'
163 augroup END
164 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
165 redir => res
166 augroup! TheWarning
167 redir END
168 call assert_true(match(res, "W19:") >= 0)
169 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
170
171 " check "Another" does not take the pace of the deleted entry
172 augroup Another
173 augroup END
174 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaar5c809082016-09-01 16:21:48 +0200175
176 " no warning for postpone aucmd delete
177 augroup StartOK
178 au VimEnter * call RemoveGroup()
179 augroup END
180 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
181 redir => res
182 doautocmd VimEnter
183 redir END
184 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200185 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200186endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200187
188func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200189 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200190 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200191 call assert_fails('augroup! x', 'E936:')
192 au VimEnter * echo
193 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200194 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200195 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
196 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200197endfunc
198