Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 1 | " Tests for autocommands |
| 2 | |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 3 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 4 | func! s:cleanup_buffers() abort |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 5 | for bnr in range(1, bufnr('$')) |
| 6 | if bufloaded(bnr) && bufnr('%') != bnr |
| 7 | execute 'bd! ' . bnr |
| 8 | endif |
| 9 | endfor |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 10 | endfunc |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 11 | |
Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 12 | func 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. |
| 17 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 18 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 19 | if has('timers') |
| 20 | func ExitInsertMode(id) |
| 21 | call feedkeys("\<Esc>") |
| 22 | endfunc |
| 23 | |
| 24 | func Test_cursorhold_insert() |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 25 | " Need to move the cursor. |
| 26 | call feedkeys("ggG", "xt") |
| 27 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 28 | 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 Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 34 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 35 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 36 | 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 Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 46 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 47 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 48 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 49 | endif |
| 50 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 51 | func Test_bufunload() |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 52 | 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 Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 58 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 59 | let s:li=[] |
| 60 | new |
| 61 | setlocal bufhidden= |
| 62 | bunload |
| 63 | call assert_equal(["bufunload", "bufdelete"], s:li) |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 64 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 65 | 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 Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 77 | au! test_bufunload_group |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 78 | augroup! test_bufunload_group |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 79 | endfunc |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 80 | |
| 81 | " SEGV occurs in older versions. (At least 7.4.2005 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 82 | func Test_autocmd_bufunload_with_tabnext() |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 83 | 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 Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 94 | autocmd! test_autocmd_bufunload_with_tabnext_group |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 95 | augroup! test_autocmd_bufunload_with_tabnext_group |
| 96 | tablast |
| 97 | quit |
| 98 | endfunc |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 99 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 100 | func Test_autocmd_bufwinleave_with_tabfirst() |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 101 | 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 Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 108 | tabclose |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 109 | endfunc |
| 110 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 111 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 112 | func Test_autocmd_bufunload_avoiding_SEGV_01() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 113 | 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 |
| 127 | endfunc |
| 128 | |
| 129 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 130 | func Test_autocmd_bufunload_avoiding_SEGV_02() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 131 | 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 |
| 146 | endfunc |
| 147 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 148 | func 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 Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 156 | au TabClosed * call add(g:record, 'TabClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 157 | 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 Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 169 | \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter', |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 170 | \ 'WinLeave', 'WinEnter' |
| 171 | \ ], g:record) |
| 172 | |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 173 | 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 Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 184 | augroup testing |
| 185 | au! |
| 186 | augroup END |
| 187 | unlet g:record |
| 188 | endfunc |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 189 | |
| 190 | func s:AddAnAutocmd() |
| 191 | augroup vimBarTest |
| 192 | au BufReadCmd * echo 'hello' |
| 193 | augroup END |
| 194 | call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) |
| 195 | endfunc |
| 196 | |
| 197 | func 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"))) |
| 216 | endfunc |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 217 | |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 218 | func RemoveGroup() |
| 219 | autocmd! StartOK |
| 220 | augroup! StartOK |
| 221 | endfunc |
| 222 | |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 223 | func 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 Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 238 | augroup! Another |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 239 | |
| 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 Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 249 | au! VimEnter |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 250 | endfunc |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 251 | |
| 252 | func Test_augroup_deleted() |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 253 | " This caused a crash before E936 was introduced |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 254 | augroup x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 255 | call assert_fails('augroup! x', 'E936:') |
| 256 | au VimEnter * echo |
| 257 | augroup end |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 258 | augroup! x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 259 | call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) |
| 260 | au! VimEnter |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 261 | endfunc |
| 262 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 263 | " Tests for autocommands on :close command. |
| 264 | " This used to be in test13. |
| 265 | func Test_three_windows() |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 266 | " Clean up buffers, because in some cases this function fails. |
| 267 | call s:cleanup_buffers() |
| 268 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 269 | " 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 Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 322 | enew |
| 323 | bwipe! Xtestje1 |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 324 | call delete('Xtestje1') |
| 325 | call delete('Xtestje2') |
| 326 | call delete('Xtestje3') |
| 327 | endfunc |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 328 | |
| 329 | func 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 Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 342 | |
| 343 | " On MS-Windows we can't edit the directory, make sure we wipe the right |
| 344 | " buffer. |
| 345 | bwipe! Xdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 346 | |
| 347 | call delete('Xdir', 'd') |
| 348 | au! BufEnter |
| 349 | endfunc |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 350 | |
| 351 | " Closing a window might cause an endless loop |
| 352 | " E814 for older Vims |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 353 | func Test_autocmd_bufwipe_in_SessLoadPost() |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 354 | edit Xtest |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 355 | tabnew |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 356 | file Xsomething |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 357 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 358 | mksession! |
| 359 | |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 360 | let content = ['set nocp noswapfile', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 361 | \ 'let v:swapchoice="e"', |
| 362 | \ 'augroup test_autocmd_sessionload', |
| 363 | \ 'autocmd!', |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 364 | \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 365 | \ 'augroup END', |
| 366 | \ '', |
| 367 | \ 'func WriteErrors()', |
| 368 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 369 | \ 'endfunc', |
| 370 | \ 'au VimLeave * call WriteErrors()', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 371 | \ ] |
| 372 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 373 | 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 Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 376 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 377 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 378 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 379 | call delete(file) |
| 380 | endfor |
| 381 | endfunc |
| 382 | |
| 383 | " SEGV occurs in older versions. |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 384 | func Test_autocmd_bufwipe_in_SessLoadPost2() |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 385 | tabnew |
| 386 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 387 | 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 Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 401 | \ ' echomsg "SessionLoadPost DONE"', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 402 | \ 'endfunction', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 403 | \ 'au SessionLoadPost * call DeleteInactiveBufs()', |
| 404 | \ '', |
| 405 | \ 'func WriteErrors()', |
| 406 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 407 | \ 'endfunc', |
| 408 | \ 'au VimLeave * call WriteErrors()', |
| 409 | \ ] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 410 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 411 | 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 Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 416 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 417 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 418 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 419 | call delete(file) |
| 420 | endfor |
| 421 | endfunc |
Bram Moolenaar | faf29d7 | 2017-07-09 11:07:16 +0200 | [diff] [blame] | 422 | |
| 423 | func Test_empty_doau() |
| 424 | doau \| |
| 425 | endfunc |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 426 | |
| 427 | func 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) |
| 433 | endfunc |
| 434 | |
| 435 | func Test_OptionSet() |
| 436 | if !has("eval") || !has("autocmd") || !exists("+autochdir") |
| 437 | return |
| 438 | endif |
| 439 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 440 | badd test_autocmd.vim |
| 441 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 442 | 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 Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 535 | let bnum = bufnr('%') |
| 536 | call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355") |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 537 | " should trigger, use correct option name |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 538 | call setbufvar(bnum, '&backup', 1) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 539 | 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 Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 555 | " 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 Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 568 | " 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 |
| 575 | endfunc |
| 576 | |
| 577 | func 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) |
| 608 | endfunc |
| 609 | |
| 610 | func 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 |
| 632 | endfunc |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 633 | |
| 634 | " Test for Bufleave autocommand that deletes the buffer we are about to edit. |
| 635 | func 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 |
| 651 | endfunc |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 652 | |
| 653 | " Test for autocommand that changes the buffer list, when doing ":ball". |
| 654 | func 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 |
| 685 | endfunc |
| 686 | |
| 687 | " Test for autocommand that changes current buffer on BufEnter event. |
| 688 | " Check if modelines are interpreted for the correct buffer. |
| 689 | func 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 |
| 722 | endfunc |
| 723 | |
| 724 | " Test for issue #57 |
| 725 | " do not move cursor on <c-o> when autoindent is set |
| 726 | func 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! |
| 738 | endfunc |
| 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. |
| 742 | func 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 Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 771 | let g:bufinfo = getbufinfo() |
| 772 | call assert_equal(1, len(g:bufinfo)) |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 773 | |
| 774 | call delete('Xxx1') |
| 775 | call delete('Xxx2') |
| 776 | %bwipe |
| 777 | au! BufLeave |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 778 | |
| 779 | " check that bufinfo doesn't contain a pointer to freed memory |
| 780 | call test_garbagecollect_now() |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 781 | endfunc |