diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/functional/autocmd/safestate_spec.lua | 57 | ||||
-rw-r--r-- | test/functional/vimscript/state_spec.lua | 69 | ||||
-rw-r--r-- | test/old/testdir/test_autocmd.vim | 33 | ||||
-rw-r--r-- | test/old/testdir/test_functions.vim | 60 |
4 files changed, 219 insertions, 0 deletions
diff --git a/test/functional/autocmd/safestate_spec.lua b/test/functional/autocmd/safestate_spec.lua new file mode 100644 index 0000000000..73693749e4 --- /dev/null +++ b/test/functional/autocmd/safestate_spec.lua @@ -0,0 +1,57 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local eq = helpers.eq +local exec = helpers.exec +local feed = helpers.feed +local meths = helpers.meths + +before_each(clear) + +describe('SafeState autocommand', function() + local function create_autocmd() + exec([[ + let g:safe = 0 + autocmd SafeState * ++once let g:safe += 1 + ]]) + end + + it('with pending operator', function() + feed('d') + create_autocmd() + eq(0, meths.get_var('safe')) + feed('d') + eq(1, meths.get_var('safe')) + end) + + it('with specified register', function() + feed('"r') + create_autocmd() + eq(0, meths.get_var('safe')) + feed('x') + eq(1, meths.get_var('safe')) + end) + + it('with i_CTRL-O', function() + feed('i<C-O>') + create_autocmd() + eq(0, meths.get_var('safe')) + feed('x') + eq(1, meths.get_var('safe')) + end) + + it('with Insert mode completion', function() + feed('i<C-X><C-V>') + create_autocmd() + eq(0, meths.get_var('safe')) + feed('<C-X><C-Z>') + eq(1, meths.get_var('safe')) + end) + + it('with Cmdline completion', function() + feed(':<Tab>') + create_autocmd() + eq(0, meths.get_var('safe')) + feed('<C-E>') + eq(1, meths.get_var('safe')) + end) +end) diff --git a/test/functional/vimscript/state_spec.lua b/test/functional/vimscript/state_spec.lua new file mode 100644 index 0000000000..70f68a7494 --- /dev/null +++ b/test/functional/vimscript/state_spec.lua @@ -0,0 +1,69 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local eq = helpers.eq +local exec = helpers.exec +local exec_lua = helpers.exec_lua +local feed = helpers.feed +local meths = helpers.meths +local poke_eventloop = helpers.poke_eventloop + +before_each(clear) + +describe('state() function', function() + it('works', function() + meths.ui_attach(80, 24, {}) -- Allow hit-enter-prompt + + exec_lua([[ + function _G.Get_state_mode() + _G.res = { vim.fn.state(), vim.api.nvim_get_mode().mode:sub(1, 1) } + end + function _G.Run_timer() + local timer = vim.uv.new_timer() + timer:start(0, 0, function() + _G.Get_state_mode() + timer:close() + end) + end + ]]) + exec([[ + call setline(1, ['one', 'two', 'three']) + map ;; gg + set complete=. + func RunTimer() + call timer_start(0, {id -> v:lua.Get_state_mode()}) + endfunc + au Filetype foobar call v:lua.Get_state_mode() + ]]) + + -- Using a ":" command Vim is busy, thus "S" is returned + feed([[:call v:lua.Get_state_mode()<CR>]]) + eq({ 'S', 'n' }, exec_lua('return _G.res')) + + -- Using a timer callback + feed([[:call RunTimer()<CR>]]) + poke_eventloop() -- Allow polling for events + eq({ 'c', 'n' }, exec_lua('return _G.res')) + + -- Halfway a mapping + feed([[:call v:lua.Run_timer()<CR>;]]) + meths.get_mode() -- Allow polling for fast events + feed(';') + eq({ 'mS', 'n' }, exec_lua('return _G.res')) + + -- Insert mode completion + feed([[:call RunTimer()<CR>Got<C-N>]]) + poke_eventloop() -- Allow polling for events + feed('<Esc>') + eq({ 'aSc', 'i' }, exec_lua('return _G.res')) + + -- Autocommand executing + feed([[:set filetype=foobar<CR>]]) + eq({ 'xS', 'n' }, exec_lua('return _G.res')) + + -- messages scrolled + feed([[:call v:lua.Run_timer() | echo "one\ntwo\nthree"<CR>]]) + meths.get_mode() -- Allow polling for fast events + feed('<CR>') + eq({ 'Ss', 'r' }, exec_lua('return _G.res')) + end) +end) diff --git a/test/old/testdir/test_autocmd.vim b/test/old/testdir/test_autocmd.vim index c44988321f..959efc0b49 100644 --- a/test/old/testdir/test_autocmd.vim +++ b/test/old/testdir/test_autocmd.vim @@ -2959,6 +2959,39 @@ func Test_autocmd_in_try_block() au! BufEnter endfunc +func Test_autocmd_SafeState() + CheckRunVimInTerminal + + let lines =<< trim END + let g:safe = 0 + let g:again = '' + au SafeState * let g:safe += 1 + au SafeStateAgain * let g:again ..= 'x' + func CallTimer() + call timer_start(10, {id -> execute('let g:again ..= "t"')}) + endfunc + END + call writefile(lines, 'XSafeState') + let buf = RunVimInTerminal('-S XSafeState', #{rows: 6}) + + " Sometimes we loop to handle an K_IGNORE + call term_sendkeys(buf, ":echo g:safe\<CR>") + call WaitForAssert({-> assert_match('^[12] ', term_getline(buf, 6))}, 1000) + + call term_sendkeys(buf, ":echo g:again\<CR>") + call WaitForAssert({-> assert_match('^xxxx', term_getline(buf, 6))}, 1000) + + call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>") + call term_wait(buf) + call term_sendkeys(buf, ":\<CR>") + call term_wait(buf) + call term_sendkeys(buf, ":echo g:again\<CR>") + call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000) + + call StopVimInTerminal(buf) + call delete('XSafeState') +endfunc + func Test_autocmd_CmdWinEnter() CheckRunVimInTerminal " There is not cmdwin switch, so diff --git a/test/old/testdir/test_functions.vim b/test/old/testdir/test_functions.vim index 4ad01c8531..5981b5214b 100644 --- a/test/old/testdir/test_functions.vim +++ b/test/old/testdir/test_functions.vim @@ -2565,6 +2565,66 @@ func Test_bufadd_bufload() call delete('XotherName') endfunc +func Test_state() + CheckRunVimInTerminal + + let lines =<< trim END + call setline(1, ['one', 'two', 'three']) + map ;; gg + set complete=. + func RunTimer() + call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')}) + endfunc + au Filetype foobar let g:state = state()|let g:mode = mode() + END + call writefile(lines, 'XState') + let buf = RunVimInTerminal('-S XState', #{rows: 6}) + + " Using a ":" command Vim is busy, thus "S" is returned + call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>") + call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000) + call term_sendkeys(buf, ":\<CR>") + + " Using a timer callback + call term_sendkeys(buf, ":call RunTimer()\<CR>") + call term_wait(buf, 50) + let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>" + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000) + + " Halfway a mapping + call term_sendkeys(buf, ":call RunTimer()\<CR>;") + call term_wait(buf, 50) + call term_sendkeys(buf, ";") + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000) + + " Insert mode completion (bit slower on Mac) + call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>") + call term_wait(buf, 200) + call term_sendkeys(buf, "\<Esc>") + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000) + + " Autocommand executing + call term_sendkeys(buf, ":set filetype=foobar\<CR>") + call term_wait(buf, 50) + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000) + + " Todo: "w" - waiting for ch_evalexpr() + + " messages scrolled + call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>") + call term_wait(buf, 50) + call term_sendkeys(buf, "\<CR>") + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000) + + call StopVimInTerminal(buf) + call delete('XState') +endfunc + func Test_range() " destructuring let [x, y] = range(2) |