commands_spec.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. -- Test suite for checking :lua* commands
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local Screen = require('test.functional.ui.screen')
  4. local eq = helpers.eq
  5. local NIL = helpers.NIL
  6. local eval = helpers.eval
  7. local feed = helpers.feed
  8. local clear = helpers.clear
  9. local meths = helpers.meths
  10. local exec_lua = helpers.exec_lua
  11. local exec_capture = helpers.exec_capture
  12. local funcs = helpers.funcs
  13. local source = helpers.source
  14. local dedent = helpers.dedent
  15. local command = helpers.command
  16. local exc_exec = helpers.exc_exec
  17. local pcall_err = helpers.pcall_err
  18. local write_file = helpers.write_file
  19. local curbufmeths = helpers.curbufmeths
  20. local remove_trace = helpers.remove_trace
  21. before_each(clear)
  22. describe(':lua command', function()
  23. it('works', function()
  24. eq('', exec_capture(
  25. 'lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TEST"})'))
  26. eq({'', 'TEST'}, curbufmeths.get_lines(0, 100, false))
  27. source(dedent([[
  28. lua << EOF
  29. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TSET"})
  30. EOF]]))
  31. eq({'', 'TSET'}, curbufmeths.get_lines(0, 100, false))
  32. source(dedent([[
  33. lua << EOF
  34. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"SETT"})]]))
  35. eq({'', 'SETT'}, curbufmeths.get_lines(0, 100, false))
  36. source(dedent([[
  37. lua << EOF
  38. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
  39. vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
  40. vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
  41. EOF]]))
  42. eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false))
  43. end)
  44. it('throws catchable errors', function()
  45. eq([[Vim(lua):E5107: Error loading lua [string ":lua"]:0: unexpected symbol near ')']],
  46. pcall_err(command, 'lua ()'))
  47. eq([[Vim(lua):E5108: Error executing lua [string ":lua"]:1: TEST]],
  48. remove_trace(exc_exec('lua error("TEST")')))
  49. eq([[Vim(lua):E5108: Error executing lua [string ":lua"]:1: Invalid buffer id: -10]],
  50. remove_trace(exc_exec('lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})')))
  51. eq({''}, curbufmeths.get_lines(0, 100, false))
  52. end)
  53. it('works with NULL errors', function()
  54. eq([=[Vim(lua):E5108: Error executing lua [NULL]]=],
  55. exc_exec('lua error(nil)'))
  56. end)
  57. it('accepts embedded NLs without heredoc', function()
  58. -- Such code is usually used for `:execute 'lua' {generated_string}`:
  59. -- heredocs do not work in this case.
  60. meths.command([[
  61. lua
  62. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
  63. vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
  64. vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
  65. ]])
  66. eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false))
  67. end)
  68. it('preserves global and not preserves local variables', function()
  69. eq('', exec_capture('lua gvar = 42'))
  70. eq('', exec_capture('lua local lvar = 100500'))
  71. eq(NIL, funcs.luaeval('lvar'))
  72. eq(42, funcs.luaeval('gvar'))
  73. end)
  74. it('works with long strings', function()
  75. local s = ('x'):rep(100500)
  76. eq('Vim(lua):E5107: Error loading lua [string ":lua"]:0: unfinished string near \'<eof>\'',
  77. pcall_err(command, ('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s})'):format(s)))
  78. eq({''}, curbufmeths.get_lines(0, -1, false))
  79. eq('', exec_capture(('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s"})'):format(s)))
  80. eq({'', s}, curbufmeths.get_lines(0, -1, false))
  81. end)
  82. it('can show multiline error messages', function()
  83. local screen = Screen.new(40,10)
  84. screen:attach()
  85. screen:set_default_attr_ids({
  86. [1] = {bold = true, foreground = Screen.colors.Blue1},
  87. [2] = {bold = true, reverse = true},
  88. [3] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red},
  89. [4] = {bold = true, foreground = Screen.colors.SeaGreen4},
  90. })
  91. feed(':lua error("fail\\nmuch error\\nsuch details")<cr>')
  92. screen:expect{grid=[[
  93. {2: }|
  94. {3:E5108: Error executing lua [string ":lua}|
  95. {3:"]:1: fail} |
  96. {3:much error} |
  97. {3:such details} |
  98. {3:stack traceback:} |
  99. {3: [C]: in function 'error'} |
  100. {3: [string ":lua"]:1: in main chunk}|
  101. |
  102. {4:Press ENTER or type command to continue}^ |
  103. ]]}
  104. feed('<cr>')
  105. screen:expect{grid=[[
  106. ^ |
  107. {1:~ }|
  108. {1:~ }|
  109. {1:~ }|
  110. {1:~ }|
  111. {1:~ }|
  112. {1:~ }|
  113. {1:~ }|
  114. {1:~ }|
  115. |
  116. ]]}
  117. eq('E5108: Error executing lua [string ":lua"]:1: fail\nmuch error\nsuch details', remove_trace(eval('v:errmsg')))
  118. local status, err = pcall(command,'lua error("some error\\nin a\\nAPI command")')
  119. local expected = 'Vim(lua):E5108: Error executing lua [string ":lua"]:1: some error\nin a\nAPI command'
  120. eq(false, status)
  121. eq(expected, string.sub(remove_trace(err), -string.len(expected)))
  122. feed(':messages<cr>')
  123. screen:expect{grid=[[
  124. {2: }|
  125. {3:E5108: Error executing lua [string ":lua}|
  126. {3:"]:1: fail} |
  127. {3:much error} |
  128. {3:such details} |
  129. {3:stack traceback:} |
  130. {3: [C]: in function 'error'} |
  131. {3: [string ":lua"]:1: in main chunk}|
  132. |
  133. {4:Press ENTER or type command to continue}^ |
  134. ]]}
  135. end)
  136. it('prints result of =expr', function()
  137. exec_lua("x = 5")
  138. eq("5", exec_capture(':lua =x'))
  139. eq("5", exec_capture('=x'))
  140. exec_lua("function x() return 'hello' end")
  141. eq('hello', exec_capture(':lua = x()'))
  142. exec_lua("x = {a = 1, b = 2}")
  143. eq("{\n a = 1,\n b = 2\n}", exec_capture(':lua =x'))
  144. exec_lua([[function x(success)
  145. if success then
  146. return true, "Return value"
  147. else
  148. return false, nil, "Error message"
  149. end
  150. end]])
  151. eq(dedent[[
  152. true
  153. Return value]],
  154. exec_capture(':lua =x(true)'))
  155. eq(dedent[[
  156. false
  157. nil
  158. Error message]],
  159. exec_capture('=x(false)'))
  160. end)
  161. end)
  162. describe(':luado command', function()
  163. it('works', function()
  164. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  165. eq('', exec_capture('luado lines = (lines or {}) lines[#lines + 1] = {linenr, line}'))
  166. eq({'ABC', 'def', 'gHi'}, curbufmeths.get_lines(0, -1, false))
  167. eq({{1, 'ABC'}, {2, 'def'}, {3, 'gHi'}}, funcs.luaeval('lines'))
  168. -- Automatic transformation of numbers
  169. eq('', exec_capture('luado return linenr'))
  170. eq({'1', '2', '3'}, curbufmeths.get_lines(0, -1, false))
  171. eq('', exec_capture('luado return ("<%02x>"):format(line:byte())'))
  172. eq({'<31>', '<32>', '<33>'}, curbufmeths.get_lines(0, -1, false))
  173. end)
  174. it('stops processing lines when suddenly out of lines', function()
  175. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  176. eq('', exec_capture('2,$luado runs = ((runs or 0) + 1) vim.api.nvim_command("%d")'))
  177. eq({''}, curbufmeths.get_lines(0, -1, false))
  178. eq(1, funcs.luaeval('runs'))
  179. end)
  180. it('works correctly when changing lines out of range', function()
  181. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  182. eq('Vim(luado):E322: line number out of range: 1 past the end',
  183. pcall_err(command, '2,$luado vim.api.nvim_command("%d") return linenr'))
  184. eq({''}, curbufmeths.get_lines(0, -1, false))
  185. end)
  186. it('fails on errors', function()
  187. eq([[Vim(luado):E5109: Error loading lua: [string ":luado"]:0: unexpected symbol near ')']],
  188. pcall_err(command, 'luado ()'))
  189. eq([[Vim(luado):E5111: Error calling lua: [string ":luado"]:0: attempt to perform arithmetic on global 'liness' (a nil value)]],
  190. pcall_err(command, 'luado return liness + 1'))
  191. end)
  192. it('works with NULL errors', function()
  193. eq([=[Vim(luado):E5111: Error calling lua: [NULL]]=],
  194. exc_exec('luado error(nil)'))
  195. end)
  196. it('fails in sandbox when needed', function()
  197. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  198. eq('Vim(luado):E48: Not allowed in sandbox',
  199. pcall_err(command, 'sandbox luado runs = (runs or 0) + 1'))
  200. eq(NIL, funcs.luaeval('runs'))
  201. end)
  202. it('works with long strings', function()
  203. local s = ('x'):rep(100500)
  204. eq('Vim(luado):E5109: Error loading lua: [string ":luado"]:0: unfinished string near \'<eof>\'',
  205. pcall_err(command, ('luado return "%s'):format(s)))
  206. eq({''}, curbufmeths.get_lines(0, -1, false))
  207. eq('', exec_capture(('luado return "%s"'):format(s)))
  208. eq({s}, curbufmeths.get_lines(0, -1, false))
  209. end)
  210. end)
  211. describe(':luafile', function()
  212. local fname = 'Xtest-functional-lua-commands-luafile'
  213. after_each(function()
  214. os.remove(fname)
  215. end)
  216. it('works', function()
  217. write_file(fname, [[
  218. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
  219. vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
  220. vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
  221. ]])
  222. eq('', exec_capture('luafile ' .. fname))
  223. eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false))
  224. end)
  225. it('correctly errors out', function()
  226. write_file(fname, '()')
  227. eq(("Vim(luafile):E5112: Error while creating lua chunk: %s:1: unexpected symbol near ')'"):format(fname),
  228. exc_exec('luafile ' .. fname))
  229. write_file(fname, 'vimm.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})')
  230. eq(("Vim(luafile):E5113: Error while calling lua chunk: %s:1: attempt to index global 'vimm' (a nil value)"):format(fname),
  231. remove_trace(exc_exec('luafile ' .. fname)))
  232. end)
  233. it('works with NULL errors', function()
  234. write_file(fname, 'error(nil)')
  235. eq([=[Vim(luafile):E5113: Error while calling lua chunk: [NULL]]=],
  236. exc_exec('luafile ' .. fname))
  237. end)
  238. end)