Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 1 | " Tests for autocommands |
| 2 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 3 | func! s:cleanup_buffers() abort |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 4 | for bnr in range(1, bufnr('$')) |
| 5 | if bufloaded(bnr) && bufnr('%') != bnr |
| 6 | execute 'bd! ' . bnr |
| 7 | endif |
| 8 | endfor |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 9 | endfunc |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 10 | |
Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 11 | func Test_vim_did_enter() |
| 12 | call assert_false(v:vim_did_enter) |
| 13 | |
| 14 | " This script will never reach the main loop, can't check if v:vim_did_enter |
| 15 | " becomes one. |
| 16 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 17 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 18 | if has('timers') |
| 19 | func ExitInsertMode(id) |
| 20 | call feedkeys("\<Esc>") |
| 21 | endfunc |
| 22 | |
| 23 | func Test_cursorhold_insert() |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 24 | " Need to move the cursor. |
| 25 | call feedkeys("ggG", "xt") |
| 26 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 27 | let g:triggered = 0 |
| 28 | au CursorHoldI * let g:triggered += 1 |
| 29 | set updatetime=20 |
| 30 | call timer_start(100, 'ExitInsertMode') |
| 31 | call feedkeys('a', 'x!') |
| 32 | call assert_equal(1, g:triggered) |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 33 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 34 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 35 | endfunc |
| 36 | |
| 37 | func Test_cursorhold_insert_ctrl_x() |
| 38 | let g:triggered = 0 |
| 39 | au CursorHoldI * let g:triggered += 1 |
| 40 | set updatetime=20 |
| 41 | call timer_start(100, 'ExitInsertMode') |
| 42 | " CursorHoldI does not trigger after CTRL-X |
| 43 | call feedkeys("a\<C-X>", 'x!') |
| 44 | call assert_equal(0, g:triggered) |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 45 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 46 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 47 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 48 | endif |
| 49 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 50 | func Test_bufunload() |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 51 | augroup test_bufunload_group |
| 52 | autocmd! |
| 53 | autocmd BufUnload * call add(s:li, "bufunload") |
| 54 | autocmd BufDelete * call add(s:li, "bufdelete") |
| 55 | autocmd BufWipeout * call add(s:li, "bufwipeout") |
| 56 | augroup END |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 57 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 58 | let s:li=[] |
| 59 | new |
| 60 | setlocal bufhidden= |
| 61 | bunload |
| 62 | call assert_equal(["bufunload", "bufdelete"], s:li) |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 63 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 64 | let s:li=[] |
| 65 | new |
| 66 | setlocal bufhidden=delete |
| 67 | bunload |
| 68 | call assert_equal(["bufunload", "bufdelete"], s:li) |
| 69 | |
| 70 | let s:li=[] |
| 71 | new |
| 72 | setlocal bufhidden=unload |
| 73 | bwipeout |
| 74 | call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li) |
| 75 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 76 | au! test_bufunload_group |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 77 | augroup! test_bufunload_group |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 78 | endfunc |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 79 | |
| 80 | " SEGV occurs in older versions. (At least 7.4.2005 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 81 | func Test_autocmd_bufunload_with_tabnext() |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 82 | tabedit |
| 83 | tabfirst |
| 84 | |
| 85 | augroup test_autocmd_bufunload_with_tabnext_group |
| 86 | autocmd! |
| 87 | autocmd BufUnload <buffer> tabnext |
| 88 | augroup END |
| 89 | |
| 90 | quit |
| 91 | call assert_equal(2, tabpagenr('$')) |
| 92 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 93 | autocmd! test_autocmd_bufunload_with_tabnext_group |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 94 | augroup! test_autocmd_bufunload_with_tabnext_group |
| 95 | tablast |
| 96 | quit |
| 97 | endfunc |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 98 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 99 | func Test_autocmd_bufwinleave_with_tabfirst() |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 100 | tabedit |
| 101 | augroup sample |
| 102 | autocmd! |
| 103 | autocmd BufWinLeave <buffer> tabfirst |
| 104 | augroup END |
| 105 | call setline(1, ['a', 'b', 'c']) |
| 106 | edit! a.txt |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 107 | tabclose |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 108 | endfunc |
| 109 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 110 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 111 | func Test_autocmd_bufunload_avoiding_SEGV_01() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 112 | split aa.txt |
| 113 | let lastbuf = bufnr('$') |
| 114 | |
| 115 | augroup test_autocmd_bufunload |
| 116 | autocmd! |
| 117 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 118 | augroup END |
| 119 | |
| 120 | call assert_fails('edit bb.txt', 'E937:') |
| 121 | |
| 122 | autocmd! test_autocmd_bufunload |
| 123 | augroup! test_autocmd_bufunload |
| 124 | bwipe! aa.txt |
| 125 | bwipe! bb.txt |
| 126 | endfunc |
| 127 | |
| 128 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 129 | func Test_autocmd_bufunload_avoiding_SEGV_02() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 130 | setlocal buftype=nowrite |
| 131 | let lastbuf = bufnr('$') |
| 132 | |
| 133 | augroup test_autocmd_bufunload |
| 134 | autocmd! |
| 135 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 136 | augroup END |
| 137 | |
| 138 | normal! i1 |
| 139 | call assert_fails('edit a.txt', 'E517:') |
| 140 | call feedkeys("\<CR>") |
| 141 | |
| 142 | autocmd! test_autocmd_bufunload |
| 143 | augroup! test_autocmd_bufunload |
| 144 | bwipe! a.txt |
| 145 | endfunc |
| 146 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 147 | func Test_win_tab_autocmd() |
| 148 | let g:record = [] |
| 149 | |
| 150 | augroup testing |
| 151 | au WinNew * call add(g:record, 'WinNew') |
| 152 | au WinEnter * call add(g:record, 'WinEnter') |
| 153 | au WinLeave * call add(g:record, 'WinLeave') |
| 154 | au TabNew * call add(g:record, 'TabNew') |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 155 | au TabClosed * call add(g:record, 'TabClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 156 | au TabEnter * call add(g:record, 'TabEnter') |
| 157 | au TabLeave * call add(g:record, 'TabLeave') |
| 158 | augroup END |
| 159 | |
| 160 | split |
| 161 | tabnew |
| 162 | close |
| 163 | close |
| 164 | |
| 165 | call assert_equal([ |
| 166 | \ 'WinLeave', 'WinNew', 'WinEnter', |
| 167 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 168 | \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter', |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 169 | \ 'WinLeave', 'WinEnter' |
| 170 | \ ], g:record) |
| 171 | |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 172 | let g:record = [] |
| 173 | tabnew somefile |
| 174 | tabnext |
| 175 | bwipe somefile |
| 176 | |
| 177 | call assert_equal([ |
| 178 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
| 179 | \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', |
| 180 | \ 'TabClosed' |
| 181 | \ ], g:record) |
| 182 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 183 | augroup testing |
| 184 | au! |
| 185 | augroup END |
| 186 | unlet g:record |
| 187 | endfunc |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 188 | |
| 189 | func s:AddAnAutocmd() |
| 190 | augroup vimBarTest |
| 191 | au BufReadCmd * echo 'hello' |
| 192 | augroup END |
| 193 | call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) |
| 194 | endfunc |
| 195 | |
| 196 | func Test_early_bar() |
| 197 | " test that a bar is recognized before the {event} |
| 198 | call s:AddAnAutocmd() |
| 199 | augroup vimBarTest | au! | augroup END |
| 200 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 201 | |
| 202 | call s:AddAnAutocmd() |
| 203 | augroup vimBarTest| au!| augroup END |
| 204 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 205 | |
| 206 | " test that a bar is recognized after the {event} |
| 207 | call s:AddAnAutocmd() |
| 208 | augroup vimBarTest| au!BufReadCmd| augroup END |
| 209 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 210 | |
| 211 | " test that a bar is recognized after the {group} |
| 212 | call s:AddAnAutocmd() |
| 213 | au! vimBarTest|echo 'hello' |
| 214 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 215 | endfunc |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 216 | |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 217 | func RemoveGroup() |
| 218 | autocmd! StartOK |
| 219 | augroup! StartOK |
| 220 | endfunc |
| 221 | |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 222 | func Test_augroup_warning() |
| 223 | augroup TheWarning |
| 224 | au VimEnter * echo 'entering' |
| 225 | augroup END |
| 226 | call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0) |
| 227 | redir => res |
| 228 | augroup! TheWarning |
| 229 | redir END |
| 230 | call assert_true(match(res, "W19:") >= 0) |
| 231 | call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) |
| 232 | |
| 233 | " check "Another" does not take the pace of the deleted entry |
| 234 | augroup Another |
| 235 | augroup END |
| 236 | call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 237 | augroup! Another |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 238 | |
| 239 | " no warning for postpone aucmd delete |
| 240 | augroup StartOK |
| 241 | au VimEnter * call RemoveGroup() |
| 242 | augroup END |
| 243 | call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0) |
| 244 | redir => res |
| 245 | doautocmd VimEnter |
| 246 | redir END |
| 247 | call assert_true(match(res, "W19:") < 0) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 248 | au! VimEnter |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 249 | endfunc |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 250 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 251 | func Test_BufReadCmdHelp() |
| 252 | " This used to cause access to free memory |
| 253 | au BufReadCmd * e +h |
| 254 | help |
| 255 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 256 | au! BufReadCmd |
| 257 | endfunc |
| 258 | |
| 259 | func Test_BufReadCmdHelpJump() |
| 260 | " This used to cause access to free memory |
| 261 | au BufReadCmd * e +h{ |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 262 | " } to fix highlighting |
| 263 | call assert_fails('help', 'E434:') |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 264 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 265 | au! BufReadCmd |
| 266 | endfunc |
| 267 | |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 268 | func Test_augroup_deleted() |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 269 | " This caused a crash before E936 was introduced |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 270 | augroup x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 271 | call assert_fails('augroup! x', 'E936:') |
| 272 | au VimEnter * echo |
| 273 | augroup end |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 274 | augroup! x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 275 | call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) |
| 276 | au! VimEnter |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 277 | endfunc |
| 278 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 279 | " Tests for autocommands on :close command. |
| 280 | " This used to be in test13. |
| 281 | func Test_three_windows() |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 282 | " Clean up buffers, because in some cases this function fails. |
| 283 | call s:cleanup_buffers() |
| 284 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 285 | " Write three files and open them, each in a window. |
| 286 | " Then go to next window, with autocommand that deletes the previous one. |
| 287 | " Do this twice, writing the file. |
| 288 | e! Xtestje1 |
| 289 | call setline(1, 'testje1') |
| 290 | w |
| 291 | sp Xtestje2 |
| 292 | call setline(1, 'testje2') |
| 293 | w |
| 294 | sp Xtestje3 |
| 295 | call setline(1, 'testje3') |
| 296 | w |
| 297 | wincmd w |
| 298 | au WinLeave Xtestje2 bwipe |
| 299 | wincmd w |
| 300 | call assert_equal('Xtestje1', expand('%')) |
| 301 | |
| 302 | au WinLeave Xtestje1 bwipe Xtestje3 |
| 303 | close |
| 304 | call assert_equal('Xtestje1', expand('%')) |
| 305 | |
| 306 | " Test deleting the buffer on a Unload event. If this goes wrong there |
| 307 | " will be the ATTENTION prompt. |
| 308 | e Xtestje1 |
| 309 | au! |
| 310 | au! BufUnload Xtestje1 bwipe |
| 311 | call assert_fails('e Xtestje3', 'E937:') |
| 312 | call assert_equal('Xtestje3', expand('%')) |
| 313 | |
| 314 | e Xtestje2 |
| 315 | sp Xtestje1 |
| 316 | call assert_fails('e', 'E937:') |
| 317 | call assert_equal('Xtestje2', expand('%')) |
| 318 | |
| 319 | " Test changing buffers in a BufWipeout autocommand. If this goes wrong |
| 320 | " there are ml_line errors and/or a Crash. |
| 321 | au! |
| 322 | only |
| 323 | e Xanother |
| 324 | e Xtestje1 |
| 325 | bwipe Xtestje2 |
| 326 | bwipe Xtestje3 |
| 327 | au BufWipeout Xtestje1 buf Xtestje1 |
| 328 | bwipe |
| 329 | call assert_equal('Xanother', expand('%')) |
| 330 | |
| 331 | only |
| 332 | help |
| 333 | wincmd w |
| 334 | 1quit |
| 335 | call assert_equal('Xanother', expand('%')) |
| 336 | |
| 337 | au! |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 338 | enew |
| 339 | bwipe! Xtestje1 |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 340 | call delete('Xtestje1') |
| 341 | call delete('Xtestje2') |
| 342 | call delete('Xtestje3') |
| 343 | endfunc |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 344 | |
| 345 | func Test_BufEnter() |
| 346 | au! BufEnter |
| 347 | au Bufenter * let val = val . '+' |
| 348 | let g:val = '' |
| 349 | split NewFile |
| 350 | call assert_equal('+', g:val) |
| 351 | bwipe! |
| 352 | call assert_equal('++', g:val) |
| 353 | |
| 354 | " Also get BufEnter when editing a directory |
| 355 | call mkdir('Xdir') |
| 356 | split Xdir |
| 357 | call assert_equal('+++', g:val) |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 358 | |
| 359 | " On MS-Windows we can't edit the directory, make sure we wipe the right |
| 360 | " buffer. |
| 361 | bwipe! Xdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 362 | |
| 363 | call delete('Xdir', 'd') |
| 364 | au! BufEnter |
| 365 | endfunc |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 366 | |
| 367 | " Closing a window might cause an endless loop |
| 368 | " E814 for older Vims |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 369 | func Test_autocmd_bufwipe_in_SessLoadPost() |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 370 | edit Xtest |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 371 | tabnew |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 372 | file Xsomething |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 373 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 374 | mksession! |
| 375 | |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 376 | let content = ['set nocp noswapfile', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 377 | \ 'let v:swapchoice="e"', |
| 378 | \ 'augroup test_autocmd_sessionload', |
| 379 | \ 'autocmd!', |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 380 | \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 381 | \ 'augroup END', |
| 382 | \ '', |
| 383 | \ 'func WriteErrors()', |
| 384 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 385 | \ 'endfunc', |
| 386 | \ 'au VimLeave * call WriteErrors()', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 387 | \ ] |
| 388 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 389 | call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq') |
| 390 | let errors = join(readfile('Xerrors')) |
| 391 | call assert_match('E814', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 392 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 393 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 394 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 395 | call delete(file) |
| 396 | endfor |
| 397 | endfunc |
| 398 | |
| 399 | " SEGV occurs in older versions. |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 400 | func Test_autocmd_bufwipe_in_SessLoadPost2() |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 401 | tabnew |
| 402 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 403 | mksession! |
| 404 | |
| 405 | let content = ['set nocp noswapfile', |
| 406 | \ 'function! DeleteInactiveBufs()', |
| 407 | \ ' tabfirst', |
| 408 | \ ' let tabblist = []', |
| 409 | \ ' for i in range(1, tabpagenr(''$''))', |
| 410 | \ ' call extend(tabblist, tabpagebuflist(i))', |
| 411 | \ ' endfor', |
| 412 | \ ' for b in range(1, bufnr(''$''))', |
| 413 | \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')', |
| 414 | \ ' exec ''bwipeout '' . b', |
| 415 | \ ' endif', |
| 416 | \ ' endfor', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 417 | \ ' echomsg "SessionLoadPost DONE"', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 418 | \ 'endfunction', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 419 | \ 'au SessionLoadPost * call DeleteInactiveBufs()', |
| 420 | \ '', |
| 421 | \ 'func WriteErrors()', |
| 422 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 423 | \ 'endfunc', |
| 424 | \ 'au VimLeave * call WriteErrors()', |
| 425 | \ ] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 426 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 427 | call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq') |
| 428 | let errors = join(readfile('Xerrors')) |
| 429 | " This probably only ever matches on unix. |
| 430 | call assert_notmatch('Caught deadly signal SEGV', errors) |
| 431 | call assert_match('SessionLoadPost DONE', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 432 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 433 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 434 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 435 | call delete(file) |
| 436 | endfor |
| 437 | endfunc |
Bram Moolenaar | faf29d7 | 2017-07-09 11:07:16 +0200 | [diff] [blame] | 438 | |
| 439 | func Test_empty_doau() |
| 440 | doau \| |
| 441 | endfunc |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 442 | |
| 443 | func s:AutoCommandOptionSet(match) |
| 444 | let item = remove(g:options, 0) |
| 445 | let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3]) |
| 446 | let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type) |
| 447 | let g:opt = [expected, actual] |
| 448 | "call assert_equal(expected, actual) |
| 449 | endfunc |
| 450 | |
| 451 | func Test_OptionSet() |
| 452 | if !has("eval") || !has("autocmd") || !exists("+autochdir") |
| 453 | return |
| 454 | endif |
| 455 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 456 | badd test_autocmd.vim |
| 457 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 458 | call test_override('starting', 1) |
| 459 | set nocp |
| 460 | au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>")) |
| 461 | |
| 462 | " 1: Setting number option" |
| 463 | let g:options=[['number', 0, 1, 'global']] |
| 464 | set nu |
| 465 | call assert_equal([], g:options) |
| 466 | call assert_equal(g:opt[0], g:opt[1]) |
| 467 | |
| 468 | " 2: Setting local number option" |
| 469 | let g:options=[['number', 1, 0, 'local']] |
| 470 | setlocal nonu |
| 471 | call assert_equal([], g:options) |
| 472 | call assert_equal(g:opt[0], g:opt[1]) |
| 473 | |
| 474 | " 3: Setting global number option" |
| 475 | let g:options=[['number', 1, 0, 'global']] |
| 476 | setglobal nonu |
| 477 | call assert_equal([], g:options) |
| 478 | call assert_equal(g:opt[0], g:opt[1]) |
| 479 | |
| 480 | " 4: Setting local autoindent option" |
| 481 | let g:options=[['autoindent', 0, 1, 'local']] |
| 482 | setlocal ai |
| 483 | call assert_equal([], g:options) |
| 484 | call assert_equal(g:opt[0], g:opt[1]) |
| 485 | |
| 486 | " 5: Setting global autoindent option" |
| 487 | let g:options=[['autoindent', 0, 1, 'global']] |
| 488 | setglobal ai |
| 489 | call assert_equal([], g:options) |
| 490 | call assert_equal(g:opt[0], g:opt[1]) |
| 491 | |
| 492 | " 6: Setting global autoindent option" |
| 493 | let g:options=[['autoindent', 1, 0, 'global']] |
| 494 | set ai! |
| 495 | call assert_equal([], g:options) |
| 496 | call assert_equal(g:opt[0], g:opt[1]) |
| 497 | |
| 498 | " Should not print anything, use :noa |
| 499 | " 7: don't trigger OptionSet" |
| 500 | let g:options=[['invalid', 1, 1, 'invalid']] |
| 501 | noa set nonu |
| 502 | call assert_equal([['invalid', 1, 1, 'invalid']], g:options) |
| 503 | call assert_equal(g:opt[0], g:opt[1]) |
| 504 | |
| 505 | " 8: Setting several global list and number option" |
| 506 | let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] |
| 507 | set list nu |
| 508 | call assert_equal([], g:options) |
| 509 | call assert_equal(g:opt[0], g:opt[1]) |
| 510 | |
| 511 | " 9: don't trigger OptionSet" |
| 512 | let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']] |
| 513 | noa set nolist nonu |
| 514 | call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options) |
| 515 | call assert_equal(g:opt[0], g:opt[1]) |
| 516 | |
| 517 | " 10: Setting global acd" |
| 518 | let g:options=[['autochdir', 0, 1, 'local']] |
| 519 | setlocal acd |
| 520 | call assert_equal([], g:options) |
| 521 | call assert_equal(g:opt[0], g:opt[1]) |
| 522 | |
| 523 | " 11: Setting global autoread (also sets local value)" |
| 524 | let g:options=[['autoread', 0, 1, 'global']] |
| 525 | set ar |
| 526 | call assert_equal([], g:options) |
| 527 | call assert_equal(g:opt[0], g:opt[1]) |
| 528 | |
| 529 | " 12: Setting local autoread" |
| 530 | let g:options=[['autoread', 1, 1, 'local']] |
| 531 | setlocal ar |
| 532 | call assert_equal([], g:options) |
| 533 | call assert_equal(g:opt[0], g:opt[1]) |
| 534 | |
| 535 | " 13: Setting global autoread" |
| 536 | let g:options=[['autoread', 1, 0, 'global']] |
| 537 | setglobal invar |
| 538 | call assert_equal([], g:options) |
| 539 | call assert_equal(g:opt[0], g:opt[1]) |
| 540 | |
| 541 | " 14: Setting option backspace through :let" |
| 542 | let g:options=[['backspace', '', 'eol,indent,start', 'global']] |
| 543 | let &bs="eol,indent,start" |
| 544 | call assert_equal([], g:options) |
| 545 | call assert_equal(g:opt[0], g:opt[1]) |
| 546 | |
| 547 | " 15: Setting option backspace through setbufvar()" |
| 548 | let g:options=[['backup', 0, 1, 'local']] |
| 549 | " try twice, first time, shouldn't trigger because option name is invalid, |
| 550 | " second time, it should trigger |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 551 | let bnum = bufnr('%') |
| 552 | call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355") |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 553 | " should trigger, use correct option name |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 554 | call setbufvar(bnum, '&backup', 1) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 555 | call assert_equal([], g:options) |
| 556 | call assert_equal(g:opt[0], g:opt[1]) |
| 557 | |
| 558 | " 16: Setting number option using setwinvar" |
| 559 | let g:options=[['number', 0, 1, 'local']] |
| 560 | call setwinvar(0, '&number', 1) |
| 561 | call assert_equal([], g:options) |
| 562 | call assert_equal(g:opt[0], g:opt[1]) |
| 563 | |
| 564 | " 17: Setting key option, shouldn't trigger" |
| 565 | let g:options=[['key', 'invalid', 'invalid1', 'invalid']] |
| 566 | setlocal key=blah |
| 567 | setlocal key= |
| 568 | call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options) |
| 569 | call assert_equal(g:opt[0], g:opt[1]) |
| 570 | |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 571 | " 18: Setting string option" |
| 572 | let oldval = &tags |
| 573 | let g:options=[['tags', oldval, 'tagpath', 'global']] |
| 574 | set tags=tagpath |
| 575 | call assert_equal([], g:options) |
| 576 | call assert_equal(g:opt[0], g:opt[1]) |
| 577 | |
| 578 | " 1l: Resetting string option" |
| 579 | let g:options=[['tags', 'tagpath', oldval, 'global']] |
| 580 | set tags& |
| 581 | call assert_equal([], g:options) |
| 582 | call assert_equal(g:opt[0], g:opt[1]) |
| 583 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 584 | " Cleanup |
| 585 | au! OptionSet |
| 586 | for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp'] |
| 587 | exe printf(":set %s&vi", opt) |
| 588 | endfor |
| 589 | call test_override('starting', 0) |
| 590 | delfunc! AutoCommandOptionSet |
| 591 | endfunc |
| 592 | |
| 593 | func Test_OptionSet_diffmode() |
| 594 | call test_override('starting', 1) |
| 595 | " 18: Changing an option when enetering diff mode |
| 596 | new |
| 597 | au OptionSet diff :let &l:cul=v:option_new |
| 598 | |
| 599 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 600 | call assert_equal(0, &l:cul) |
| 601 | diffthis |
| 602 | call assert_equal(1, &l:cul) |
| 603 | |
| 604 | vnew |
| 605 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 606 | call assert_equal(0, &l:cul) |
| 607 | diffthis |
| 608 | call assert_equal(1, &l:cul) |
| 609 | |
| 610 | diffoff |
| 611 | call assert_equal(0, &l:cul) |
| 612 | call assert_equal(1, getwinvar(2, '&l:cul')) |
| 613 | bw! |
| 614 | |
| 615 | call assert_equal(1, &l:cul) |
| 616 | diffoff! |
| 617 | call assert_equal(0, &l:cul) |
| 618 | call assert_equal(0, getwinvar(1, '&l:cul')) |
| 619 | bw! |
| 620 | |
| 621 | " Cleanup |
| 622 | au! OptionSet |
| 623 | call test_override('starting', 0) |
| 624 | endfunc |
| 625 | |
| 626 | func Test_OptionSet_diffmode_close() |
| 627 | call test_override('starting', 1) |
| 628 | " 19: Try to close the current window when entering diff mode |
| 629 | " should not segfault |
| 630 | new |
| 631 | au OptionSet diff close |
| 632 | |
| 633 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 634 | call assert_fails(':diffthis', 'E788') |
| 635 | call assert_equal(1, &diff) |
| 636 | vnew |
| 637 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 638 | call assert_fails(':diffthis', 'E788') |
| 639 | call assert_equal(1, &diff) |
| 640 | bw! |
| 641 | call assert_fails(':diffoff!', 'E788') |
| 642 | bw! |
| 643 | |
| 644 | " Cleanup |
| 645 | au! OptionSet |
| 646 | call test_override('starting', 0) |
| 647 | "delfunc! AutoCommandOptionSet |
| 648 | endfunc |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 649 | |
| 650 | " Test for Bufleave autocommand that deletes the buffer we are about to edit. |
| 651 | func Test_BufleaveWithDelete() |
| 652 | new | edit Xfile1 |
| 653 | |
| 654 | augroup test_bufleavewithdelete |
| 655 | autocmd! |
| 656 | autocmd BufLeave Xfile1 bwipe Xfile2 |
| 657 | augroup END |
| 658 | |
| 659 | call assert_fails('edit Xfile2', 'E143:') |
| 660 | call assert_equal('Xfile1', bufname('%')) |
| 661 | |
| 662 | autocmd! test_bufleavewithdelete BufLeave Xfile1 |
| 663 | augroup! test_bufleavewithdelete |
| 664 | |
| 665 | new |
| 666 | bwipe! Xfile1 |
| 667 | endfunc |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 668 | |
| 669 | " Test for autocommand that changes the buffer list, when doing ":ball". |
| 670 | func Test_Acmd_BufAll() |
| 671 | enew! |
| 672 | %bwipe! |
| 673 | call writefile(['Test file Xxx1'], 'Xxx1') |
| 674 | call writefile(['Test file Xxx2'], 'Xxx2') |
| 675 | call writefile(['Test file Xxx3'], 'Xxx3') |
| 676 | |
| 677 | " Add three files to the buffer list |
| 678 | split Xxx1 |
| 679 | close |
| 680 | split Xxx2 |
| 681 | close |
| 682 | split Xxx3 |
| 683 | close |
| 684 | |
| 685 | " Wipe the buffer when the buffer is opened |
| 686 | au BufReadPost Xxx2 bwipe |
| 687 | |
| 688 | call append(0, 'Test file Xxx4') |
| 689 | ball |
| 690 | |
| 691 | call assert_equal(2, winnr('$')) |
| 692 | call assert_equal('Xxx1', bufname(winbufnr(winnr('$')))) |
| 693 | wincmd t |
| 694 | |
| 695 | au! BufReadPost |
| 696 | %bwipe! |
| 697 | call delete('Xxx1') |
| 698 | call delete('Xxx2') |
| 699 | call delete('Xxx3') |
| 700 | enew! | only |
| 701 | endfunc |
| 702 | |
| 703 | " Test for autocommand that changes current buffer on BufEnter event. |
| 704 | " Check if modelines are interpreted for the correct buffer. |
| 705 | func Test_Acmd_BufEnter() |
| 706 | %bwipe! |
| 707 | call writefile(['start of test file Xxx1', |
| 708 | \ "\<Tab>this is a test", |
| 709 | \ 'end of test file Xxx1'], 'Xxx1') |
| 710 | call writefile(['start of test file Xxx2', |
| 711 | \ 'vim: set noai :', |
| 712 | \ "\<Tab>this is a test", |
| 713 | \ 'end of test file Xxx2'], 'Xxx2') |
| 714 | |
| 715 | au BufEnter Xxx2 brew |
| 716 | set ai modeline modelines=3 |
| 717 | edit Xxx1 |
| 718 | " edit Xxx2, autocmd will do :brew |
| 719 | edit Xxx2 |
| 720 | exe "normal G?this is a\<CR>" |
| 721 | " Append text with autoindent to this file |
| 722 | normal othis should be auto-indented |
| 723 | call assert_equal("\<Tab>this should be auto-indented", getline('.')) |
| 724 | call assert_equal(3, line('.')) |
| 725 | " Remove autocmd and edit Xxx2 again |
| 726 | au! BufEnter Xxx2 |
| 727 | buf! Xxx2 |
| 728 | exe "normal G?this is a\<CR>" |
| 729 | " append text without autoindent to Xxx |
| 730 | normal othis should be in column 1 |
| 731 | call assert_equal("this should be in column 1", getline('.')) |
| 732 | call assert_equal(4, line('.')) |
| 733 | |
| 734 | %bwipe! |
| 735 | call delete('Xxx1') |
| 736 | call delete('Xxx2') |
| 737 | set ai&vim modeline&vim modelines&vim |
| 738 | endfunc |
| 739 | |
| 740 | " Test for issue #57 |
| 741 | " do not move cursor on <c-o> when autoindent is set |
| 742 | func Test_ai_CTRL_O() |
| 743 | enew! |
| 744 | set ai |
| 745 | let save_fo = &fo |
| 746 | set fo+=r |
| 747 | exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>" |
| 748 | exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>" |
| 749 | call assert_equal(['# abc', 'def', 'def'], getline(2, 4)) |
| 750 | |
| 751 | set ai&vim |
| 752 | let &fo = save_fo |
| 753 | enew! |
| 754 | endfunc |
| 755 | |
| 756 | " Test for autocommand that deletes the current buffer on BufLeave event. |
| 757 | " Also test deleting the last buffer, should give a new, empty buffer. |
| 758 | func Test_BufLeave_Wipe() |
| 759 | %bwipe! |
| 760 | let content = ['start of test file Xxx', |
| 761 | \ 'this is a test', |
| 762 | \ 'end of test file Xxx'] |
| 763 | call writefile(content, 'Xxx1') |
| 764 | call writefile(content, 'Xxx2') |
| 765 | |
| 766 | au BufLeave Xxx2 bwipe |
| 767 | edit Xxx1 |
| 768 | split Xxx2 |
| 769 | " delete buffer Xxx2, we should be back to Xxx1 |
| 770 | bwipe |
| 771 | call assert_equal('Xxx1', bufname('%')) |
| 772 | call assert_equal(1, winnr('$')) |
| 773 | |
| 774 | " Create an alternate buffer |
| 775 | %write! test.out |
| 776 | call assert_equal('test.out', bufname('#')) |
| 777 | " delete alternate buffer |
| 778 | bwipe test.out |
| 779 | call assert_equal('Xxx1', bufname('%')) |
| 780 | call assert_equal('', bufname('#')) |
| 781 | |
| 782 | au BufLeave Xxx1 bwipe |
| 783 | " delete current buffer, get an empty one |
| 784 | bwipe! |
| 785 | call assert_equal(1, line('$')) |
| 786 | call assert_equal('', bufname('%')) |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 787 | let g:bufinfo = getbufinfo() |
| 788 | call assert_equal(1, len(g:bufinfo)) |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 789 | |
| 790 | call delete('Xxx1') |
| 791 | call delete('Xxx2') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 792 | call delete('test.out') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 793 | %bwipe |
| 794 | au! BufLeave |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 795 | |
| 796 | " check that bufinfo doesn't contain a pointer to freed memory |
| 797 | call test_garbagecollect_now() |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 798 | endfunc |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 799 | |
| 800 | func Test_QuitPre() |
| 801 | edit Xfoo |
| 802 | let winid = win_getid(winnr()) |
| 803 | split Xbar |
| 804 | au! QuitPre * let g:afile = expand('<afile>') |
| 805 | " Close the other window, <afile> should be correct. |
| 806 | exe win_id2win(winid) . 'q' |
| 807 | call assert_equal('Xfoo', g:afile) |
| 808 | |
| 809 | unlet g:afile |
| 810 | bwipe Xfoo |
| 811 | bwipe Xbar |
| 812 | endfunc |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 813 | |
| 814 | func Test_Cmdline() |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 815 | au! CmdlineChanged : let g:text = getcmdline() |
| 816 | let g:text = 0 |
| 817 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 818 | call assert_equal("echom 'hello'", g:text) |
| 819 | au! CmdlineChanged |
| 820 | |
| 821 | au! CmdlineChanged : let g:entered = expand('<afile>') |
| 822 | let g:entered = 0 |
| 823 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 824 | call assert_equal(':', g:entered) |
| 825 | au! CmdlineChanged |
| 826 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 827 | au! CmdlineEnter : let g:entered = expand('<afile>') |
| 828 | au! CmdlineLeave : let g:left = expand('<afile>') |
| 829 | let g:entered = 0 |
| 830 | let g:left = 0 |
| 831 | call feedkeys(":echo 'hello'\<CR>", 'xt') |
| 832 | call assert_equal(':', g:entered) |
| 833 | call assert_equal(':', g:left) |
| 834 | au! CmdlineEnter |
| 835 | au! CmdlineLeave |
| 836 | |
| 837 | au! CmdlineEnter / let g:entered = expand('<afile>') |
| 838 | au! CmdlineLeave / let g:left = expand('<afile>') |
| 839 | let g:entered = 0 |
| 840 | let g:left = 0 |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 841 | new |
| 842 | call setline(1, 'hello') |
| 843 | call feedkeys("/hello\<CR>", 'xt') |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 844 | call assert_equal('/', g:entered) |
| 845 | call assert_equal('/', g:left) |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 846 | bwipe! |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 847 | au! CmdlineEnter |
| 848 | au! CmdlineLeave |
| 849 | endfunc |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 850 | |
| 851 | " Test for BufWritePre autocommand that deletes or unloads the buffer. |
| 852 | func Test_BufWritePre() |
| 853 | %bwipe |
| 854 | au BufWritePre Xxx1 bunload |
| 855 | au BufWritePre Xxx2 bwipe |
| 856 | |
| 857 | call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1') |
| 858 | call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2') |
| 859 | |
| 860 | edit Xtest |
| 861 | e! Xxx2 |
| 862 | bdel Xtest |
| 863 | e Xxx1 |
| 864 | " write it, will unload it and give an error msg |
| 865 | call assert_fails('w', 'E203') |
| 866 | call assert_equal('Xxx2', bufname('%')) |
| 867 | edit Xtest |
| 868 | e! Xxx2 |
| 869 | bwipe Xtest |
| 870 | " write it, will delete the buffer and give an error msg |
| 871 | call assert_fails('w', 'E203') |
| 872 | call assert_equal('Xxx1', bufname('%')) |
| 873 | au! BufWritePre |
| 874 | call delete('Xxx1') |
| 875 | call delete('Xxx2') |
| 876 | endfunc |
| 877 | |
| 878 | " Test for BufUnload autocommand that unloads all the other buffers |
| 879 | func Test_bufunload_all() |
| 880 | call writefile(['Test file Xxx1'], 'Xxx1')" |
| 881 | call writefile(['Test file Xxx2'], 'Xxx2')" |
| 882 | |
| 883 | let content = [ |
| 884 | \ "func UnloadAllBufs()", |
| 885 | \ " let i = 1", |
| 886 | \ " while i <= bufnr('$')", |
| 887 | \ " if i != bufnr('%') && bufloaded(i)", |
| 888 | \ " exe i . 'bunload'", |
| 889 | \ " endif", |
| 890 | \ " let i += 1", |
| 891 | \ " endwhile", |
| 892 | \ "endfunc", |
| 893 | \ "au BufUnload * call UnloadAllBufs()", |
| 894 | \ "au VimLeave * call writefile(['Test Finished'], 'Xout')", |
| 895 | \ "edit Xxx1", |
| 896 | \ "split Xxx2", |
| 897 | \ "q"] |
| 898 | call writefile(content, 'Xtest') |
| 899 | |
| 900 | call delete('Xout') |
| 901 | call system(v:progpath. ' --clean -N --not-a-term -S Xtest') |
| 902 | call assert_true(filereadable('Xout')) |
| 903 | |
| 904 | call delete('Xxx1') |
| 905 | call delete('Xxx2') |
| 906 | call delete('Xtest') |
| 907 | call delete('Xout') |
| 908 | endfunc |
| 909 | |
| 910 | " Some tests for buffer-local autocommands |
| 911 | func Test_buflocal_autocmd() |
| 912 | let g:bname = '' |
| 913 | edit xx |
| 914 | au BufLeave <buffer> let g:bname = expand("%") |
| 915 | " here, autocommand for xx should trigger. |
| 916 | " but autocommand shall not apply to buffer named <buffer>. |
| 917 | edit somefile |
| 918 | call assert_equal('xx', g:bname) |
| 919 | let g:bname = '' |
| 920 | " here, autocommand shall be auto-deleted |
| 921 | bwipe xx |
| 922 | " autocmd should not trigger |
| 923 | edit xx |
| 924 | call assert_equal('', g:bname) |
| 925 | " autocmd should not trigger |
| 926 | edit somefile |
| 927 | call assert_equal('', g:bname) |
| 928 | enew |
| 929 | unlet g:bname |
| 930 | endfunc |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 931 | |
| 932 | " Test for "*Cmd" autocommands |
| 933 | func Test_Cmd_Autocmds() |
| 934 | call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx') |
| 935 | |
| 936 | enew! |
| 937 | au BufReadCmd XtestA 0r Xxx|$del |
| 938 | edit XtestA " will read text of Xxd instead |
| 939 | call assert_equal('start of Xxx', getline(1)) |
| 940 | |
| 941 | au BufWriteCmd XtestA call append(line("$"), "write") |
| 942 | write " will append a line to the file |
| 943 | call assert_equal('write', getline('$')) |
| 944 | call assert_fails('read XtestA', 'E484') " should not read anything |
| 945 | call assert_equal('write', getline(4)) |
| 946 | |
| 947 | " now we have: |
| 948 | " 1 start of Xxx |
| 949 | " 2 abc2 |
| 950 | " 3 end of Xxx |
| 951 | " 4 write |
| 952 | |
| 953 | au FileReadCmd XtestB '[r Xxx |
| 954 | 2r XtestB " will read Xxx below line 2 instead |
| 955 | call assert_equal('start of Xxx', getline(3)) |
| 956 | |
| 957 | " now we have: |
| 958 | " 1 start of Xxx |
| 959 | " 2 abc2 |
| 960 | " 3 start of Xxx |
| 961 | " 4 abc2 |
| 962 | " 5 end of Xxx |
| 963 | " 6 end of Xxx |
| 964 | " 7 write |
| 965 | |
| 966 | au FileWriteCmd XtestC '[,']copy $ |
| 967 | normal 4GA1 |
| 968 | 4,5w XtestC " will copy lines 4 and 5 to the end |
| 969 | call assert_equal("\tabc21", getline(8)) |
| 970 | call assert_fails('r XtestC', 'E484') " should not read anything |
| 971 | call assert_equal("end of Xxx", getline(9)) |
| 972 | |
| 973 | " now we have: |
| 974 | " 1 start of Xxx |
| 975 | " 2 abc2 |
| 976 | " 3 start of Xxx |
| 977 | " 4 abc21 |
| 978 | " 5 end of Xxx |
| 979 | " 6 end of Xxx |
| 980 | " 7 write |
| 981 | " 8 abc21 |
| 982 | " 9 end of Xxx |
| 983 | |
| 984 | let g:lines = [] |
| 985 | au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']"))) |
| 986 | w >>XtestD " will add lines to 'lines' |
| 987 | call assert_equal(9, len(g:lines)) |
| 988 | call assert_fails('$r XtestD', 'E484') " should not read anything |
| 989 | call assert_equal(9, line('$')) |
| 990 | call assert_equal('end of Xxx', getline('$')) |
| 991 | |
| 992 | au BufReadCmd XtestE 0r Xxx|$del |
| 993 | sp XtestE " split window with test.out |
| 994 | call assert_equal('end of Xxx', getline(3)) |
| 995 | |
| 996 | let g:lines = [] |
| 997 | exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>" |
| 998 | au BufWriteCmd XtestE call extend(g:lines, getline(0, '$')) |
| 999 | wall " will write other window to 'lines' |
| 1000 | call assert_equal(4, len(g:lines), g:lines) |
| 1001 | call assert_equal('asdf', g:lines[2]) |
| 1002 | |
| 1003 | au! BufReadCmd |
| 1004 | au! BufWriteCmd |
| 1005 | au! FileReadCmd |
| 1006 | au! FileWriteCmd |
| 1007 | au! FileAppendCmd |
| 1008 | %bwipe! |
| 1009 | call delete('Xxx') |
| 1010 | enew! |
| 1011 | endfunc |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1012 | |
| 1013 | func SetChangeMarks(start, end) |
| 1014 | exe a:start. 'mark [' |
| 1015 | exe a:end. 'mark ]' |
| 1016 | endfunc |
| 1017 | |
| 1018 | " Verify the effects of autocmds on '[ and '] |
| 1019 | func Test_change_mark_in_autocmds() |
| 1020 | edit! Xtest |
| 1021 | call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn') |
| 1022 | |
| 1023 | call SetChangeMarks(2, 3) |
| 1024 | write |
| 1025 | call assert_equal([1, 4], [line("'["), line("']")]) |
| 1026 | |
| 1027 | call SetChangeMarks(2, 3) |
| 1028 | au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1029 | write |
| 1030 | au! BufWritePre |
| 1031 | |
| 1032 | if executable('cat') |
| 1033 | write XtestFilter |
| 1034 | write >> XtestFilter |
| 1035 | |
| 1036 | call SetChangeMarks(2, 3) |
| 1037 | " Marks are set to the entire range of the write |
| 1038 | au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1039 | " '[ is adjusted to just before the line that will receive the filtered |
| 1040 | " data |
| 1041 | au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")]) |
| 1042 | " The filtered data is read into the buffer, and the source lines are |
| 1043 | " still present, so the range is after the source lines |
| 1044 | au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")]) |
| 1045 | %!cat XtestFilter |
| 1046 | " After the filtered data is read, the original lines are deleted |
| 1047 | call assert_equal([1, 8], [line("'["), line("']")]) |
| 1048 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1049 | undo |
| 1050 | |
| 1051 | call SetChangeMarks(1, 4) |
| 1052 | au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1053 | au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")]) |
| 1054 | au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1055 | 2,3!cat XtestFilter |
| 1056 | call assert_equal([2, 9], [line("'["), line("']")]) |
| 1057 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1058 | undo |
| 1059 | |
| 1060 | call delete('XtestFilter') |
| 1061 | endif |
| 1062 | |
| 1063 | call SetChangeMarks(1, 4) |
| 1064 | au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1065 | 2,3write Xtest2 |
| 1066 | au! FileWritePre |
| 1067 | |
| 1068 | call SetChangeMarks(2, 3) |
| 1069 | au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1070 | write >> Xtest2 |
| 1071 | au! FileAppendPre |
| 1072 | |
| 1073 | call SetChangeMarks(1, 4) |
| 1074 | au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1075 | 2,3write >> Xtest2 |
| 1076 | au! FileAppendPre |
| 1077 | |
| 1078 | call SetChangeMarks(1, 1) |
| 1079 | au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")]) |
| 1080 | au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1081 | 3read Xtest2 |
| 1082 | au! FileReadPre,FileReadPost |
| 1083 | undo |
| 1084 | |
| 1085 | call SetChangeMarks(4, 4) |
| 1086 | " When the line is 0, it's adjusted to 1 |
| 1087 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1088 | au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")]) |
| 1089 | 0read Xtest2 |
| 1090 | au! FileReadPre,FileReadPost |
| 1091 | undo |
| 1092 | |
| 1093 | call SetChangeMarks(4, 4) |
| 1094 | " When the line is 0, it's adjusted to 1 |
| 1095 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1096 | au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")]) |
| 1097 | 1read Xtest2 |
| 1098 | au! FileReadPre,FileReadPost |
| 1099 | undo |
| 1100 | |
| 1101 | bwipe! |
| 1102 | call delete('Xtest') |
| 1103 | call delete('Xtest2') |
| 1104 | endfunc |
| 1105 | |
| 1106 | func Test_Filter_noshelltemp() |
| 1107 | if !executable('cat') |
| 1108 | return |
| 1109 | endif |
| 1110 | |
| 1111 | enew! |
| 1112 | call setline(1, ['a', 'b', 'c', 'd']) |
| 1113 | |
| 1114 | let shelltemp = &shelltemp |
| 1115 | set shelltemp |
| 1116 | |
| 1117 | let g:filter_au = 0 |
| 1118 | au FilterWritePre * let g:filter_au += 1 |
| 1119 | au FilterReadPre * let g:filter_au += 1 |
| 1120 | au FilterReadPost * let g:filter_au += 1 |
| 1121 | %!cat |
| 1122 | call assert_equal(3, g:filter_au) |
| 1123 | |
| 1124 | if has('filterpipe') |
| 1125 | set noshelltemp |
| 1126 | |
| 1127 | let g:filter_au = 0 |
| 1128 | au FilterWritePre * let g:filter_au += 1 |
| 1129 | au FilterReadPre * let g:filter_au += 1 |
| 1130 | au FilterReadPost * let g:filter_au += 1 |
| 1131 | %!cat |
| 1132 | call assert_equal(0, g:filter_au) |
| 1133 | endif |
| 1134 | |
| 1135 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1136 | let &shelltemp = shelltemp |
| 1137 | bwipe! |
| 1138 | endfunc |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1139 | |
| 1140 | func Test_TextYankPost() |
| 1141 | enew! |
| 1142 | call setline(1, ['foo']) |
| 1143 | |
| 1144 | let g:event = [] |
| 1145 | au TextYankPost * let g:event = copy(v:event) |
| 1146 | |
| 1147 | call assert_equal({}, v:event) |
| 1148 | call assert_fails('let v:event = {}', 'E46:') |
| 1149 | call assert_fails('let v:event.mykey = 0', 'E742:') |
| 1150 | |
| 1151 | norm "ayiw |
| 1152 | call assert_equal( |
| 1153 | \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'}, |
| 1154 | \g:event) |
| 1155 | norm y_ |
| 1156 | call assert_equal( |
| 1157 | \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'}, |
| 1158 | \g:event) |
| 1159 | call feedkeys("\<C-V>y", 'x') |
| 1160 | call assert_equal( |
| 1161 | \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"}, |
| 1162 | \g:event) |
| 1163 | norm "xciwbar |
| 1164 | call assert_equal( |
| 1165 | \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'}, |
| 1166 | \g:event) |
| 1167 | norm "bdiw |
| 1168 | call assert_equal( |
| 1169 | \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'}, |
| 1170 | \g:event) |
| 1171 | |
| 1172 | call assert_equal({}, v:event) |
| 1173 | |
| 1174 | au! TextYankPost |
| 1175 | unlet g:event |
| 1176 | bwipe! |
| 1177 | endfunc |
Bram Moolenaar | 9bca805 | 2017-12-18 12:37:55 +0100 | [diff] [blame] | 1178 | |
| 1179 | func Test_nocatch_wipe_all_buffers() |
| 1180 | " Real nasty autocommand: wipe all buffers on any event. |
| 1181 | au * * bwipe * |
| 1182 | call assert_fails('next x', 'E93') |
| 1183 | bwipe |
| 1184 | au! |
| 1185 | endfunc |
Bram Moolenaar | 4fb921e | 2017-12-18 15:33:00 +0100 | [diff] [blame] | 1186 | |
| 1187 | func Test_nocatch_wipe_dummy_buffer() |
| 1188 | " Nasty autocommand: wipe buffer on any event. |
| 1189 | au * x bwipe |
| 1190 | call assert_fails('lv½ /x', 'E480') |
| 1191 | au! |
| 1192 | endfunc |