neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

state_spec.lua (2606B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local clear = n.clear
      5 local eq = t.eq
      6 local exec = n.exec
      7 local exec_lua = n.exec_lua
      8 local feed = n.feed
      9 local api = n.api
     10 local poke_eventloop = n.poke_eventloop
     11 
     12 before_each(clear)
     13 
     14 describe('state() function', function()
     15  -- oldtest: Test_state()
     16  it('works', function()
     17    api.nvim_ui_attach(80, 24, {}) -- Allow hit-enter-prompt
     18 
     19    exec_lua([[
     20      function _G.Get_state_mode()
     21        _G.res = { vim.fn.state(), vim.api.nvim_get_mode().mode:sub(1, 1) }
     22      end
     23      function _G.Run_timer()
     24        local timer = vim.uv.new_timer()
     25        timer:start(0, 0, function()
     26          _G.Get_state_mode()
     27          timer:close()
     28        end)
     29      end
     30    ]])
     31    exec([[
     32      call setline(1, ['one', 'two', 'three'])
     33      map ;; gg
     34      set complete=.
     35      func RunTimer()
     36        call timer_start(0, {id -> v:lua.Get_state_mode()})
     37      endfunc
     38      au Filetype foobar call v:lua.Get_state_mode()
     39    ]])
     40 
     41    -- Using a ":" command Vim is busy, thus "S" is returned
     42    feed([[:call v:lua.Get_state_mode()<CR>]])
     43    eq({ 'S', 'n' }, exec_lua('return _G.res'))
     44 
     45    -- Using a timer callback
     46    feed([[:call RunTimer()<CR>]])
     47    poke_eventloop() -- Process pending input
     48    poke_eventloop() -- Process time_event
     49    eq({ 'c', 'n' }, exec_lua('return _G.res'))
     50 
     51    -- Halfway a mapping
     52    feed([[:call v:lua.Run_timer()<CR>;]])
     53    api.nvim_get_mode() -- Process pending input and luv timer callback
     54    feed(';')
     55    eq({ 'mS', 'n' }, exec_lua('return _G.res'))
     56 
     57    -- An operator is pending
     58    feed([[:call RunTimer()<CR>y]])
     59    poke_eventloop() -- Process pending input
     60    poke_eventloop() -- Process time_event
     61    feed('y')
     62    eq({ 'oSc', 'n' }, exec_lua('return _G.res'))
     63 
     64    -- A register was specified
     65    feed([[:call RunTimer()<CR>"r]])
     66    poke_eventloop() -- Process pending input
     67    poke_eventloop() -- Process time_event
     68    feed('yy')
     69    eq({ 'oSc', 'n' }, exec_lua('return _G.res'))
     70 
     71    -- Insert mode completion
     72    feed([[:call RunTimer()<CR>Got<C-N>]])
     73    poke_eventloop() -- Process pending input
     74    poke_eventloop() -- Process time_event
     75    feed('<Esc>')
     76    eq({ 'aSc', 'i' }, exec_lua('return _G.res'))
     77 
     78    -- Autocommand executing
     79    feed([[:set filetype=foobar<CR>]])
     80    eq({ 'xS', 'n' }, exec_lua('return _G.res'))
     81 
     82    -- messages scrolled
     83    feed([[:call v:lua.Run_timer() | echo "one\ntwo\nthree"<CR>]])
     84    api.nvim_get_mode() -- Process pending input and luv timer callback
     85    feed('<CR>')
     86    eq({ 'Ss', 'r' }, exec_lua('return _G.res'))
     87  end)
     88 end)