modechanged_spec.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear, eval, eq = n.clear, n.eval, t.eq
  4. local feed, command = n.feed, n.command
  5. local exec_lua = n.exec_lua
  6. describe('ModeChanged', function()
  7. before_each(function()
  8. clear()
  9. end)
  10. it('picks up terminal mode changes', function()
  11. command('let g:count = 0')
  12. command('au ModeChanged * let g:event = copy(v:event)')
  13. command('au ModeChanged * let g:count += 1')
  14. command('term')
  15. feed('i')
  16. eq({
  17. old_mode = 'nt',
  18. new_mode = 't',
  19. }, eval('g:event'))
  20. feed('<c-\\><c-n>')
  21. eq({
  22. old_mode = 't',
  23. new_mode = 'nt',
  24. }, eval('g:event'))
  25. eq(3, eval('g:count'))
  26. command('bd!')
  27. -- v:event is cleared after the autocommand is done
  28. eq({}, eval('v:event'))
  29. end)
  30. it('does not repeatedly trigger for scheduled callback', function()
  31. exec_lua([[
  32. vim.g.s_count = 0
  33. vim.g.s_mode = ""
  34. vim.g.t_count = 0
  35. vim.g.t_mode = ""
  36. vim.api.nvim_create_autocmd("ModeChanged", {
  37. callback = function()
  38. vim.g.s_count = vim.g.s_count + 1
  39. vim.g.s_mode = vim.api.nvim_get_mode().mode
  40. vim.schedule(function()
  41. vim.g.t_count = vim.g.t_count + 1
  42. vim.g.t_mode = vim.api.nvim_get_mode().mode
  43. end)
  44. end,
  45. })
  46. ]])
  47. feed('d')
  48. eq(1, eval('g:s_count'))
  49. eq('no', eval('g:s_mode'))
  50. eq(1, eval('g:t_count'))
  51. eq('no', eval('g:t_mode'))
  52. feed('<Esc>')
  53. eq(2, eval('g:s_count'))
  54. eq('n', eval('g:s_mode'))
  55. eq(2, eval('g:t_count'))
  56. eq('n', eval('g:t_mode'))
  57. end)
  58. end)