loop_spec.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. -- Test suite for testing interactions with API bindings
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local Screen = require('test.functional.ui.screen')
  4. local funcs = helpers.funcs
  5. local meths = helpers.meths
  6. local clear = helpers.clear
  7. local sleep = helpers.sleep
  8. local feed = helpers.feed
  9. local eq = helpers.eq
  10. local eval = helpers.eval
  11. local matches = helpers.matches
  12. local exec_lua = helpers.exec_lua
  13. local retry = helpers.retry
  14. before_each(clear)
  15. describe('vim.loop', function()
  16. it('version', function()
  17. assert(funcs.luaeval('vim.loop.version()')>=72961, "libuv version too old")
  18. matches("(%d+)%.(%d+)%.(%d+)", funcs.luaeval('vim.loop.version_string()'))
  19. end)
  20. it('timer', function()
  21. exec_lua('vim.api.nvim_set_var("coroutine_cnt", 0)', {})
  22. local code=[[
  23. local loop = vim.loop
  24. local touch = 0
  25. local function wait(ms)
  26. local this = coroutine.running()
  27. assert(this)
  28. local timer = loop.new_timer()
  29. timer:start(ms, 0, vim.schedule_wrap(function ()
  30. timer:close()
  31. touch = touch + 1
  32. coroutine.resume(this)
  33. touch = touch + 1
  34. assert(touch==3)
  35. vim.api.nvim_set_var("coroutine_cnt_1", touch)
  36. end))
  37. coroutine.yield()
  38. touch = touch + 1
  39. return touch
  40. end
  41. coroutine.wrap(function()
  42. local touched = wait(10)
  43. assert(touched==touch)
  44. vim.api.nvim_set_var("coroutine_cnt", touched)
  45. end)()
  46. ]]
  47. eq(0, meths.get_var('coroutine_cnt'))
  48. exec_lua(code)
  49. retry(2, nil, function()
  50. sleep(50)
  51. eq(2, meths.get_var('coroutine_cnt'))
  52. end)
  53. eq(3, meths.get_var('coroutine_cnt_1'))
  54. end)
  55. it('is API safe', function()
  56. local screen = Screen.new(50,10)
  57. screen:attach()
  58. screen:set_default_attr_ids({
  59. [1] = {bold = true, foreground = Screen.colors.Blue1},
  60. [2] = {bold = true, reverse = true},
  61. [3] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red},
  62. [4] = {bold = true, foreground = Screen.colors.SeaGreen4},
  63. [5] = {bold = true},
  64. })
  65. -- deferred API functions are disabled, as their safety can't be guaranteed
  66. exec_lua([[
  67. local timer = vim.loop.new_timer()
  68. timer:start(20, 0, function ()
  69. _G.is_fast = vim.in_fast_event()
  70. timer:close()
  71. vim.api.nvim_set_var("valid", true)
  72. vim.api.nvim_command("echomsg 'howdy'")
  73. end)
  74. ]])
  75. screen:expect([[
  76. |
  77. {2: }|
  78. {3:Error executing luv callback:} |
  79. {3:[string "<nvim>"]:5: E5560: nvim_set_var must not }|
  80. {3:be called in a lua loop callback} |
  81. {3:stack traceback:} |
  82. {3: [C]: in function 'nvim_set_var'} |
  83. {3: [string "<nvim>"]:5: in function <[string }|
  84. {3:"<nvim>"]:2>} |
  85. {4:Press ENTER or type command to continue}^ |
  86. ]])
  87. feed('<cr>')
  88. eq(false, eval("get(g:, 'valid', v:false)"))
  89. eq(true, exec_lua("return _G.is_fast"))
  90. -- callbacks can be scheduled to be executed in the main event loop
  91. -- where the entire API is available
  92. exec_lua([[
  93. local timer = vim.loop.new_timer()
  94. timer:start(20, 0, vim.schedule_wrap(function ()
  95. _G.is_fast = vim.in_fast_event()
  96. timer:close()
  97. vim.api.nvim_set_var("valid", true)
  98. vim.api.nvim_command("echomsg 'howdy'")
  99. end))
  100. ]])
  101. screen:expect([[
  102. ^ |
  103. {1:~ }|
  104. {1:~ }|
  105. {1:~ }|
  106. {1:~ }|
  107. {1:~ }|
  108. {1:~ }|
  109. {1:~ }|
  110. {1:~ }|
  111. howdy |
  112. ]])
  113. eq(true, eval("get(g:, 'valid', v:false)"))
  114. eq(false, exec_lua("return _G.is_fast"))
  115. -- fast (not deferred) API functions are allowed to be called directly
  116. exec_lua([[
  117. local timer = vim.loop.new_timer()
  118. timer:start(20, 0, function ()
  119. timer:close()
  120. -- input is queued for processing after the callback returns
  121. vim.api.nvim_input("isneaky")
  122. _G.mode = vim.api.nvim_get_mode()
  123. end)
  124. ]])
  125. screen:expect([[
  126. sneaky^ |
  127. {1:~ }|
  128. {1:~ }|
  129. {1:~ }|
  130. {1:~ }|
  131. {1:~ }|
  132. {1:~ }|
  133. {1:~ }|
  134. {1:~ }|
  135. {5:-- INSERT --} |
  136. ]])
  137. eq({blocking=false, mode='n'}, exec_lua("return _G.mode"))
  138. end)
  139. it("is equal to require('luv')", function()
  140. eq(true, exec_lua("return vim.loop == require('luv')"))
  141. end)
  142. end)