commands_spec.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. -- Test suite for checking :lua* commands
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local eq = helpers.eq
  4. local NIL = helpers.NIL
  5. local clear = helpers.clear
  6. local meths = helpers.meths
  7. local funcs = helpers.funcs
  8. local source = helpers.source
  9. local dedent = helpers.dedent
  10. local exc_exec = helpers.exc_exec
  11. local write_file = helpers.write_file
  12. local redir_exec = helpers.redir_exec
  13. local curbufmeths = helpers.curbufmeths
  14. before_each(clear)
  15. describe(':lua command', function()
  16. it('works', function()
  17. eq('', redir_exec(
  18. 'lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TEST"})'))
  19. eq({'', 'TEST'}, curbufmeths.get_lines(0, 100, false))
  20. source(dedent([[
  21. lua << EOF
  22. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TSET"})
  23. EOF]]))
  24. eq({'', 'TSET'}, curbufmeths.get_lines(0, 100, false))
  25. source(dedent([[
  26. lua << EOF
  27. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"SETT"})]]))
  28. eq({'', 'SETT'}, curbufmeths.get_lines(0, 100, false))
  29. source(dedent([[
  30. lua << EOF
  31. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
  32. vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
  33. vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
  34. EOF]]))
  35. eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false))
  36. end)
  37. it('throws catchable errors', function()
  38. eq([[Vim(lua):E5104: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unexpected symbol near ')']],
  39. exc_exec('lua ()'))
  40. eq([[Vim(lua):E5105: Error while calling lua chunk: [string "<VimL compiled string>"]:1: TEST]],
  41. exc_exec('lua error("TEST")'))
  42. eq([[Vim(lua):E5105: Error while calling lua chunk: [string "<VimL compiled string>"]:1: Invalid buffer id]],
  43. exc_exec('lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})'))
  44. eq({''}, curbufmeths.get_lines(0, 100, false))
  45. end)
  46. it('works with NULL errors', function()
  47. eq([=[Vim(lua):E5105: Error while calling lua chunk: [NULL]]=],
  48. exc_exec('lua error(nil)'))
  49. end)
  50. it('accepts embedded NLs without heredoc', function()
  51. -- Such code is usually used for `:execute 'lua' {generated_string}`:
  52. -- heredocs do not work in this case.
  53. meths.command([[
  54. lua
  55. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
  56. vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
  57. vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
  58. ]])
  59. eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false))
  60. end)
  61. it('preserves global and not preserves local variables', function()
  62. eq('', redir_exec('lua gvar = 42'))
  63. eq('', redir_exec('lua local lvar = 100500'))
  64. eq(NIL, funcs.luaeval('lvar'))
  65. eq(42, funcs.luaeval('gvar'))
  66. end)
  67. it('works with long strings', function()
  68. local s = ('x'):rep(100500)
  69. eq('\nE5104: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unfinished string near \'<eof>\'', redir_exec(('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s})'):format(s)))
  70. eq({''}, curbufmeths.get_lines(0, -1, false))
  71. eq('', redir_exec(('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s"})'):format(s)))
  72. eq({'', s}, curbufmeths.get_lines(0, -1, false))
  73. end)
  74. end)
  75. describe(':luado command', function()
  76. it('works', function()
  77. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  78. eq('', redir_exec('luado lines = (lines or {}) lines[#lines + 1] = {linenr, line}'))
  79. eq({'ABC', 'def', 'gHi'}, curbufmeths.get_lines(0, -1, false))
  80. eq({{1, 'ABC'}, {2, 'def'}, {3, 'gHi'}}, funcs.luaeval('lines'))
  81. -- Automatic transformation of numbers
  82. eq('', redir_exec('luado return linenr'))
  83. eq({'1', '2', '3'}, curbufmeths.get_lines(0, -1, false))
  84. eq('', redir_exec('luado return ("<%02x>"):format(line:byte())'))
  85. eq({'<31>', '<32>', '<33>'}, curbufmeths.get_lines(0, -1, false))
  86. end)
  87. it('stops processing lines when suddenly out of lines', function()
  88. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  89. eq('', redir_exec('2,$luado runs = ((runs or 0) + 1) vim.api.nvim_command("%d")'))
  90. eq({''}, curbufmeths.get_lines(0, -1, false))
  91. eq(1, funcs.luaeval('runs'))
  92. end)
  93. it('works correctly when changing lines out of range', function()
  94. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  95. eq('\nE322: line number out of range: 1 past the end\nE320: Cannot find line 2',
  96. redir_exec('2,$luado vim.api.nvim_command("%d") return linenr'))
  97. eq({''}, curbufmeths.get_lines(0, -1, false))
  98. end)
  99. it('fails on errors', function()
  100. eq([[Vim(luado):E5109: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unexpected symbol near ')']],
  101. exc_exec('luado ()'))
  102. eq([[Vim(luado):E5111: Error while calling lua function: [string "<VimL compiled string>"]:1: attempt to perform arithmetic on global 'liness' (a nil value)]],
  103. exc_exec('luado return liness + 1'))
  104. end)
  105. it('works with NULL errors', function()
  106. eq([=[Vim(luado):E5111: Error while calling lua function: [NULL]]=],
  107. exc_exec('luado error(nil)'))
  108. end)
  109. it('fails in sandbox when needed', function()
  110. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  111. eq('\nE48: Not allowed in sandbox: sandbox luado runs = (runs or 0) + 1',
  112. redir_exec('sandbox luado runs = (runs or 0) + 1'))
  113. eq(NIL, funcs.luaeval('runs'))
  114. end)
  115. it('works with long strings', function()
  116. local s = ('x'):rep(100500)
  117. eq('\nE5109: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unfinished string near \'<eof>\'', redir_exec(('luado return "%s'):format(s)))
  118. eq({''}, curbufmeths.get_lines(0, -1, false))
  119. eq('', redir_exec(('luado return "%s"'):format(s)))
  120. eq({s}, curbufmeths.get_lines(0, -1, false))
  121. end)
  122. end)
  123. describe(':luafile', function()
  124. local fname = 'Xtest-functional-lua-commands-luafile'
  125. after_each(function()
  126. os.remove(fname)
  127. end)
  128. it('works', function()
  129. write_file(fname, [[
  130. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
  131. vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
  132. vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
  133. ]])
  134. eq('', redir_exec('luafile ' .. fname))
  135. eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false))
  136. end)
  137. it('correctly errors out', function()
  138. write_file(fname, '()')
  139. eq(("Vim(luafile):E5112: Error while creating lua chunk: %s:1: unexpected symbol near ')'"):format(fname),
  140. exc_exec('luafile ' .. fname))
  141. write_file(fname, 'vimm.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})')
  142. eq(("Vim(luafile):E5113: Error while calling lua chunk: %s:1: attempt to index global 'vimm' (a nil value)"):format(fname),
  143. exc_exec('luafile ' .. fname))
  144. end)
  145. it('works with NULL errors', function()
  146. write_file(fname, 'error(nil)')
  147. eq([=[Vim(luafile):E5113: Error while calling lua chunk: [NULL]]=],
  148. exc_exec('luafile ' .. fname))
  149. end)
  150. end)