ui_event_spec.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local Screen = require('test.functional.ui.screen')
  3. local eq = helpers.eq
  4. local exec_lua = helpers.exec_lua
  5. local clear = helpers.clear
  6. local feed = helpers.feed
  7. local funcs = helpers.funcs
  8. local inspect = require'vim.inspect'
  9. describe('vim.ui_attach', function()
  10. local screen
  11. before_each(function()
  12. clear()
  13. exec_lua [[
  14. ns = vim.api.nvim_create_namespace 'testspace'
  15. events = {}
  16. function on_event(event, ...)
  17. events[#events+1] = {event, ...}
  18. return true
  19. end
  20. function get_events()
  21. local ret_events = events
  22. events = {}
  23. return ret_events
  24. end
  25. ]]
  26. screen = Screen.new(40,5)
  27. screen:set_default_attr_ids({
  28. [1] = {bold = true, foreground = Screen.colors.Blue1};
  29. [2] = {bold = true};
  30. [3] = {background = Screen.colors.Grey};
  31. [4] = {background = Screen.colors.LightMagenta};
  32. })
  33. screen:attach()
  34. end)
  35. local function expect_events(expected)
  36. local evs = exec_lua "return get_events(...)"
  37. eq(expected, evs, inspect(evs))
  38. end
  39. it('can receive popupmenu events', function()
  40. exec_lua [[ vim.ui_attach(ns, {ext_popupmenu=true}, on_event) ]]
  41. feed('ifo')
  42. screen:expect{grid=[[
  43. fo^ |
  44. {1:~ }|
  45. {1:~ }|
  46. {1:~ }|
  47. {2:-- INSERT --} |
  48. ]]}
  49. funcs.complete(1, {'food', 'foobar', 'foo'})
  50. screen:expect{grid=[[
  51. food^ |
  52. {1:~ }|
  53. {1:~ }|
  54. {1:~ }|
  55. {2:-- INSERT --} |
  56. ]]}
  57. expect_events {
  58. { "popupmenu_show", { { "food", "", "", "" }, { "foobar", "", "", "" }, { "foo", "", "", "" } }, 0, 0, 0, 1 };
  59. }
  60. feed '<c-n>'
  61. screen:expect{grid=[[
  62. foobar^ |
  63. {1:~ }|
  64. {1:~ }|
  65. {1:~ }|
  66. {2:-- INSERT --} |
  67. ]]}
  68. expect_events {
  69. { "popupmenu_select", 1 };
  70. }
  71. feed '<c-y>'
  72. -- There is an intermediate state where the 'showmode' message disappears.
  73. screen:expect_unchanged(true)
  74. expect_events {
  75. { "popupmenu_hide" };
  76. }
  77. -- vim.ui_detach() stops events, and reenables builtin pum immediately
  78. exec_lua [[
  79. vim.ui_detach(ns)
  80. vim.fn.complete(1, {'food', 'foobar', 'foo'})
  81. ]]
  82. screen:expect{grid=[[
  83. food^ |
  84. {3:food }{1: }|
  85. {4:foobar }{1: }|
  86. {4:foo }{1: }|
  87. {2:-- INSERT --} |
  88. ]]}
  89. expect_events {
  90. }
  91. end)
  92. it('does not crash on exit', function()
  93. helpers.funcs.system({
  94. helpers.nvim_prog,
  95. '-u', 'NONE',
  96. '-i', 'NONE',
  97. '--cmd', [[ lua ns = vim.api.nvim_create_namespace 'testspace' ]],
  98. '--cmd', [[ lua vim.ui_attach(ns, {ext_popupmenu=true}, function() end) ]],
  99. '--cmd', 'quitall!',
  100. })
  101. eq(0, helpers.eval('v:shell_error'))
  102. end)
  103. it('can receive accurate message kinds even if they are history', function()
  104. exec_lua([[
  105. vim.cmd.echomsg("'message1'")
  106. print('message2')
  107. vim.ui_attach(ns, { ext_messages = true }, on_event)
  108. vim.cmd.echomsg("'message3'")
  109. ]])
  110. feed(':messages<cr>')
  111. feed('<cr>')
  112. local actual = exec_lua([[
  113. return vim.tbl_filter(function (event)
  114. return event[1] == "msg_history_show"
  115. end, events)
  116. ]])
  117. eq({
  118. {
  119. 'msg_history_show',
  120. {
  121. { 'echomsg', { { 0, 'message1' } } },
  122. { '', { { 0, 'message2' } } },
  123. { 'echomsg', { { 0, 'message3' } } },
  124. },
  125. },
  126. }, actual, inspect(actual))
  127. end)
  128. end)