diff options
Diffstat (limited to 'src/testdir/test_autocmd.vim')
-rw-r--r-- | src/testdir/test_autocmd.vim | 351 |
1 files changed, 284 insertions, 67 deletions
diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim index 564d568..6745ecb 100644 --- a/src/testdir/test_autocmd.vim +++ b/src/testdir/test_autocmd.vim @@ -3702,73 +3702,6 @@ func Test_autocmd_with_block() augroup END endfunc -" Test TextChangedI and TextChanged -func Test_Changed_ChangedI() - new - call test_override("char_avail", 1) - let [g:autocmd_i, g:autocmd_n] = ['',''] - - func! TextChangedAutocmdI(char) - let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick - endfunc - - augroup Test_TextChanged - au! - au TextChanged <buffer> :call TextChangedAutocmdI('N') - au TextChangedI <buffer> :call TextChangedAutocmdI('I') - augroup END - - call feedkeys("ifoo\<esc>", 'tnix') - " TODO: Test test does not seem to trigger TextChanged autocommand, this - " requires running Vim in a terminal window. - " call assert_equal('N3', g:autocmd_n) - call assert_equal('I3', g:autocmd_i) - - call feedkeys("yyp", 'tnix') - " TODO: Test test does not seem to trigger TextChanged autocommand. - " call assert_equal('N4', g:autocmd_n) - call assert_equal('I3', g:autocmd_i) - - " TextChangedI should only trigger if change was done in Insert mode - let g:autocmd_i = '' - call feedkeys("yypi\<esc>", 'tnix') - call assert_equal('', g:autocmd_i) - - " TextChanged should only trigger if change was done in Normal mode - let g:autocmd_n = '' - call feedkeys("ibar\<esc>", 'tnix') - call assert_equal('', g:autocmd_n) - - " If change is a mix of Normal and Insert modes, TextChangedI should trigger - func s:validate_mixed_textchangedi(keys) - call feedkeys("ifoo\<esc>", 'tnix') - let g:autocmd_i = '' - let g:autocmd_n = '' - call feedkeys(a:keys, 'tnix') - call assert_notequal('', g:autocmd_i) - call assert_equal('', g:autocmd_n) - endfunc - - call s:validate_mixed_textchangedi("o\<esc>") - call s:validate_mixed_textchangedi("O\<esc>") - call s:validate_mixed_textchangedi("ciw\<esc>") - call s:validate_mixed_textchangedi("cc\<esc>") - call s:validate_mixed_textchangedi("C\<esc>") - call s:validate_mixed_textchangedi("s\<esc>") - call s:validate_mixed_textchangedi("S\<esc>") - - - " CleanUp - call test_override("char_avail", 0) - au! TextChanged <buffer> - au! TextChangedI <buffer> - augroup! Test_TextChanged - delfu TextChangedAutocmdI - unlet! g:autocmd_i g:autocmd_n - - bw! -endfunc - func Test_closing_autocmd_window() let lines =<< trim END edit Xa.txt @@ -3794,6 +3727,49 @@ func Test_switch_window_in_autocmd_window() call assert_false(bufexists('Xb.txt')) endfunc +" Test that using the autocommand window doesn't change current directory. +func Test_autocmd_window_cwd() + let saveddir = getcwd() + call mkdir('Xcwd/a/b/c/d', 'pR') + + new Xa.txt + tabnew + new Xb.txt + + tabprev + cd Xcwd + call assert_match('/Xcwd$', getcwd()) + call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd'))) + + autocmd BufEnter Xb.txt lcd ./a/b/c/d + doautoall BufEnter + au! BufEnter + call assert_match('/Xcwd$', getcwd()) + call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd'))) + + tabnext + cd ./a + tcd ./b + lcd ./c + call assert_match('/Xcwd/a/b/c$', getcwd()) + call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd'))) + + autocmd BufEnter Xa.txt call assert_match('Xcwd/a/b/c$', getcwd()) + doautoall BufEnter + au! BufEnter + call assert_match('/Xcwd/a/b/c$', getcwd()) + call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd'))) + bwipe! + call assert_match('/Xcwd/a/b$', getcwd()) + call assert_match('\[tabpage\] .*/Xcwd/a/b$', trim(execute('verbose pwd'))) + bwipe! + call assert_match('/Xcwd/a$', getcwd()) + call assert_match('\[global\] .*/Xcwd/a$', trim(execute('verbose pwd'))) + bwipe! + + call chdir(saveddir) +endfunc + func Test_bufwipeout_changes_window() " This should not crash, but we don't have any expectations about what " happens, changing window in BufWipeout has unpredictable results. @@ -4474,4 +4450,245 @@ func Test_autocmd_creates_new_buffer_on_bufleave() bw c.txt endfunc +" Ensure `expected` was just recently written as a Vim session +func s:assert_session_path(expected) + call assert_equal(a:expected, v:this_session) +endfunc + +" Check for `expected` after a session is written to-disk. +func s:watch_for_session_path(expected) + execute 'autocmd SessionWritePost * ++once execute "call s:assert_session_path(\"' + \ . a:expected + \ . '\")"' +endfunc + +" Ensure v:this_session gets the full session path, if explicitly stated +func Test_explicit_session_absolute_path() + %bwipeout! + + let directory = getcwd() + + let v:this_session = "" + let name = "some_file.vim" + let expected = fnamemodify(name, ":p") + call s:watch_for_session_path(expected) + execute "mksession! " .. expected + + call delete(expected) +endfunc + +" Ensure v:this_session gets the full session path, if explicitly stated +func Test_explicit_session_relative_path() + %bwipeout! + + let directory = getcwd() + + let v:this_session = "" + let name = "some_file.vim" + let expected = fnamemodify(name, ":p") + call s:watch_for_session_path(expected) + execute "mksession! " .. name + + call delete(expected) +endfunc + +" Ensure v:this_session gets the full session path, if not specified +func Test_implicit_session() + %bwipeout! + + let directory = getcwd() + + let v:this_session = "" + let expected = fnamemodify("Session.vim", ":p") + call s:watch_for_session_path(expected) + mksession! + + call delete(expected) +endfunc + +" Test TextChangedI and TextChanged +func Test_Changed_ChangedI() + " Run this test in a terminal because it requires running the main loop. + " Don't use CheckRunVimInTerminal as that will skip the test on Windows. + CheckFeature terminal + CheckNotGui + " Starting a terminal to run Vim is always considered flaky. + let g:test_is_flaky = 1 + + call writefile(['one', 'two', 'three'], 'XTextChangedI2', 'D') + let before =<< trim END + set ttimeout ttimeoutlen=10 + let [g:autocmd_n, g:autocmd_i] = ['',''] + + func TextChangedAutocmd(char) + let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick + call writefile([$'{g:autocmd_n},{g:autocmd_i}'], 'XTextChangedI3') + endfunc + + au TextChanged <buffer> :call TextChangedAutocmd('N') + au TextChangedI <buffer> :call TextChangedAutocmd('I') + + nnoremap <CR> o<Esc> + autocmd SafeState * ++once call writefile([''], 'XTextChangedI3') + END + + call writefile(before, 'Xinit', 'D') + let buf = term_start( + \ GetVimCommandCleanTerm() .. '-n -S Xinit XTextChangedI2', + \ {'term_rows': 10}) + call assert_equal('running', term_getstatus(buf)) + call WaitForAssert({-> assert_true(filereadable('XTextChangedI3'))}) + defer delete('XTextChangedI3') + call WaitForAssert({-> assert_equal([''], readfile('XTextChangedI3'))}) + + " TextChanged should trigger if a mapping enters and leaves Insert mode. + call term_sendkeys(buf, "\<CR>") + call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))}) + + call term_sendkeys(buf, "i") + call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))}) + call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))}) + " TextChangedI should trigger if change is done in Insert mode. + call term_sendkeys(buf, "f") + call WaitForAssert({-> assert_equal('N4,I5', readfile('XTextChangedI3')->join("\n"))}) + call term_sendkeys(buf, "o") + call WaitForAssert({-> assert_equal('N4,I6', readfile('XTextChangedI3')->join("\n"))}) + call term_sendkeys(buf, "o") + call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))}) + " TextChanged shouldn't trigger when leaving Insert mode and TextChangedI + " has been triggered. + call term_sendkeys(buf, "\<Esc>") + call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))}) + call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))}) + + " TextChanged should trigger if change is done in Normal mode. + call term_sendkeys(buf, "yyp") + call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))}) + + " TextChangedI shouldn't trigger if change isn't done in Insert mode. + call term_sendkeys(buf, "i") + call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))}) + call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))}) + call term_sendkeys(buf, "\<Esc>") + call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))}) + call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))}) + + " TextChangedI should trigger if change is a mix of Normal and Insert modes. + func! s:validate_mixed_textchangedi(buf, keys) + let buf = a:buf + call term_sendkeys(buf, "ifoo") + call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))}) + call term_sendkeys(buf, "\<Esc>") + call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))}) + call term_sendkeys(buf, ":let [g:autocmd_n, g:autocmd_i] = ['', '']\<CR>") + call writefile([], 'XTextChangedI3') + call term_sendkeys(buf, a:keys) + call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))}) + call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))}) + call term_sendkeys(buf, "\<Esc>") + call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))}) + call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))}) + endfunc + + call s:validate_mixed_textchangedi(buf, "o") + call s:validate_mixed_textchangedi(buf, "O") + call s:validate_mixed_textchangedi(buf, "ciw") + call s:validate_mixed_textchangedi(buf, "cc") + call s:validate_mixed_textchangedi(buf, "C") + call s:validate_mixed_textchangedi(buf, "s") + call s:validate_mixed_textchangedi(buf, "S") + + " clean up + bwipe! +endfunc + +" Test that filetype detection still works when SwapExists autocommand sets +" filetype in another buffer. +func Test_SwapExists_set_other_buf_filetype() + let lines =<< trim END + set nocompatible directory=. + filetype on + + let g:buf = bufnr() + new + + func SwapExists() + let v:swapchoice = 'o' + call setbufvar(g:buf, '&filetype', 'text') + endfunc + + func SafeState() + edit <script> + redir! > XftSwapExists.out + set readonly? filetype? + redir END + qall! + endfunc + + autocmd SwapExists * ++nested call SwapExists() + autocmd SafeState * ++nested ++once call SafeState() + END + call writefile(lines, 'XftSwapExists.vim', 'D') + + new XftSwapExists.vim + if RunVim('', '', ' -S XftSwapExists.vim') + call assert_equal( + \ ['', ' readonly', ' filetype=vim'], + \ readfile('XftSwapExists.out')) + call delete('XftSwapExists.out') + endif + + bwipe! +endfunc + +" Test that file is not marked as modified when SwapExists autocommand sets +" 'modified' in another buffer. +func Test_SwapExists_set_other_buf_modified() + let lines =<< trim END + set nocompatible directory=. + + let g:buf = bufnr() + new + + func SwapExists() + let v:swapchoice = 'o' + call setbufvar(g:buf, '&modified', 1) + endfunc + + func SafeState() + edit <script> + redir! > XmodSwapExists.out + set readonly? modified? + redir END + qall! + endfunc + + autocmd SwapExists * ++nested call SwapExists() + autocmd SafeState * ++nested ++once call SafeState() + END + call writefile(lines, 'XmodSwapExists.vim', 'D') + + new XmodSwapExists.vim + if RunVim('', '', ' -S XmodSwapExists.vim') + call assert_equal( + \ ['', ' readonly', 'nomodified'], + \ readfile('XmodSwapExists.out')) + call delete('XmodSwapExists.out') + endif + + bwipe! +endfunc + +func Test_BufEnter_botline() + set hidden + call writefile(range(10), 'Xxx1', 'D') + call writefile(range(20), 'Xxx2', 'D') + edit Xxx1 + edit Xxx2 + au BufEnter Xxx1 call assert_true(line('w$') > 1) + edit Xxx1 + au! BufEnter Xxx1 + set hidden&vim +endfunc + " vim: shiftwidth=2 sts=2 expandtab |