Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 1 | " Tests for autocommands |
| 2 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 3 | source shared.vim |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 4 | source check.vim |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 5 | source term_util.vim |
LemonBoy | 0937182 | 2022-04-08 15:18:45 +0100 | [diff] [blame] | 6 | source screendump.vim |
Bram Moolenaar | 6f2465d | 2022-03-22 18:13:01 +0000 | [diff] [blame] | 7 | import './vim9.vim' as v9 |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 8 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 9 | func s:cleanup_buffers() abort |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 10 | for bnr in range(1, bufnr('$')) |
| 11 | if bufloaded(bnr) && bufnr('%') != bnr |
| 12 | execute 'bd! ' . bnr |
| 13 | endif |
| 14 | endfor |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 15 | endfunc |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 16 | |
Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 17 | func Test_vim_did_enter() |
| 18 | call assert_false(v:vim_did_enter) |
| 19 | |
| 20 | " This script will never reach the main loop, can't check if v:vim_did_enter |
| 21 | " becomes one. |
| 22 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 23 | |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 24 | " Test for the CursorHold autocmd |
| 25 | func Test_CursorHold_autocmd() |
| 26 | CheckRunVimInTerminal |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 27 | call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D') |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 28 | let before =<< trim END |
| 29 | set updatetime=10 |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 30 | au CursorHold * call writefile([line('.')], 'XCHoutput', 'a') |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 31 | END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 32 | call writefile(before, 'XCHinit', 'D') |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 33 | let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {}) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 34 | call term_sendkeys(buf, "G") |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 35 | call term_wait(buf, 50) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 36 | call term_sendkeys(buf, "gg") |
| 37 | call term_wait(buf) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 38 | call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 39 | call term_sendkeys(buf, "j") |
| 40 | call term_wait(buf) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 41 | call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 42 | call term_sendkeys(buf, "j") |
| 43 | call term_wait(buf) |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 44 | call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 45 | call StopVimInTerminal(buf) |
| 46 | |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 47 | call delete('XCHoutput') |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 48 | endfunc |
| 49 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 50 | if has('timers') |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 51 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 52 | func ExitInsertMode(id) |
| 53 | call feedkeys("\<Esc>") |
| 54 | endfunc |
| 55 | |
| 56 | func Test_cursorhold_insert() |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 57 | " Need to move the cursor. |
| 58 | call feedkeys("ggG", "xt") |
| 59 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 60 | let g:triggered = 0 |
| 61 | au CursorHoldI * let g:triggered += 1 |
| 62 | set updatetime=20 |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 63 | call timer_start(200, 'ExitInsertMode') |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 64 | call feedkeys('a', 'x!') |
| 65 | call assert_equal(1, g:triggered) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 66 | unlet g:triggered |
| 67 | au! CursorHoldI |
| 68 | set updatetime& |
| 69 | endfunc |
| 70 | |
| 71 | func Test_cursorhold_insert_with_timer_interrupt() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 72 | CheckFeature job |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 73 | " Need to move the cursor. |
| 74 | call feedkeys("ggG", "xt") |
| 75 | |
| 76 | " Confirm the timer invoked in exit_cb of the job doesn't disturb |
| 77 | " CursorHoldI event. |
| 78 | let g:triggered = 0 |
| 79 | au CursorHoldI * let g:triggered += 1 |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 80 | set updatetime=100 |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 81 | call job_start(has('win32') ? 'cmd /c echo:' : 'echo', |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 82 | \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}}) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 83 | call feedkeys('a', 'x!') |
| 84 | call assert_equal(1, g:triggered) |
| 85 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 86 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 87 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 88 | endfunc |
| 89 | |
| 90 | func Test_cursorhold_insert_ctrl_x() |
| 91 | let g:triggered = 0 |
| 92 | au CursorHoldI * let g:triggered += 1 |
| 93 | set updatetime=20 |
| 94 | call timer_start(100, 'ExitInsertMode') |
| 95 | " CursorHoldI does not trigger after CTRL-X |
| 96 | call feedkeys("a\<C-X>", 'x!') |
| 97 | call assert_equal(0, g:triggered) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 98 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 99 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 100 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 101 | endfunc |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 102 | |
Bram Moolenaar | 5a9357d | 2021-10-03 16:22:05 +0100 | [diff] [blame] | 103 | func Test_cursorhold_insert_ctrl_g_U() |
| 104 | au CursorHoldI * : |
| 105 | set updatetime=20 |
| 106 | new |
| 107 | call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') }) |
| 108 | call feedkeys("i()\<C-g>U", 'tx!') |
| 109 | sleep 200m |
| 110 | call assert_equal('(foo)', getline(1)) |
| 111 | undo |
| 112 | call assert_equal('', getline(1)) |
| 113 | |
| 114 | bwipe! |
| 115 | au! CursorHoldI |
| 116 | set updatetime& |
| 117 | endfunc |
| 118 | |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 119 | func Test_OptionSet_modeline() |
| 120 | call test_override('starting', 1) |
| 121 | au! OptionSet |
| 122 | augroup set_tabstop |
| 123 | au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")}) |
| 124 | augroup END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 125 | call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D') |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 126 | set modeline |
| 127 | let v:errmsg = '' |
| 128 | call assert_fails('split XoptionsetModeline', 'E12:') |
| 129 | call assert_equal(7, &ts) |
| 130 | call assert_equal('', v:errmsg) |
| 131 | |
| 132 | augroup set_tabstop |
| 133 | au! |
| 134 | augroup END |
| 135 | bwipe! |
| 136 | set ts& |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 137 | call test_override('starting', 0) |
| 138 | endfunc |
| 139 | |
| 140 | endif "has('timers') |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 142 | func Test_bufunload() |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 143 | augroup test_bufunload_group |
| 144 | autocmd! |
| 145 | autocmd BufUnload * call add(s:li, "bufunload") |
| 146 | autocmd BufDelete * call add(s:li, "bufdelete") |
| 147 | autocmd BufWipeout * call add(s:li, "bufwipeout") |
| 148 | augroup END |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 149 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 150 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 151 | new |
| 152 | setlocal bufhidden= |
| 153 | bunload |
| 154 | call assert_equal(["bufunload", "bufdelete"], s:li) |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 155 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 156 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 157 | new |
| 158 | setlocal bufhidden=delete |
| 159 | bunload |
| 160 | call assert_equal(["bufunload", "bufdelete"], s:li) |
| 161 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 162 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 163 | new |
| 164 | setlocal bufhidden=unload |
| 165 | bwipeout |
| 166 | call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li) |
| 167 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 168 | au! test_bufunload_group |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 169 | augroup! test_bufunload_group |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 170 | endfunc |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 171 | |
| 172 | " SEGV occurs in older versions. (At least 7.4.2005 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 173 | func Test_autocmd_bufunload_with_tabnext() |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 174 | tabedit |
| 175 | tabfirst |
| 176 | |
| 177 | augroup test_autocmd_bufunload_with_tabnext_group |
| 178 | autocmd! |
| 179 | autocmd BufUnload <buffer> tabnext |
| 180 | augroup END |
| 181 | |
| 182 | quit |
| 183 | call assert_equal(2, tabpagenr('$')) |
| 184 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 185 | autocmd! test_autocmd_bufunload_with_tabnext_group |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 186 | augroup! test_autocmd_bufunload_with_tabnext_group |
| 187 | tablast |
| 188 | quit |
| 189 | endfunc |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 190 | |
Bram Moolenaar | 5ed58c7 | 2021-01-28 14:24:55 +0100 | [diff] [blame] | 191 | func Test_argdelete_in_next() |
| 192 | au BufNew,BufEnter,BufLeave,BufWinEnter * argdel |
| 193 | call assert_fails('next a b', 'E1156:') |
| 194 | au! BufNew,BufEnter,BufLeave,BufWinEnter * |
| 195 | endfunc |
| 196 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 197 | func Test_autocmd_bufwinleave_with_tabfirst() |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 198 | tabedit |
| 199 | augroup sample |
| 200 | autocmd! |
| 201 | autocmd BufWinLeave <buffer> tabfirst |
| 202 | augroup END |
| 203 | call setline(1, ['a', 'b', 'c']) |
| 204 | edit! a.txt |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 205 | tabclose |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 206 | endfunc |
| 207 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 208 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 209 | func Test_autocmd_bufunload_avoiding_SEGV_01() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 210 | split aa.txt |
| 211 | let lastbuf = bufnr('$') |
| 212 | |
| 213 | augroup test_autocmd_bufunload |
| 214 | autocmd! |
| 215 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 216 | augroup END |
| 217 | |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 218 | call assert_fails('edit bb.txt', 'E937:') |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 219 | |
| 220 | autocmd! test_autocmd_bufunload |
| 221 | augroup! test_autocmd_bufunload |
| 222 | bwipe! aa.txt |
| 223 | bwipe! bb.txt |
| 224 | endfunc |
| 225 | |
| 226 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 227 | func Test_autocmd_bufunload_avoiding_SEGV_02() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 228 | setlocal buftype=nowrite |
| 229 | let lastbuf = bufnr('$') |
| 230 | |
| 231 | augroup test_autocmd_bufunload |
| 232 | autocmd! |
| 233 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 234 | augroup END |
| 235 | |
| 236 | normal! i1 |
| 237 | call assert_fails('edit a.txt', 'E517:') |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 238 | |
| 239 | autocmd! test_autocmd_bufunload |
| 240 | augroup! test_autocmd_bufunload |
| 241 | bwipe! a.txt |
| 242 | endfunc |
| 243 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 244 | func Test_autocmd_dummy_wipeout() |
| 245 | " prepare files |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 246 | call writefile([''], 'Xdummywipetest1.txt', 'D') |
| 247 | call writefile([''], 'Xdummywipetest2.txt', 'D') |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 248 | augroup test_bufunload_group |
| 249 | autocmd! |
| 250 | autocmd BufUnload * call add(s:li, "bufunload") |
| 251 | autocmd BufDelete * call add(s:li, "bufdelete") |
| 252 | autocmd BufWipeout * call add(s:li, "bufwipeout") |
| 253 | augroup END |
| 254 | |
| 255 | let s:li = [] |
| 256 | split Xdummywipetest1.txt |
| 257 | silent! vimgrep /notmatched/ Xdummywipetest* |
| 258 | call assert_equal(["bufunload", "bufwipeout"], s:li) |
| 259 | |
| 260 | bwipeout |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 261 | au! test_bufunload_group |
| 262 | augroup! test_bufunload_group |
| 263 | endfunc |
| 264 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 265 | func Test_win_tab_autocmd() |
| 266 | let g:record = [] |
| 267 | |
| 268 | augroup testing |
| 269 | au WinNew * call add(g:record, 'WinNew') |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 270 | au WinClosed * call add(g:record, 'WinClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 271 | au WinEnter * call add(g:record, 'WinEnter') |
| 272 | au WinLeave * call add(g:record, 'WinLeave') |
| 273 | au TabNew * call add(g:record, 'TabNew') |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 274 | au TabClosed * call add(g:record, 'TabClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 275 | au TabEnter * call add(g:record, 'TabEnter') |
| 276 | au TabLeave * call add(g:record, 'TabLeave') |
| 277 | augroup END |
| 278 | |
| 279 | split |
| 280 | tabnew |
| 281 | close |
| 282 | close |
| 283 | |
| 284 | call assert_equal([ |
| 285 | \ 'WinLeave', 'WinNew', 'WinEnter', |
| 286 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 287 | \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter', |
| 288 | \ 'WinLeave', 'WinClosed', 'WinEnter' |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 289 | \ ], g:record) |
| 290 | |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 291 | let g:record = [] |
| 292 | tabnew somefile |
| 293 | tabnext |
| 294 | bwipe somefile |
| 295 | |
| 296 | call assert_equal([ |
| 297 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
| 298 | \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 299 | \ 'WinClosed', 'TabClosed' |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 300 | \ ], g:record) |
| 301 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 302 | augroup testing |
| 303 | au! |
| 304 | augroup END |
| 305 | unlet g:record |
| 306 | endfunc |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 307 | |
LemonBoy | 0937182 | 2022-04-08 15:18:45 +0100 | [diff] [blame] | 308 | func Test_WinScrolled() |
| 309 | CheckRunVimInTerminal |
| 310 | |
| 311 | let lines =<< trim END |
zeertzjq | d58862d | 2022-04-12 11:32:48 +0100 | [diff] [blame] | 312 | set nowrap scrolloff=0 |
| 313 | for ii in range(1, 18) |
| 314 | call setline(ii, repeat(nr2char(96 + ii), ii * 2)) |
| 315 | endfor |
| 316 | let win_id = win_getid() |
| 317 | let g:matched = v:false |
| 318 | execute 'au WinScrolled' win_id 'let g:matched = v:true' |
| 319 | let g:scrolled = 0 |
| 320 | au WinScrolled * let g:scrolled += 1 |
| 321 | au WinScrolled * let g:amatch = str2nr(expand('<amatch>')) |
| 322 | au WinScrolled * let g:afile = str2nr(expand('<afile>')) |
LemonBoy | 0937182 | 2022-04-08 15:18:45 +0100 | [diff] [blame] | 323 | END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 324 | call writefile(lines, 'Xtest_winscrolled', 'D') |
LemonBoy | 0937182 | 2022-04-08 15:18:45 +0100 | [diff] [blame] | 325 | let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6}) |
| 326 | |
| 327 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 328 | call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000) |
| 329 | |
| 330 | " Scroll left/right in Normal mode. |
| 331 | call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>") |
| 332 | call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000) |
| 333 | |
| 334 | " Scroll up/down in Normal mode. |
| 335 | call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>") |
| 336 | call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000) |
| 337 | |
| 338 | " Scroll up/down in Insert mode. |
| 339 | call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>") |
| 340 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 341 | call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000) |
| 342 | |
| 343 | " Scroll the window horizontally to focus the last letter of the third line |
| 344 | " containing only six characters. Moving to the previous and shorter lines |
| 345 | " should trigger another autocommand as Vim has to make them visible. |
| 346 | call term_sendkeys(buf, "5zl2k") |
| 347 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 348 | call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000) |
| 349 | |
| 350 | " Ensure the command was triggered for the specified window ID. |
| 351 | call term_sendkeys(buf, ":echo g:matched\<CR>") |
| 352 | call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000) |
| 353 | |
| 354 | " Ensure the expansion of <amatch> and <afile> matches the window ID. |
| 355 | call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>") |
| 356 | call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000) |
| 357 | |
| 358 | call StopVimInTerminal(buf) |
LemonBoy | 0937182 | 2022-04-08 15:18:45 +0100 | [diff] [blame] | 359 | endfunc |
| 360 | |
LemonBoy | 66e13ae | 2022-04-21 22:52:11 +0100 | [diff] [blame] | 361 | func Test_WinScrolled_mouse() |
| 362 | CheckRunVimInTerminal |
| 363 | |
| 364 | let lines =<< trim END |
| 365 | set nowrap scrolloff=0 |
| 366 | set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard= |
| 367 | call setline(1, ['foo']->repeat(32)) |
| 368 | split |
| 369 | let g:scrolled = 0 |
| 370 | au WinScrolled * let g:scrolled += 1 |
| 371 | END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 372 | call writefile(lines, 'Xtest_winscrolled_mouse', 'D') |
LemonBoy | 66e13ae | 2022-04-21 22:52:11 +0100 | [diff] [blame] | 373 | let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10}) |
| 374 | |
| 375 | " With the upper split focused, send a scroll-down event to the unfocused one. |
| 376 | call test_setmouse(7, 1) |
| 377 | call term_sendkeys(buf, "\<ScrollWheelDown>") |
| 378 | call TermWait(buf) |
| 379 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 380 | call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000) |
| 381 | |
| 382 | " Again, but this time while we're in insert mode. |
| 383 | call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>") |
| 384 | call TermWait(buf) |
| 385 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 386 | call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000) |
| 387 | |
| 388 | call StopVimInTerminal(buf) |
LemonBoy | 66e13ae | 2022-04-21 22:52:11 +0100 | [diff] [blame] | 389 | endfunc |
| 390 | |
zeertzjq | d58862d | 2022-04-12 11:32:48 +0100 | [diff] [blame] | 391 | func Test_WinScrolled_close_curwin() |
| 392 | CheckRunVimInTerminal |
| 393 | |
| 394 | let lines =<< trim END |
| 395 | set nowrap scrolloff=0 |
| 396 | call setline(1, ['aaa', 'bbb']) |
| 397 | vsplit |
| 398 | au WinScrolled * close |
| 399 | au VimLeave * call writefile(['123456'], 'Xtestout') |
| 400 | END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 401 | call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D') |
zeertzjq | d58862d | 2022-04-12 11:32:48 +0100 | [diff] [blame] | 402 | let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6}) |
| 403 | |
| 404 | " This was using freed memory |
| 405 | call term_sendkeys(buf, "\<C-E>") |
| 406 | call TermWait(buf) |
| 407 | call StopVimInTerminal(buf) |
| 408 | |
| 409 | call assert_equal(['123456'], readfile('Xtestout')) |
| 410 | |
zeertzjq | d58862d | 2022-04-12 11:32:48 +0100 | [diff] [blame] | 411 | call delete('Xtestout') |
| 412 | endfunc |
| 413 | |
zeertzjq | 670ab03 | 2022-08-28 19:16:15 +0100 | [diff] [blame] | 414 | func Test_WinScrolled_long_wrapped() |
| 415 | CheckRunVimInTerminal |
| 416 | |
| 417 | let lines =<< trim END |
| 418 | set scrolloff=0 |
| 419 | let height = winheight(0) |
| 420 | let width = winwidth(0) |
| 421 | let g:scrolled = 0 |
| 422 | au WinScrolled * let g:scrolled += 1 |
| 423 | call setline(1, repeat('foo', height * width)) |
| 424 | call cursor(1, height * width) |
| 425 | END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 426 | call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D') |
zeertzjq | 670ab03 | 2022-08-28 19:16:15 +0100 | [diff] [blame] | 427 | let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6}) |
| 428 | |
| 429 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 430 | call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000) |
| 431 | |
| 432 | call term_sendkeys(buf, 'gj') |
| 433 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 434 | call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000) |
| 435 | |
| 436 | call term_sendkeys(buf, '0') |
| 437 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 438 | call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000) |
| 439 | |
| 440 | call term_sendkeys(buf, '$') |
| 441 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 442 | call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000) |
zeertzjq | 670ab03 | 2022-08-28 19:16:15 +0100 | [diff] [blame] | 443 | endfunc |
| 444 | |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 445 | func Test_WinClosed() |
| 446 | " Test that the pattern is matched against the closed window's ID, and both |
| 447 | " <amatch> and <afile> are set to it. |
| 448 | new |
| 449 | let winid = win_getid() |
| 450 | let g:matched = v:false |
| 451 | augroup test-WinClosed |
| 452 | autocmd! |
| 453 | execute 'autocmd WinClosed' winid 'let g:matched = v:true' |
| 454 | autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>')) |
| 455 | autocmd WinClosed * let g:afile = str2nr(expand('<afile>')) |
| 456 | augroup END |
| 457 | close |
| 458 | call assert_true(g:matched) |
| 459 | call assert_equal(winid, g:amatch) |
| 460 | call assert_equal(winid, g:afile) |
| 461 | |
| 462 | " Test that WinClosed is non-recursive. |
| 463 | new |
| 464 | new |
| 465 | call assert_equal(3, winnr('$')) |
| 466 | let g:triggered = 0 |
| 467 | augroup test-WinClosed |
| 468 | autocmd! |
| 469 | autocmd WinClosed * let g:triggered += 1 |
| 470 | autocmd WinClosed * 2 wincmd c |
| 471 | augroup END |
| 472 | close |
| 473 | call assert_equal(1, winnr('$')) |
| 474 | call assert_equal(1, g:triggered) |
| 475 | |
| 476 | autocmd! test-WinClosed |
| 477 | augroup! test-WinClosed |
| 478 | unlet g:matched |
| 479 | unlet g:amatch |
| 480 | unlet g:afile |
| 481 | unlet g:triggered |
| 482 | endfunc |
| 483 | |
Bram Moolenaar | c947b9a | 2022-04-06 17:59:21 +0100 | [diff] [blame] | 484 | func Test_WinClosed_throws() |
| 485 | vnew |
| 486 | let bnr = bufnr() |
| 487 | call assert_equal(1, bufloaded(bnr)) |
| 488 | augroup test-WinClosed |
| 489 | autocmd WinClosed * throw 'foo' |
| 490 | augroup END |
| 491 | try |
| 492 | close |
| 493 | catch /.*/ |
| 494 | endtry |
| 495 | call assert_equal(0, bufloaded(bnr)) |
| 496 | |
| 497 | autocmd! test-WinClosed |
| 498 | augroup! test-WinClosed |
| 499 | endfunc |
| 500 | |
zeertzjq | 6a06940 | 2022-04-07 14:08:29 +0100 | [diff] [blame] | 501 | func Test_WinClosed_throws_with_tabs() |
| 502 | tabnew |
| 503 | let bnr = bufnr() |
| 504 | call assert_equal(1, bufloaded(bnr)) |
| 505 | augroup test-WinClosed |
| 506 | autocmd WinClosed * throw 'foo' |
| 507 | augroup END |
| 508 | try |
| 509 | close |
| 510 | catch /.*/ |
| 511 | endtry |
| 512 | call assert_equal(0, bufloaded(bnr)) |
| 513 | |
| 514 | autocmd! test-WinClosed |
| 515 | augroup! test-WinClosed |
| 516 | endfunc |
| 517 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 518 | func s:AddAnAutocmd() |
| 519 | augroup vimBarTest |
| 520 | au BufReadCmd * echo 'hello' |
| 521 | augroup END |
| 522 | call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) |
| 523 | endfunc |
| 524 | |
| 525 | func Test_early_bar() |
| 526 | " test that a bar is recognized before the {event} |
| 527 | call s:AddAnAutocmd() |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 528 | augroup vimBarTest | au! | let done = 77 | augroup END |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 529 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 530 | call assert_equal(77, done) |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 531 | |
| 532 | call s:AddAnAutocmd() |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 533 | augroup vimBarTest| au!| let done = 88 | augroup END |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 534 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 535 | call assert_equal(88, done) |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 536 | |
| 537 | " test that a bar is recognized after the {event} |
| 538 | call s:AddAnAutocmd() |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 539 | augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 540 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 541 | call assert_equal(99, done) |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 542 | |
| 543 | " test that a bar is recognized after the {group} |
| 544 | call s:AddAnAutocmd() |
| 545 | au! vimBarTest|echo 'hello' |
| 546 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 547 | endfunc |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 548 | |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 549 | func RemoveGroup() |
| 550 | autocmd! StartOK |
| 551 | augroup! StartOK |
| 552 | endfunc |
| 553 | |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 554 | func Test_augroup_warning() |
| 555 | augroup TheWarning |
| 556 | au VimEnter * echo 'entering' |
| 557 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 558 | call assert_match("TheWarning.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 559 | redir => res |
| 560 | augroup! TheWarning |
| 561 | redir END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 562 | call assert_match("W19:", res) |
| 563 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 564 | |
| 565 | " check "Another" does not take the pace of the deleted entry |
| 566 | augroup Another |
| 567 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 568 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 569 | augroup! Another |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 570 | |
| 571 | " no warning for postpone aucmd delete |
| 572 | augroup StartOK |
| 573 | au VimEnter * call RemoveGroup() |
| 574 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 575 | call assert_match("StartOK.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 576 | redir => res |
| 577 | doautocmd VimEnter |
| 578 | redir END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 579 | call assert_notmatch("W19:", res) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 580 | au! VimEnter |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 581 | |
| 582 | call assert_fails('augroup!', 'E471:') |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 583 | endfunc |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 584 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 585 | func Test_BufReadCmdHelp() |
| 586 | " This used to cause access to free memory |
| 587 | au BufReadCmd * e +h |
| 588 | help |
| 589 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 590 | au! BufReadCmd |
| 591 | endfunc |
| 592 | |
| 593 | func Test_BufReadCmdHelpJump() |
| 594 | " This used to cause access to free memory |
| 595 | au BufReadCmd * e +h{ |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 596 | " } to fix highlighting |
| 597 | call assert_fails('help', 'E434:') |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 598 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 599 | au! BufReadCmd |
| 600 | endfunc |
| 601 | |
zeertzjq | 93f72cc | 2022-08-26 15:34:52 +0100 | [diff] [blame] | 602 | " BufReadCmd is triggered for a "nofile" buffer. Check all values. |
Bram Moolenaar | b1d2c81 | 2022-08-26 11:55:01 +0100 | [diff] [blame] | 603 | func Test_BufReadCmdNofile() |
zeertzjq | 93f72cc | 2022-08-26 15:34:52 +0100 | [diff] [blame] | 604 | for val in ['nofile', |
| 605 | \ 'nowrite', |
| 606 | \ 'acwrite', |
| 607 | \ 'quickfix', |
| 608 | \ 'help', |
| 609 | \ 'terminal', |
| 610 | \ 'prompt', |
| 611 | \ 'popup', |
| 612 | \ ] |
| 613 | new somefile |
| 614 | exe 'set buftype=' .. val |
| 615 | au BufReadCmd somefile call setline(1, 'triggered') |
| 616 | edit |
| 617 | call assert_equal('triggered', getline(1)) |
Bram Moolenaar | b1d2c81 | 2022-08-26 11:55:01 +0100 | [diff] [blame] | 618 | |
zeertzjq | 93f72cc | 2022-08-26 15:34:52 +0100 | [diff] [blame] | 619 | au! BufReadCmd |
| 620 | bwipe! |
| 621 | endfor |
Bram Moolenaar | b1d2c81 | 2022-08-26 11:55:01 +0100 | [diff] [blame] | 622 | endfunc |
| 623 | |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 624 | func Test_augroup_deleted() |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 625 | " This caused a crash before E936 was introduced |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 626 | augroup x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 627 | call assert_fails('augroup! x', 'E936:') |
| 628 | au VimEnter * echo |
| 629 | augroup end |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 630 | augroup! x |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 631 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 632 | au! VimEnter |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 633 | endfunc |
| 634 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 635 | " Tests for autocommands on :close command. |
| 636 | " This used to be in test13. |
| 637 | func Test_three_windows() |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 638 | " Clean up buffers, because in some cases this function fails. |
| 639 | call s:cleanup_buffers() |
| 640 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 641 | " Write three files and open them, each in a window. |
| 642 | " Then go to next window, with autocommand that deletes the previous one. |
| 643 | " Do this twice, writing the file. |
| 644 | e! Xtestje1 |
| 645 | call setline(1, 'testje1') |
| 646 | w |
| 647 | sp Xtestje2 |
| 648 | call setline(1, 'testje2') |
| 649 | w |
| 650 | sp Xtestje3 |
| 651 | call setline(1, 'testje3') |
| 652 | w |
| 653 | wincmd w |
| 654 | au WinLeave Xtestje2 bwipe |
| 655 | wincmd w |
| 656 | call assert_equal('Xtestje1', expand('%')) |
| 657 | |
| 658 | au WinLeave Xtestje1 bwipe Xtestje3 |
| 659 | close |
| 660 | call assert_equal('Xtestje1', expand('%')) |
| 661 | |
| 662 | " Test deleting the buffer on a Unload event. If this goes wrong there |
| 663 | " will be the ATTENTION prompt. |
| 664 | e Xtestje1 |
| 665 | au! |
| 666 | au! BufUnload Xtestje1 bwipe |
| 667 | call assert_fails('e Xtestje3', 'E937:') |
| 668 | call assert_equal('Xtestje3', expand('%')) |
| 669 | |
| 670 | e Xtestje2 |
| 671 | sp Xtestje1 |
| 672 | call assert_fails('e', 'E937:') |
Bram Moolenaar | a997b45 | 2018-04-17 23:24:06 +0200 | [diff] [blame] | 673 | call assert_equal('Xtestje1', expand('%')) |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 674 | |
| 675 | " Test changing buffers in a BufWipeout autocommand. If this goes wrong |
| 676 | " there are ml_line errors and/or a Crash. |
| 677 | au! |
| 678 | only |
| 679 | e Xanother |
| 680 | e Xtestje1 |
| 681 | bwipe Xtestje2 |
| 682 | bwipe Xtestje3 |
| 683 | au BufWipeout Xtestje1 buf Xtestje1 |
| 684 | bwipe |
| 685 | call assert_equal('Xanother', expand('%')) |
| 686 | |
| 687 | only |
| 688 | help |
| 689 | wincmd w |
| 690 | 1quit |
| 691 | call assert_equal('Xanother', expand('%')) |
| 692 | |
| 693 | au! |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 694 | enew |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 695 | call delete('Xtestje1') |
| 696 | call delete('Xtestje2') |
| 697 | call delete('Xtestje3') |
| 698 | endfunc |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 699 | |
| 700 | func Test_BufEnter() |
| 701 | au! BufEnter |
| 702 | au Bufenter * let val = val . '+' |
| 703 | let g:val = '' |
| 704 | split NewFile |
| 705 | call assert_equal('+', g:val) |
| 706 | bwipe! |
| 707 | call assert_equal('++', g:val) |
| 708 | |
| 709 | " Also get BufEnter when editing a directory |
Bram Moolenaar | 6f14da1 | 2022-09-07 21:30:44 +0100 | [diff] [blame] | 710 | call mkdir('Xbufenterdir', 'D') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 711 | split Xbufenterdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 712 | call assert_equal('+++', g:val) |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 713 | |
| 714 | " On MS-Windows we can't edit the directory, make sure we wipe the right |
| 715 | " buffer. |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 716 | bwipe! Xbufenterdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 717 | au! BufEnter |
Bram Moolenaar | a9b5b85 | 2022-08-26 13:16:20 +0100 | [diff] [blame] | 718 | |
| 719 | " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter |
zeertzjq | 93f72cc | 2022-08-26 15:34:52 +0100 | [diff] [blame] | 720 | " for historic reasons. Also test other 'buftype' values. |
| 721 | for val in ['nofile', |
| 722 | \ 'nowrite', |
| 723 | \ 'acwrite', |
| 724 | \ 'quickfix', |
| 725 | \ 'help', |
| 726 | \ 'terminal', |
| 727 | \ 'prompt', |
| 728 | \ 'popup', |
| 729 | \ ] |
| 730 | new somefile |
| 731 | exe 'set buftype=' .. val |
| 732 | au BufEnter somefile call setline(1, 'some text') |
| 733 | edit |
| 734 | call assert_equal('some text', getline(1)) |
| 735 | bwipe! |
| 736 | au! BufEnter |
| 737 | endfor |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 738 | endfunc |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 739 | |
| 740 | " Closing a window might cause an endless loop |
| 741 | " E814 for older Vims |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 742 | func Test_autocmd_bufwipe_in_SessLoadPost() |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 743 | edit Xtest |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 744 | tabnew |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 745 | file Xsomething |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 746 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 747 | mksession! |
| 748 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 749 | let content =<< trim [CODE] |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 750 | call test_override('ui_delay', 10) |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 751 | set nocp noswapfile |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 752 | let v:swapchoice = "e" |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 753 | augroup test_autocmd_sessionload |
| 754 | autocmd! |
| 755 | autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!" |
| 756 | augroup END |
| 757 | |
| 758 | func WriteErrors() |
| 759 | call writefile([execute("messages")], "Xerrors") |
| 760 | endfunc |
| 761 | au VimLeave * call WriteErrors() |
| 762 | [CODE] |
| 763 | |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 764 | call writefile(content, 'Xvimrc', 'D') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 765 | call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq') |
K.Takata | 0500e87 | 2022-09-08 12:28:02 +0100 | [diff] [blame] | 766 | sleep 50m |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 767 | let errors = join(readfile('Xerrors')) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 768 | call assert_match('E814:', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 769 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 770 | set swapfile |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 771 | for file in ['Session.vim', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 772 | call delete(file) |
| 773 | endfor |
| 774 | endfunc |
| 775 | |
Bram Moolenaar | 797e63b | 2021-01-15 16:22:52 +0100 | [diff] [blame] | 776 | " Using :blast and :ball for many events caused a crash, because b_nwindows was |
| 777 | " not incremented correctly. |
| 778 | func Test_autocmd_blast_badd() |
| 779 | let content =<< trim [CODE] |
| 780 | au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast |
| 781 | edit foo1 |
| 782 | au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball |
| 783 | edit foo2 |
| 784 | call writefile(['OK'], 'Xerrors') |
| 785 | qall |
| 786 | [CODE] |
| 787 | |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 788 | call writefile(content, 'XblastBall', 'D') |
Bram Moolenaar | 797e63b | 2021-01-15 16:22:52 +0100 | [diff] [blame] | 789 | call system(GetVimCommand() .. ' --clean -S XblastBall') |
| 790 | call assert_match('OK', readfile('Xerrors')->join()) |
| 791 | |
Bram Moolenaar | 797e63b | 2021-01-15 16:22:52 +0100 | [diff] [blame] | 792 | call delete('Xerrors') |
| 793 | endfunc |
| 794 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 795 | " SEGV occurs in older versions. |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 796 | func Test_autocmd_bufwipe_in_SessLoadPost2() |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 797 | tabnew |
| 798 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 799 | mksession! |
| 800 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 801 | let content =<< trim [CODE] |
| 802 | set nocp noswapfile |
| 803 | function! DeleteInactiveBufs() |
| 804 | tabfirst |
| 805 | let tabblist = [] |
| 806 | for i in range(1, tabpagenr(''$'')) |
| 807 | call extend(tabblist, tabpagebuflist(i)) |
| 808 | endfor |
| 809 | for b in range(1, bufnr(''$'')) |
| 810 | if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'') |
| 811 | exec ''bwipeout '' . b |
| 812 | endif |
| 813 | endfor |
| 814 | echomsg "SessionLoadPost DONE" |
| 815 | endfunction |
| 816 | au SessionLoadPost * call DeleteInactiveBufs() |
| 817 | |
| 818 | func WriteErrors() |
| 819 | call writefile([execute("messages")], "Xerrors") |
| 820 | endfunc |
| 821 | au VimLeave * call WriteErrors() |
| 822 | [CODE] |
| 823 | |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 824 | call writefile(content, 'Xvimrc', 'D') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 825 | call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 826 | let errors = join(readfile('Xerrors')) |
| 827 | " This probably only ever matches on unix. |
| 828 | call assert_notmatch('Caught deadly signal SEGV', errors) |
| 829 | call assert_match('SessionLoadPost DONE', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 830 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 831 | set swapfile |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 832 | for file in ['Session.vim', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 833 | call delete(file) |
| 834 | endfor |
| 835 | endfunc |
Bram Moolenaar | faf29d7 | 2017-07-09 11:07:16 +0200 | [diff] [blame] | 836 | |
| 837 | func Test_empty_doau() |
| 838 | doau \| |
| 839 | endfunc |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 840 | |
| 841 | func s:AutoCommandOptionSet(match) |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 842 | let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n" |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 843 | let item = remove(g:options, 0) |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 844 | let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6]) |
| 845 | let actual = printf(template, a:match, v:option_old, v:option_oldlocal, v:option_oldglobal, v:option_new, v:option_type, v:option_command) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 846 | let g:opt = [expected, actual] |
| 847 | "call assert_equal(expected, actual) |
| 848 | endfunc |
| 849 | |
| 850 | func Test_OptionSet() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 851 | CheckOption autochdir |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 852 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 853 | badd test_autocmd.vim |
| 854 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 855 | call test_override('starting', 1) |
| 856 | set nocp |
| 857 | au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>")) |
| 858 | |
| 859 | " 1: Setting number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 860 | let g:options = [['number', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 861 | set nu |
| 862 | call assert_equal([], g:options) |
| 863 | call assert_equal(g:opt[0], g:opt[1]) |
| 864 | |
| 865 | " 2: Setting local number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 866 | let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 867 | setlocal nonu |
| 868 | call assert_equal([], g:options) |
| 869 | call assert_equal(g:opt[0], g:opt[1]) |
| 870 | |
| 871 | " 3: Setting global number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 872 | let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 873 | setglobal nonu |
| 874 | call assert_equal([], g:options) |
| 875 | call assert_equal(g:opt[0], g:opt[1]) |
| 876 | |
| 877 | " 4: Setting local autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 878 | let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 879 | setlocal ai |
| 880 | call assert_equal([], g:options) |
| 881 | call assert_equal(g:opt[0], g:opt[1]) |
| 882 | |
| 883 | " 5: Setting global autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 884 | let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 885 | setglobal ai |
| 886 | call assert_equal([], g:options) |
| 887 | call assert_equal(g:opt[0], g:opt[1]) |
| 888 | |
| 889 | " 6: Setting global autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 890 | let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 891 | set ai! |
| 892 | call assert_equal([], g:options) |
| 893 | call assert_equal(g:opt[0], g:opt[1]) |
| 894 | |
| 895 | " 6a: Setting global autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 896 | let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 897 | noa setlocal ai |
| 898 | noa setglobal noai |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 899 | set ai! |
| 900 | call assert_equal([], g:options) |
| 901 | call assert_equal(g:opt[0], g:opt[1]) |
| 902 | |
| 903 | " Should not print anything, use :noa |
| 904 | " 7: don't trigger OptionSet" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 905 | let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 906 | noa set nonu |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 907 | call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 908 | call assert_equal(g:opt[0], g:opt[1]) |
| 909 | |
| 910 | " 8: Setting several global list and number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 911 | let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 912 | set list nu |
| 913 | call assert_equal([], g:options) |
| 914 | call assert_equal(g:opt[0], g:opt[1]) |
| 915 | |
| 916 | " 9: don't trigger OptionSet" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 917 | let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 918 | noa set nolist nonu |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 919 | call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 920 | call assert_equal(g:opt[0], g:opt[1]) |
| 921 | |
| 922 | " 10: Setting global acd" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 923 | let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 924 | setlocal acd |
| 925 | call assert_equal([], g:options) |
| 926 | call assert_equal(g:opt[0], g:opt[1]) |
| 927 | |
| 928 | " 11: Setting global autoread (also sets local value)" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 929 | let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 930 | set ar |
| 931 | call assert_equal([], g:options) |
| 932 | call assert_equal(g:opt[0], g:opt[1]) |
| 933 | |
| 934 | " 12: Setting local autoread" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 935 | let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 936 | setlocal ar |
| 937 | call assert_equal([], g:options) |
| 938 | call assert_equal(g:opt[0], g:opt[1]) |
| 939 | |
| 940 | " 13: Setting global autoread" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 941 | let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 942 | setglobal invar |
| 943 | call assert_equal([], g:options) |
| 944 | call assert_equal(g:opt[0], g:opt[1]) |
| 945 | |
| 946 | " 14: Setting option backspace through :let" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 947 | let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']] |
| 948 | let &bs = "eol,indent,start" |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 949 | call assert_equal([], g:options) |
| 950 | call assert_equal(g:opt[0], g:opt[1]) |
| 951 | |
| 952 | " 15: Setting option backspace through setbufvar()" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 953 | let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 954 | " try twice, first time, shouldn't trigger because option name is invalid, |
| 955 | " second time, it should trigger |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 956 | let bnum = bufnr('%') |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 957 | call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 958 | " should trigger, use correct option name |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 959 | call setbufvar(bnum, '&backup', 1) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 960 | call assert_equal([], g:options) |
| 961 | call assert_equal(g:opt[0], g:opt[1]) |
| 962 | |
| 963 | " 16: Setting number option using setwinvar" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 964 | let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 965 | call setwinvar(0, '&number', 1) |
| 966 | call assert_equal([], g:options) |
| 967 | call assert_equal(g:opt[0], g:opt[1]) |
| 968 | |
| 969 | " 17: Setting key option, shouldn't trigger" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 970 | let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 971 | setlocal key=blah |
| 972 | setlocal key= |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 973 | call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 974 | call assert_equal(g:opt[0], g:opt[1]) |
| 975 | |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 976 | |
| 977 | " 18a: Setting string global option" |
| 978 | let oldval = &backupext |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 979 | let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 980 | set backupext=foo |
| 981 | call assert_equal([], g:options) |
| 982 | call assert_equal(g:opt[0], g:opt[1]) |
| 983 | |
| 984 | " 18b: Resetting string global option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 985 | let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 986 | set backupext& |
| 987 | call assert_equal([], g:options) |
| 988 | call assert_equal(g:opt[0], g:opt[1]) |
| 989 | |
| 990 | " 18c: Setting global string global option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 991 | let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 992 | setglobal backupext=bar |
| 993 | call assert_equal([], g:options) |
| 994 | call assert_equal(g:opt[0], g:opt[1]) |
| 995 | |
| 996 | " 18d: Setting local string global option" |
| 997 | " As this is a global option this sets the global value even though |
| 998 | " :setlocal is used! |
| 999 | noa set backupext& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1000 | let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1001 | setlocal backupext=baz |
| 1002 | call assert_equal([], g:options) |
| 1003 | call assert_equal(g:opt[0], g:opt[1]) |
| 1004 | |
| 1005 | " 18e: Setting again string global option" |
| 1006 | noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd) |
| 1007 | noa setlocal backupext=ext_local " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1008 | let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1009 | set backupext=fuu |
| 1010 | call assert_equal([], g:options) |
| 1011 | call assert_equal(g:opt[0], g:opt[1]) |
| 1012 | |
| 1013 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1014 | " 19a: Setting string global-local (to buffer) option" |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 1015 | let oldval = &tags |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1016 | let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']] |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 1017 | set tags=tagpath |
| 1018 | call assert_equal([], g:options) |
| 1019 | call assert_equal(g:opt[0], g:opt[1]) |
| 1020 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1021 | " 19b: Resetting string global-local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1022 | let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']] |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 1023 | set tags& |
| 1024 | call assert_equal([], g:options) |
| 1025 | call assert_equal(g:opt[0], g:opt[1]) |
| 1026 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1027 | " 19c: Setting global string global-local (to buffer) option " |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1028 | let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1029 | setglobal tags=tagpath1 |
| 1030 | call assert_equal([], g:options) |
| 1031 | call assert_equal(g:opt[0], g:opt[1]) |
| 1032 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1033 | " 19d: Setting local string global-local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1034 | let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1035 | setlocal tags=tagpath2 |
| 1036 | call assert_equal([], g:options) |
| 1037 | call assert_equal(g:opt[0], g:opt[1]) |
| 1038 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1039 | " 19e: Setting again string global-local (to buffer) option" |
| 1040 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1041 | " but the old local value for all other kinds of options. |
| 1042 | noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd) |
| 1043 | noa setlocal tags=tag_local |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1044 | let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1045 | set tags=tagpath |
| 1046 | call assert_equal([], g:options) |
| 1047 | call assert_equal(g:opt[0], g:opt[1]) |
| 1048 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1049 | " 19f: Setting string global-local (to buffer) option to an empty string" |
| 1050 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1051 | " but the old local value for all other kinds of options. |
| 1052 | noa set tags=tag_global " Reset global and local value (without triggering autocmd) |
| 1053 | noa setlocal tags= " empty string |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1054 | let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1055 | set tags=tagpath |
| 1056 | call assert_equal([], g:options) |
| 1057 | call assert_equal(g:opt[0], g:opt[1]) |
| 1058 | |
| 1059 | |
| 1060 | " 20a: Setting string local (to buffer) option" |
| 1061 | let oldval = &spelllang |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1062 | let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1063 | set spelllang=elvish,klingon |
| 1064 | call assert_equal([], g:options) |
| 1065 | call assert_equal(g:opt[0], g:opt[1]) |
| 1066 | |
| 1067 | " 20b: Resetting string local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1068 | let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1069 | set spelllang& |
| 1070 | call assert_equal([], g:options) |
| 1071 | call assert_equal(g:opt[0], g:opt[1]) |
| 1072 | |
| 1073 | " 20c: Setting global string local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1074 | let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1075 | setglobal spelllang=elvish |
| 1076 | call assert_equal([], g:options) |
| 1077 | call assert_equal(g:opt[0], g:opt[1]) |
| 1078 | |
| 1079 | " 20d: Setting local string local (to buffer) option" |
| 1080 | noa set spelllang& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1081 | let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1082 | setlocal spelllang=klingon |
| 1083 | call assert_equal([], g:options) |
| 1084 | call assert_equal(g:opt[0], g:opt[1]) |
| 1085 | |
| 1086 | " 20e: Setting again string local (to buffer) option" |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1087 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1088 | " but the old local value for all other kinds of options. |
| 1089 | noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd) |
| 1090 | noa setlocal spelllang=spelllocal |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1091 | let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1092 | set spelllang=foo |
| 1093 | call assert_equal([], g:options) |
| 1094 | call assert_equal(g:opt[0], g:opt[1]) |
| 1095 | |
| 1096 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1097 | " 21a: Setting string global-local (to window) option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1098 | let oldval = &statusline |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1099 | let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1100 | set statusline=foo |
| 1101 | call assert_equal([], g:options) |
| 1102 | call assert_equal(g:opt[0], g:opt[1]) |
| 1103 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1104 | " 21b: Resetting string global-local (to window) option" |
| 1105 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1106 | " but the old local value for all other kinds of options. |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1107 | let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1108 | set statusline& |
| 1109 | call assert_equal([], g:options) |
| 1110 | call assert_equal(g:opt[0], g:opt[1]) |
| 1111 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1112 | " 21c: Setting global string global-local (to window) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1113 | let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1114 | setglobal statusline=bar |
| 1115 | call assert_equal([], g:options) |
| 1116 | call assert_equal(g:opt[0], g:opt[1]) |
| 1117 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1118 | " 21d: Setting local string global-local (to window) option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1119 | noa set statusline& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1120 | let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1121 | setlocal statusline=baz |
| 1122 | call assert_equal([], g:options) |
| 1123 | call assert_equal(g:opt[0], g:opt[1]) |
| 1124 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1125 | " 21e: Setting again string global-local (to window) option" |
| 1126 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1127 | " but the old local value for all other kinds of options. |
| 1128 | noa setglobal statusline=bar " Reset global and local value (without triggering autocmd) |
| 1129 | noa setlocal statusline=baz |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1130 | let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1131 | set statusline=foo |
| 1132 | call assert_equal([], g:options) |
| 1133 | call assert_equal(g:opt[0], g:opt[1]) |
| 1134 | |
| 1135 | |
| 1136 | " 22a: Setting string local (to window) option" |
| 1137 | let oldval = &foldignore |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1138 | let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1139 | set foldignore=fo |
| 1140 | call assert_equal([], g:options) |
| 1141 | call assert_equal(g:opt[0], g:opt[1]) |
| 1142 | |
| 1143 | " 22b: Resetting string local (to window) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1144 | let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1145 | set foldignore& |
| 1146 | call assert_equal([], g:options) |
| 1147 | call assert_equal(g:opt[0], g:opt[1]) |
| 1148 | |
| 1149 | " 22c: Setting global string local (to window) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1150 | let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1151 | setglobal foldignore=bar |
| 1152 | call assert_equal([], g:options) |
| 1153 | call assert_equal(g:opt[0], g:opt[1]) |
| 1154 | |
| 1155 | " 22d: Setting local string local (to window) option" |
| 1156 | noa set foldignore& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1157 | let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1158 | setlocal foldignore=baz |
| 1159 | call assert_equal([], g:options) |
| 1160 | call assert_equal(g:opt[0], g:opt[1]) |
| 1161 | |
| 1162 | " 22e: Setting again string local (to window) option" |
| 1163 | noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd) |
| 1164 | noa setlocal foldignore=loc |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1165 | let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1166 | set foldignore=fo |
| 1167 | call assert_equal([], g:options) |
| 1168 | call assert_equal(g:opt[0], g:opt[1]) |
| 1169 | |
| 1170 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1171 | " 23a: Setting global number global option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1172 | noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) |
| 1173 | noa setlocal cmdheight=1 " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1174 | let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1175 | setglobal cmdheight=2 |
| 1176 | call assert_equal([], g:options) |
| 1177 | call assert_equal(g:opt[0], g:opt[1]) |
| 1178 | |
| 1179 | " 23b: Setting local number global option" |
| 1180 | noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) |
| 1181 | noa setlocal cmdheight=1 " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1182 | let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1183 | setlocal cmdheight=2 |
| 1184 | call assert_equal([], g:options) |
| 1185 | call assert_equal(g:opt[0], g:opt[1]) |
| 1186 | |
| 1187 | " 23c: Setting again number global option" |
| 1188 | noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) |
| 1189 | noa setlocal cmdheight=1 " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1190 | let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1191 | set cmdheight=2 |
| 1192 | call assert_equal([], g:options) |
| 1193 | call assert_equal(g:opt[0], g:opt[1]) |
| 1194 | |
| 1195 | " 23d: Setting again number global option" |
| 1196 | noa set cmdheight=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1197 | let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1198 | set cmdheight=2 |
| 1199 | call assert_equal([], g:options) |
| 1200 | call assert_equal(g:opt[0], g:opt[1]) |
| 1201 | |
| 1202 | |
| 1203 | " 24a: Setting global number global-local (to buffer) option" |
| 1204 | noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) |
| 1205 | noa setlocal undolevels=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1206 | let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1207 | setglobal undolevels=2 |
| 1208 | call assert_equal([], g:options) |
| 1209 | call assert_equal(g:opt[0], g:opt[1]) |
| 1210 | |
| 1211 | " 24b: Setting local number global-local (to buffer) option" |
| 1212 | noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) |
| 1213 | noa setlocal undolevels=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1214 | let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1215 | setlocal undolevels=2 |
| 1216 | call assert_equal([], g:options) |
| 1217 | call assert_equal(g:opt[0], g:opt[1]) |
| 1218 | |
| 1219 | " 24c: Setting again number global-local (to buffer) option" |
| 1220 | noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) |
| 1221 | noa setlocal undolevels=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1222 | let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1223 | set undolevels=2 |
| 1224 | call assert_equal([], g:options) |
| 1225 | call assert_equal(g:opt[0], g:opt[1]) |
| 1226 | |
| 1227 | " 24d: Setting again global number global-local (to buffer) option" |
| 1228 | noa set undolevels=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1229 | let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1230 | set undolevels=2 |
| 1231 | call assert_equal([], g:options) |
| 1232 | call assert_equal(g:opt[0], g:opt[1]) |
| 1233 | |
| 1234 | |
| 1235 | " 25a: Setting global number local (to buffer) option" |
| 1236 | noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) |
| 1237 | noa setlocal wrapmargin=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1238 | let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1239 | setglobal wrapmargin=2 |
| 1240 | call assert_equal([], g:options) |
| 1241 | call assert_equal(g:opt[0], g:opt[1]) |
| 1242 | |
| 1243 | " 25b: Setting local number local (to buffer) option" |
| 1244 | noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) |
| 1245 | noa setlocal wrapmargin=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1246 | let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1247 | setlocal wrapmargin=2 |
| 1248 | call assert_equal([], g:options) |
| 1249 | call assert_equal(g:opt[0], g:opt[1]) |
| 1250 | |
| 1251 | " 25c: Setting again number local (to buffer) option" |
| 1252 | noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) |
| 1253 | noa setlocal wrapmargin=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1254 | let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1255 | set wrapmargin=2 |
| 1256 | call assert_equal([], g:options) |
| 1257 | call assert_equal(g:opt[0], g:opt[1]) |
| 1258 | |
| 1259 | " 25d: Setting again global number local (to buffer) option" |
| 1260 | noa set wrapmargin=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1261 | let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1262 | set wrapmargin=2 |
| 1263 | call assert_equal([], g:options) |
| 1264 | call assert_equal(g:opt[0], g:opt[1]) |
| 1265 | |
| 1266 | |
| 1267 | " 26: Setting number global-local (to window) option. |
| 1268 | " Such option does currently not exist. |
| 1269 | |
| 1270 | |
| 1271 | " 27a: Setting global number local (to window) option" |
| 1272 | noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) |
| 1273 | noa setlocal foldcolumn=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1274 | let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1275 | setglobal foldcolumn=2 |
| 1276 | call assert_equal([], g:options) |
| 1277 | call assert_equal(g:opt[0], g:opt[1]) |
| 1278 | |
| 1279 | " 27b: Setting local number local (to window) option" |
| 1280 | noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) |
| 1281 | noa setlocal foldcolumn=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1282 | let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1283 | setlocal foldcolumn=2 |
| 1284 | call assert_equal([], g:options) |
| 1285 | call assert_equal(g:opt[0], g:opt[1]) |
| 1286 | |
| 1287 | " 27c: Setting again number local (to window) option" |
| 1288 | noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) |
| 1289 | noa setlocal foldcolumn=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1290 | let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1291 | set foldcolumn=2 |
| 1292 | call assert_equal([], g:options) |
| 1293 | call assert_equal(g:opt[0], g:opt[1]) |
| 1294 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1295 | " 27d: Setting again global number local (to window) option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1296 | noa set foldcolumn=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1297 | let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1298 | set foldcolumn=2 |
| 1299 | call assert_equal([], g:options) |
| 1300 | call assert_equal(g:opt[0], g:opt[1]) |
| 1301 | |
| 1302 | |
| 1303 | " 28a: Setting global boolean global option" |
| 1304 | noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) |
| 1305 | noa setlocal wrapscan " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1306 | let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1307 | setglobal nowrapscan |
| 1308 | call assert_equal([], g:options) |
| 1309 | call assert_equal(g:opt[0], g:opt[1]) |
| 1310 | |
| 1311 | " 28b: Setting local boolean global option" |
| 1312 | noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) |
| 1313 | noa setlocal wrapscan " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1314 | let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1315 | setlocal nowrapscan |
| 1316 | call assert_equal([], g:options) |
| 1317 | call assert_equal(g:opt[0], g:opt[1]) |
| 1318 | |
| 1319 | " 28c: Setting again boolean global option" |
| 1320 | noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) |
| 1321 | noa setlocal wrapscan " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1322 | let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1323 | set nowrapscan |
| 1324 | call assert_equal([], g:options) |
| 1325 | call assert_equal(g:opt[0], g:opt[1]) |
| 1326 | |
| 1327 | " 28d: Setting again global boolean global option" |
| 1328 | noa set nowrapscan " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1329 | let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1330 | set wrapscan |
| 1331 | call assert_equal([], g:options) |
| 1332 | call assert_equal(g:opt[0], g:opt[1]) |
| 1333 | |
| 1334 | |
| 1335 | " 29a: Setting global boolean global-local (to buffer) option" |
| 1336 | noa setglobal noautoread " Reset global and local value (without triggering autocmd) |
| 1337 | noa setlocal autoread |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1338 | let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1339 | setglobal autoread |
| 1340 | call assert_equal([], g:options) |
| 1341 | call assert_equal(g:opt[0], g:opt[1]) |
| 1342 | |
| 1343 | " 29b: Setting local boolean global-local (to buffer) option" |
| 1344 | noa setglobal noautoread " Reset global and local value (without triggering autocmd) |
| 1345 | noa setlocal autoread |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1346 | let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1347 | setlocal noautoread |
| 1348 | call assert_equal([], g:options) |
| 1349 | call assert_equal(g:opt[0], g:opt[1]) |
| 1350 | |
| 1351 | " 29c: Setting again boolean global-local (to buffer) option" |
| 1352 | noa setglobal noautoread " Reset global and local value (without triggering autocmd) |
| 1353 | noa setlocal autoread |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1354 | let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1355 | set autoread |
| 1356 | call assert_equal([], g:options) |
| 1357 | call assert_equal(g:opt[0], g:opt[1]) |
| 1358 | |
| 1359 | " 29d: Setting again global boolean global-local (to buffer) option" |
| 1360 | noa set noautoread " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1361 | let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1362 | set autoread |
| 1363 | call assert_equal([], g:options) |
| 1364 | call assert_equal(g:opt[0], g:opt[1]) |
| 1365 | |
| 1366 | |
| 1367 | " 30a: Setting global boolean local (to buffer) option" |
| 1368 | noa setglobal nocindent " Reset global and local value (without triggering autocmd) |
| 1369 | noa setlocal cindent |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1370 | let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1371 | setglobal cindent |
| 1372 | call assert_equal([], g:options) |
| 1373 | call assert_equal(g:opt[0], g:opt[1]) |
| 1374 | |
| 1375 | " 30b: Setting local boolean local (to buffer) option" |
| 1376 | noa setglobal nocindent " Reset global and local value (without triggering autocmd) |
| 1377 | noa setlocal cindent |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1378 | let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1379 | setlocal nocindent |
| 1380 | call assert_equal([], g:options) |
| 1381 | call assert_equal(g:opt[0], g:opt[1]) |
| 1382 | |
| 1383 | " 30c: Setting again boolean local (to buffer) option" |
| 1384 | noa setglobal nocindent " Reset global and local value (without triggering autocmd) |
| 1385 | noa setlocal cindent |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1386 | let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1387 | set cindent |
| 1388 | call assert_equal([], g:options) |
| 1389 | call assert_equal(g:opt[0], g:opt[1]) |
| 1390 | |
| 1391 | " 30d: Setting again global boolean local (to buffer) option" |
| 1392 | noa set nocindent " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1393 | let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1394 | set cindent |
| 1395 | call assert_equal([], g:options) |
| 1396 | call assert_equal(g:opt[0], g:opt[1]) |
| 1397 | |
| 1398 | |
| 1399 | " 31: Setting boolean global-local (to window) option |
| 1400 | " Currently no such option exists. |
| 1401 | |
| 1402 | |
| 1403 | " 32a: Setting global boolean local (to window) option" |
| 1404 | noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) |
| 1405 | noa setlocal cursorcolumn |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1406 | let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1407 | setglobal cursorcolumn |
| 1408 | call assert_equal([], g:options) |
| 1409 | call assert_equal(g:opt[0], g:opt[1]) |
| 1410 | |
| 1411 | " 32b: Setting local boolean local (to window) option" |
| 1412 | noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) |
| 1413 | noa setlocal cursorcolumn |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1414 | let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1415 | setlocal nocursorcolumn |
| 1416 | call assert_equal([], g:options) |
| 1417 | call assert_equal(g:opt[0], g:opt[1]) |
| 1418 | |
| 1419 | " 32c: Setting again boolean local (to window) option" |
| 1420 | noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) |
| 1421 | noa setlocal cursorcolumn |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1422 | let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1423 | set cursorcolumn |
| 1424 | call assert_equal([], g:options) |
| 1425 | call assert_equal(g:opt[0], g:opt[1]) |
| 1426 | |
| 1427 | " 32d: Setting again global boolean local (to window) option" |
| 1428 | noa set nocursorcolumn " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1429 | let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1430 | set cursorcolumn |
| 1431 | call assert_equal([], g:options) |
| 1432 | call assert_equal(g:opt[0], g:opt[1]) |
| 1433 | |
| 1434 | |
Bram Moolenaar | 1bc353b | 2019-09-01 14:45:28 +0200 | [diff] [blame] | 1435 | " 33: Test autocommands when an option value is converted internally. |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1436 | noa set backspace=1 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1437 | let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1438 | set backspace=2 |
| 1439 | call assert_equal([], g:options) |
| 1440 | call assert_equal(g:opt[0], g:opt[1]) |
| 1441 | |
| 1442 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1443 | " Cleanup |
| 1444 | au! OptionSet |
Bram Moolenaar | 0331faf | 2019-06-15 18:40:37 +0200 | [diff] [blame] | 1445 | " set tags& |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1446 | for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp', 'backupext', 'tags', 'spelllang', 'statusline', 'foldignore', 'cmdheight', 'undolevels', 'wrapmargin', 'foldcolumn', 'wrapscan', 'autoread', 'cindent', 'cursorcolumn'] |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1447 | exe printf(":set %s&vim", opt) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1448 | endfor |
| 1449 | call test_override('starting', 0) |
| 1450 | delfunc! AutoCommandOptionSet |
| 1451 | endfunc |
| 1452 | |
| 1453 | func Test_OptionSet_diffmode() |
| 1454 | call test_override('starting', 1) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 1455 | " 18: Changing an option when entering diff mode |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1456 | new |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1457 | au OptionSet diff :let &l:cul = v:option_new |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1458 | |
| 1459 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 1460 | call assert_equal(0, &l:cul) |
| 1461 | diffthis |
| 1462 | call assert_equal(1, &l:cul) |
| 1463 | |
| 1464 | vnew |
| 1465 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 1466 | call assert_equal(0, &l:cul) |
| 1467 | diffthis |
| 1468 | call assert_equal(1, &l:cul) |
| 1469 | |
| 1470 | diffoff |
| 1471 | call assert_equal(0, &l:cul) |
| 1472 | call assert_equal(1, getwinvar(2, '&l:cul')) |
| 1473 | bw! |
| 1474 | |
| 1475 | call assert_equal(1, &l:cul) |
| 1476 | diffoff! |
| 1477 | call assert_equal(0, &l:cul) |
| 1478 | call assert_equal(0, getwinvar(1, '&l:cul')) |
| 1479 | bw! |
| 1480 | |
| 1481 | " Cleanup |
| 1482 | au! OptionSet |
| 1483 | call test_override('starting', 0) |
| 1484 | endfunc |
| 1485 | |
| 1486 | func Test_OptionSet_diffmode_close() |
| 1487 | call test_override('starting', 1) |
| 1488 | " 19: Try to close the current window when entering diff mode |
| 1489 | " should not segfault |
| 1490 | new |
| 1491 | au OptionSet diff close |
| 1492 | |
| 1493 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1494 | call assert_fails(':diffthis', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1495 | call assert_equal(1, &diff) |
| 1496 | vnew |
| 1497 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1498 | call assert_fails(':diffthis', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1499 | call assert_equal(1, &diff) |
Bram Moolenaar | a9aa86f | 2019-11-10 21:25:45 +0100 | [diff] [blame] | 1500 | set diffopt-=closeoff |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1501 | bw! |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1502 | call assert_fails(':diffoff!', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1503 | bw! |
| 1504 | |
| 1505 | " Cleanup |
| 1506 | au! OptionSet |
| 1507 | call test_override('starting', 0) |
| 1508 | "delfunc! AutoCommandOptionSet |
| 1509 | endfunc |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1510 | |
| 1511 | " Test for Bufleave autocommand that deletes the buffer we are about to edit. |
| 1512 | func Test_BufleaveWithDelete() |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 1513 | new | edit XbufLeave1 |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1514 | |
| 1515 | augroup test_bufleavewithdelete |
| 1516 | autocmd! |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 1517 | autocmd BufLeave XbufLeave1 bwipe XbufLeave2 |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1518 | augroup END |
| 1519 | |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 1520 | call assert_fails('edit XbufLeave2', 'E143:') |
| 1521 | call assert_equal('XbufLeave1', bufname('%')) |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1522 | |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 1523 | autocmd! test_bufleavewithdelete BufLeave XbufLeave1 |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1524 | augroup! test_bufleavewithdelete |
| 1525 | |
| 1526 | new |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 1527 | bwipe! XbufLeave1 |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1528 | endfunc |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1529 | |
| 1530 | " Test for autocommand that changes the buffer list, when doing ":ball". |
| 1531 | func Test_Acmd_BufAll() |
| 1532 | enew! |
| 1533 | %bwipe! |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1534 | call writefile(['Test file Xxx1'], 'Xxx1', 'D') |
| 1535 | call writefile(['Test file Xxx2'], 'Xxx2', 'D') |
| 1536 | call writefile(['Test file Xxx3'], 'Xxx3', 'D') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1537 | |
| 1538 | " Add three files to the buffer list |
| 1539 | split Xxx1 |
| 1540 | close |
| 1541 | split Xxx2 |
| 1542 | close |
| 1543 | split Xxx3 |
| 1544 | close |
| 1545 | |
| 1546 | " Wipe the buffer when the buffer is opened |
| 1547 | au BufReadPost Xxx2 bwipe |
| 1548 | |
| 1549 | call append(0, 'Test file Xxx4') |
| 1550 | ball |
| 1551 | |
| 1552 | call assert_equal(2, winnr('$')) |
| 1553 | call assert_equal('Xxx1', bufname(winbufnr(winnr('$')))) |
| 1554 | wincmd t |
| 1555 | |
| 1556 | au! BufReadPost |
| 1557 | %bwipe! |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1558 | enew! | only |
| 1559 | endfunc |
| 1560 | |
| 1561 | " Test for autocommand that changes current buffer on BufEnter event. |
| 1562 | " Check if modelines are interpreted for the correct buffer. |
| 1563 | func Test_Acmd_BufEnter() |
| 1564 | %bwipe! |
| 1565 | call writefile(['start of test file Xxx1', |
| 1566 | \ "\<Tab>this is a test", |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1567 | \ 'end of test file Xxx1'], 'Xxx1', 'D') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1568 | call writefile(['start of test file Xxx2', |
| 1569 | \ 'vim: set noai :', |
| 1570 | \ "\<Tab>this is a test", |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1571 | \ 'end of test file Xxx2'], 'Xxx2', 'D') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1572 | |
| 1573 | au BufEnter Xxx2 brew |
| 1574 | set ai modeline modelines=3 |
| 1575 | edit Xxx1 |
| 1576 | " edit Xxx2, autocmd will do :brew |
| 1577 | edit Xxx2 |
| 1578 | exe "normal G?this is a\<CR>" |
| 1579 | " Append text with autoindent to this file |
| 1580 | normal othis should be auto-indented |
| 1581 | call assert_equal("\<Tab>this should be auto-indented", getline('.')) |
| 1582 | call assert_equal(3, line('.')) |
| 1583 | " Remove autocmd and edit Xxx2 again |
| 1584 | au! BufEnter Xxx2 |
| 1585 | buf! Xxx2 |
| 1586 | exe "normal G?this is a\<CR>" |
| 1587 | " append text without autoindent to Xxx |
| 1588 | normal othis should be in column 1 |
| 1589 | call assert_equal("this should be in column 1", getline('.')) |
| 1590 | call assert_equal(4, line('.')) |
| 1591 | |
| 1592 | %bwipe! |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1593 | set ai&vim modeline&vim modelines&vim |
| 1594 | endfunc |
| 1595 | |
| 1596 | " Test for issue #57 |
| 1597 | " do not move cursor on <c-o> when autoindent is set |
| 1598 | func Test_ai_CTRL_O() |
| 1599 | enew! |
| 1600 | set ai |
| 1601 | let save_fo = &fo |
| 1602 | set fo+=r |
| 1603 | exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>" |
| 1604 | exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>" |
| 1605 | call assert_equal(['# abc', 'def', 'def'], getline(2, 4)) |
| 1606 | |
| 1607 | set ai&vim |
| 1608 | let &fo = save_fo |
| 1609 | enew! |
| 1610 | endfunc |
| 1611 | |
| 1612 | " Test for autocommand that deletes the current buffer on BufLeave event. |
| 1613 | " Also test deleting the last buffer, should give a new, empty buffer. |
| 1614 | func Test_BufLeave_Wipe() |
| 1615 | %bwipe! |
| 1616 | let content = ['start of test file Xxx', |
| 1617 | \ 'this is a test', |
| 1618 | \ 'end of test file Xxx'] |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1619 | call writefile(content, 'Xxx1', 'D') |
| 1620 | call writefile(content, 'Xxx2', 'D') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1621 | |
| 1622 | au BufLeave Xxx2 bwipe |
| 1623 | edit Xxx1 |
| 1624 | split Xxx2 |
| 1625 | " delete buffer Xxx2, we should be back to Xxx1 |
| 1626 | bwipe |
| 1627 | call assert_equal('Xxx1', bufname('%')) |
| 1628 | call assert_equal(1, winnr('$')) |
| 1629 | |
| 1630 | " Create an alternate buffer |
| 1631 | %write! test.out |
| 1632 | call assert_equal('test.out', bufname('#')) |
| 1633 | " delete alternate buffer |
| 1634 | bwipe test.out |
| 1635 | call assert_equal('Xxx1', bufname('%')) |
| 1636 | call assert_equal('', bufname('#')) |
| 1637 | |
| 1638 | au BufLeave Xxx1 bwipe |
| 1639 | " delete current buffer, get an empty one |
| 1640 | bwipe! |
| 1641 | call assert_equal(1, line('$')) |
| 1642 | call assert_equal('', bufname('%')) |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 1643 | let g:bufinfo = getbufinfo() |
| 1644 | call assert_equal(1, len(g:bufinfo)) |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1645 | |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1646 | call delete('test.out') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1647 | %bwipe |
| 1648 | au! BufLeave |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 1649 | |
| 1650 | " check that bufinfo doesn't contain a pointer to freed memory |
| 1651 | call test_garbagecollect_now() |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1652 | endfunc |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 1653 | |
| 1654 | func Test_QuitPre() |
| 1655 | edit Xfoo |
| 1656 | let winid = win_getid(winnr()) |
| 1657 | split Xbar |
| 1658 | au! QuitPre * let g:afile = expand('<afile>') |
| 1659 | " Close the other window, <afile> should be correct. |
| 1660 | exe win_id2win(winid) . 'q' |
| 1661 | call assert_equal('Xfoo', g:afile) |
LemonBoy | 66e13ae | 2022-04-21 22:52:11 +0100 | [diff] [blame] | 1662 | |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 1663 | unlet g:afile |
| 1664 | bwipe Xfoo |
| 1665 | bwipe Xbar |
| 1666 | endfunc |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1667 | |
| 1668 | func Test_Cmdline() |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 1669 | au! CmdlineChanged : let g:text = getcmdline() |
| 1670 | let g:text = 0 |
| 1671 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 1672 | call assert_equal("echom 'hello'", g:text) |
| 1673 | au! CmdlineChanged |
| 1674 | |
| 1675 | au! CmdlineChanged : let g:entered = expand('<afile>') |
| 1676 | let g:entered = 0 |
| 1677 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 1678 | call assert_equal(':', g:entered) |
| 1679 | au! CmdlineChanged |
| 1680 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1681 | au! CmdlineEnter : let g:entered = expand('<afile>') |
| 1682 | au! CmdlineLeave : let g:left = expand('<afile>') |
| 1683 | let g:entered = 0 |
| 1684 | let g:left = 0 |
| 1685 | call feedkeys(":echo 'hello'\<CR>", 'xt') |
| 1686 | call assert_equal(':', g:entered) |
| 1687 | call assert_equal(':', g:left) |
| 1688 | au! CmdlineEnter |
| 1689 | au! CmdlineLeave |
| 1690 | |
Bram Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 1691 | let save_shellslash = &shellslash |
| 1692 | set noshellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1693 | au! CmdlineEnter / let g:entered = expand('<afile>') |
| 1694 | au! CmdlineLeave / let g:left = expand('<afile>') |
| 1695 | let g:entered = 0 |
| 1696 | let g:left = 0 |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1697 | new |
| 1698 | call setline(1, 'hello') |
| 1699 | call feedkeys("/hello\<CR>", 'xt') |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1700 | call assert_equal('/', g:entered) |
| 1701 | call assert_equal('/', g:left) |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1702 | bwipe! |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1703 | au! CmdlineEnter |
| 1704 | au! CmdlineLeave |
Bram Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 1705 | let &shellslash = save_shellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1706 | endfunc |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1707 | |
| 1708 | " Test for BufWritePre autocommand that deletes or unloads the buffer. |
| 1709 | func Test_BufWritePre() |
| 1710 | %bwipe |
| 1711 | au BufWritePre Xxx1 bunload |
| 1712 | au BufWritePre Xxx2 bwipe |
| 1713 | |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1714 | call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D') |
| 1715 | call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1716 | |
| 1717 | edit Xtest |
| 1718 | e! Xxx2 |
| 1719 | bdel Xtest |
| 1720 | e Xxx1 |
| 1721 | " write it, will unload it and give an error msg |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1722 | call assert_fails('w', 'E203:') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1723 | call assert_equal('Xxx2', bufname('%')) |
| 1724 | edit Xtest |
| 1725 | e! Xxx2 |
| 1726 | bwipe Xtest |
| 1727 | " write it, will delete the buffer and give an error msg |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1728 | call assert_fails('w', 'E203:') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1729 | call assert_equal('Xxx1', bufname('%')) |
| 1730 | au! BufWritePre |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1731 | endfunc |
| 1732 | |
| 1733 | " Test for BufUnload autocommand that unloads all the other buffers |
| 1734 | func Test_bufunload_all() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1735 | let g:test_is_flaky = 1 |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1736 | call writefile(['Test file Xxx1'], 'Xxx1', 'D')" |
| 1737 | call writefile(['Test file Xxx2'], 'Xxx2', 'D')" |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1738 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 1739 | let content =<< trim [CODE] |
| 1740 | func UnloadAllBufs() |
| 1741 | let i = 1 |
| 1742 | while i <= bufnr('$') |
| 1743 | if i != bufnr('%') && bufloaded(i) |
| 1744 | exe i . 'bunload' |
| 1745 | endif |
| 1746 | let i += 1 |
| 1747 | endwhile |
| 1748 | endfunc |
| 1749 | au BufUnload * call UnloadAllBufs() |
| 1750 | au VimLeave * call writefile(['Test Finished'], 'Xout') |
| 1751 | edit Xxx1 |
| 1752 | split Xxx2 |
| 1753 | q |
| 1754 | [CODE] |
| 1755 | |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1756 | call writefile(content, 'Xbunloadtest', 'D') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1757 | |
| 1758 | call delete('Xout') |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1759 | call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1760 | call assert_true(filereadable('Xout')) |
| 1761 | |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1762 | call delete('Xout') |
| 1763 | endfunc |
| 1764 | |
| 1765 | " Some tests for buffer-local autocommands |
| 1766 | func Test_buflocal_autocmd() |
| 1767 | let g:bname = '' |
| 1768 | edit xx |
| 1769 | au BufLeave <buffer> let g:bname = expand("%") |
| 1770 | " here, autocommand for xx should trigger. |
| 1771 | " but autocommand shall not apply to buffer named <buffer>. |
| 1772 | edit somefile |
| 1773 | call assert_equal('xx', g:bname) |
| 1774 | let g:bname = '' |
| 1775 | " here, autocommand shall be auto-deleted |
| 1776 | bwipe xx |
| 1777 | " autocmd should not trigger |
| 1778 | edit xx |
| 1779 | call assert_equal('', g:bname) |
| 1780 | " autocmd should not trigger |
| 1781 | edit somefile |
| 1782 | call assert_equal('', g:bname) |
| 1783 | enew |
| 1784 | unlet g:bname |
| 1785 | endfunc |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1786 | |
| 1787 | " Test for "*Cmd" autocommands |
| 1788 | func Test_Cmd_Autocmds() |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1789 | call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D') |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1790 | |
| 1791 | enew! |
| 1792 | au BufReadCmd XtestA 0r Xxx|$del |
| 1793 | edit XtestA " will read text of Xxd instead |
| 1794 | call assert_equal('start of Xxx', getline(1)) |
| 1795 | |
| 1796 | au BufWriteCmd XtestA call append(line("$"), "write") |
| 1797 | write " will append a line to the file |
| 1798 | call assert_equal('write', getline('$')) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1799 | call assert_fails('read XtestA', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1800 | call assert_equal('write', getline(4)) |
| 1801 | |
| 1802 | " now we have: |
| 1803 | " 1 start of Xxx |
| 1804 | " 2 abc2 |
| 1805 | " 3 end of Xxx |
| 1806 | " 4 write |
| 1807 | |
| 1808 | au FileReadCmd XtestB '[r Xxx |
| 1809 | 2r XtestB " will read Xxx below line 2 instead |
| 1810 | call assert_equal('start of Xxx', getline(3)) |
| 1811 | |
| 1812 | " now we have: |
| 1813 | " 1 start of Xxx |
| 1814 | " 2 abc2 |
| 1815 | " 3 start of Xxx |
| 1816 | " 4 abc2 |
| 1817 | " 5 end of Xxx |
| 1818 | " 6 end of Xxx |
| 1819 | " 7 write |
| 1820 | |
| 1821 | au FileWriteCmd XtestC '[,']copy $ |
| 1822 | normal 4GA1 |
| 1823 | 4,5w XtestC " will copy lines 4 and 5 to the end |
| 1824 | call assert_equal("\tabc21", getline(8)) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1825 | call assert_fails('r XtestC', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1826 | call assert_equal("end of Xxx", getline(9)) |
| 1827 | |
| 1828 | " now we have: |
| 1829 | " 1 start of Xxx |
| 1830 | " 2 abc2 |
| 1831 | " 3 start of Xxx |
| 1832 | " 4 abc21 |
| 1833 | " 5 end of Xxx |
| 1834 | " 6 end of Xxx |
| 1835 | " 7 write |
| 1836 | " 8 abc21 |
| 1837 | " 9 end of Xxx |
| 1838 | |
| 1839 | let g:lines = [] |
| 1840 | au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']"))) |
| 1841 | w >>XtestD " will add lines to 'lines' |
| 1842 | call assert_equal(9, len(g:lines)) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1843 | call assert_fails('$r XtestD', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1844 | call assert_equal(9, line('$')) |
| 1845 | call assert_equal('end of Xxx', getline('$')) |
| 1846 | |
| 1847 | au BufReadCmd XtestE 0r Xxx|$del |
| 1848 | sp XtestE " split window with test.out |
| 1849 | call assert_equal('end of Xxx', getline(3)) |
| 1850 | |
| 1851 | let g:lines = [] |
| 1852 | exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>" |
| 1853 | au BufWriteCmd XtestE call extend(g:lines, getline(0, '$')) |
| 1854 | wall " will write other window to 'lines' |
| 1855 | call assert_equal(4, len(g:lines), g:lines) |
| 1856 | call assert_equal('asdf', g:lines[2]) |
| 1857 | |
| 1858 | au! BufReadCmd |
| 1859 | au! BufWriteCmd |
| 1860 | au! FileReadCmd |
| 1861 | au! FileWriteCmd |
| 1862 | au! FileAppendCmd |
| 1863 | %bwipe! |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1864 | enew! |
| 1865 | endfunc |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1866 | |
Bram Moolenaar | 0fff441 | 2020-03-29 16:06:29 +0200 | [diff] [blame] | 1867 | func s:ReadFile() |
| 1868 | setl noswapfile nomodified |
| 1869 | let filename = resolve(expand("<afile>:p")) |
| 1870 | execute 'read' fnameescape(filename) |
| 1871 | 1d_ |
| 1872 | exe 'file' fnameescape(filename) |
| 1873 | setl buftype=acwrite |
| 1874 | endfunc |
| 1875 | |
| 1876 | func s:WriteFile() |
| 1877 | let filename = resolve(expand("<afile>:p")) |
| 1878 | setl buftype= |
| 1879 | noautocmd execute 'write' fnameescape(filename) |
| 1880 | setl buftype=acwrite |
| 1881 | setl nomodified |
| 1882 | endfunc |
| 1883 | |
| 1884 | func Test_BufReadCmd() |
| 1885 | autocmd BufReadCmd *.test call s:ReadFile() |
| 1886 | autocmd BufWriteCmd *.test call s:WriteFile() |
| 1887 | |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 1888 | call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D') |
Bram Moolenaar | 0fff441 | 2020-03-29 16:06:29 +0200 | [diff] [blame] | 1889 | edit Xcmd.test |
| 1890 | call assert_match('Xcmd.test" line 1 of 3', execute('file')) |
| 1891 | normal! Gofour |
| 1892 | write |
| 1893 | call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test')) |
| 1894 | |
| 1895 | bwipe! |
Bram Moolenaar | 0fff441 | 2020-03-29 16:06:29 +0200 | [diff] [blame] | 1896 | au! BufReadCmd |
| 1897 | au! BufWriteCmd |
| 1898 | endfunc |
| 1899 | |
zeertzjq | 9c8f946 | 2022-08-30 18:17:15 +0100 | [diff] [blame] | 1900 | func Test_BufWriteCmd() |
| 1901 | autocmd BufWriteCmd Xbufwritecmd let g:written = 1 |
| 1902 | new |
| 1903 | file Xbufwritecmd |
| 1904 | set buftype=acwrite |
Bram Moolenaar | 6f14da1 | 2022-09-07 21:30:44 +0100 | [diff] [blame] | 1905 | call mkdir('Xbufwritecmd', 'D') |
zeertzjq | 9c8f946 | 2022-08-30 18:17:15 +0100 | [diff] [blame] | 1906 | write |
| 1907 | " BufWriteCmd should be triggered even if a directory has the same name |
| 1908 | call assert_equal(1, g:written) |
zeertzjq | 9c8f946 | 2022-08-30 18:17:15 +0100 | [diff] [blame] | 1909 | unlet g:written |
| 1910 | au! BufWriteCmd |
| 1911 | bwipe! |
| 1912 | endfunc |
| 1913 | |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1914 | func SetChangeMarks(start, end) |
Bram Moolenaar | 97c6943 | 2021-01-15 16:45:21 +0100 | [diff] [blame] | 1915 | exe a:start .. 'mark [' |
| 1916 | exe a:end .. 'mark ]' |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1917 | endfunc |
| 1918 | |
| 1919 | " Verify the effects of autocmds on '[ and '] |
| 1920 | func Test_change_mark_in_autocmds() |
| 1921 | edit! Xtest |
Bram Moolenaar | 97c6943 | 2021-01-15 16:45:21 +0100 | [diff] [blame] | 1922 | call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn') |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1923 | |
| 1924 | call SetChangeMarks(2, 3) |
| 1925 | write |
| 1926 | call assert_equal([1, 4], [line("'["), line("']")]) |
| 1927 | |
| 1928 | call SetChangeMarks(2, 3) |
| 1929 | au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1930 | write |
| 1931 | au! BufWritePre |
| 1932 | |
Bram Moolenaar | 14ddd22 | 2020-08-05 12:02:40 +0200 | [diff] [blame] | 1933 | if has('unix') |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1934 | write XtestFilter |
| 1935 | write >> XtestFilter |
| 1936 | |
| 1937 | call SetChangeMarks(2, 3) |
| 1938 | " Marks are set to the entire range of the write |
| 1939 | au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1940 | " '[ is adjusted to just before the line that will receive the filtered |
| 1941 | " data |
| 1942 | au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")]) |
| 1943 | " The filtered data is read into the buffer, and the source lines are |
| 1944 | " still present, so the range is after the source lines |
| 1945 | au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")]) |
| 1946 | %!cat XtestFilter |
| 1947 | " After the filtered data is read, the original lines are deleted |
| 1948 | call assert_equal([1, 8], [line("'["), line("']")]) |
| 1949 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1950 | undo |
| 1951 | |
| 1952 | call SetChangeMarks(1, 4) |
| 1953 | au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1954 | au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")]) |
| 1955 | au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1956 | 2,3!cat XtestFilter |
| 1957 | call assert_equal([2, 9], [line("'["), line("']")]) |
| 1958 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1959 | undo |
| 1960 | |
| 1961 | call delete('XtestFilter') |
| 1962 | endif |
| 1963 | |
| 1964 | call SetChangeMarks(1, 4) |
| 1965 | au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1966 | 2,3write Xtest2 |
| 1967 | au! FileWritePre |
| 1968 | |
| 1969 | call SetChangeMarks(2, 3) |
| 1970 | au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1971 | write >> Xtest2 |
| 1972 | au! FileAppendPre |
| 1973 | |
| 1974 | call SetChangeMarks(1, 4) |
| 1975 | au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1976 | 2,3write >> Xtest2 |
| 1977 | au! FileAppendPre |
| 1978 | |
| 1979 | call SetChangeMarks(1, 1) |
| 1980 | au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")]) |
| 1981 | au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1982 | 3read Xtest2 |
| 1983 | au! FileReadPre,FileReadPost |
| 1984 | undo |
| 1985 | |
| 1986 | call SetChangeMarks(4, 4) |
| 1987 | " When the line is 0, it's adjusted to 1 |
| 1988 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1989 | au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")]) |
| 1990 | 0read Xtest2 |
| 1991 | au! FileReadPre,FileReadPost |
| 1992 | undo |
| 1993 | |
| 1994 | call SetChangeMarks(4, 4) |
| 1995 | " When the line is 0, it's adjusted to 1 |
| 1996 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1997 | au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")]) |
| 1998 | 1read Xtest2 |
| 1999 | au! FileReadPre,FileReadPost |
| 2000 | undo |
| 2001 | |
| 2002 | bwipe! |
| 2003 | call delete('Xtest') |
| 2004 | call delete('Xtest2') |
| 2005 | endfunc |
| 2006 | |
| 2007 | func Test_Filter_noshelltemp() |
Bram Moolenaar | aeb313f | 2020-11-27 19:13:28 +0100 | [diff] [blame] | 2008 | CheckExecutable cat |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 2009 | |
| 2010 | enew! |
| 2011 | call setline(1, ['a', 'b', 'c', 'd']) |
| 2012 | |
| 2013 | let shelltemp = &shelltemp |
| 2014 | set shelltemp |
| 2015 | |
| 2016 | let g:filter_au = 0 |
| 2017 | au FilterWritePre * let g:filter_au += 1 |
| 2018 | au FilterReadPre * let g:filter_au += 1 |
| 2019 | au FilterReadPost * let g:filter_au += 1 |
| 2020 | %!cat |
| 2021 | call assert_equal(3, g:filter_au) |
| 2022 | |
| 2023 | if has('filterpipe') |
| 2024 | set noshelltemp |
| 2025 | |
| 2026 | let g:filter_au = 0 |
| 2027 | au FilterWritePre * let g:filter_au += 1 |
| 2028 | au FilterReadPre * let g:filter_au += 1 |
| 2029 | au FilterReadPost * let g:filter_au += 1 |
| 2030 | %!cat |
| 2031 | call assert_equal(0, g:filter_au) |
| 2032 | endif |
| 2033 | |
| 2034 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 2035 | let &shelltemp = shelltemp |
| 2036 | bwipe! |
| 2037 | endfunc |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2038 | |
| 2039 | func Test_TextYankPost() |
| 2040 | enew! |
| 2041 | call setline(1, ['foo']) |
| 2042 | |
| 2043 | let g:event = [] |
| 2044 | au TextYankPost * let g:event = copy(v:event) |
| 2045 | |
| 2046 | call assert_equal({}, v:event) |
| 2047 | call assert_fails('let v:event = {}', 'E46:') |
| 2048 | call assert_fails('let v:event.mykey = 0', 'E742:') |
| 2049 | |
| 2050 | norm "ayiw |
| 2051 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2052 | \ #{regcontents: ['foo'], regname: 'a', operator: 'y', |
| 2053 | \ regtype: 'v', visual: v:false, inclusive: v:true}, |
| 2054 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2055 | norm y_ |
| 2056 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2057 | \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V', |
| 2058 | \ visual: v:false, inclusive: v:false}, |
| 2059 | \ g:event) |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 2060 | norm Vy |
| 2061 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2062 | \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V', |
| 2063 | \ visual: v:true, inclusive: v:true}, |
| 2064 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2065 | call feedkeys("\<C-V>y", 'x') |
| 2066 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2067 | \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161", |
| 2068 | \ visual: v:true, inclusive: v:true}, |
| 2069 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2070 | norm "xciwbar |
| 2071 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2072 | \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v', |
| 2073 | \ visual: v:false, inclusive: v:true}, |
| 2074 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2075 | norm "bdiw |
| 2076 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2077 | \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v', |
| 2078 | \ visual: v:false, inclusive: v:true}, |
| 2079 | \ g:event) |
| 2080 | |
| 2081 | call setline(1, 'foobar') |
| 2082 | " exclusive motion |
| 2083 | norm $"ay0 |
| 2084 | call assert_equal( |
| 2085 | \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v', |
| 2086 | \ visual: v:false, inclusive: v:false}, |
| 2087 | \ g:event) |
| 2088 | " inclusive motion |
| 2089 | norm 0"ay$ |
| 2090 | call assert_equal( |
| 2091 | \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v', |
| 2092 | \ visual: v:false, inclusive: v:true}, |
| 2093 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2094 | |
| 2095 | call assert_equal({}, v:event) |
| 2096 | |
Bram Moolenaar | fccbf06 | 2020-11-26 20:34:00 +0100 | [diff] [blame] | 2097 | if has('clipboard_working') && !has('gui_running') |
| 2098 | " Test that when the visual selection is automatically copied to clipboard |
| 2099 | " register a TextYankPost is emitted |
| 2100 | call setline(1, ['foobar']) |
| 2101 | |
| 2102 | let @* = '' |
| 2103 | set clipboard=autoselect |
| 2104 | exe "norm! ggviw\<Esc>" |
| 2105 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2106 | \ #{regcontents: ['foobar'], regname: '*', operator: 'y', |
| 2107 | \ regtype: 'v', visual: v:true, inclusive: v:false}, |
| 2108 | \ g:event) |
Bram Moolenaar | fccbf06 | 2020-11-26 20:34:00 +0100 | [diff] [blame] | 2109 | |
| 2110 | let @+ = '' |
| 2111 | set clipboard=autoselectplus |
| 2112 | exe "norm! ggviw\<Esc>" |
| 2113 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2114 | \ #{regcontents: ['foobar'], regname: '+', operator: 'y', |
| 2115 | \ regtype: 'v', visual: v:true, inclusive: v:false}, |
| 2116 | \ g:event) |
Bram Moolenaar | fccbf06 | 2020-11-26 20:34:00 +0100 | [diff] [blame] | 2117 | |
| 2118 | set clipboard&vim |
| 2119 | endif |
| 2120 | |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2121 | au! TextYankPost |
| 2122 | unlet g:event |
| 2123 | bwipe! |
| 2124 | endfunc |
Bram Moolenaar | 9bca805 | 2017-12-18 12:37:55 +0100 | [diff] [blame] | 2125 | |
Bram Moolenaar | 9a046fd | 2021-01-28 13:47:59 +0100 | [diff] [blame] | 2126 | func Test_autocommand_all_events() |
| 2127 | call assert_fails('au * * bwipe', 'E1155:') |
| 2128 | call assert_fails('au * x bwipe', 'E1155:') |
Bram Moolenaar | b6db146 | 2021-12-24 19:24:47 +0000 | [diff] [blame] | 2129 | call assert_fails('au! * x bwipe', 'E1155:') |
Bram Moolenaar | 4fb921e | 2017-12-18 15:33:00 +0100 | [diff] [blame] | 2130 | endfunc |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2131 | |
Bram Moolenaar | f6246f5 | 2022-02-11 16:30:12 +0000 | [diff] [blame] | 2132 | func Test_autocmd_user() |
| 2133 | au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")] |
| 2134 | doautocmd User MyEvent |
| 2135 | call assert_equal(['MyEvent', 'MyEvent'], s:res) |
| 2136 | au! User |
| 2137 | unlet s:res |
| 2138 | endfunc |
| 2139 | |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2140 | function s:Before_test_dirchanged() |
| 2141 | augroup test_dirchanged |
| 2142 | autocmd! |
| 2143 | augroup END |
| 2144 | let s:li = [] |
| 2145 | let s:dir_this = getcwd() |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2146 | let s:dir_foo = s:dir_this . '/Xfoo' |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2147 | call mkdir(s:dir_foo) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2148 | let s:dir_bar = s:dir_this . '/Xbar' |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2149 | call mkdir(s:dir_bar) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2150 | endfunc |
| 2151 | |
| 2152 | function s:After_test_dirchanged() |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2153 | call chdir(s:dir_this) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2154 | call delete(s:dir_foo, 'd') |
| 2155 | call delete(s:dir_bar, 'd') |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2156 | augroup test_dirchanged |
| 2157 | autocmd! |
| 2158 | augroup END |
| 2159 | endfunc |
| 2160 | |
| 2161 | function Test_dirchanged_global() |
| 2162 | call s:Before_test_dirchanged() |
Bram Moolenaar | f6246f5 | 2022-02-11 16:30:12 +0000 | [diff] [blame] | 2163 | autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2164 | autocmd test_dirchanged DirChanged global call add(s:li, "cd:") |
| 2165 | autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>")) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2166 | call chdir(s:dir_foo) |
Bram Moolenaar | f6246f5 | 2022-02-11 16:30:12 +0000 | [diff] [blame] | 2167 | let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo] |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2168 | call assert_equal(expected, s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2169 | call chdir(s:dir_foo) |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2170 | call assert_equal(expected, s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2171 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2172 | call assert_equal(expected, s:li) |
Bram Moolenaar | d8c9d32 | 2022-06-12 11:49:16 +0100 | [diff] [blame] | 2173 | |
| 2174 | exe 'cd ' .. s:dir_foo |
| 2175 | exe 'cd ' .. s:dir_bar |
| 2176 | autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>") |
| 2177 | cd - |
Bram Moolenaar | db77c49 | 2022-06-12 23:26:50 +0100 | [diff] [blame] | 2178 | call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g')) |
Bram Moolenaar | d8c9d32 | 2022-06-12 11:49:16 +0100 | [diff] [blame] | 2179 | |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2180 | call s:After_test_dirchanged() |
| 2181 | endfunc |
| 2182 | |
| 2183 | function Test_dirchanged_local() |
| 2184 | call s:Before_test_dirchanged() |
| 2185 | autocmd test_dirchanged DirChanged window call add(s:li, "lcd:") |
| 2186 | autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>")) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2187 | call chdir(s:dir_foo) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2188 | call assert_equal([], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2189 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2190 | call assert_equal(["lcd:", s:dir_bar], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2191 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2192 | call assert_equal(["lcd:", s:dir_bar], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2193 | call s:After_test_dirchanged() |
| 2194 | endfunc |
| 2195 | |
| 2196 | function Test_dirchanged_auto() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 2197 | CheckOption autochdir |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2198 | call s:Before_test_dirchanged() |
| 2199 | call test_autochdir() |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2200 | autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2201 | autocmd test_dirchanged DirChanged auto call add(s:li, "auto:") |
| 2202 | autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>")) |
| 2203 | set acd |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2204 | cd .. |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2205 | call assert_equal([], s:li) |
Bram Moolenaar | 61abe7d | 2022-08-30 21:46:08 +0100 | [diff] [blame] | 2206 | exe 'edit ' . s:dir_foo . '/Xautofile' |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2207 | call assert_equal(s:dir_foo, getcwd()) |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2208 | let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo] |
| 2209 | call assert_equal(expected, s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2210 | set noacd |
| 2211 | bwipe! |
| 2212 | call s:After_test_dirchanged() |
| 2213 | endfunc |
Bram Moolenaar | 5a09343 | 2018-02-10 18:15:19 +0100 | [diff] [blame] | 2214 | |
| 2215 | " Test TextChangedI and TextChangedP |
| 2216 | func Test_ChangedP() |
| 2217 | new |
| 2218 | call setline(1, ['foo', 'bar', 'foobar']) |
| 2219 | call test_override("char_avail", 1) |
| 2220 | set complete=. completeopt=menuone |
| 2221 | |
| 2222 | func! TextChangedAutocmd(char) |
| 2223 | let g:autocmd .= a:char |
| 2224 | endfunc |
| 2225 | |
Christian Brabandt | db3b446 | 2021-10-16 11:58:55 +0100 | [diff] [blame] | 2226 | " TextChanged will not be triggered, only check that it isn't. |
Bram Moolenaar | 5a09343 | 2018-02-10 18:15:19 +0100 | [diff] [blame] | 2227 | au! TextChanged <buffer> :call TextChangedAutocmd('N') |
| 2228 | au! TextChangedI <buffer> :call TextChangedAutocmd('I') |
| 2229 | au! TextChangedP <buffer> :call TextChangedAutocmd('P') |
| 2230 | |
| 2231 | call cursor(3, 1) |
| 2232 | let g:autocmd = '' |
| 2233 | call feedkeys("o\<esc>", 'tnix') |
| 2234 | call assert_equal('I', g:autocmd) |
| 2235 | |
| 2236 | let g:autocmd = '' |
| 2237 | call feedkeys("Sf", 'tnix') |
| 2238 | call assert_equal('II', g:autocmd) |
| 2239 | |
| 2240 | let g:autocmd = '' |
| 2241 | call feedkeys("Sf\<C-N>", 'tnix') |
| 2242 | call assert_equal('IIP', g:autocmd) |
| 2243 | |
| 2244 | let g:autocmd = '' |
| 2245 | call feedkeys("Sf\<C-N>\<C-N>", 'tnix') |
| 2246 | call assert_equal('IIPP', g:autocmd) |
| 2247 | |
| 2248 | let g:autocmd = '' |
| 2249 | call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix') |
| 2250 | call assert_equal('IIPPP', g:autocmd) |
| 2251 | |
| 2252 | let g:autocmd = '' |
| 2253 | call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix') |
| 2254 | call assert_equal('IIPPPP', g:autocmd) |
| 2255 | |
| 2256 | call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$')) |
| 2257 | " TODO: how should it handle completeopt=noinsert,noselect? |
| 2258 | |
| 2259 | " CleanUp |
| 2260 | call test_override("char_avail", 0) |
| 2261 | au! TextChanged |
| 2262 | au! TextChangedI |
| 2263 | au! TextChangedP |
| 2264 | delfu TextChangedAutocmd |
| 2265 | unlet! g:autocmd |
| 2266 | set complete&vim completeopt&vim |
| 2267 | |
| 2268 | bw! |
| 2269 | endfunc |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2270 | |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2271 | let g:setline_handled = v:false |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 2272 | func SetLineOne() |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2273 | if !g:setline_handled |
| 2274 | call setline(1, "(x)") |
| 2275 | let g:setline_handled = v:true |
| 2276 | endif |
| 2277 | endfunc |
| 2278 | |
| 2279 | func Test_TextChangedI_with_setline() |
| 2280 | new |
| 2281 | call test_override('char_avail', 1) |
| 2282 | autocmd TextChangedI <buffer> call SetLineOne() |
| 2283 | call feedkeys("i(\<CR>\<Esc>", 'tx') |
| 2284 | call assert_equal('(', getline(1)) |
| 2285 | call assert_equal('x)', getline(2)) |
| 2286 | undo |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2287 | call assert_equal('', getline(1)) |
Bram Moolenaar | 9fa9506 | 2018-08-08 22:08:32 +0200 | [diff] [blame] | 2288 | call assert_equal('', getline(2)) |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2289 | |
Bram Moolenaar | ca34db3 | 2022-01-20 11:17:18 +0000 | [diff] [blame] | 2290 | call test_override('char_avail', 0) |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2291 | bwipe! |
| 2292 | endfunc |
| 2293 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2294 | func Test_Changed_FirstTime() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 2295 | CheckFeature terminal |
| 2296 | CheckNotGui |
Bram Moolenaar | 3cdcb09 | 2020-03-18 19:18:10 +0100 | [diff] [blame] | 2297 | " Starting a terminal to run Vim is always considered flaky. |
Bram Moolenaar | 30d53e2 | 2020-03-18 21:10:44 +0100 | [diff] [blame] | 2298 | let g:test_is_flaky = 1 |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 2299 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2300 | " Prepare file for TextChanged event. |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 2301 | call writefile([''], 'Xchanged.txt', 'D') |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2302 | let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3}) |
| 2303 | call assert_equal('running', term_getstatus(buf)) |
Bram Moolenaar | 1834d37 | 2018-03-29 17:40:46 +0200 | [diff] [blame] | 2304 | " Wait for the ruler (in the status line) to be shown. |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 2305 | " In ConPTY, there is additional character which is drawn up to the width of |
| 2306 | " the screen. |
| 2307 | if has('conpty') |
| 2308 | call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))}) |
| 2309 | else |
| 2310 | call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))}) |
| 2311 | endif |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2312 | " It's only adding autocmd, so that no event occurs. |
| 2313 | call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>") |
| 2314 | call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 2315 | call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2316 | call assert_equal([''], readfile('Xchanged.txt')) |
| 2317 | |
| 2318 | " clean up |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2319 | bwipe! |
| 2320 | endfunc |
Bram Moolenaar | 0566e89 | 2019-01-24 19:37:40 +0100 | [diff] [blame] | 2321 | |
Bram Moolenaar | eb93f3f | 2019-04-04 15:04:56 +0200 | [diff] [blame] | 2322 | func Test_autocmd_nested() |
| 2323 | let g:did_nested = 0 |
| 2324 | augroup Testing |
| 2325 | au WinNew * edit somefile |
| 2326 | au BufNew * let g:did_nested = 1 |
| 2327 | augroup END |
| 2328 | split |
| 2329 | call assert_equal(0, g:did_nested) |
| 2330 | close |
| 2331 | bwipe! somefile |
| 2332 | |
| 2333 | " old nested argument still works |
| 2334 | augroup Testing |
| 2335 | au! |
| 2336 | au WinNew * nested edit somefile |
| 2337 | au BufNew * let g:did_nested = 1 |
| 2338 | augroup END |
| 2339 | split |
| 2340 | call assert_equal(1, g:did_nested) |
| 2341 | close |
| 2342 | bwipe! somefile |
| 2343 | |
| 2344 | " New ++nested argument works |
| 2345 | augroup Testing |
| 2346 | au! |
| 2347 | au WinNew * ++nested edit somefile |
| 2348 | au BufNew * let g:did_nested = 1 |
| 2349 | augroup END |
| 2350 | split |
| 2351 | call assert_equal(1, g:did_nested) |
| 2352 | close |
| 2353 | bwipe! somefile |
| 2354 | |
Bram Moolenaar | f077514 | 2022-03-04 20:10:38 +0000 | [diff] [blame] | 2355 | " nested without ++ does not work in Vim9 script |
| 2356 | call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:') |
| 2357 | |
Bram Moolenaar | eb93f3f | 2019-04-04 15:04:56 +0200 | [diff] [blame] | 2358 | augroup Testing |
| 2359 | au! |
| 2360 | augroup END |
| 2361 | |
| 2362 | call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:') |
| 2363 | call assert_fails('au WinNew * nested nested echo bad', 'E983:') |
| 2364 | endfunc |
| 2365 | |
Bram Moolenaar | 5fa9f23 | 2022-07-23 09:06:48 +0100 | [diff] [blame] | 2366 | func Test_autocmd_nested_cursor_invalid() |
| 2367 | set laststatus=0 |
| 2368 | copen |
| 2369 | cclose |
| 2370 | call setline(1, ['foo', 'bar', 'baz']) |
| 2371 | 3 |
| 2372 | augroup nested_inv |
| 2373 | autocmd User foo ++nested copen |
| 2374 | autocmd BufAdd * let &laststatus = 2 - &laststatus |
| 2375 | augroup END |
| 2376 | doautocmd User foo |
| 2377 | |
| 2378 | augroup nested_inv |
| 2379 | au! |
| 2380 | augroup END |
| 2381 | set laststatus& |
Bram Moolenaar | b03950f | 2022-07-26 13:47:13 +0100 | [diff] [blame] | 2382 | cclose |
Bram Moolenaar | 5fa9f23 | 2022-07-23 09:06:48 +0100 | [diff] [blame] | 2383 | bwipe! |
| 2384 | endfunc |
| 2385 | |
Bram Moolenaar | 3d6ee8b | 2022-07-27 15:23:35 +0100 | [diff] [blame] | 2386 | func Test_autocmd_nested_keeps_cursor_pos() |
| 2387 | enew |
| 2388 | call setline(1, 'foo') |
| 2389 | autocmd User foo ++nested normal! $a |
| 2390 | autocmd InsertLeave * : |
| 2391 | doautocmd User foo |
| 2392 | call assert_equal([0, 1, 3, 0], getpos('.')) |
| 2393 | |
| 2394 | bwipe! |
| 2395 | endfunc |
| 2396 | |
Bram Moolenaar | b03950f | 2022-07-26 13:47:13 +0100 | [diff] [blame] | 2397 | func Test_autocmd_nested_switch_window() |
| 2398 | " run this in a separate Vim so that SafeState works |
| 2399 | CheckRunVimInTerminal |
| 2400 | |
| 2401 | let lines =<< trim END |
| 2402 | vim9script |
| 2403 | ['()']->writefile('Xautofile') |
| 2404 | autocmd VimEnter * ++nested edit Xautofile | split |
| 2405 | autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.') |
| 2406 | autocmd WinEnter * matchadd('ErrorMsg', 'pat') |
| 2407 | END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 2408 | call writefile(lines, 'Xautoscript', 'D') |
Bram Moolenaar | b03950f | 2022-07-26 13:47:13 +0100 | [diff] [blame] | 2409 | let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10}) |
| 2410 | call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {}) |
| 2411 | |
| 2412 | call StopVimInTerminal(buf) |
| 2413 | call delete('Xautofile') |
Bram Moolenaar | b03950f | 2022-07-26 13:47:13 +0100 | [diff] [blame] | 2414 | endfunc |
| 2415 | |
Bram Moolenaar | eb93f3f | 2019-04-04 15:04:56 +0200 | [diff] [blame] | 2416 | func Test_autocmd_once() |
| 2417 | " Without ++once WinNew triggers twice |
| 2418 | let g:did_split = 0 |
| 2419 | augroup Testing |
| 2420 | au WinNew * let g:did_split += 1 |
| 2421 | augroup END |
| 2422 | split |
| 2423 | split |
| 2424 | call assert_equal(2, g:did_split) |
| 2425 | call assert_true(exists('#WinNew')) |
| 2426 | close |
| 2427 | close |
| 2428 | |
| 2429 | " With ++once WinNew triggers once |
| 2430 | let g:did_split = 0 |
| 2431 | augroup Testing |
| 2432 | au! |
| 2433 | au WinNew * ++once let g:did_split += 1 |
| 2434 | augroup END |
| 2435 | split |
| 2436 | split |
| 2437 | call assert_equal(1, g:did_split) |
| 2438 | call assert_false(exists('#WinNew')) |
| 2439 | close |
| 2440 | close |
| 2441 | |
| 2442 | call assert_fails('au WinNew * ++once ++once echo bad', 'E983:') |
| 2443 | endfunc |
| 2444 | |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2445 | func Test_autocmd_bufreadpre() |
| 2446 | new |
| 2447 | let b:bufreadpre = 1 |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2448 | call append(0, range(1000)) |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2449 | w! XAutocmdBufReadPre.txt |
| 2450 | autocmd BufReadPre <buffer> :let b:bufreadpre += 1 |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2451 | norm! 500gg |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2452 | sp |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2453 | norm! 1000gg |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2454 | wincmd p |
| 2455 | let g:wsv1 = winsaveview() |
| 2456 | wincmd p |
| 2457 | let g:wsv2 = winsaveview() |
| 2458 | " triggers BufReadPre, should not move the cursor in either window |
| 2459 | " The topline may change one line in a large window. |
| 2460 | edit |
| 2461 | call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline) |
| 2462 | call assert_equal(g:wsv2.lnum, winsaveview().lnum) |
| 2463 | call assert_equal(2, b:bufreadpre) |
| 2464 | wincmd p |
| 2465 | call assert_equal(g:wsv1.topline, winsaveview().topline) |
| 2466 | call assert_equal(g:wsv1.lnum, winsaveview().lnum) |
| 2467 | call assert_equal(2, b:bufreadpre) |
| 2468 | " Now set the cursor position in an BufReadPre autocommand |
| 2469 | " (even though the position will be invalid, this should make Vim reset the |
| 2470 | " cursor position in the other window. |
| 2471 | wincmd p |
| 2472 | set cpo+=g |
| 2473 | " won't do anything, but try to set the cursor on an invalid lnum |
| 2474 | autocmd BufReadPre <buffer> :norm! 70gg |
| 2475 | " triggers BufReadPre, should not move the cursor in either window |
| 2476 | e |
| 2477 | call assert_equal(1, winsaveview().topline) |
| 2478 | call assert_equal(1, winsaveview().lnum) |
| 2479 | call assert_equal(3, b:bufreadpre) |
| 2480 | wincmd p |
| 2481 | call assert_equal(g:wsv1.topline, winsaveview().topline) |
| 2482 | call assert_equal(g:wsv1.lnum, winsaveview().lnum) |
| 2483 | call assert_equal(3, b:bufreadpre) |
| 2484 | close |
| 2485 | close |
| 2486 | call delete('XAutocmdBufReadPre.txt') |
| 2487 | set cpo-=g |
| 2488 | endfunc |
| 2489 | |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 2490 | " FileChangedShell tested in test_filechanged.vim |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2491 | |
| 2492 | " Tests for the following autocommands: |
| 2493 | " - FileWritePre writing a compressed file |
| 2494 | " - FileReadPost reading a compressed file |
| 2495 | " - BufNewFile reading a file template |
| 2496 | " - BufReadPre decompressing the file to be read |
| 2497 | " - FilterReadPre substituting characters in the temp file |
| 2498 | " - FilterReadPost substituting characters after filtering |
| 2499 | " - FileReadPre set options for decompression |
| 2500 | " - FileReadPost decompress the file |
| 2501 | func Test_ReadWrite_Autocmds() |
| 2502 | " Run this test only on Unix-like systems and if gzip is available |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 2503 | CheckUnix |
| 2504 | CheckExecutable gzip |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2505 | |
| 2506 | " Make $GZIP empty, "-v" would cause trouble. |
| 2507 | let $GZIP = "" |
| 2508 | |
| 2509 | " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz' |
| 2510 | " being modified outside of Vim (noticed on Solaris). |
| 2511 | au FileChangedShell * echo 'caught FileChangedShell' |
| 2512 | |
| 2513 | " Test for the FileReadPost, FileWritePre and FileWritePost autocmds |
| 2514 | augroup Test1 |
| 2515 | au! |
| 2516 | au FileWritePre *.gz '[,']!gzip |
| 2517 | au FileWritePost *.gz undo |
| 2518 | au FileReadPost *.gz '[,']!gzip -d |
| 2519 | augroup END |
| 2520 | |
| 2521 | new |
| 2522 | set bin |
| 2523 | call append(0, [ |
| 2524 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2525 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2526 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2527 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2528 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2529 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2530 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2531 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2532 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2533 | \ ]) |
| 2534 | 1,9write! Xtestfile.gz |
| 2535 | enew! | close |
| 2536 | |
| 2537 | new |
| 2538 | " Read and decompress the testfile |
| 2539 | 0read Xtestfile.gz |
| 2540 | call assert_equal([ |
| 2541 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2542 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2543 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2544 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2545 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2546 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2547 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2548 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2549 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2550 | \ ], getline(1, 9)) |
| 2551 | enew! | close |
| 2552 | |
| 2553 | augroup Test1 |
| 2554 | au! |
| 2555 | augroup END |
| 2556 | |
| 2557 | " Test for the FileAppendPre and FileAppendPost autocmds |
| 2558 | augroup Test2 |
| 2559 | au! |
| 2560 | au BufNewFile *.c read Xtest.c |
| 2561 | au FileAppendPre *.out '[,']s/new/NEW/ |
| 2562 | au FileAppendPost *.out !cat Xtest.c >> test.out |
| 2563 | augroup END |
| 2564 | |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 2565 | call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D') |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2566 | new foo.c " should load Xtest.c |
| 2567 | call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4)) |
| 2568 | w! >> test.out " append it to the output file |
| 2569 | |
| 2570 | let contents = readfile('test.out') |
| 2571 | call assert_equal(' * Here is a NEW .c file', contents[2]) |
| 2572 | call assert_equal(' * Here is a new .c file', contents[5]) |
| 2573 | |
| 2574 | call delete('test.out') |
| 2575 | enew! | close |
| 2576 | augroup Test2 |
| 2577 | au! |
| 2578 | augroup END |
| 2579 | |
| 2580 | " Test for the BufReadPre and BufReadPost autocmds |
| 2581 | augroup Test3 |
| 2582 | au! |
| 2583 | " setup autocommands to decompress before reading and re-compress |
| 2584 | " afterwards |
| 2585 | au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>")) |
| 2586 | au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) |
| 2587 | au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r")) |
| 2588 | au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r")) |
| 2589 | augroup END |
| 2590 | |
| 2591 | e! Xtestfile.gz " Edit compressed file |
| 2592 | call assert_equal([ |
| 2593 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2594 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2595 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2596 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2597 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2598 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2599 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2600 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2601 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2602 | \ ], getline(1, 9)) |
| 2603 | |
| 2604 | w! >> test.out " Append it to the output file |
| 2605 | |
| 2606 | augroup Test3 |
| 2607 | au! |
| 2608 | augroup END |
| 2609 | |
| 2610 | " Test for the FilterReadPre and FilterReadPost autocmds. |
| 2611 | set shelltemp " need temp files here |
| 2612 | augroup Test4 |
| 2613 | au! |
| 2614 | au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t") |
| 2615 | au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>")) |
| 2616 | au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t' |
| 2617 | au FilterReadPost *.out '[,']s/x/X/g |
| 2618 | augroup END |
| 2619 | |
| 2620 | e! test.out " Edit the output file |
| 2621 | 1,$!cat |
| 2622 | call assert_equal([ |
| 2623 | \ 'linE 2 AbcdefghijklmnopqrstuvwXyz', |
| 2624 | \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2625 | \ 'linE 4 AbcdefghijklmnopqrstuvwXyz', |
| 2626 | \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2627 | \ 'linE 6 AbcdefghijklmnopqrstuvwXyz', |
| 2628 | \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2629 | \ 'linE 8 AbcdefghijklmnopqrstuvwXyz', |
| 2630 | \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2631 | \ 'linE 10 AbcdefghijklmnopqrstuvwXyz' |
| 2632 | \ ], getline(1, 9)) |
| 2633 | call assert_equal([ |
| 2634 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2635 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2636 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2637 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2638 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2639 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2640 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2641 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2642 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2643 | \ ], readfile('test.out')) |
| 2644 | |
| 2645 | augroup Test4 |
| 2646 | au! |
| 2647 | augroup END |
| 2648 | set shelltemp&vim |
| 2649 | |
| 2650 | " Test for the FileReadPre and FileReadPost autocmds. |
| 2651 | augroup Test5 |
| 2652 | au! |
| 2653 | au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>")) |
| 2654 | au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) |
| 2655 | au FileReadPost *.gz '[,']s/l/L/ |
| 2656 | augroup END |
| 2657 | |
| 2658 | new |
| 2659 | 0r Xtestfile.gz " Read compressed file |
| 2660 | call assert_equal([ |
| 2661 | \ 'Line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2662 | \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2663 | \ 'Line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2664 | \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2665 | \ 'Line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2666 | \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2667 | \ 'Line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2668 | \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2669 | \ 'Line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2670 | \ ], getline(1, 9)) |
| 2671 | call assert_equal([ |
| 2672 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2673 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2674 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2675 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2676 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2677 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2678 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2679 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2680 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2681 | \ ], readfile('Xtestfile.gz')) |
| 2682 | |
| 2683 | augroup Test5 |
| 2684 | au! |
| 2685 | augroup END |
| 2686 | |
| 2687 | au! FileChangedShell |
| 2688 | call delete('Xtestfile.gz') |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2689 | call delete('test.out') |
| 2690 | endfunc |
Bram Moolenaar | 23b5139 | 2019-05-09 21:38:43 +0200 | [diff] [blame] | 2691 | |
| 2692 | func Test_throw_in_BufWritePre() |
| 2693 | new |
| 2694 | call setline(1, ['one', 'two', 'three']) |
| 2695 | call assert_false(filereadable('Xthefile')) |
| 2696 | augroup throwing |
| 2697 | au BufWritePre X* throw 'do not write' |
| 2698 | augroup END |
| 2699 | try |
| 2700 | w Xthefile |
| 2701 | catch |
| 2702 | let caught = 1 |
| 2703 | endtry |
| 2704 | call assert_equal(1, caught) |
| 2705 | call assert_false(filereadable('Xthefile')) |
| 2706 | |
| 2707 | bwipe! |
| 2708 | au! throwing |
| 2709 | endfunc |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2710 | |
Bram Moolenaar | 40fa12a | 2021-09-22 14:18:13 +0200 | [diff] [blame] | 2711 | func Test_autocmd_in_try_block() |
Bram Moolenaar | 6f14da1 | 2022-09-07 21:30:44 +0100 | [diff] [blame] | 2712 | call mkdir('Xintrydir', 'R') |
Bram Moolenaar | 40fa12a | 2021-09-22 14:18:13 +0200 | [diff] [blame] | 2713 | au BufEnter * let g:fname = expand('%') |
| 2714 | try |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2715 | edit Xintrydir/ |
Bram Moolenaar | 40fa12a | 2021-09-22 14:18:13 +0200 | [diff] [blame] | 2716 | endtry |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2717 | call assert_match('Xintrydir', g:fname) |
Bram Moolenaar | 40fa12a | 2021-09-22 14:18:13 +0200 | [diff] [blame] | 2718 | |
| 2719 | unlet g:fname |
| 2720 | au! BufEnter |
Bram Moolenaar | 40fa12a | 2021-09-22 14:18:13 +0200 | [diff] [blame] | 2721 | endfunc |
| 2722 | |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2723 | func Test_autocmd_SafeState() |
| 2724 | CheckRunVimInTerminal |
| 2725 | |
| 2726 | let lines =<< trim END |
| 2727 | let g:safe = 0 |
| 2728 | let g:again = '' |
| 2729 | au SafeState * let g:safe += 1 |
| 2730 | au SafeStateAgain * let g:again ..= 'x' |
| 2731 | func CallTimer() |
| 2732 | call timer_start(10, {id -> execute('let g:again ..= "t"')}) |
| 2733 | endfunc |
| 2734 | END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 2735 | call writefile(lines, 'XSafeState', 'D') |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2736 | let buf = RunVimInTerminal('-S XSafeState', #{rows: 6}) |
| 2737 | |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 2738 | " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2739 | " more often. |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2740 | call term_sendkeys(buf, ":echo g:safe\<CR>") |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2741 | call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2742 | |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2743 | " SafeStateAgain should be invoked at least three times |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2744 | call term_sendkeys(buf, ":echo g:again\<CR>") |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2745 | call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2746 | |
| 2747 | call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2748 | call TermWait(buf, 50) |
Bram Moolenaar | 0f6629a | 2019-09-22 23:24:13 +0200 | [diff] [blame] | 2749 | call term_sendkeys(buf, ":\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2750 | call TermWait(buf, 50) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2751 | call term_sendkeys(buf, ":echo g:again\<CR>") |
| 2752 | call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000) |
| 2753 | |
| 2754 | call StopVimInTerminal(buf) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2755 | endfunc |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2756 | |
| 2757 | func Test_autocmd_CmdWinEnter() |
| 2758 | CheckRunVimInTerminal |
Bram Moolenaar | 21829c5 | 2021-01-26 22:42:21 +0100 | [diff] [blame] | 2759 | CheckFeature cmdwin |
| 2760 | |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2761 | let lines =<< trim END |
Egor Zvorykin | 125ffd2 | 2021-11-17 14:01:14 +0000 | [diff] [blame] | 2762 | augroup vimHints | au! | augroup END |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2763 | let b:dummy_var = 'This is a dummy' |
| 2764 | autocmd CmdWinEnter * quit |
| 2765 | let winnr = winnr('$') |
| 2766 | END |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 2767 | let filename = 'XCmdWinEnter' |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2768 | call writefile(lines, filename) |
| 2769 | let buf = RunVimInTerminal('-S '.filename, #{rows: 6}) |
| 2770 | |
| 2771 | call term_sendkeys(buf, "q:") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2772 | call TermWait(buf) |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2773 | call term_sendkeys(buf, ":echo b:dummy_var\<cr>") |
Bram Moolenaar | 353c351 | 2020-03-15 14:19:26 +0100 | [diff] [blame] | 2774 | call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000) |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2775 | call term_sendkeys(buf, ":echo &buftype\<cr>") |
| 2776 | call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000) |
| 2777 | call term_sendkeys(buf, ":echo winnr\<cr>") |
| 2778 | call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000) |
| 2779 | |
| 2780 | " clean up |
| 2781 | call StopVimInTerminal(buf) |
| 2782 | call delete(filename) |
| 2783 | endfunc |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2784 | |
| 2785 | func Test_autocmd_was_using_freed_memory() |
Bram Moolenaar | 5a4c308 | 2019-12-01 15:23:11 +0100 | [diff] [blame] | 2786 | CheckFeature quickfix |
| 2787 | |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2788 | pedit xx |
| 2789 | n x |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 2790 | augroup winenter |
| 2791 | au WinEnter * if winnr('$') > 2 | quit | endif |
| 2792 | augroup END |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2793 | split |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 2794 | |
| 2795 | augroup winenter |
| 2796 | au! WinEnter |
| 2797 | augroup END |
| 2798 | |
| 2799 | bwipe xx |
| 2800 | bwipe x |
| 2801 | pclose |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2802 | endfunc |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 2803 | |
| 2804 | func Test_BufWrite_lockmarks() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 2805 | let g:test_is_flaky = 1 |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 2806 | edit! Xtest |
| 2807 | call setline(1, ['a', 'b', 'c', 'd']) |
| 2808 | |
| 2809 | " :lockmarks preserves the marks |
| 2810 | call SetChangeMarks(2, 3) |
| 2811 | lockmarks write |
| 2812 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2813 | |
| 2814 | " *WritePre autocmds get the correct line range, but lockmarks preserves the |
| 2815 | " original values for the user |
| 2816 | augroup lockmarks |
| 2817 | au! |
| 2818 | au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 2819 | au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")]) |
| 2820 | augroup END |
| 2821 | |
| 2822 | lockmarks write |
| 2823 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2824 | |
| 2825 | if executable('cat') |
| 2826 | lockmarks %!cat |
| 2827 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2828 | endif |
| 2829 | |
| 2830 | lockmarks 3,4write Xtest2 |
| 2831 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2832 | |
| 2833 | au! lockmarks |
| 2834 | augroup! lockmarks |
| 2835 | call delete('Xtest') |
| 2836 | call delete('Xtest2') |
| 2837 | endfunc |
Bram Moolenaar | ce6db02 | 2020-01-07 20:11:42 +0100 | [diff] [blame] | 2838 | |
| 2839 | func Test_FileType_spell() |
| 2840 | if !isdirectory('/tmp') |
| 2841 | throw "Skipped: requires /tmp directory" |
| 2842 | endif |
| 2843 | |
| 2844 | " this was crashing with an invalid free() |
| 2845 | setglobal spellfile=/tmp/en.utf-8.add |
| 2846 | augroup crash |
| 2847 | autocmd! |
| 2848 | autocmd BufNewFile,BufReadPost crashfile setf somefiletype |
| 2849 | autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype |
| 2850 | autocmd FileType anotherfiletype setlocal spell |
| 2851 | augroup END |
| 2852 | func! NoCrash() abort |
| 2853 | edit /tmp/crashfile |
| 2854 | endfunc |
| 2855 | call NoCrash() |
| 2856 | |
| 2857 | au! crash |
| 2858 | setglobal spellfile= |
| 2859 | endfunc |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 2860 | |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2861 | " Test closing a window or editing another buffer from a FileChangedRO handler |
| 2862 | " in a readonly buffer |
| 2863 | func Test_FileChangedRO_winclose() |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2864 | call test_override('ui_delay', 10) |
| 2865 | |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2866 | augroup FileChangedROTest |
| 2867 | au! |
| 2868 | autocmd FileChangedRO * quit |
| 2869 | augroup END |
| 2870 | new |
| 2871 | set readonly |
| 2872 | call assert_fails('normal i', 'E788:') |
| 2873 | close |
| 2874 | augroup! FileChangedROTest |
| 2875 | |
| 2876 | augroup FileChangedROTest |
| 2877 | au! |
Bram Moolenaar | 61abe7d | 2022-08-30 21:46:08 +0100 | [diff] [blame] | 2878 | autocmd FileChangedRO * edit Xrofile |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2879 | augroup END |
| 2880 | new |
| 2881 | set readonly |
| 2882 | call assert_fails('normal i', 'E788:') |
| 2883 | close |
| 2884 | augroup! FileChangedROTest |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2885 | call test_override('ALL', 0) |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2886 | endfunc |
| 2887 | |
Bram Moolenaar | 0c81d1b | 2020-02-22 22:45:55 +0100 | [diff] [blame] | 2888 | func LogACmd() |
| 2889 | call add(g:logged, line('$')) |
| 2890 | endfunc |
| 2891 | |
| 2892 | func Test_TermChanged() |
Bram Moolenaar | d28e0b3 | 2020-02-22 23:08:52 +0100 | [diff] [blame] | 2893 | CheckNotGui |
| 2894 | |
Bram Moolenaar | 0c81d1b | 2020-02-22 22:45:55 +0100 | [diff] [blame] | 2895 | enew! |
| 2896 | tabnew |
| 2897 | call setline(1, ['a', 'b', 'c', 'd']) |
| 2898 | $ |
| 2899 | au TermChanged * call LogACmd() |
| 2900 | let g:logged = [] |
| 2901 | let term_save = &term |
| 2902 | set term=xterm |
| 2903 | call assert_equal([1, 4], g:logged) |
| 2904 | |
| 2905 | au! TermChanged |
| 2906 | let &term = term_save |
| 2907 | bwipe! |
| 2908 | endfunc |
| 2909 | |
Bram Moolenaar | e328487 | 2020-03-19 13:55:03 +0100 | [diff] [blame] | 2910 | " Test for FileReadCmd autocmd |
| 2911 | func Test_autocmd_FileReadCmd() |
| 2912 | func ReadFileCmd() |
| 2913 | call append(line('$'), "v:cmdarg = " .. v:cmdarg) |
| 2914 | endfunc |
| 2915 | augroup FileReadCmdTest |
| 2916 | au! |
| 2917 | au FileReadCmd Xtest call ReadFileCmd() |
| 2918 | augroup END |
| 2919 | |
| 2920 | new |
| 2921 | read ++bin Xtest |
| 2922 | read ++nobin Xtest |
| 2923 | read ++edit Xtest |
| 2924 | read ++bad=keep Xtest |
| 2925 | read ++bad=drop Xtest |
| 2926 | read ++bad=- Xtest |
| 2927 | read ++ff=unix Xtest |
| 2928 | read ++ff=dos Xtest |
| 2929 | read ++ff=mac Xtest |
| 2930 | read ++enc=utf-8 Xtest |
| 2931 | |
| 2932 | call assert_equal(['', |
| 2933 | \ 'v:cmdarg = ++bin', |
| 2934 | \ 'v:cmdarg = ++nobin', |
| 2935 | \ 'v:cmdarg = ++edit', |
| 2936 | \ 'v:cmdarg = ++bad=keep', |
| 2937 | \ 'v:cmdarg = ++bad=drop', |
| 2938 | \ 'v:cmdarg = ++bad=-', |
| 2939 | \ 'v:cmdarg = ++ff=unix', |
| 2940 | \ 'v:cmdarg = ++ff=dos', |
| 2941 | \ 'v:cmdarg = ++ff=mac', |
| 2942 | \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$')) |
| 2943 | |
| 2944 | close! |
| 2945 | augroup FileReadCmdTest |
| 2946 | au! |
| 2947 | augroup END |
| 2948 | delfunc ReadFileCmd |
| 2949 | endfunc |
| 2950 | |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2951 | " Test for passing invalid arguments to autocmd |
| 2952 | func Test_autocmd_invalid_args() |
| 2953 | " Additional character after * for event |
Bram Moolenaar | 61abe7d | 2022-08-30 21:46:08 +0100 | [diff] [blame] | 2954 | call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2955 | augroup Test |
| 2956 | augroup END |
| 2957 | " Invalid autocmd event |
Bram Moolenaar | 61abe7d | 2022-08-30 21:46:08 +0100 | [diff] [blame] | 2958 | call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2959 | " Invalid autocmd event in a autocmd group |
Bram Moolenaar | 61abe7d | 2022-08-30 21:46:08 +0100 | [diff] [blame] | 2960 | call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2961 | augroup! Test |
| 2962 | " Execute all autocmds |
| 2963 | call assert_fails('doautocmd * BufEnter', 'E217:') |
| 2964 | call assert_fails('augroup! x1a2b3', 'E367:') |
| 2965 | call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:') |
Bram Moolenaar | 531be47 | 2020-09-23 22:38:05 +0200 | [diff] [blame] | 2966 | call assert_fails('autocmd BufNew \) set ff=unix', 'E55:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2967 | endfunc |
| 2968 | |
| 2969 | " Test for deep nesting of autocmds |
| 2970 | func Test_autocmd_deep_nesting() |
Bram Moolenaar | 61abe7d | 2022-08-30 21:46:08 +0100 | [diff] [blame] | 2971 | autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile |
| 2972 | call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:') |
| 2973 | autocmd! BufEnter Xdeepfile |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2974 | endfunc |
| 2975 | |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 2976 | " Tests for SigUSR1 autocmd event, which is only available on posix systems. |
| 2977 | func Test_autocmd_sigusr1() |
| 2978 | CheckUnix |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2979 | CheckExecutable /bin/kill |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 2980 | |
| 2981 | let g:sigusr1_passed = 0 |
| 2982 | au SigUSR1 * let g:sigusr1_passed = 1 |
| 2983 | call system('/bin/kill -s usr1 ' . getpid()) |
| 2984 | call WaitForAssert({-> assert_true(g:sigusr1_passed)}) |
| 2985 | |
| 2986 | au! SigUSR1 |
| 2987 | unlet g:sigusr1_passed |
| 2988 | endfunc |
| 2989 | |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 2990 | " Test for BufReadPre autocmd deleting the file |
| 2991 | func Test_BufReadPre_delfile() |
| 2992 | augroup TestAuCmd |
| 2993 | au! |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 2994 | autocmd BufReadPre XbufreadPre call delete('XbufreadPre') |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 2995 | augroup END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 2996 | call writefile([], 'XbufreadPre', 'D') |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 2997 | call assert_fails('new XbufreadPre', 'E200:') |
| 2998 | call assert_equal('XbufreadPre', @%) |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 2999 | call assert_equal(1, &readonly) |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 3000 | |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 3001 | augroup TestAuCmd |
| 3002 | au! |
| 3003 | augroup END |
| 3004 | close! |
| 3005 | endfunc |
| 3006 | |
| 3007 | " Test for BufReadPre autocmd changing the current buffer |
| 3008 | func Test_BufReadPre_changebuf() |
| 3009 | augroup TestAuCmd |
| 3010 | au! |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 3011 | autocmd BufReadPre Xchangebuf edit Xsomeotherfile |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 3012 | augroup END |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 3013 | call writefile([], 'Xchangebuf', 'D') |
Bram Moolenaar | e7cda97 | 2022-08-29 11:02:59 +0100 | [diff] [blame] | 3014 | call assert_fails('new Xchangebuf', 'E201:') |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 3015 | call assert_equal('Xsomeotherfile', @%) |
| 3016 | call assert_equal(1, &readonly) |
Bram Moolenaar | e1f3ab7 | 2022-09-04 21:29:08 +0100 | [diff] [blame] | 3017 | |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 3018 | augroup TestAuCmd |
| 3019 | au! |
| 3020 | augroup END |
| 3021 | close! |
| 3022 | endfunc |
| 3023 | |
| 3024 | " Test for BufWipeouti autocmd changing the current buffer when reading a file |
| 3025 | " in an empty buffer with 'f' flag in 'cpo' |
| 3026 | func Test_BufDelete_changebuf() |
| 3027 | new |
| 3028 | augroup TestAuCmd |
| 3029 | au! |
| 3030 | autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr |
| 3031 | augroup END |
| 3032 | let save_cpo = &cpo |
| 3033 | set cpo+=f |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 3034 | call assert_fails('r Xchangebuf', ['E812:', 'E484:']) |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 3035 | call assert_equal('somefile', @%) |
| 3036 | let &cpo = save_cpo |
| 3037 | augroup TestAuCmd |
| 3038 | au! |
| 3039 | augroup END |
| 3040 | close! |
| 3041 | endfunc |
| 3042 | |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 3043 | " Test for the temporary internal window used to execute autocmds |
| 3044 | func Test_autocmd_window() |
| 3045 | %bw! |
| 3046 | edit one.txt |
| 3047 | tabnew two.txt |
Bram Moolenaar | 41cd803 | 2021-03-13 15:47:56 +0100 | [diff] [blame] | 3048 | vnew three.txt |
| 3049 | tabnew four.txt |
| 3050 | tabprevious |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 3051 | let g:blist = [] |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 3052 | augroup aucmd_win_test1 |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 3053 | au! |
| 3054 | au BufEnter * call add(g:blist, [expand('<afile>'), |
| 3055 | \ win_gettype(bufwinnr(expand('<afile>')))]) |
| 3056 | augroup END |
| 3057 | |
| 3058 | doautoall BufEnter |
Bram Moolenaar | 41cd803 | 2021-03-13 15:47:56 +0100 | [diff] [blame] | 3059 | call assert_equal([ |
| 3060 | \ ['one.txt', 'autocmd'], |
| 3061 | \ ['two.txt', ''], |
| 3062 | \ ['four.txt', 'autocmd'], |
| 3063 | \ ['three.txt', ''], |
| 3064 | \ ], g:blist) |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 3065 | |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 3066 | augroup aucmd_win_test1 |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 3067 | au! |
| 3068 | augroup END |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 3069 | augroup! aucmd_win_test1 |
| 3070 | %bw! |
| 3071 | endfunc |
| 3072 | |
| 3073 | " Test for trying to close the temporary window used for executing an autocmd |
| 3074 | func Test_close_autocmd_window() |
| 3075 | %bw! |
| 3076 | edit one.txt |
| 3077 | tabnew two.txt |
| 3078 | augroup aucmd_win_test2 |
| 3079 | au! |
| 3080 | au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif |
| 3081 | augroup END |
| 3082 | |
| 3083 | call assert_fails('doautoall BufEnter', 'E813:') |
| 3084 | |
| 3085 | augroup aucmd_win_test2 |
| 3086 | au! |
| 3087 | augroup END |
| 3088 | augroup! aucmd_win_test2 |
Bram Moolenaar | cf84417 | 2020-06-26 19:44:06 +0200 | [diff] [blame] | 3089 | %bwipe! |
| 3090 | endfunc |
| 3091 | |
| 3092 | " Test for trying to close the tab that has the temporary window for exeucing |
| 3093 | " an autocmd. |
| 3094 | func Test_close_autocmd_tab() |
| 3095 | edit one.txt |
| 3096 | tabnew two.txt |
| 3097 | augroup aucmd_win_test |
| 3098 | au! |
| 3099 | au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif |
| 3100 | augroup END |
| 3101 | |
| 3102 | call assert_fails('doautoall BufEnter', 'E813:') |
| 3103 | |
| 3104 | tabonly |
| 3105 | augroup aucmd_win_test |
| 3106 | au! |
| 3107 | augroup END |
| 3108 | augroup! aucmd_win_test |
| 3109 | %bwipe! |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 3110 | endfunc |
| 3111 | |
Bram Moolenaar | cb1956d | 2022-01-07 15:45:18 +0000 | [diff] [blame] | 3112 | func Test_Visual_doautoall_redraw() |
| 3113 | call setline(1, ['a', 'b']) |
| 3114 | new |
| 3115 | wincmd p |
| 3116 | call feedkeys("G\<C-V>", 'txn') |
| 3117 | autocmd User Explode ++once redraw |
| 3118 | doautoall User Explode |
| 3119 | %bwipe! |
| 3120 | endfunc |
| 3121 | |
Bram Moolenaar | 6bcb877 | 2021-02-03 21:23:29 +0100 | [diff] [blame] | 3122 | " This was using freed memory. |
| 3123 | func Test_BufNew_arglocal() |
| 3124 | arglocal |
| 3125 | au BufNew * arglocal |
| 3126 | call assert_fails('drop xx', 'E1156:') |
| 3127 | |
| 3128 | au! BufNew |
| 3129 | endfunc |
| 3130 | |
Bram Moolenaar | 8ab3757 | 2021-02-03 21:56:59 +0100 | [diff] [blame] | 3131 | func Test_autocmd_closes_window() |
| 3132 | au BufNew,BufWinLeave * e %e |
| 3133 | file yyy |
| 3134 | au BufNew,BufWinLeave * ball |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3135 | n xxx |
Bram Moolenaar | 8ab3757 | 2021-02-03 21:56:59 +0100 | [diff] [blame] | 3136 | |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3137 | %bwipe |
Bram Moolenaar | 8ab3757 | 2021-02-03 21:56:59 +0100 | [diff] [blame] | 3138 | au! BufNew |
| 3139 | au! BufWinLeave |
| 3140 | endfunc |
| 3141 | |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 3142 | func Test_autocmd_quit_psearch() |
| 3143 | sn aa bb |
| 3144 | augroup aucmd_win_test |
| 3145 | au! |
| 3146 | au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif |
| 3147 | augroup END |
| 3148 | ps / |
| 3149 | |
| 3150 | augroup aucmd_win_test |
| 3151 | au! |
| 3152 | augroup END |
zeertzjq | 7851c69 | 2022-04-21 11:14:01 +0100 | [diff] [blame] | 3153 | new |
| 3154 | pclose |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 3155 | endfunc |
| 3156 | |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3157 | " Fuzzer found some strange combination that caused a crash. |
| 3158 | func Test_autocmd_normal_mess() |
Bram Moolenaar | dd07c02 | 2021-02-07 13:32:46 +0100 | [diff] [blame] | 3159 | " For unknown reason this hangs on MS-Windows |
| 3160 | CheckNotMSWindows |
| 3161 | |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3162 | augroup aucmd_normal_test |
| 3163 | au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc |
| 3164 | augroup END |
Bram Moolenaar | 983d83f | 2021-02-07 12:12:43 +0100 | [diff] [blame] | 3165 | call assert_fails('o4', 'E1159') |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3166 | silent! H |
Bram Moolenaar | 983d83f | 2021-02-07 12:12:43 +0100 | [diff] [blame] | 3167 | call assert_fails('e xx', 'E1159') |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3168 | normal G |
| 3169 | |
| 3170 | augroup aucmd_normal_test |
| 3171 | au! |
| 3172 | augroup END |
| 3173 | endfunc |
| 3174 | |
Bram Moolenaar | 8c6951f | 2021-02-06 18:08:45 +0100 | [diff] [blame] | 3175 | func Test_autocmd_closing_cmdwin() |
Bram Moolenaar | dd07c02 | 2021-02-07 13:32:46 +0100 | [diff] [blame] | 3176 | " For unknown reason this hangs on MS-Windows |
| 3177 | CheckNotMSWindows |
| 3178 | |
Bram Moolenaar | 8c6951f | 2021-02-06 18:08:45 +0100 | [diff] [blame] | 3179 | au BufWinLeave * nested q |
| 3180 | call assert_fails("norm 7q?\n", 'E855:') |
| 3181 | |
| 3182 | au! BufWinLeave |
| 3183 | new |
| 3184 | only |
| 3185 | endfunc |
| 3186 | |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 3187 | func Test_autocmd_vimgrep() |
| 3188 | augroup aucmd_vimgrep |
Charlie Groves | fef4485 | 2022-04-19 16:24:12 +0100 | [diff] [blame] | 3189 | au QuickfixCmdPre,BufNew,BufReadCmd * sb |
zeertzjq | 7851c69 | 2022-04-21 11:14:01 +0100 | [diff] [blame] | 3190 | au QuickfixCmdPre,BufNew,BufReadCmd * q9 |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 3191 | augroup END |
Bram Moolenaar | dd07c02 | 2021-02-07 13:32:46 +0100 | [diff] [blame] | 3192 | call assert_fails('lv ?a? foo', 'E926:') |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 3193 | |
| 3194 | augroup aucmd_vimgrep |
| 3195 | au! |
| 3196 | augroup END |
| 3197 | endfunc |
| 3198 | |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 3199 | func Test_autocmd_with_block() |
| 3200 | augroup block_testing |
| 3201 | au BufReadPost *.xml { |
| 3202 | setlocal matchpairs+=<:> |
| 3203 | /<start |
| 3204 | } |
Bram Moolenaar | 63b9173 | 2021-08-05 20:40:03 +0200 | [diff] [blame] | 3205 | au CursorHold * { |
| 3206 | autocmd BufReadPre * ++once echo 'one' | echo 'two' |
| 3207 | g:gotSafeState = 77 |
| 3208 | } |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 3209 | augroup END |
| 3210 | |
| 3211 | let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }" |
| 3212 | call assert_equal(expected, execute('au BufReadPost *.xml')) |
| 3213 | |
Bram Moolenaar | 63b9173 | 2021-08-05 20:40:03 +0200 | [diff] [blame] | 3214 | doautocmd CursorHold |
| 3215 | call assert_equal(77, g:gotSafeState) |
| 3216 | unlet g:gotSafeState |
| 3217 | |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 3218 | augroup block_testing |
| 3219 | au! |
Bram Moolenaar | 75ebd2a | 2022-06-03 17:39:46 +0100 | [diff] [blame] | 3220 | autocmd CursorHold * { |
| 3221 | if true |
| 3222 | # comment |
| 3223 | && true |
| 3224 | |
| 3225 | && true |
| 3226 | g:done = 'yes' |
| 3227 | endif |
| 3228 | } |
| 3229 | augroup END |
| 3230 | doautocmd CursorHold |
| 3231 | call assert_equal('yes', g:done) |
| 3232 | |
| 3233 | unlet g:done |
| 3234 | augroup block_testing |
| 3235 | au! |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 3236 | augroup END |
| 3237 | endfunc |
| 3238 | |
Christian Brabandt | db3b446 | 2021-10-16 11:58:55 +0100 | [diff] [blame] | 3239 | " Test TextChangedI and TextChanged |
| 3240 | func Test_Changed_ChangedI() |
| 3241 | new |
| 3242 | call test_override("char_avail", 1) |
| 3243 | let [g:autocmd_i, g:autocmd_n] = ['',''] |
| 3244 | |
| 3245 | func! TextChangedAutocmdI(char) |
| 3246 | let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick |
| 3247 | endfunc |
| 3248 | |
| 3249 | augroup Test_TextChanged |
| 3250 | au! |
| 3251 | au TextChanged <buffer> :call TextChangedAutocmdI('N') |
| 3252 | au TextChangedI <buffer> :call TextChangedAutocmdI('I') |
| 3253 | augroup END |
| 3254 | |
| 3255 | call feedkeys("ifoo\<esc>", 'tnix') |
| 3256 | " TODO: Test test does not seem to trigger TextChanged autocommand, this |
| 3257 | " requires running Vim in a terminal window. |
| 3258 | " call assert_equal('N3', g:autocmd_n) |
| 3259 | call assert_equal('I3', g:autocmd_i) |
| 3260 | |
| 3261 | call feedkeys("yyp", 'tnix') |
| 3262 | " TODO: Test test does not seem to trigger TextChanged autocommand. |
| 3263 | " call assert_equal('N4', g:autocmd_n) |
| 3264 | call assert_equal('I3', g:autocmd_i) |
| 3265 | |
| 3266 | " CleanUp |
| 3267 | call test_override("char_avail", 0) |
| 3268 | au! TextChanged <buffer> |
| 3269 | au! TextChangedI <buffer> |
| 3270 | augroup! Test_TextChanged |
| 3271 | delfu TextChangedAutocmdI |
| 3272 | unlet! g:autocmd_i g:autocmd_n |
| 3273 | |
| 3274 | bw! |
| 3275 | endfunc |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 3276 | |
Bram Moolenaar | 6f2465d | 2022-03-22 18:13:01 +0000 | [diff] [blame] | 3277 | func Test_closing_autocmd_window() |
| 3278 | let lines =<< trim END |
| 3279 | edit Xa.txt |
| 3280 | tabnew Xb.txt |
| 3281 | autocmd BufEnter Xa.txt unhide 1 |
| 3282 | doautoall BufEnter |
| 3283 | END |
| 3284 | call v9.CheckScriptFailure(lines, 'E814:') |
| 3285 | au! BufEnter |
| 3286 | only! |
| 3287 | bwipe Xa.txt |
| 3288 | bwipe Xb.txt |
| 3289 | endfunc |
| 3290 | |
Bram Moolenaar | 347538f | 2022-03-26 16:28:06 +0000 | [diff] [blame] | 3291 | func Test_bufwipeout_changes_window() |
| 3292 | " This should not crash, but we don't have any expectations about what |
| 3293 | " happens, changing window in BufWipeout has unpredictable results. |
| 3294 | tabedit |
| 3295 | let g:window_id = win_getid() |
| 3296 | topleft new |
| 3297 | setlocal bufhidden=wipe |
| 3298 | autocmd BufWipeout <buffer> call win_gotoid(g:window_id) |
| 3299 | tabprevious |
| 3300 | +tabclose |
| 3301 | |
| 3302 | unlet g:window_id |
| 3303 | au! BufWipeout |
| 3304 | %bwipe! |
| 3305 | endfunc |
| 3306 | |
zeertzjq | 021996f | 2022-04-10 11:44:04 +0100 | [diff] [blame] | 3307 | func Test_v_event_readonly() |
| 3308 | autocmd CompleteChanged * let v:event.width = 0 |
| 3309 | call assert_fails("normal! i\<C-X>\<C-V>", 'E46:') |
| 3310 | au! CompleteChanged |
| 3311 | |
| 3312 | autocmd DirChangedPre * let v:event.directory = '' |
| 3313 | call assert_fails('cd .', 'E46:') |
| 3314 | au! DirChangedPre |
| 3315 | |
| 3316 | autocmd ModeChanged * let v:event.new_mode = '' |
| 3317 | call assert_fails('normal! cc', 'E46:') |
| 3318 | au! ModeChanged |
| 3319 | |
| 3320 | autocmd TextYankPost * let v:event.operator = '' |
| 3321 | call assert_fails('normal! yy', 'E46:') |
| 3322 | au! TextYankPost |
| 3323 | endfunc |
| 3324 | |
zeertzjq | c9e8fd6 | 2022-07-26 18:12:38 +0100 | [diff] [blame] | 3325 | " Test for ModeChanged pattern |
| 3326 | func Test_mode_changes() |
| 3327 | let g:index = 0 |
| 3328 | let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n'] |
| 3329 | func! TestMode() |
| 3330 | call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode")) |
| 3331 | call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode")) |
| 3332 | call assert_equal(mode(1), get(v:event, "new_mode")) |
| 3333 | let g:index += 1 |
| 3334 | endfunc |
| 3335 | |
| 3336 | au ModeChanged * :call TestMode() |
| 3337 | let g:n_to_any = 0 |
| 3338 | au ModeChanged n:* let g:n_to_any += 1 |
| 3339 | call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix') |
| 3340 | |
| 3341 | let g:V_to_v = 0 |
| 3342 | au ModeChanged V:v let g:V_to_v += 1 |
| 3343 | call feedkeys("Vv\<C-G>\<esc>", 'tnix') |
| 3344 | call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any) |
| 3345 | call assert_equal(1, g:V_to_v) |
| 3346 | call assert_equal(len(g:mode_seq) - 1, g:index) |
| 3347 | |
| 3348 | let g:n_to_i = 0 |
| 3349 | au ModeChanged n:i let g:n_to_i += 1 |
| 3350 | let g:n_to_niI = 0 |
| 3351 | au ModeChanged i:niI let g:n_to_niI += 1 |
| 3352 | let g:niI_to_i = 0 |
| 3353 | au ModeChanged niI:i let g:niI_to_i += 1 |
| 3354 | let g:nany_to_i = 0 |
| 3355 | au ModeChanged n*:i let g:nany_to_i += 1 |
| 3356 | let g:i_to_n = 0 |
| 3357 | au ModeChanged i:n let g:i_to_n += 1 |
| 3358 | let g:nori_to_any = 0 |
| 3359 | au ModeChanged [ni]:* let g:nori_to_any += 1 |
| 3360 | let g:i_to_any = 0 |
| 3361 | au ModeChanged i:* let g:i_to_any += 1 |
| 3362 | let g:index = 0 |
| 3363 | let g:mode_seq = ['n', 'i', 'niI', 'i', 'n'] |
| 3364 | call feedkeys("a\<C-O>l\<esc>", 'tnix') |
| 3365 | call assert_equal(len(g:mode_seq) - 1, g:index) |
| 3366 | call assert_equal(1, g:n_to_i) |
| 3367 | call assert_equal(1, g:n_to_niI) |
| 3368 | call assert_equal(1, g:niI_to_i) |
| 3369 | call assert_equal(2, g:nany_to_i) |
| 3370 | call assert_equal(1, g:i_to_n) |
| 3371 | call assert_equal(2, g:i_to_any) |
| 3372 | call assert_equal(3, g:nori_to_any) |
| 3373 | |
| 3374 | if has('terminal') |
| 3375 | let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n'] |
| 3376 | call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix') |
| 3377 | call assert_equal(len(g:mode_seq) - 1, g:index) |
| 3378 | call assert_equal(1, g:n_to_i) |
| 3379 | call assert_equal(1, g:n_to_niI) |
| 3380 | call assert_equal(1, g:niI_to_i) |
| 3381 | call assert_equal(2, g:nany_to_i) |
| 3382 | call assert_equal(1, g:i_to_n) |
| 3383 | call assert_equal(2, g:i_to_any) |
| 3384 | call assert_equal(5, g:nori_to_any) |
| 3385 | endif |
| 3386 | |
| 3387 | if has('cmdwin') |
| 3388 | let g:n_to_c = 0 |
| 3389 | au ModeChanged n:c let g:n_to_c += 1 |
| 3390 | let g:c_to_n = 0 |
| 3391 | au ModeChanged c:n let g:c_to_n += 1 |
| 3392 | let g:mode_seq += ['c', 'n', 'c', 'n'] |
| 3393 | call feedkeys("q:\<C-C>\<Esc>", 'tnix') |
| 3394 | call assert_equal(len(g:mode_seq) - 1, g:index) |
| 3395 | call assert_equal(2, g:n_to_c) |
| 3396 | call assert_equal(2, g:c_to_n) |
| 3397 | unlet g:n_to_c |
| 3398 | unlet g:c_to_n |
| 3399 | endif |
| 3400 | |
| 3401 | au! ModeChanged |
| 3402 | delfunc TestMode |
| 3403 | unlet! g:mode_seq |
| 3404 | unlet! g:index |
| 3405 | unlet! g:n_to_any |
| 3406 | unlet! g:V_to_v |
| 3407 | unlet! g:n_to_i |
| 3408 | unlet! g:n_to_niI |
| 3409 | unlet! g:niI_to_i |
| 3410 | unlet! g:nany_to_i |
| 3411 | unlet! g:i_to_n |
| 3412 | unlet! g:nori_to_any |
| 3413 | unlet! g:i_to_any |
| 3414 | endfunc |
| 3415 | |
| 3416 | func Test_recursive_ModeChanged() |
| 3417 | au! ModeChanged * norm 0u |
| 3418 | sil! norm |
| 3419 | au! ModeChanged |
| 3420 | endfunc |
| 3421 | |
| 3422 | func Test_ModeChanged_starts_visual() |
| 3423 | " This was triggering ModeChanged before setting VIsual, causing a crash. |
| 3424 | au! ModeChanged * norm 0u |
| 3425 | sil! norm |
| 3426 | |
| 3427 | au! ModeChanged |
| 3428 | endfunc |
Bram Moolenaar | 347538f | 2022-03-26 16:28:06 +0000 | [diff] [blame] | 3429 | |
Charlie Groves | fef4485 | 2022-04-19 16:24:12 +0100 | [diff] [blame] | 3430 | func Test_noname_autocmd() |
| 3431 | augroup test_noname_autocmd_group |
| 3432 | autocmd! |
| 3433 | autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")]) |
| 3434 | autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")]) |
| 3435 | autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")]) |
| 3436 | autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")]) |
| 3437 | autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")]) |
| 3438 | augroup END |
| 3439 | |
| 3440 | let s:li = [] |
| 3441 | edit foo |
| 3442 | call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li) |
| 3443 | |
| 3444 | au! test_noname_autocmd_group |
| 3445 | augroup! test_noname_autocmd_group |
| 3446 | endfunc |
| 3447 | |
Yegappan Lakshmanan | 1755a91 | 2022-05-19 10:31:47 +0100 | [diff] [blame] | 3448 | " Test for the autocmd_get() function |
| 3449 | func Test_autocmd_get() |
| 3450 | augroup TestAutoCmdFns |
| 3451 | au! |
| 3452 | autocmd BufAdd *.vim echo "bufadd-vim" |
| 3453 | autocmd BufAdd *.py echo "bufadd-py" |
| 3454 | autocmd BufHidden *.vim echo "bufhidden" |
| 3455 | augroup END |
| 3456 | augroup TestAutoCmdFns2 |
| 3457 | autocmd BufAdd *.vim echo "bufadd-vim-2" |
| 3458 | autocmd BufRead *.a1b2c3 echo "bufadd-vim-2" |
| 3459 | augroup END |
| 3460 | |
| 3461 | let l = autocmd_get() |
| 3462 | call assert_true(l->len() > 0) |
| 3463 | |
| 3464 | " Test for getting all the autocmds in a group |
| 3465 | let expected = [ |
| 3466 | \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns', |
| 3467 | \ pattern: '*.vim', nested: v:false, once: v:false, |
| 3468 | \ event: 'BufAdd'}, |
| 3469 | \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns', |
| 3470 | \ pattern: '*.py', nested: v:false, once: v:false, |
| 3471 | \ event: 'BufAdd'}, |
| 3472 | \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns', |
| 3473 | \ pattern: '*.vim', nested: v:false, |
| 3474 | \ once: v:false, event: 'BufHidden'}] |
| 3475 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'})) |
| 3476 | |
| 3477 | " Test for getting autocmds for all the patterns in a group |
| 3478 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns', |
| 3479 | \ event: '*'})) |
| 3480 | |
| 3481 | " Test for getting autocmds for an event in a group |
| 3482 | let expected = [ |
| 3483 | \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns', |
| 3484 | \ pattern: '*.vim', nested: v:false, once: v:false, |
| 3485 | \ event: 'BufAdd'}, |
| 3486 | \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns', |
| 3487 | \ pattern: '*.py', nested: v:false, once: v:false, |
| 3488 | \ event: 'BufAdd'}] |
| 3489 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns', |
| 3490 | \ event: 'BufAdd'})) |
| 3491 | |
| 3492 | " Test for getting the autocmds for all the events in a group for particular |
| 3493 | " pattern |
| 3494 | call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns', |
| 3495 | \ 'pattern': '*.py', 'nested': v:false, 'once': v:false, |
| 3496 | \ 'event': 'BufAdd'}], |
| 3497 | \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'})) |
| 3498 | |
| 3499 | " Test for getting the autocmds for an events in a group for particular |
| 3500 | " pattern |
| 3501 | let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd', |
| 3502 | \ pattern: '*.vim'}) |
| 3503 | call assert_equal([ |
| 3504 | \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns', |
| 3505 | \ pattern: '*.vim', nested: v:false, once: v:false, |
| 3506 | \ event: 'BufAdd'}], l) |
| 3507 | |
| 3508 | " Test for getting the autocmds for a pattern in a group |
| 3509 | let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'}) |
| 3510 | call assert_equal([ |
| 3511 | \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns', |
| 3512 | \ pattern: '*.vim', nested: v:false, once: v:false, |
| 3513 | \ event: 'BufAdd'}, |
| 3514 | \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns', |
| 3515 | \ pattern: '*.vim', nested: v:false, |
| 3516 | \ once: v:false, event: 'BufHidden'}], l) |
| 3517 | |
| 3518 | " Test for getting the autocmds for a pattern in all the groups |
| 3519 | let l = autocmd_get(#{pattern: '*.a1b2c3'}) |
| 3520 | call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2', |
| 3521 | \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false, |
| 3522 | \ 'event': 'BufRead'}], l) |
| 3523 | |
| 3524 | " Test for getting autocmds for a pattern without any autocmds |
| 3525 | call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns', |
| 3526 | \ pattern: '*.abc'})) |
| 3527 | call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns', |
| 3528 | \ event: 'BufAdd', pattern: '*.abc'})) |
| 3529 | call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns', |
| 3530 | \ event: 'BufWipeout'})) |
| 3531 | call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})", |
| 3532 | \ 'E367:') |
| 3533 | let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})" |
| 3534 | call assert_fails(cmd, 'E216:') |
| 3535 | call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:') |
| 3536 | call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:') |
| 3537 | |
| 3538 | augroup TestAutoCmdFns |
| 3539 | au! |
| 3540 | augroup END |
| 3541 | call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'})) |
| 3542 | |
| 3543 | " Test for nested and once autocmds |
| 3544 | augroup TestAutoCmdFns |
| 3545 | au! |
| 3546 | autocmd VimSuspend * ++nested echo "suspend" |
| 3547 | autocmd VimResume * ++once echo "resume" |
| 3548 | augroup END |
| 3549 | |
| 3550 | let expected = [ |
| 3551 | \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*', |
| 3552 | \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'}, |
| 3553 | \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*', |
| 3554 | \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}] |
| 3555 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'})) |
| 3556 | |
| 3557 | " Test for buffer-local autocmd |
| 3558 | augroup TestAutoCmdFns |
| 3559 | au! |
| 3560 | autocmd TextYankPost <buffer> echo "textyankpost" |
| 3561 | augroup END |
| 3562 | |
| 3563 | let expected = [ |
| 3564 | \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns', |
| 3565 | \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false, |
| 3566 | \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}] |
| 3567 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'})) |
| 3568 | |
| 3569 | augroup TestAutoCmdFns |
| 3570 | au! |
| 3571 | augroup END |
| 3572 | augroup! TestAutoCmdFns |
| 3573 | augroup TestAutoCmdFns2 |
| 3574 | au! |
| 3575 | augroup END |
| 3576 | augroup! TestAutoCmdFns2 |
| 3577 | |
| 3578 | call assert_fails("echo autocmd_get(#{group: []})", 'E730:') |
| 3579 | call assert_fails("echo autocmd_get(#{event: {}})", 'E731:') |
| 3580 | call assert_fails("echo autocmd_get([])", 'E1206:') |
| 3581 | endfunc |
| 3582 | |
| 3583 | " Test for the autocmd_add() function |
| 3584 | func Test_autocmd_add() |
| 3585 | " Define a single autocmd in a group |
| 3586 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh', |
| 3587 | \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}]) |
| 3588 | call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet', |
| 3589 | \ pattern: '*.sh', nested: v:true, once: v:true, |
| 3590 | \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'})) |
| 3591 | |
| 3592 | " Define two autocmds in the same group |
| 3593 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3594 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh', |
| 3595 | \ cmd: 'echo "bufadd"'}, |
| 3596 | \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh', |
| 3597 | \ cmd: 'echo "bufenter"'}]) |
| 3598 | call assert_equal([ |
| 3599 | \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh', |
| 3600 | \ nested: v:false, once: v:false, event: 'BufAdd'}, |
| 3601 | \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh', |
| 3602 | \ nested: v:false, once: v:false, event: 'BufEnter'}], |
| 3603 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3604 | |
| 3605 | " Define a buffer-local autocmd |
| 3606 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3607 | call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold', |
| 3608 | \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}]) |
| 3609 | call assert_equal([ |
| 3610 | \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet', |
| 3611 | \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false, |
| 3612 | \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}], |
| 3613 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3614 | |
| 3615 | " Use an invalid buffer number |
| 3616 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3617 | call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter', |
| 3618 | \ bufnr: -1, cmd: 'echo "bufenter"'}]) |
| 3619 | let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999, |
| 3620 | \ cmd: 'echo "bufadd"'}] |
| 3621 | call assert_fails("echo autocmd_add(l)", 'E680:') |
Yegappan Lakshmanan | 00e977c | 2022-06-01 12:31:53 +0100 | [diff] [blame] | 3622 | let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999, |
| 3623 | \ pattern: '*.py', cmd: 'echo "bufadd"'}] |
| 3624 | call assert_fails("echo autocmd_add(l)", 'E680:') |
| 3625 | let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999, |
| 3626 | \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}] |
| 3627 | call assert_fails("echo autocmd_add(l)", 'E680:') |
Yegappan Lakshmanan | 1755a91 | 2022-05-19 10:31:47 +0100 | [diff] [blame] | 3628 | let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [], |
| 3629 | \ cmd: 'echo "bufread"'}] |
| 3630 | call assert_fails("echo autocmd_add(l)", 'E745:') |
| 3631 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3632 | |
| 3633 | " Add two commands to the same group, event and pattern |
| 3634 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3635 | call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload', |
| 3636 | \ pattern: 'abc', cmd: 'echo "cmd1"'}]) |
| 3637 | call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload', |
| 3638 | \ pattern: 'abc', cmd: 'echo "cmd2"'}]) |
| 3639 | call assert_equal([ |
| 3640 | \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc', |
| 3641 | \ nested: v:false, once: v:false, event: 'BufUnload'}, |
| 3642 | \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc', |
| 3643 | \ nested: v:false, once: v:false, event: 'BufUnload'}], |
| 3644 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3645 | |
| 3646 | " When adding a new autocmd, if the autocmd 'group' is not specified, then |
| 3647 | " the current autocmd group should be used. |
| 3648 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3649 | augroup TestAcSet |
| 3650 | call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}]) |
| 3651 | augroup END |
| 3652 | call assert_equal([ |
| 3653 | \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc', |
| 3654 | \ nested: v:false, once: v:false, event: 'BufHidden'}], |
| 3655 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3656 | |
Yegappan Lakshmanan | 971f682 | 2022-05-24 11:40:11 +0100 | [diff] [blame] | 3657 | " Test for replacing a cmd for an event in a group |
| 3658 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3659 | call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter', |
| 3660 | \ pattern: '*.py', cmd: 'echo "bufenter"'}]) |
| 3661 | call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter', |
| 3662 | \ pattern: '*.py', cmd: 'echo "bufenter"'}]) |
| 3663 | call assert_equal([ |
| 3664 | \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py', |
| 3665 | \ nested: v:false, once: v:false, event: 'BufEnter'}], |
| 3666 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3667 | |
| 3668 | " Test for adding a command for an unsupported autocmd event |
Yegappan Lakshmanan | 1755a91 | 2022-05-19 10:31:47 +0100 | [diff] [blame] | 3669 | let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh', |
| 3670 | \ cmd: 'echo "bufadd"'}] |
| 3671 | call assert_fails('call autocmd_add(l)', 'E216:') |
| 3672 | |
Yegappan Lakshmanan | e0ff3a7 | 2022-05-27 18:05:33 +0100 | [diff] [blame] | 3673 | " Test for using a list of events and patterns |
| 3674 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3675 | let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'], |
| 3676 | \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}] |
| 3677 | call autocmd_add(l) |
| 3678 | call assert_equal([ |
| 3679 | \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py', |
| 3680 | \ nested: v:false, once: v:false, event: 'BufEnter'}, |
| 3681 | \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh', |
| 3682 | \ nested: v:false, once: v:false, event: 'BufEnter'}, |
| 3683 | \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py', |
| 3684 | \ nested: v:false, once: v:false, event: 'BufLeave'}, |
| 3685 | \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh', |
| 3686 | \ nested: v:false, once: v:false, event: 'BufLeave'}], |
| 3687 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3688 | |
| 3689 | " Test for invalid values for 'event' item |
| 3690 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3691 | let l = [#{group: 'TestAcSet', event: test_null_string(), |
| 3692 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3693 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3694 | let l = [#{group: 'TestAcSet', event: test_null_list(), |
| 3695 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3696 | call assert_fails('call autocmd_add(l)', 'E714:') |
| 3697 | let l = [#{group: 'TestAcSet', event: {}, |
| 3698 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3699 | call assert_fails('call autocmd_add(l)', 'E777:') |
| 3700 | let l = [#{group: 'TestAcSet', event: [{}], |
| 3701 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3702 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3703 | let l = [#{group: 'TestAcSet', event: [test_null_string()], |
| 3704 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3705 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3706 | let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave', |
| 3707 | \ pattern: '*.py', cmd: 'echo "bufcmds"'}] |
| 3708 | call assert_fails('call autocmd_add(l)', 'E216:') |
| 3709 | let l = [#{group: 'TestAcSet', event: [], |
| 3710 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3711 | call autocmd_add(l) |
| 3712 | let l = [#{group: 'TestAcSet', event: [""], |
| 3713 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3714 | call assert_fails('call autocmd_add(l)', 'E216:') |
| 3715 | let l = [#{group: 'TestAcSet', event: "", |
| 3716 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3717 | call autocmd_add(l) |
| 3718 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3719 | |
| 3720 | " Test for invalid values for 'pattern' item |
| 3721 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3722 | \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}] |
Yegappan Lakshmanan | 00e977c | 2022-06-01 12:31:53 +0100 | [diff] [blame] | 3723 | call assert_fails('call autocmd_add(l)', 'E928:') |
Yegappan Lakshmanan | e0ff3a7 | 2022-05-27 18:05:33 +0100 | [diff] [blame] | 3724 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3725 | \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}] |
| 3726 | call assert_fails('call autocmd_add(l)', 'E714:') |
| 3727 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3728 | \ pattern: {}, cmd: 'echo "bufcmds"'}] |
| 3729 | call assert_fails('call autocmd_add(l)', 'E777:') |
| 3730 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3731 | \ pattern: [{}], cmd: 'echo "bufcmds"'}] |
| 3732 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3733 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3734 | \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}] |
| 3735 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3736 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3737 | \ pattern: [], cmd: 'echo "bufcmds"'}] |
| 3738 | call autocmd_add(l) |
| 3739 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3740 | \ pattern: [""], cmd: 'echo "bufcmds"'}] |
| 3741 | call autocmd_add(l) |
| 3742 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3743 | \ pattern: "", cmd: 'echo "bufcmds"'}] |
| 3744 | call autocmd_add(l) |
| 3745 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3746 | |
| 3747 | let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave', |
| 3748 | \ pattern: '*.py', cmd: 'echo "bufcmds"'}] |
| 3749 | call assert_fails('call autocmd_add(l)', 'E216:') |
| 3750 | |
Yegappan Lakshmanan | 1755a91 | 2022-05-19 10:31:47 +0100 | [diff] [blame] | 3751 | call assert_fails("call autocmd_add({})", 'E1211:') |
| 3752 | call assert_equal(v:false, autocmd_add(test_null_list())) |
| 3753 | call assert_true(autocmd_add([[]])) |
| 3754 | call assert_true(autocmd_add([test_null_dict()])) |
| 3755 | |
| 3756 | augroup TestAcSet |
| 3757 | au! |
| 3758 | augroup END |
| 3759 | |
| 3760 | call autocmd_add([#{group: 'TestAcSet'}]) |
| 3761 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}]) |
| 3762 | call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}]) |
| 3763 | call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}]) |
| 3764 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}]) |
| 3765 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}]) |
| 3766 | call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}]) |
| 3767 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3768 | |
| 3769 | augroup! TestAcSet |
| 3770 | endfunc |
| 3771 | |
| 3772 | " Test for deleting autocmd events and groups |
| 3773 | func Test_autocmd_delete() |
| 3774 | " Delete an event in an autocmd group |
| 3775 | augroup TestAcSet |
| 3776 | au! |
| 3777 | au BufAdd *.sh echo "bufadd" |
| 3778 | au BufEnter *.sh echo "bufenter" |
| 3779 | augroup END |
| 3780 | call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}]) |
| 3781 | call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet', |
| 3782 | \ pattern: '*.sh', nested: v:false, once: v:false, |
| 3783 | \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'})) |
| 3784 | |
| 3785 | " Delete all the events in an autocmd group |
| 3786 | augroup TestAcSet |
| 3787 | au BufAdd *.sh echo "bufadd" |
| 3788 | augroup END |
| 3789 | call autocmd_delete([#{group: 'TestAcSet', event: '*'}]) |
| 3790 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3791 | |
| 3792 | " Delete a non-existing autocmd group |
| 3793 | call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:') |
| 3794 | " Delete a non-existing autocmd event |
| 3795 | let l = [#{group: 'TestAcSet', event: 'abc'}] |
| 3796 | call assert_fails("call autocmd_delete(l)", 'E216:') |
| 3797 | " Delete a non-existing autocmd pattern |
| 3798 | let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}] |
| 3799 | call assert_true(autocmd_delete(l)) |
Yegappan Lakshmanan | 00e977c | 2022-06-01 12:31:53 +0100 | [diff] [blame] | 3800 | " Delete an autocmd for a non-existing buffer |
| 3801 | let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}] |
| 3802 | call assert_fails('call autocmd_delete(l)', 'E680:') |
Yegappan Lakshmanan | 1755a91 | 2022-05-19 10:31:47 +0100 | [diff] [blame] | 3803 | |
| 3804 | " Delete an autocmd group |
| 3805 | augroup TestAcSet |
| 3806 | au! |
| 3807 | au BufAdd *.sh echo "bufadd" |
| 3808 | au BufEnter *.sh echo "bufenter" |
| 3809 | augroup END |
| 3810 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3811 | call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:') |
| 3812 | |
| 3813 | call assert_true(autocmd_delete([[]])) |
| 3814 | call assert_true(autocmd_delete([test_null_dict()])) |
| 3815 | endfunc |
| 3816 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 3817 | " vim: shiftwidth=2 sts=2 expandtab |