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 | set belloff=all |
| 4 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 5 | func! s:cleanup_buffers() abort |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 6 | for bnr in range(1, bufnr('$')) |
| 7 | if bufloaded(bnr) && bufnr('%') != bnr |
| 8 | execute 'bd! ' . bnr |
| 9 | endif |
| 10 | endfor |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 11 | endfunc |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 12 | |
Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 13 | func 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. |
| 18 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 19 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 20 | if has('timers') |
| 21 | func ExitInsertMode(id) |
| 22 | call feedkeys("\<Esc>") |
| 23 | endfunc |
| 24 | |
| 25 | func Test_cursorhold_insert() |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 26 | " Need to move the cursor. |
| 27 | call feedkeys("ggG", "xt") |
| 28 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 29 | 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 Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 35 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 36 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 37 | 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 Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 47 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 48 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 49 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 50 | endif |
| 51 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 52 | func Test_bufunload() |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 53 | 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 Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 59 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 60 | let s:li=[] |
| 61 | new |
| 62 | setlocal bufhidden= |
| 63 | bunload |
| 64 | call assert_equal(["bufunload", "bufdelete"], s:li) |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 65 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 66 | 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 Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 78 | au! test_bufunload_group |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 79 | augroup! test_bufunload_group |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 80 | endfunc |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 81 | |
| 82 | " SEGV occurs in older versions. (At least 7.4.2005 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 83 | func Test_autocmd_bufunload_with_tabnext() |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 84 | 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 Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 95 | autocmd! test_autocmd_bufunload_with_tabnext_group |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 96 | augroup! test_autocmd_bufunload_with_tabnext_group |
| 97 | tablast |
| 98 | quit |
| 99 | endfunc |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 100 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 101 | func Test_autocmd_bufwinleave_with_tabfirst() |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 102 | 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 Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 109 | tabclose |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 110 | endfunc |
| 111 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 112 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 113 | func Test_autocmd_bufunload_avoiding_SEGV_01() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 114 | 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 |
| 128 | endfunc |
| 129 | |
| 130 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 131 | func Test_autocmd_bufunload_avoiding_SEGV_02() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 132 | 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 |
| 147 | endfunc |
| 148 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 149 | func 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 Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 157 | au TabClosed * call add(g:record, 'TabClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 158 | 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 Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 170 | \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter', |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 171 | \ 'WinLeave', 'WinEnter' |
| 172 | \ ], g:record) |
| 173 | |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 174 | 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 Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 185 | augroup testing |
| 186 | au! |
| 187 | augroup END |
| 188 | unlet g:record |
| 189 | endfunc |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 190 | |
| 191 | func s:AddAnAutocmd() |
| 192 | augroup vimBarTest |
| 193 | au BufReadCmd * echo 'hello' |
| 194 | augroup END |
| 195 | call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) |
| 196 | endfunc |
| 197 | |
| 198 | func 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"))) |
| 217 | endfunc |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 218 | |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 219 | func RemoveGroup() |
| 220 | autocmd! StartOK |
| 221 | augroup! StartOK |
| 222 | endfunc |
| 223 | |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 224 | func 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 Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 239 | augroup! Another |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 240 | |
| 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 Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 250 | au! VimEnter |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 251 | endfunc |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 252 | |
| 253 | func Test_augroup_deleted() |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 254 | " This caused a crash before E936 was introduced |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 255 | augroup x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 256 | call assert_fails('augroup! x', 'E936:') |
| 257 | au VimEnter * echo |
| 258 | augroup end |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 259 | augroup! x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 260 | call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) |
| 261 | au! VimEnter |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 262 | endfunc |
| 263 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 264 | " Tests for autocommands on :close command. |
| 265 | " This used to be in test13. |
| 266 | func Test_three_windows() |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 267 | " Clean up buffers, because in some cases this function fails. |
| 268 | call s:cleanup_buffers() |
| 269 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 270 | " Write three files and open them, each in a window. |
| 271 | " Then go to next window, with autocommand that deletes the previous one. |
| 272 | " Do this twice, writing the file. |
| 273 | e! Xtestje1 |
| 274 | call setline(1, 'testje1') |
| 275 | w |
| 276 | sp Xtestje2 |
| 277 | call setline(1, 'testje2') |
| 278 | w |
| 279 | sp Xtestje3 |
| 280 | call setline(1, 'testje3') |
| 281 | w |
| 282 | wincmd w |
| 283 | au WinLeave Xtestje2 bwipe |
| 284 | wincmd w |
| 285 | call assert_equal('Xtestje1', expand('%')) |
| 286 | |
| 287 | au WinLeave Xtestje1 bwipe Xtestje3 |
| 288 | close |
| 289 | call assert_equal('Xtestje1', expand('%')) |
| 290 | |
| 291 | " Test deleting the buffer on a Unload event. If this goes wrong there |
| 292 | " will be the ATTENTION prompt. |
| 293 | e Xtestje1 |
| 294 | au! |
| 295 | au! BufUnload Xtestje1 bwipe |
| 296 | call assert_fails('e Xtestje3', 'E937:') |
| 297 | call assert_equal('Xtestje3', expand('%')) |
| 298 | |
| 299 | e Xtestje2 |
| 300 | sp Xtestje1 |
| 301 | call assert_fails('e', 'E937:') |
| 302 | call assert_equal('Xtestje2', expand('%')) |
| 303 | |
| 304 | " Test changing buffers in a BufWipeout autocommand. If this goes wrong |
| 305 | " there are ml_line errors and/or a Crash. |
| 306 | au! |
| 307 | only |
| 308 | e Xanother |
| 309 | e Xtestje1 |
| 310 | bwipe Xtestje2 |
| 311 | bwipe Xtestje3 |
| 312 | au BufWipeout Xtestje1 buf Xtestje1 |
| 313 | bwipe |
| 314 | call assert_equal('Xanother', expand('%')) |
| 315 | |
| 316 | only |
| 317 | help |
| 318 | wincmd w |
| 319 | 1quit |
| 320 | call assert_equal('Xanother', expand('%')) |
| 321 | |
| 322 | au! |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 323 | enew |
| 324 | bwipe! Xtestje1 |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 325 | call delete('Xtestje1') |
| 326 | call delete('Xtestje2') |
| 327 | call delete('Xtestje3') |
| 328 | endfunc |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 329 | |
| 330 | func Test_BufEnter() |
| 331 | au! BufEnter |
| 332 | au Bufenter * let val = val . '+' |
| 333 | let g:val = '' |
| 334 | split NewFile |
| 335 | call assert_equal('+', g:val) |
| 336 | bwipe! |
| 337 | call assert_equal('++', g:val) |
| 338 | |
| 339 | " Also get BufEnter when editing a directory |
| 340 | call mkdir('Xdir') |
| 341 | split Xdir |
| 342 | call assert_equal('+++', g:val) |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 343 | |
| 344 | " On MS-Windows we can't edit the directory, make sure we wipe the right |
| 345 | " buffer. |
| 346 | bwipe! Xdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 347 | |
| 348 | call delete('Xdir', 'd') |
| 349 | au! BufEnter |
| 350 | endfunc |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 351 | |
| 352 | " Closing a window might cause an endless loop |
| 353 | " E814 for older Vims |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 354 | func Test_autocmd_bufwipe_in_SessLoadPost() |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 355 | edit Xtest |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 356 | tabnew |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 357 | file Xsomething |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 358 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 359 | mksession! |
| 360 | |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 361 | let content = ['set nocp noswapfile', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 362 | \ 'let v:swapchoice="e"', |
| 363 | \ 'augroup test_autocmd_sessionload', |
| 364 | \ 'autocmd!', |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 365 | \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 366 | \ 'augroup END', |
| 367 | \ '', |
| 368 | \ 'func WriteErrors()', |
| 369 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 370 | \ 'endfunc', |
| 371 | \ 'au VimLeave * call WriteErrors()', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 372 | \ ] |
| 373 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 374 | call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq') |
| 375 | let errors = join(readfile('Xerrors')) |
| 376 | call assert_match('E814', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 377 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 378 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 379 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 380 | call delete(file) |
| 381 | endfor |
| 382 | endfunc |
| 383 | |
| 384 | " SEGV occurs in older versions. |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 385 | func Test_autocmd_bufwipe_in_SessLoadPost2() |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 386 | tabnew |
| 387 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 388 | mksession! |
| 389 | |
| 390 | let content = ['set nocp noswapfile', |
| 391 | \ 'function! DeleteInactiveBufs()', |
| 392 | \ ' tabfirst', |
| 393 | \ ' let tabblist = []', |
| 394 | \ ' for i in range(1, tabpagenr(''$''))', |
| 395 | \ ' call extend(tabblist, tabpagebuflist(i))', |
| 396 | \ ' endfor', |
| 397 | \ ' for b in range(1, bufnr(''$''))', |
| 398 | \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')', |
| 399 | \ ' exec ''bwipeout '' . b', |
| 400 | \ ' endif', |
| 401 | \ ' endfor', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 402 | \ ' echomsg "SessionLoadPost DONE"', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 403 | \ 'endfunction', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 404 | \ 'au SessionLoadPost * call DeleteInactiveBufs()', |
| 405 | \ '', |
| 406 | \ 'func WriteErrors()', |
| 407 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 408 | \ 'endfunc', |
| 409 | \ 'au VimLeave * call WriteErrors()', |
| 410 | \ ] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 411 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 412 | call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq') |
| 413 | let errors = join(readfile('Xerrors')) |
| 414 | " This probably only ever matches on unix. |
| 415 | call assert_notmatch('Caught deadly signal SEGV', errors) |
| 416 | call assert_match('SessionLoadPost DONE', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 417 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 418 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 419 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 420 | call delete(file) |
| 421 | endfor |
| 422 | endfunc |
Bram Moolenaar | faf29d7 | 2017-07-09 11:07:16 +0200 | [diff] [blame] | 423 | |
| 424 | func Test_empty_doau() |
| 425 | doau \| |
| 426 | endfunc |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 427 | |
| 428 | func s:AutoCommandOptionSet(match) |
| 429 | let item = remove(g:options, 0) |
| 430 | let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3]) |
| 431 | let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type) |
| 432 | let g:opt = [expected, actual] |
| 433 | "call assert_equal(expected, actual) |
| 434 | endfunc |
| 435 | |
| 436 | func Test_OptionSet() |
| 437 | if !has("eval") || !has("autocmd") || !exists("+autochdir") |
| 438 | return |
| 439 | endif |
| 440 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 441 | badd test_autocmd.vim |
| 442 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 443 | call test_override('starting', 1) |
| 444 | set nocp |
| 445 | au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>")) |
| 446 | |
| 447 | " 1: Setting number option" |
| 448 | let g:options=[['number', 0, 1, 'global']] |
| 449 | set nu |
| 450 | call assert_equal([], g:options) |
| 451 | call assert_equal(g:opt[0], g:opt[1]) |
| 452 | |
| 453 | " 2: Setting local number option" |
| 454 | let g:options=[['number', 1, 0, 'local']] |
| 455 | setlocal nonu |
| 456 | call assert_equal([], g:options) |
| 457 | call assert_equal(g:opt[0], g:opt[1]) |
| 458 | |
| 459 | " 3: Setting global number option" |
| 460 | let g:options=[['number', 1, 0, 'global']] |
| 461 | setglobal nonu |
| 462 | call assert_equal([], g:options) |
| 463 | call assert_equal(g:opt[0], g:opt[1]) |
| 464 | |
| 465 | " 4: Setting local autoindent option" |
| 466 | let g:options=[['autoindent', 0, 1, 'local']] |
| 467 | setlocal ai |
| 468 | call assert_equal([], g:options) |
| 469 | call assert_equal(g:opt[0], g:opt[1]) |
| 470 | |
| 471 | " 5: Setting global autoindent option" |
| 472 | let g:options=[['autoindent', 0, 1, 'global']] |
| 473 | setglobal ai |
| 474 | call assert_equal([], g:options) |
| 475 | call assert_equal(g:opt[0], g:opt[1]) |
| 476 | |
| 477 | " 6: Setting global autoindent option" |
| 478 | let g:options=[['autoindent', 1, 0, 'global']] |
| 479 | set ai! |
| 480 | call assert_equal([], g:options) |
| 481 | call assert_equal(g:opt[0], g:opt[1]) |
| 482 | |
| 483 | " Should not print anything, use :noa |
| 484 | " 7: don't trigger OptionSet" |
| 485 | let g:options=[['invalid', 1, 1, 'invalid']] |
| 486 | noa set nonu |
| 487 | call assert_equal([['invalid', 1, 1, 'invalid']], g:options) |
| 488 | call assert_equal(g:opt[0], g:opt[1]) |
| 489 | |
| 490 | " 8: Setting several global list and number option" |
| 491 | let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] |
| 492 | set list nu |
| 493 | call assert_equal([], g:options) |
| 494 | call assert_equal(g:opt[0], g:opt[1]) |
| 495 | |
| 496 | " 9: don't trigger OptionSet" |
| 497 | let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']] |
| 498 | noa set nolist nonu |
| 499 | call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options) |
| 500 | call assert_equal(g:opt[0], g:opt[1]) |
| 501 | |
| 502 | " 10: Setting global acd" |
| 503 | let g:options=[['autochdir', 0, 1, 'local']] |
| 504 | setlocal acd |
| 505 | call assert_equal([], g:options) |
| 506 | call assert_equal(g:opt[0], g:opt[1]) |
| 507 | |
| 508 | " 11: Setting global autoread (also sets local value)" |
| 509 | let g:options=[['autoread', 0, 1, 'global']] |
| 510 | set ar |
| 511 | call assert_equal([], g:options) |
| 512 | call assert_equal(g:opt[0], g:opt[1]) |
| 513 | |
| 514 | " 12: Setting local autoread" |
| 515 | let g:options=[['autoread', 1, 1, 'local']] |
| 516 | setlocal ar |
| 517 | call assert_equal([], g:options) |
| 518 | call assert_equal(g:opt[0], g:opt[1]) |
| 519 | |
| 520 | " 13: Setting global autoread" |
| 521 | let g:options=[['autoread', 1, 0, 'global']] |
| 522 | setglobal invar |
| 523 | call assert_equal([], g:options) |
| 524 | call assert_equal(g:opt[0], g:opt[1]) |
| 525 | |
| 526 | " 14: Setting option backspace through :let" |
| 527 | let g:options=[['backspace', '', 'eol,indent,start', 'global']] |
| 528 | let &bs="eol,indent,start" |
| 529 | call assert_equal([], g:options) |
| 530 | call assert_equal(g:opt[0], g:opt[1]) |
| 531 | |
| 532 | " 15: Setting option backspace through setbufvar()" |
| 533 | let g:options=[['backup', 0, 1, 'local']] |
| 534 | " try twice, first time, shouldn't trigger because option name is invalid, |
| 535 | " second time, it should trigger |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 536 | let bnum = bufnr('%') |
| 537 | call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355") |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 538 | " should trigger, use correct option name |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 539 | call setbufvar(bnum, '&backup', 1) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 540 | call assert_equal([], g:options) |
| 541 | call assert_equal(g:opt[0], g:opt[1]) |
| 542 | |
| 543 | " 16: Setting number option using setwinvar" |
| 544 | let g:options=[['number', 0, 1, 'local']] |
| 545 | call setwinvar(0, '&number', 1) |
| 546 | call assert_equal([], g:options) |
| 547 | call assert_equal(g:opt[0], g:opt[1]) |
| 548 | |
| 549 | " 17: Setting key option, shouldn't trigger" |
| 550 | let g:options=[['key', 'invalid', 'invalid1', 'invalid']] |
| 551 | setlocal key=blah |
| 552 | setlocal key= |
| 553 | call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options) |
| 554 | call assert_equal(g:opt[0], g:opt[1]) |
| 555 | |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 556 | " 18: Setting string option" |
| 557 | let oldval = &tags |
| 558 | let g:options=[['tags', oldval, 'tagpath', 'global']] |
| 559 | set tags=tagpath |
| 560 | call assert_equal([], g:options) |
| 561 | call assert_equal(g:opt[0], g:opt[1]) |
| 562 | |
| 563 | " 1l: Resetting string option" |
| 564 | let g:options=[['tags', 'tagpath', oldval, 'global']] |
| 565 | set tags& |
| 566 | call assert_equal([], g:options) |
| 567 | call assert_equal(g:opt[0], g:opt[1]) |
| 568 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 569 | " Cleanup |
| 570 | au! OptionSet |
| 571 | for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp'] |
| 572 | exe printf(":set %s&vi", opt) |
| 573 | endfor |
| 574 | call test_override('starting', 0) |
| 575 | delfunc! AutoCommandOptionSet |
| 576 | endfunc |
| 577 | |
| 578 | func Test_OptionSet_diffmode() |
| 579 | call test_override('starting', 1) |
| 580 | " 18: Changing an option when enetering diff mode |
| 581 | new |
| 582 | au OptionSet diff :let &l:cul=v:option_new |
| 583 | |
| 584 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 585 | call assert_equal(0, &l:cul) |
| 586 | diffthis |
| 587 | call assert_equal(1, &l:cul) |
| 588 | |
| 589 | vnew |
| 590 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 591 | call assert_equal(0, &l:cul) |
| 592 | diffthis |
| 593 | call assert_equal(1, &l:cul) |
| 594 | |
| 595 | diffoff |
| 596 | call assert_equal(0, &l:cul) |
| 597 | call assert_equal(1, getwinvar(2, '&l:cul')) |
| 598 | bw! |
| 599 | |
| 600 | call assert_equal(1, &l:cul) |
| 601 | diffoff! |
| 602 | call assert_equal(0, &l:cul) |
| 603 | call assert_equal(0, getwinvar(1, '&l:cul')) |
| 604 | bw! |
| 605 | |
| 606 | " Cleanup |
| 607 | au! OptionSet |
| 608 | call test_override('starting', 0) |
| 609 | endfunc |
| 610 | |
| 611 | func Test_OptionSet_diffmode_close() |
| 612 | call test_override('starting', 1) |
| 613 | " 19: Try to close the current window when entering diff mode |
| 614 | " should not segfault |
| 615 | new |
| 616 | au OptionSet diff close |
| 617 | |
| 618 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 619 | call assert_fails(':diffthis', 'E788') |
| 620 | call assert_equal(1, &diff) |
| 621 | vnew |
| 622 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 623 | call assert_fails(':diffthis', 'E788') |
| 624 | call assert_equal(1, &diff) |
| 625 | bw! |
| 626 | call assert_fails(':diffoff!', 'E788') |
| 627 | bw! |
| 628 | |
| 629 | " Cleanup |
| 630 | au! OptionSet |
| 631 | call test_override('starting', 0) |
| 632 | "delfunc! AutoCommandOptionSet |
| 633 | endfunc |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 634 | |
| 635 | " Test for Bufleave autocommand that deletes the buffer we are about to edit. |
| 636 | func Test_BufleaveWithDelete() |
| 637 | new | edit Xfile1 |
| 638 | |
| 639 | augroup test_bufleavewithdelete |
| 640 | autocmd! |
| 641 | autocmd BufLeave Xfile1 bwipe Xfile2 |
| 642 | augroup END |
| 643 | |
| 644 | call assert_fails('edit Xfile2', 'E143:') |
| 645 | call assert_equal('Xfile1', bufname('%')) |
| 646 | |
| 647 | autocmd! test_bufleavewithdelete BufLeave Xfile1 |
| 648 | augroup! test_bufleavewithdelete |
| 649 | |
| 650 | new |
| 651 | bwipe! Xfile1 |
| 652 | endfunc |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 653 | |
| 654 | " Test for autocommand that changes the buffer list, when doing ":ball". |
| 655 | func Test_Acmd_BufAll() |
| 656 | enew! |
| 657 | %bwipe! |
| 658 | call writefile(['Test file Xxx1'], 'Xxx1') |
| 659 | call writefile(['Test file Xxx2'], 'Xxx2') |
| 660 | call writefile(['Test file Xxx3'], 'Xxx3') |
| 661 | |
| 662 | " Add three files to the buffer list |
| 663 | split Xxx1 |
| 664 | close |
| 665 | split Xxx2 |
| 666 | close |
| 667 | split Xxx3 |
| 668 | close |
| 669 | |
| 670 | " Wipe the buffer when the buffer is opened |
| 671 | au BufReadPost Xxx2 bwipe |
| 672 | |
| 673 | call append(0, 'Test file Xxx4') |
| 674 | ball |
| 675 | |
| 676 | call assert_equal(2, winnr('$')) |
| 677 | call assert_equal('Xxx1', bufname(winbufnr(winnr('$')))) |
| 678 | wincmd t |
| 679 | |
| 680 | au! BufReadPost |
| 681 | %bwipe! |
| 682 | call delete('Xxx1') |
| 683 | call delete('Xxx2') |
| 684 | call delete('Xxx3') |
| 685 | enew! | only |
| 686 | endfunc |
| 687 | |
| 688 | " Test for autocommand that changes current buffer on BufEnter event. |
| 689 | " Check if modelines are interpreted for the correct buffer. |
| 690 | func Test_Acmd_BufEnter() |
| 691 | %bwipe! |
| 692 | call writefile(['start of test file Xxx1', |
| 693 | \ "\<Tab>this is a test", |
| 694 | \ 'end of test file Xxx1'], 'Xxx1') |
| 695 | call writefile(['start of test file Xxx2', |
| 696 | \ 'vim: set noai :', |
| 697 | \ "\<Tab>this is a test", |
| 698 | \ 'end of test file Xxx2'], 'Xxx2') |
| 699 | |
| 700 | au BufEnter Xxx2 brew |
| 701 | set ai modeline modelines=3 |
| 702 | edit Xxx1 |
| 703 | " edit Xxx2, autocmd will do :brew |
| 704 | edit Xxx2 |
| 705 | exe "normal G?this is a\<CR>" |
| 706 | " Append text with autoindent to this file |
| 707 | normal othis should be auto-indented |
| 708 | call assert_equal("\<Tab>this should be auto-indented", getline('.')) |
| 709 | call assert_equal(3, line('.')) |
| 710 | " Remove autocmd and edit Xxx2 again |
| 711 | au! BufEnter Xxx2 |
| 712 | buf! Xxx2 |
| 713 | exe "normal G?this is a\<CR>" |
| 714 | " append text without autoindent to Xxx |
| 715 | normal othis should be in column 1 |
| 716 | call assert_equal("this should be in column 1", getline('.')) |
| 717 | call assert_equal(4, line('.')) |
| 718 | |
| 719 | %bwipe! |
| 720 | call delete('Xxx1') |
| 721 | call delete('Xxx2') |
| 722 | set ai&vim modeline&vim modelines&vim |
| 723 | endfunc |
| 724 | |
| 725 | " Test for issue #57 |
| 726 | " do not move cursor on <c-o> when autoindent is set |
| 727 | func Test_ai_CTRL_O() |
| 728 | enew! |
| 729 | set ai |
| 730 | let save_fo = &fo |
| 731 | set fo+=r |
| 732 | exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>" |
| 733 | exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>" |
| 734 | call assert_equal(['# abc', 'def', 'def'], getline(2, 4)) |
| 735 | |
| 736 | set ai&vim |
| 737 | let &fo = save_fo |
| 738 | enew! |
| 739 | endfunc |
| 740 | |
| 741 | " Test for autocommand that deletes the current buffer on BufLeave event. |
| 742 | " Also test deleting the last buffer, should give a new, empty buffer. |
| 743 | func Test_BufLeave_Wipe() |
| 744 | %bwipe! |
| 745 | let content = ['start of test file Xxx', |
| 746 | \ 'this is a test', |
| 747 | \ 'end of test file Xxx'] |
| 748 | call writefile(content, 'Xxx1') |
| 749 | call writefile(content, 'Xxx2') |
| 750 | |
| 751 | au BufLeave Xxx2 bwipe |
| 752 | edit Xxx1 |
| 753 | split Xxx2 |
| 754 | " delete buffer Xxx2, we should be back to Xxx1 |
| 755 | bwipe |
| 756 | call assert_equal('Xxx1', bufname('%')) |
| 757 | call assert_equal(1, winnr('$')) |
| 758 | |
| 759 | " Create an alternate buffer |
| 760 | %write! test.out |
| 761 | call assert_equal('test.out', bufname('#')) |
| 762 | " delete alternate buffer |
| 763 | bwipe test.out |
| 764 | call assert_equal('Xxx1', bufname('%')) |
| 765 | call assert_equal('', bufname('#')) |
| 766 | |
| 767 | au BufLeave Xxx1 bwipe |
| 768 | " delete current buffer, get an empty one |
| 769 | bwipe! |
| 770 | call assert_equal(1, line('$')) |
| 771 | call assert_equal('', bufname('%')) |
| 772 | call assert_equal(1, len(getbufinfo())) |
| 773 | |
| 774 | call delete('Xxx1') |
| 775 | call delete('Xxx2') |
| 776 | %bwipe |
| 777 | au! BufLeave |
| 778 | endfunc |