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 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 252 | func Test_BufReadCmdHelp() |
| 253 | " This used to cause access to free memory |
| 254 | au BufReadCmd * e +h |
| 255 | help |
| 256 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 257 | au! BufReadCmd |
| 258 | endfunc |
| 259 | |
| 260 | func Test_BufReadCmdHelpJump() |
| 261 | " This used to cause access to free memory |
| 262 | au BufReadCmd * e +h{ |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 263 | " } to fix highlighting |
| 264 | call assert_fails('help', 'E434:') |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 265 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 266 | au! BufReadCmd |
| 267 | endfunc |
| 268 | |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 269 | func Test_augroup_deleted() |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 270 | " This caused a crash before E936 was introduced |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 271 | augroup x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 272 | call assert_fails('augroup! x', 'E936:') |
| 273 | au VimEnter * echo |
| 274 | augroup end |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 275 | augroup! x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 276 | call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) |
| 277 | au! VimEnter |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 278 | endfunc |
| 279 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 280 | " Tests for autocommands on :close command. |
| 281 | " This used to be in test13. |
| 282 | func Test_three_windows() |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 283 | " Clean up buffers, because in some cases this function fails. |
| 284 | call s:cleanup_buffers() |
| 285 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 286 | " Write three files and open them, each in a window. |
| 287 | " Then go to next window, with autocommand that deletes the previous one. |
| 288 | " Do this twice, writing the file. |
| 289 | e! Xtestje1 |
| 290 | call setline(1, 'testje1') |
| 291 | w |
| 292 | sp Xtestje2 |
| 293 | call setline(1, 'testje2') |
| 294 | w |
| 295 | sp Xtestje3 |
| 296 | call setline(1, 'testje3') |
| 297 | w |
| 298 | wincmd w |
| 299 | au WinLeave Xtestje2 bwipe |
| 300 | wincmd w |
| 301 | call assert_equal('Xtestje1', expand('%')) |
| 302 | |
| 303 | au WinLeave Xtestje1 bwipe Xtestje3 |
| 304 | close |
| 305 | call assert_equal('Xtestje1', expand('%')) |
| 306 | |
| 307 | " Test deleting the buffer on a Unload event. If this goes wrong there |
| 308 | " will be the ATTENTION prompt. |
| 309 | e Xtestje1 |
| 310 | au! |
| 311 | au! BufUnload Xtestje1 bwipe |
| 312 | call assert_fails('e Xtestje3', 'E937:') |
| 313 | call assert_equal('Xtestje3', expand('%')) |
| 314 | |
| 315 | e Xtestje2 |
| 316 | sp Xtestje1 |
| 317 | call assert_fails('e', 'E937:') |
| 318 | call assert_equal('Xtestje2', expand('%')) |
| 319 | |
| 320 | " Test changing buffers in a BufWipeout autocommand. If this goes wrong |
| 321 | " there are ml_line errors and/or a Crash. |
| 322 | au! |
| 323 | only |
| 324 | e Xanother |
| 325 | e Xtestje1 |
| 326 | bwipe Xtestje2 |
| 327 | bwipe Xtestje3 |
| 328 | au BufWipeout Xtestje1 buf Xtestje1 |
| 329 | bwipe |
| 330 | call assert_equal('Xanother', expand('%')) |
| 331 | |
| 332 | only |
| 333 | help |
| 334 | wincmd w |
| 335 | 1quit |
| 336 | call assert_equal('Xanother', expand('%')) |
| 337 | |
| 338 | au! |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 339 | enew |
| 340 | bwipe! Xtestje1 |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 341 | call delete('Xtestje1') |
| 342 | call delete('Xtestje2') |
| 343 | call delete('Xtestje3') |
| 344 | endfunc |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 345 | |
| 346 | func Test_BufEnter() |
| 347 | au! BufEnter |
| 348 | au Bufenter * let val = val . '+' |
| 349 | let g:val = '' |
| 350 | split NewFile |
| 351 | call assert_equal('+', g:val) |
| 352 | bwipe! |
| 353 | call assert_equal('++', g:val) |
| 354 | |
| 355 | " Also get BufEnter when editing a directory |
| 356 | call mkdir('Xdir') |
| 357 | split Xdir |
| 358 | call assert_equal('+++', g:val) |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 359 | |
| 360 | " On MS-Windows we can't edit the directory, make sure we wipe the right |
| 361 | " buffer. |
| 362 | bwipe! Xdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 363 | |
| 364 | call delete('Xdir', 'd') |
| 365 | au! BufEnter |
| 366 | endfunc |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 367 | |
| 368 | " Closing a window might cause an endless loop |
| 369 | " E814 for older Vims |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 370 | func Test_autocmd_bufwipe_in_SessLoadPost() |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 371 | edit Xtest |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 372 | tabnew |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 373 | file Xsomething |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 374 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 375 | mksession! |
| 376 | |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 377 | let content = ['set nocp noswapfile', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 378 | \ 'let v:swapchoice="e"', |
| 379 | \ 'augroup test_autocmd_sessionload', |
| 380 | \ 'autocmd!', |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 381 | \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 382 | \ 'augroup END', |
| 383 | \ '', |
| 384 | \ 'func WriteErrors()', |
| 385 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 386 | \ 'endfunc', |
| 387 | \ 'au VimLeave * call WriteErrors()', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 388 | \ ] |
| 389 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 390 | call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq') |
| 391 | let errors = join(readfile('Xerrors')) |
| 392 | call assert_match('E814', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 393 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 394 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 395 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 396 | call delete(file) |
| 397 | endfor |
| 398 | endfunc |
| 399 | |
| 400 | " SEGV occurs in older versions. |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 401 | func Test_autocmd_bufwipe_in_SessLoadPost2() |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 402 | tabnew |
| 403 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 404 | mksession! |
| 405 | |
| 406 | let content = ['set nocp noswapfile', |
| 407 | \ 'function! DeleteInactiveBufs()', |
| 408 | \ ' tabfirst', |
| 409 | \ ' let tabblist = []', |
| 410 | \ ' for i in range(1, tabpagenr(''$''))', |
| 411 | \ ' call extend(tabblist, tabpagebuflist(i))', |
| 412 | \ ' endfor', |
| 413 | \ ' for b in range(1, bufnr(''$''))', |
| 414 | \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')', |
| 415 | \ ' exec ''bwipeout '' . b', |
| 416 | \ ' endif', |
| 417 | \ ' endfor', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 418 | \ ' echomsg "SessionLoadPost DONE"', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 419 | \ 'endfunction', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 420 | \ 'au SessionLoadPost * call DeleteInactiveBufs()', |
| 421 | \ '', |
| 422 | \ 'func WriteErrors()', |
| 423 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 424 | \ 'endfunc', |
| 425 | \ 'au VimLeave * call WriteErrors()', |
| 426 | \ ] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 427 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 428 | call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq') |
| 429 | let errors = join(readfile('Xerrors')) |
| 430 | " This probably only ever matches on unix. |
| 431 | call assert_notmatch('Caught deadly signal SEGV', errors) |
| 432 | call assert_match('SessionLoadPost DONE', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 433 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 434 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 435 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 436 | call delete(file) |
| 437 | endfor |
| 438 | endfunc |
Bram Moolenaar | faf29d7 | 2017-07-09 11:07:16 +0200 | [diff] [blame] | 439 | |
| 440 | func Test_empty_doau() |
| 441 | doau \| |
| 442 | endfunc |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 443 | |
| 444 | func s:AutoCommandOptionSet(match) |
| 445 | let item = remove(g:options, 0) |
| 446 | let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3]) |
| 447 | let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type) |
| 448 | let g:opt = [expected, actual] |
| 449 | "call assert_equal(expected, actual) |
| 450 | endfunc |
| 451 | |
| 452 | func Test_OptionSet() |
| 453 | if !has("eval") || !has("autocmd") || !exists("+autochdir") |
| 454 | return |
| 455 | endif |
| 456 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 457 | badd test_autocmd.vim |
| 458 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 459 | call test_override('starting', 1) |
| 460 | set nocp |
| 461 | au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>")) |
| 462 | |
| 463 | " 1: Setting number option" |
| 464 | let g:options=[['number', 0, 1, 'global']] |
| 465 | set nu |
| 466 | call assert_equal([], g:options) |
| 467 | call assert_equal(g:opt[0], g:opt[1]) |
| 468 | |
| 469 | " 2: Setting local number option" |
| 470 | let g:options=[['number', 1, 0, 'local']] |
| 471 | setlocal nonu |
| 472 | call assert_equal([], g:options) |
| 473 | call assert_equal(g:opt[0], g:opt[1]) |
| 474 | |
| 475 | " 3: Setting global number option" |
| 476 | let g:options=[['number', 1, 0, 'global']] |
| 477 | setglobal nonu |
| 478 | call assert_equal([], g:options) |
| 479 | call assert_equal(g:opt[0], g:opt[1]) |
| 480 | |
| 481 | " 4: Setting local autoindent option" |
| 482 | let g:options=[['autoindent', 0, 1, 'local']] |
| 483 | setlocal ai |
| 484 | call assert_equal([], g:options) |
| 485 | call assert_equal(g:opt[0], g:opt[1]) |
| 486 | |
| 487 | " 5: Setting global autoindent option" |
| 488 | let g:options=[['autoindent', 0, 1, 'global']] |
| 489 | setglobal ai |
| 490 | call assert_equal([], g:options) |
| 491 | call assert_equal(g:opt[0], g:opt[1]) |
| 492 | |
| 493 | " 6: Setting global autoindent option" |
| 494 | let g:options=[['autoindent', 1, 0, 'global']] |
| 495 | set ai! |
| 496 | call assert_equal([], g:options) |
| 497 | call assert_equal(g:opt[0], g:opt[1]) |
| 498 | |
| 499 | " Should not print anything, use :noa |
| 500 | " 7: don't trigger OptionSet" |
| 501 | let g:options=[['invalid', 1, 1, 'invalid']] |
| 502 | noa set nonu |
| 503 | call assert_equal([['invalid', 1, 1, 'invalid']], g:options) |
| 504 | call assert_equal(g:opt[0], g:opt[1]) |
| 505 | |
| 506 | " 8: Setting several global list and number option" |
| 507 | let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] |
| 508 | set list nu |
| 509 | call assert_equal([], g:options) |
| 510 | call assert_equal(g:opt[0], g:opt[1]) |
| 511 | |
| 512 | " 9: don't trigger OptionSet" |
| 513 | let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']] |
| 514 | noa set nolist nonu |
| 515 | call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options) |
| 516 | call assert_equal(g:opt[0], g:opt[1]) |
| 517 | |
| 518 | " 10: Setting global acd" |
| 519 | let g:options=[['autochdir', 0, 1, 'local']] |
| 520 | setlocal acd |
| 521 | call assert_equal([], g:options) |
| 522 | call assert_equal(g:opt[0], g:opt[1]) |
| 523 | |
| 524 | " 11: Setting global autoread (also sets local value)" |
| 525 | let g:options=[['autoread', 0, 1, 'global']] |
| 526 | set ar |
| 527 | call assert_equal([], g:options) |
| 528 | call assert_equal(g:opt[0], g:opt[1]) |
| 529 | |
| 530 | " 12: Setting local autoread" |
| 531 | let g:options=[['autoread', 1, 1, 'local']] |
| 532 | setlocal ar |
| 533 | call assert_equal([], g:options) |
| 534 | call assert_equal(g:opt[0], g:opt[1]) |
| 535 | |
| 536 | " 13: Setting global autoread" |
| 537 | let g:options=[['autoread', 1, 0, 'global']] |
| 538 | setglobal invar |
| 539 | call assert_equal([], g:options) |
| 540 | call assert_equal(g:opt[0], g:opt[1]) |
| 541 | |
| 542 | " 14: Setting option backspace through :let" |
| 543 | let g:options=[['backspace', '', 'eol,indent,start', 'global']] |
| 544 | let &bs="eol,indent,start" |
| 545 | call assert_equal([], g:options) |
| 546 | call assert_equal(g:opt[0], g:opt[1]) |
| 547 | |
| 548 | " 15: Setting option backspace through setbufvar()" |
| 549 | let g:options=[['backup', 0, 1, 'local']] |
| 550 | " try twice, first time, shouldn't trigger because option name is invalid, |
| 551 | " second time, it should trigger |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 552 | let bnum = bufnr('%') |
| 553 | call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355") |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 554 | " should trigger, use correct option name |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 555 | call setbufvar(bnum, '&backup', 1) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 556 | call assert_equal([], g:options) |
| 557 | call assert_equal(g:opt[0], g:opt[1]) |
| 558 | |
| 559 | " 16: Setting number option using setwinvar" |
| 560 | let g:options=[['number', 0, 1, 'local']] |
| 561 | call setwinvar(0, '&number', 1) |
| 562 | call assert_equal([], g:options) |
| 563 | call assert_equal(g:opt[0], g:opt[1]) |
| 564 | |
| 565 | " 17: Setting key option, shouldn't trigger" |
| 566 | let g:options=[['key', 'invalid', 'invalid1', 'invalid']] |
| 567 | setlocal key=blah |
| 568 | setlocal key= |
| 569 | call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options) |
| 570 | call assert_equal(g:opt[0], g:opt[1]) |
| 571 | |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 572 | " 18: Setting string option" |
| 573 | let oldval = &tags |
| 574 | let g:options=[['tags', oldval, 'tagpath', 'global']] |
| 575 | set tags=tagpath |
| 576 | call assert_equal([], g:options) |
| 577 | call assert_equal(g:opt[0], g:opt[1]) |
| 578 | |
| 579 | " 1l: Resetting string option" |
| 580 | let g:options=[['tags', 'tagpath', oldval, 'global']] |
| 581 | set tags& |
| 582 | call assert_equal([], g:options) |
| 583 | call assert_equal(g:opt[0], g:opt[1]) |
| 584 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 585 | " Cleanup |
| 586 | au! OptionSet |
| 587 | for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp'] |
| 588 | exe printf(":set %s&vi", opt) |
| 589 | endfor |
| 590 | call test_override('starting', 0) |
| 591 | delfunc! AutoCommandOptionSet |
| 592 | endfunc |
| 593 | |
| 594 | func Test_OptionSet_diffmode() |
| 595 | call test_override('starting', 1) |
| 596 | " 18: Changing an option when enetering diff mode |
| 597 | new |
| 598 | au OptionSet diff :let &l:cul=v:option_new |
| 599 | |
| 600 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 601 | call assert_equal(0, &l:cul) |
| 602 | diffthis |
| 603 | call assert_equal(1, &l:cul) |
| 604 | |
| 605 | vnew |
| 606 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 607 | call assert_equal(0, &l:cul) |
| 608 | diffthis |
| 609 | call assert_equal(1, &l:cul) |
| 610 | |
| 611 | diffoff |
| 612 | call assert_equal(0, &l:cul) |
| 613 | call assert_equal(1, getwinvar(2, '&l:cul')) |
| 614 | bw! |
| 615 | |
| 616 | call assert_equal(1, &l:cul) |
| 617 | diffoff! |
| 618 | call assert_equal(0, &l:cul) |
| 619 | call assert_equal(0, getwinvar(1, '&l:cul')) |
| 620 | bw! |
| 621 | |
| 622 | " Cleanup |
| 623 | au! OptionSet |
| 624 | call test_override('starting', 0) |
| 625 | endfunc |
| 626 | |
| 627 | func Test_OptionSet_diffmode_close() |
| 628 | call test_override('starting', 1) |
| 629 | " 19: Try to close the current window when entering diff mode |
| 630 | " should not segfault |
| 631 | new |
| 632 | au OptionSet diff close |
| 633 | |
| 634 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 635 | call assert_fails(':diffthis', 'E788') |
| 636 | call assert_equal(1, &diff) |
| 637 | vnew |
| 638 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 639 | call assert_fails(':diffthis', 'E788') |
| 640 | call assert_equal(1, &diff) |
| 641 | bw! |
| 642 | call assert_fails(':diffoff!', 'E788') |
| 643 | bw! |
| 644 | |
| 645 | " Cleanup |
| 646 | au! OptionSet |
| 647 | call test_override('starting', 0) |
| 648 | "delfunc! AutoCommandOptionSet |
| 649 | endfunc |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 650 | |
| 651 | " Test for Bufleave autocommand that deletes the buffer we are about to edit. |
| 652 | func Test_BufleaveWithDelete() |
| 653 | new | edit Xfile1 |
| 654 | |
| 655 | augroup test_bufleavewithdelete |
| 656 | autocmd! |
| 657 | autocmd BufLeave Xfile1 bwipe Xfile2 |
| 658 | augroup END |
| 659 | |
| 660 | call assert_fails('edit Xfile2', 'E143:') |
| 661 | call assert_equal('Xfile1', bufname('%')) |
| 662 | |
| 663 | autocmd! test_bufleavewithdelete BufLeave Xfile1 |
| 664 | augroup! test_bufleavewithdelete |
| 665 | |
| 666 | new |
| 667 | bwipe! Xfile1 |
| 668 | endfunc |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 669 | |
| 670 | " Test for autocommand that changes the buffer list, when doing ":ball". |
| 671 | func Test_Acmd_BufAll() |
| 672 | enew! |
| 673 | %bwipe! |
| 674 | call writefile(['Test file Xxx1'], 'Xxx1') |
| 675 | call writefile(['Test file Xxx2'], 'Xxx2') |
| 676 | call writefile(['Test file Xxx3'], 'Xxx3') |
| 677 | |
| 678 | " Add three files to the buffer list |
| 679 | split Xxx1 |
| 680 | close |
| 681 | split Xxx2 |
| 682 | close |
| 683 | split Xxx3 |
| 684 | close |
| 685 | |
| 686 | " Wipe the buffer when the buffer is opened |
| 687 | au BufReadPost Xxx2 bwipe |
| 688 | |
| 689 | call append(0, 'Test file Xxx4') |
| 690 | ball |
| 691 | |
| 692 | call assert_equal(2, winnr('$')) |
| 693 | call assert_equal('Xxx1', bufname(winbufnr(winnr('$')))) |
| 694 | wincmd t |
| 695 | |
| 696 | au! BufReadPost |
| 697 | %bwipe! |
| 698 | call delete('Xxx1') |
| 699 | call delete('Xxx2') |
| 700 | call delete('Xxx3') |
| 701 | enew! | only |
| 702 | endfunc |
| 703 | |
| 704 | " Test for autocommand that changes current buffer on BufEnter event. |
| 705 | " Check if modelines are interpreted for the correct buffer. |
| 706 | func Test_Acmd_BufEnter() |
| 707 | %bwipe! |
| 708 | call writefile(['start of test file Xxx1', |
| 709 | \ "\<Tab>this is a test", |
| 710 | \ 'end of test file Xxx1'], 'Xxx1') |
| 711 | call writefile(['start of test file Xxx2', |
| 712 | \ 'vim: set noai :', |
| 713 | \ "\<Tab>this is a test", |
| 714 | \ 'end of test file Xxx2'], 'Xxx2') |
| 715 | |
| 716 | au BufEnter Xxx2 brew |
| 717 | set ai modeline modelines=3 |
| 718 | edit Xxx1 |
| 719 | " edit Xxx2, autocmd will do :brew |
| 720 | edit Xxx2 |
| 721 | exe "normal G?this is a\<CR>" |
| 722 | " Append text with autoindent to this file |
| 723 | normal othis should be auto-indented |
| 724 | call assert_equal("\<Tab>this should be auto-indented", getline('.')) |
| 725 | call assert_equal(3, line('.')) |
| 726 | " Remove autocmd and edit Xxx2 again |
| 727 | au! BufEnter Xxx2 |
| 728 | buf! Xxx2 |
| 729 | exe "normal G?this is a\<CR>" |
| 730 | " append text without autoindent to Xxx |
| 731 | normal othis should be in column 1 |
| 732 | call assert_equal("this should be in column 1", getline('.')) |
| 733 | call assert_equal(4, line('.')) |
| 734 | |
| 735 | %bwipe! |
| 736 | call delete('Xxx1') |
| 737 | call delete('Xxx2') |
| 738 | set ai&vim modeline&vim modelines&vim |
| 739 | endfunc |
| 740 | |
| 741 | " Test for issue #57 |
| 742 | " do not move cursor on <c-o> when autoindent is set |
| 743 | func Test_ai_CTRL_O() |
| 744 | enew! |
| 745 | set ai |
| 746 | let save_fo = &fo |
| 747 | set fo+=r |
| 748 | exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>" |
| 749 | exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>" |
| 750 | call assert_equal(['# abc', 'def', 'def'], getline(2, 4)) |
| 751 | |
| 752 | set ai&vim |
| 753 | let &fo = save_fo |
| 754 | enew! |
| 755 | endfunc |
| 756 | |
| 757 | " Test for autocommand that deletes the current buffer on BufLeave event. |
| 758 | " Also test deleting the last buffer, should give a new, empty buffer. |
| 759 | func Test_BufLeave_Wipe() |
| 760 | %bwipe! |
| 761 | let content = ['start of test file Xxx', |
| 762 | \ 'this is a test', |
| 763 | \ 'end of test file Xxx'] |
| 764 | call writefile(content, 'Xxx1') |
| 765 | call writefile(content, 'Xxx2') |
| 766 | |
| 767 | au BufLeave Xxx2 bwipe |
| 768 | edit Xxx1 |
| 769 | split Xxx2 |
| 770 | " delete buffer Xxx2, we should be back to Xxx1 |
| 771 | bwipe |
| 772 | call assert_equal('Xxx1', bufname('%')) |
| 773 | call assert_equal(1, winnr('$')) |
| 774 | |
| 775 | " Create an alternate buffer |
| 776 | %write! test.out |
| 777 | call assert_equal('test.out', bufname('#')) |
| 778 | " delete alternate buffer |
| 779 | bwipe test.out |
| 780 | call assert_equal('Xxx1', bufname('%')) |
| 781 | call assert_equal('', bufname('#')) |
| 782 | |
| 783 | au BufLeave Xxx1 bwipe |
| 784 | " delete current buffer, get an empty one |
| 785 | bwipe! |
| 786 | call assert_equal(1, line('$')) |
| 787 | call assert_equal('', bufname('%')) |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 788 | let g:bufinfo = getbufinfo() |
| 789 | call assert_equal(1, len(g:bufinfo)) |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 790 | |
| 791 | call delete('Xxx1') |
| 792 | call delete('Xxx2') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 793 | call delete('test.out') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 794 | %bwipe |
| 795 | au! BufLeave |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 796 | |
| 797 | " check that bufinfo doesn't contain a pointer to freed memory |
| 798 | call test_garbagecollect_now() |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 799 | endfunc |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 800 | |
| 801 | func Test_QuitPre() |
| 802 | edit Xfoo |
| 803 | let winid = win_getid(winnr()) |
| 804 | split Xbar |
| 805 | au! QuitPre * let g:afile = expand('<afile>') |
| 806 | " Close the other window, <afile> should be correct. |
| 807 | exe win_id2win(winid) . 'q' |
| 808 | call assert_equal('Xfoo', g:afile) |
| 809 | |
| 810 | unlet g:afile |
| 811 | bwipe Xfoo |
| 812 | bwipe Xbar |
| 813 | endfunc |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 814 | |
| 815 | func Test_Cmdline() |
| 816 | au! CmdlineEnter : let g:entered = expand('<afile>') |
| 817 | au! CmdlineLeave : let g:left = expand('<afile>') |
| 818 | let g:entered = 0 |
| 819 | let g:left = 0 |
| 820 | call feedkeys(":echo 'hello'\<CR>", 'xt') |
| 821 | call assert_equal(':', g:entered) |
| 822 | call assert_equal(':', g:left) |
| 823 | au! CmdlineEnter |
| 824 | au! CmdlineLeave |
| 825 | |
| 826 | au! CmdlineEnter / let g:entered = expand('<afile>') |
| 827 | au! CmdlineLeave / let g:left = expand('<afile>') |
| 828 | let g:entered = 0 |
| 829 | let g:left = 0 |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 830 | new |
| 831 | call setline(1, 'hello') |
| 832 | call feedkeys("/hello\<CR>", 'xt') |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 833 | call assert_equal('/', g:entered) |
| 834 | call assert_equal('/', g:left) |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 835 | bwipe! |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 836 | au! CmdlineEnter |
| 837 | au! CmdlineLeave |
| 838 | endfunc |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 839 | |
| 840 | " Test for BufWritePre autocommand that deletes or unloads the buffer. |
| 841 | func Test_BufWritePre() |
| 842 | %bwipe |
| 843 | au BufWritePre Xxx1 bunload |
| 844 | au BufWritePre Xxx2 bwipe |
| 845 | |
| 846 | call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1') |
| 847 | call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2') |
| 848 | |
| 849 | edit Xtest |
| 850 | e! Xxx2 |
| 851 | bdel Xtest |
| 852 | e Xxx1 |
| 853 | " write it, will unload it and give an error msg |
| 854 | call assert_fails('w', 'E203') |
| 855 | call assert_equal('Xxx2', bufname('%')) |
| 856 | edit Xtest |
| 857 | e! Xxx2 |
| 858 | bwipe Xtest |
| 859 | " write it, will delete the buffer and give an error msg |
| 860 | call assert_fails('w', 'E203') |
| 861 | call assert_equal('Xxx1', bufname('%')) |
| 862 | au! BufWritePre |
| 863 | call delete('Xxx1') |
| 864 | call delete('Xxx2') |
| 865 | endfunc |
| 866 | |
| 867 | " Test for BufUnload autocommand that unloads all the other buffers |
| 868 | func Test_bufunload_all() |
| 869 | call writefile(['Test file Xxx1'], 'Xxx1')" |
| 870 | call writefile(['Test file Xxx2'], 'Xxx2')" |
| 871 | |
| 872 | let content = [ |
| 873 | \ "func UnloadAllBufs()", |
| 874 | \ " let i = 1", |
| 875 | \ " while i <= bufnr('$')", |
| 876 | \ " if i != bufnr('%') && bufloaded(i)", |
| 877 | \ " exe i . 'bunload'", |
| 878 | \ " endif", |
| 879 | \ " let i += 1", |
| 880 | \ " endwhile", |
| 881 | \ "endfunc", |
| 882 | \ "au BufUnload * call UnloadAllBufs()", |
| 883 | \ "au VimLeave * call writefile(['Test Finished'], 'Xout')", |
| 884 | \ "edit Xxx1", |
| 885 | \ "split Xxx2", |
| 886 | \ "q"] |
| 887 | call writefile(content, 'Xtest') |
| 888 | |
| 889 | call delete('Xout') |
| 890 | call system(v:progpath. ' --clean -N --not-a-term -S Xtest') |
| 891 | call assert_true(filereadable('Xout')) |
| 892 | |
| 893 | call delete('Xxx1') |
| 894 | call delete('Xxx2') |
| 895 | call delete('Xtest') |
| 896 | call delete('Xout') |
| 897 | endfunc |
| 898 | |
| 899 | " Some tests for buffer-local autocommands |
| 900 | func Test_buflocal_autocmd() |
| 901 | let g:bname = '' |
| 902 | edit xx |
| 903 | au BufLeave <buffer> let g:bname = expand("%") |
| 904 | " here, autocommand for xx should trigger. |
| 905 | " but autocommand shall not apply to buffer named <buffer>. |
| 906 | edit somefile |
| 907 | call assert_equal('xx', g:bname) |
| 908 | let g:bname = '' |
| 909 | " here, autocommand shall be auto-deleted |
| 910 | bwipe xx |
| 911 | " autocmd should not trigger |
| 912 | edit xx |
| 913 | call assert_equal('', g:bname) |
| 914 | " autocmd should not trigger |
| 915 | edit somefile |
| 916 | call assert_equal('', g:bname) |
| 917 | enew |
| 918 | unlet g:bname |
| 919 | endfunc |