state_spec.lua 2.5 KB

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