mirror of
				https://github.com/neovim/neovim.git
				synced 2025-11-03 17:24:29 +00:00 
			
		
		
		
	feat(test): add Lua forms for API methods (#20152)
This commit is contained in:
		@@ -3,6 +3,7 @@ local Screen = require('test.functional.ui.screen')
 | 
				
			|||||||
local clear, nvim, buffer = helpers.clear, helpers.nvim, helpers.buffer
 | 
					local clear, nvim, buffer = helpers.clear, helpers.nvim, helpers.buffer
 | 
				
			||||||
local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq
 | 
					local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq
 | 
				
			||||||
local curbufmeths, ok = helpers.curbufmeths, helpers.ok
 | 
					local curbufmeths, ok = helpers.curbufmeths, helpers.ok
 | 
				
			||||||
 | 
					local describe_lua_and_rpc = helpers.describe_lua_and_rpc(describe)
 | 
				
			||||||
local meths = helpers.meths
 | 
					local meths = helpers.meths
 | 
				
			||||||
local funcs = helpers.funcs
 | 
					local funcs = helpers.funcs
 | 
				
			||||||
local request = helpers.request
 | 
					local request = helpers.request
 | 
				
			||||||
@@ -579,9 +580,8 @@ describe('api/buf', function()
 | 
				
			|||||||
    end)
 | 
					    end)
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  describe('nvim_buf_get_text', function()
 | 
					  describe_lua_and_rpc('nvim_buf_get_text', function(api)
 | 
				
			||||||
    local get_text = curbufmeths.get_text
 | 
					    local get_text = api.curbufmeths.get_text
 | 
				
			||||||
 | 
					 | 
				
			||||||
    before_each(function()
 | 
					    before_each(function()
 | 
				
			||||||
      insert([[
 | 
					      insert([[
 | 
				
			||||||
      hello foo!
 | 
					      hello foo!
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -605,10 +605,10 @@ describe('API', function()
 | 
				
			|||||||
      eq([[Error loading lua: [string "<nvim>"]:0: unexpected symbol]],
 | 
					      eq([[Error loading lua: [string "<nvim>"]:0: unexpected symbol]],
 | 
				
			||||||
        pcall_err(meths.exec_lua, 'aa=bb\0', {}))
 | 
					        pcall_err(meths.exec_lua, 'aa=bb\0', {}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      eq([[Error executing lua: [string "<nvim>"]:0: attempt to call global 'bork' (a nil value)]],
 | 
					      eq([[attempt to call global 'bork' (a nil value)]],
 | 
				
			||||||
        pcall_err(meths.exec_lua, 'bork()', {}))
 | 
					        pcall_err(meths.exec_lua, 'bork()', {}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      eq('Error executing lua: [string "<nvim>"]:0: did\nthe\nfail',
 | 
					      eq('did\nthe\nfail',
 | 
				
			||||||
        pcall_err(meths.exec_lua, 'error("did\\nthe\\nfail")', {}))
 | 
					        pcall_err(meths.exec_lua, 'error("did\\nthe\\nfail")', {}))
 | 
				
			||||||
    end)
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1149,7 +1149,7 @@ describe('API', function()
 | 
				
			|||||||
    end)
 | 
					    end)
 | 
				
			||||||
    it('vim.paste() failure', function()
 | 
					    it('vim.paste() failure', function()
 | 
				
			||||||
      nvim('exec_lua', 'vim.paste = (function(lines, phase) error("fake fail") end)', {})
 | 
					      nvim('exec_lua', 'vim.paste = (function(lines, phase) error("fake fail") end)', {})
 | 
				
			||||||
      eq([[Error executing lua: [string "<nvim>"]:0: fake fail]],
 | 
					      eq('fake fail',
 | 
				
			||||||
        pcall_err(request, 'nvim_paste', 'line 1\nline 2\nline 3', false, 1))
 | 
					        pcall_err(request, 'nvim_paste', 'line 1\nline 2\nline 3', false, 1))
 | 
				
			||||||
    end)
 | 
					    end)
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -112,6 +112,10 @@ function module.request(method, ...)
 | 
				
			|||||||
  return rv
 | 
					  return rv
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function module.request_lua(method, ...)
 | 
				
			||||||
 | 
					  return module.exec_lua([[return vim.api[...](select(2, ...))]], method, ...)
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function module.next_msg(timeout)
 | 
					function module.next_msg(timeout)
 | 
				
			||||||
  return session:next_message(timeout and timeout or 10000)
 | 
					  return session:next_message(timeout and timeout or 10000)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
@@ -299,12 +303,18 @@ function module.eval(expr)
 | 
				
			|||||||
  return module.request('nvim_eval', expr)
 | 
					  return module.request('nvim_eval', expr)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Executes a VimL function.
 | 
					-- Executes a VimL function via RPC.
 | 
				
			||||||
-- Fails on VimL error, but does not update v:errmsg.
 | 
					-- Fails on VimL error, but does not update v:errmsg.
 | 
				
			||||||
function module.call(name, ...)
 | 
					function module.call(name, ...)
 | 
				
			||||||
  return module.request('nvim_call_function', name, {...})
 | 
					  return module.request('nvim_call_function', name, {...})
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- Executes a VimL function via Lua.
 | 
				
			||||||
 | 
					-- Fails on VimL error, but does not update v:errmsg.
 | 
				
			||||||
 | 
					function module.call_lua(name, ...)
 | 
				
			||||||
 | 
					  return module.exec_lua([[return vim.call(...)]], name, ...)
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Sends user input to Nvim.
 | 
					-- Sends user input to Nvim.
 | 
				
			||||||
-- Does not fail on VimL error, but v:errmsg will be updated.
 | 
					-- Does not fail on VimL error, but v:errmsg will be updated.
 | 
				
			||||||
local function nvim_feed(input)
 | 
					local function nvim_feed(input)
 | 
				
			||||||
@@ -575,8 +585,16 @@ function module.set_shell_powershell(fake)
 | 
				
			|||||||
  return found
 | 
					  return found
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function module.nvim(method, ...)
 | 
					function module.create_callindex(func)
 | 
				
			||||||
  return module.request('nvim_'..method, ...)
 | 
					  local table = {}
 | 
				
			||||||
 | 
					  setmetatable(table, {
 | 
				
			||||||
 | 
					    __index = function(tbl, arg1)
 | 
				
			||||||
 | 
					      local ret = function(...) return func(arg1, ...) end
 | 
				
			||||||
 | 
					      tbl[arg1] = ret
 | 
				
			||||||
 | 
					      return ret
 | 
				
			||||||
 | 
					    end,
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					  return table
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local function ui(method, ...)
 | 
					local function ui(method, ...)
 | 
				
			||||||
@@ -587,23 +605,83 @@ function module.nvim_async(method, ...)
 | 
				
			|||||||
  session:notify('nvim_'..method, ...)
 | 
					  session:notify('nvim_'..method, ...)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function module.buffer(method, ...)
 | 
					module.async_meths = module.create_callindex(module.nvim_async)
 | 
				
			||||||
  return module.request('nvim_buf_'..method, ...)
 | 
					module.uimeths = module.create_callindex(ui)
 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
function module.window(method, ...)
 | 
					local function create_api(request, call)
 | 
				
			||||||
  return module.request('nvim_win_'..method, ...)
 | 
					  local m = {}
 | 
				
			||||||
end
 | 
					  function m.nvim(method, ...)
 | 
				
			||||||
 | 
					    return request('nvim_'..method, ...)
 | 
				
			||||||
function module.tabpage(method, ...)
 | 
					 | 
				
			||||||
  return module.request('nvim_tabpage_'..method, ...)
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function module.curbuf(method, ...)
 | 
					 | 
				
			||||||
  if not method then
 | 
					 | 
				
			||||||
    return module.nvim('get_current_buf')
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
  return module.buffer(method, 0, ...)
 | 
					
 | 
				
			||||||
 | 
					  function m.buffer(method, ...)
 | 
				
			||||||
 | 
					    return request('nvim_buf_'..method, ...)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function m.window(method, ...)
 | 
				
			||||||
 | 
					    return request('nvim_win_'..method, ...)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function m.tabpage(method, ...)
 | 
				
			||||||
 | 
					    return request('nvim_tabpage_'..method, ...)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function m.curbuf(method, ...)
 | 
				
			||||||
 | 
					    if not method then
 | 
				
			||||||
 | 
					      return m.nvim('get_current_buf')
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    return m.buffer(method, 0, ...)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function m.curwin(method, ...)
 | 
				
			||||||
 | 
					    if not method then
 | 
				
			||||||
 | 
					      return m.nvim('get_current_win')
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    return m.window(method, 0, ...)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function m.curtab(method, ...)
 | 
				
			||||||
 | 
					    if not method then
 | 
				
			||||||
 | 
					      return m.nvim('get_current_tabpage')
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    return m.tabpage(method, 0, ...)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  m.funcs = module.create_callindex(call)
 | 
				
			||||||
 | 
					  m.meths = module.create_callindex(m.nvim)
 | 
				
			||||||
 | 
					  m.bufmeths = module.create_callindex(m.buffer)
 | 
				
			||||||
 | 
					  m.winmeths = module.create_callindex(m.window)
 | 
				
			||||||
 | 
					  m.tabmeths = module.create_callindex(m.tabpage)
 | 
				
			||||||
 | 
					  m.curbufmeths = module.create_callindex(m.curbuf)
 | 
				
			||||||
 | 
					  m.curwinmeths = module.create_callindex(m.curwin)
 | 
				
			||||||
 | 
					  m.curtabmeths = module.create_callindex(m.curtab)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return m
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.rpc = {
 | 
				
			||||||
 | 
					  api = create_api(module.request, module.call),
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.lua = {
 | 
				
			||||||
 | 
					  api = create_api(module.request_lua, module.call_lua),
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.describe_lua_and_rpc = function(describe)
 | 
				
			||||||
 | 
					  return function(what, tests)
 | 
				
			||||||
 | 
					    local function d(flavour)
 | 
				
			||||||
 | 
					      describe(string.format('%s (%s)', what, flavour), function(...)
 | 
				
			||||||
 | 
					        return tests(module[flavour].api, ...)
 | 
				
			||||||
 | 
					      end)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    d('rpc')
 | 
				
			||||||
 | 
					    d('lua')
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for name, fn in pairs(module.rpc.api) do
 | 
				
			||||||
 | 
					  module[name] = fn
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function module.poke_eventloop()
 | 
					function module.poke_eventloop()
 | 
				
			||||||
@@ -622,20 +700,6 @@ function module.curbuf_contents()
 | 
				
			|||||||
  return table.concat(module.curbuf('get_lines', 0, -1, true), '\n')
 | 
					  return table.concat(module.curbuf('get_lines', 0, -1, true), '\n')
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function module.curwin(method, ...)
 | 
					 | 
				
			||||||
  if not method then
 | 
					 | 
				
			||||||
    return module.nvim('get_current_win')
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
  return module.window(method, 0, ...)
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function module.curtab(method, ...)
 | 
					 | 
				
			||||||
  if not method then
 | 
					 | 
				
			||||||
    return module.nvim('get_current_tabpage')
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
  return module.tabpage(method, 0, ...)
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function module.expect(contents)
 | 
					function module.expect(contents)
 | 
				
			||||||
  return eq(dedent(contents), module.curbuf_contents())
 | 
					  return eq(dedent(contents), module.curbuf_contents())
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
@@ -751,18 +815,6 @@ function module.exc_exec(cmd)
 | 
				
			|||||||
  return ret
 | 
					  return ret
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function module.create_callindex(func)
 | 
					 | 
				
			||||||
  local table = {}
 | 
					 | 
				
			||||||
  setmetatable(table, {
 | 
					 | 
				
			||||||
    __index = function(tbl, arg1)
 | 
					 | 
				
			||||||
      local ret = function(...) return func(arg1, ...) end
 | 
					 | 
				
			||||||
      tbl[arg1] = ret
 | 
					 | 
				
			||||||
      return ret
 | 
					 | 
				
			||||||
    end,
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
  return table
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function module.skip(cond, reason)
 | 
					function module.skip(cond, reason)
 | 
				
			||||||
  if cond then
 | 
					  if cond then
 | 
				
			||||||
    local pending = getfenv(2).pending
 | 
					    local pending = getfenv(2).pending
 | 
				
			||||||
@@ -789,17 +841,6 @@ function module.skip_fragile(pending_fn, cond)
 | 
				
			|||||||
  return false
 | 
					  return false
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.funcs = module.create_callindex(module.call)
 | 
					 | 
				
			||||||
module.meths = module.create_callindex(module.nvim)
 | 
					 | 
				
			||||||
module.async_meths = module.create_callindex(module.nvim_async)
 | 
					 | 
				
			||||||
module.uimeths = module.create_callindex(ui)
 | 
					 | 
				
			||||||
module.bufmeths = module.create_callindex(module.buffer)
 | 
					 | 
				
			||||||
module.winmeths = module.create_callindex(module.window)
 | 
					 | 
				
			||||||
module.tabmeths = module.create_callindex(module.tabpage)
 | 
					 | 
				
			||||||
module.curbufmeths = module.create_callindex(module.curbuf)
 | 
					 | 
				
			||||||
module.curwinmeths = module.create_callindex(module.curwin)
 | 
					 | 
				
			||||||
module.curtabmeths = module.create_callindex(module.curtab)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function module.exec(code)
 | 
					function module.exec(code)
 | 
				
			||||||
  return module.meths.exec(code, false)
 | 
					  return module.meths.exec(code, false)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1776,7 +1776,7 @@ end)
 | 
				
			|||||||
        return lines
 | 
					        return lines
 | 
				
			||||||
      ]])
 | 
					      ]])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      eq("Error executing lua: .../diagnostic.lua:0: prefix: expected 'string' or 'table' or 'function', got 42",
 | 
					      eq(".../diagnostic.lua:0: prefix: expected 'string' or 'table' or 'function', got 42",
 | 
				
			||||||
        pcall_err(exec_lua, [[ vim.diagnostic.open_float({ prefix = 42 }) ]]))
 | 
					        pcall_err(exec_lua, [[ vim.diagnostic.open_float({ prefix = 42 }) ]]))
 | 
				
			||||||
    end)
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,7 +15,7 @@ describe('vim.spell', function()
 | 
				
			|||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it('can handle nil', function()
 | 
					    it('can handle nil', function()
 | 
				
			||||||
      eq([[Error executing lua: [string "<nvim>"]:0: bad argument #1 to 'check' (expected string)]],
 | 
					      eq([[bad argument #1 to 'check' (expected string)]],
 | 
				
			||||||
        pcall_err(exec_lua, [[vim.spell.check(nil)]]))
 | 
					        pcall_err(exec_lua, [[vim.spell.check(nil)]]))
 | 
				
			||||||
    end)
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -272,7 +272,7 @@ describe('threadpool', function()
 | 
				
			|||||||
      work:queue({})
 | 
					      work:queue({})
 | 
				
			||||||
    ]])
 | 
					    ]])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    eq([[Error executing lua: [string "<nvim>"]:0: Error: thread arg not support type 'function' at 1]],
 | 
					    eq([[Error: thread arg not support type 'function' at 1]],
 | 
				
			||||||
       status)
 | 
					       status)
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -168,8 +168,8 @@ describe('lua stdlib', function()
 | 
				
			|||||||
    for i,k in pairs(indicies16) do
 | 
					    for i,k in pairs(indicies16) do
 | 
				
			||||||
      eq(k, exec_lua("return vim.str_byteindex(_G.test_text, ..., true)", i), i)
 | 
					      eq(k, exec_lua("return vim.str_byteindex(_G.test_text, ..., true)", i), i)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    matches(": index out of range$", pcall_err(exec_lua, "return vim.str_byteindex(_G.test_text, ...)", #indicies32 + 1))
 | 
					    eq("index out of range", pcall_err(exec_lua, "return vim.str_byteindex(_G.test_text, ...)", #indicies32 + 1))
 | 
				
			||||||
    matches(": index out of range$", pcall_err(exec_lua, "return vim.str_byteindex(_G.test_text, ..., true)", #indicies16 + 1))
 | 
					    eq("index out of range", pcall_err(exec_lua, "return vim.str_byteindex(_G.test_text, ..., true)", #indicies16 + 1))
 | 
				
			||||||
    local i32, i16 = 0, 0
 | 
					    local i32, i16 = 0, 0
 | 
				
			||||||
    local len = 51
 | 
					    local len = 51
 | 
				
			||||||
    for k = 0,len do
 | 
					    for k = 0,len do
 | 
				
			||||||
@@ -184,7 +184,7 @@ describe('lua stdlib', function()
 | 
				
			|||||||
      end
 | 
					      end
 | 
				
			||||||
      eq({i32, i16}, exec_lua("return {vim.str_utfindex(_G.test_text, ...)}", k), k)
 | 
					      eq({i32, i16}, exec_lua("return {vim.str_utfindex(_G.test_text, ...)}", k), k)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    matches(": index out of range$", pcall_err(exec_lua, "return vim.str_utfindex(_G.test_text, ...)", len + 1))
 | 
					    eq("index out of range", pcall_err(exec_lua, "return vim.str_utfindex(_G.test_text, ...)", len + 1))
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it("vim.str_utf_start", function()
 | 
					  it("vim.str_utf_start", function()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -74,11 +74,13 @@ describe('xdiff bindings', function()
 | 
				
			|||||||
    end)
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it('with error callback', function()
 | 
					    it('with error callback', function()
 | 
				
			||||||
      exec_lua([[on_hunk = function(sa, ca, sb, cb)
 | 
					      exec_lua[[
 | 
				
			||||||
 | 
					        on_hunk = function(sa, ca, sb, cb)
 | 
				
			||||||
          error('ERROR1')
 | 
					          error('ERROR1')
 | 
				
			||||||
        end]])
 | 
					        end
 | 
				
			||||||
 | 
					      ]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      eq([[Error executing lua: [string "<nvim>"]:0: error running function on_hunk: [string "<nvim>"]:0: ERROR1]],
 | 
					      eq([[error running function on_hunk: [string "<nvim>"]:0: ERROR1]],
 | 
				
			||||||
        pcall_err(exec_lua, [[vim.diff(a1, b1, {on_hunk = on_hunk})]]))
 | 
					        pcall_err(exec_lua, [[vim.diff(a1, b1, {on_hunk = on_hunk})]]))
 | 
				
			||||||
    end)
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -135,19 +137,19 @@ describe('xdiff bindings', function()
 | 
				
			|||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it('can handle bad args', function()
 | 
					  it('can handle bad args', function()
 | 
				
			||||||
    eq([[Error executing lua: [string "<nvim>"]:0: Expected at least 2 arguments]],
 | 
					    eq([[Expected at least 2 arguments]],
 | 
				
			||||||
      pcall_err(exec_lua, [[vim.diff('a')]]))
 | 
					      pcall_err(exec_lua, [[vim.diff('a')]]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    eq([[Error executing lua: [string "<nvim>"]:0: bad argument #1 to 'diff' (expected string)]],
 | 
					    eq([[bad argument #1 to 'diff' (expected string)]],
 | 
				
			||||||
      pcall_err(exec_lua, [[vim.diff(1, 2)]]))
 | 
					      pcall_err(exec_lua, [[vim.diff(1, 2)]]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    eq([[Error executing lua: [string "<nvim>"]:0: bad argument #3 to 'diff' (expected table)]],
 | 
					    eq([[bad argument #3 to 'diff' (expected table)]],
 | 
				
			||||||
      pcall_err(exec_lua, [[vim.diff('a', 'b', true)]]))
 | 
					      pcall_err(exec_lua, [[vim.diff('a', 'b', true)]]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    eq([[Error executing lua: [string "<nvim>"]:0: unexpected key: bad_key]],
 | 
					    eq([[unexpected key: bad_key]],
 | 
				
			||||||
      pcall_err(exec_lua, [[vim.diff('a', 'b', { bad_key = true })]]))
 | 
					      pcall_err(exec_lua, [[vim.diff('a', 'b', { bad_key = true })]]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    eq([[Error executing lua: [string "<nvim>"]:0: on_hunk is not a function]],
 | 
					    eq([[on_hunk is not a function]],
 | 
				
			||||||
      pcall_err(exec_lua, [[vim.diff('a', 'b', { on_hunk = true })]]))
 | 
					      pcall_err(exec_lua, [[vim.diff('a', 'b', { on_hunk = true })]]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -238,9 +238,9 @@ describe('LSP', function()
 | 
				
			|||||||
    end)
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it('should invalid cmd argument', function()
 | 
					    it('should invalid cmd argument', function()
 | 
				
			||||||
      eq('Error executing lua: .../lsp.lua:0: cmd: expected list, got nvim',
 | 
					      eq('.../lsp.lua:0: cmd: expected list, got nvim',
 | 
				
			||||||
        pcall_err(_cmd_parts, 'nvim'))
 | 
					        pcall_err(_cmd_parts, 'nvim'))
 | 
				
			||||||
      eq('Error executing lua: .../lsp.lua:0: cmd argument: expected string, got number',
 | 
					      eq('.../lsp.lua:0: cmd argument: expected string, got number',
 | 
				
			||||||
        pcall_err(_cmd_parts, {'nvim', 1}))
 | 
					        pcall_err(_cmd_parts, {'nvim', 1}))
 | 
				
			||||||
    end)
 | 
					    end)
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,21 +13,21 @@ before_each(clear)
 | 
				
			|||||||
describe('treesitter language API', function()
 | 
					describe('treesitter language API', function()
 | 
				
			||||||
  -- error tests not requiring a parser library
 | 
					  -- error tests not requiring a parser library
 | 
				
			||||||
  it('handles missing language', function()
 | 
					  it('handles missing language', function()
 | 
				
			||||||
    eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
 | 
					    eq(".../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
 | 
				
			||||||
       pcall_err(exec_lua, "parser = vim.treesitter.get_parser(0, 'borklang')"))
 | 
					       pcall_err(exec_lua, "parser = vim.treesitter.get_parser(0, 'borklang')"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    -- actual message depends on platform
 | 
					    -- actual message depends on platform
 | 
				
			||||||
    matches("Error executing lua: Failed to load parser: uv_dlopen: .+",
 | 
					    matches("Failed to load parser: uv_dlopen: .+",
 | 
				
			||||||
       pcall_err(exec_lua, "parser = vim.treesitter.require_language('borklang', 'borkbork.so')"))
 | 
					       pcall_err(exec_lua, "parser = vim.treesitter.require_language('borklang', 'borkbork.so')"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    -- Should not throw an error when silent
 | 
					    -- Should not throw an error when silent
 | 
				
			||||||
    eq(false, exec_lua("return vim.treesitter.require_language('borklang', nil, true)"))
 | 
					    eq(false, exec_lua("return vim.treesitter.require_language('borklang', nil, true)"))
 | 
				
			||||||
    eq(false, exec_lua("return vim.treesitter.require_language('borklang', 'borkbork.so', true)"))
 | 
					    eq(false, exec_lua("return vim.treesitter.require_language('borklang', 'borkbork.so', true)"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
 | 
					    eq(".../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
 | 
				
			||||||
       pcall_err(exec_lua, "parser = vim.treesitter.inspect_language('borklang')"))
 | 
					       pcall_err(exec_lua, "parser = vim.treesitter.inspect_language('borklang')"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    matches("Error executing lua: Failed to load parser: uv_dlsym: .+",
 | 
					    matches("Failed to load parser: uv_dlsym: .+",
 | 
				
			||||||
       pcall_err(exec_lua, 'vim.treesitter.require_language("c", nil, false, "borklang")'))
 | 
					       pcall_err(exec_lua, 'vim.treesitter.require_language("c", nil, false, "borklang")'))
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -76,7 +76,7 @@ describe('treesitter language API', function()
 | 
				
			|||||||
    eq('c', exec_lua("return vim.treesitter.get_parser(0):lang()"))
 | 
					    eq('c', exec_lua("return vim.treesitter.get_parser(0):lang()"))
 | 
				
			||||||
    command("set filetype=borklang")
 | 
					    command("set filetype=borklang")
 | 
				
			||||||
    -- Should throw an error when filetype changes to borklang
 | 
					    -- Should throw an error when filetype changes to borklang
 | 
				
			||||||
    eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
 | 
					    eq(".../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
 | 
				
			||||||
       pcall_err(exec_lua, "new_parser = vim.treesitter.get_parser(0)"))
 | 
					       pcall_err(exec_lua, "new_parser = vim.treesitter.get_parser(0)"))
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -93,7 +93,7 @@ describe('float window', function()
 | 
				
			|||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it('closed immediately by autocmd #11383', function()
 | 
					  it('closed immediately by autocmd #11383', function()
 | 
				
			||||||
    eq('Error executing lua: [string "<nvim>"]:0: Window was closed immediately',
 | 
					    eq('Window was closed immediately',
 | 
				
			||||||
      pcall_err(exec_lua, [[
 | 
					      pcall_err(exec_lua, [[
 | 
				
			||||||
        local a = vim.api
 | 
					        local a = vim.api
 | 
				
			||||||
        local function crashes(contents)
 | 
					        local function crashes(contents)
 | 
				
			||||||
@@ -118,7 +118,7 @@ describe('float window', function()
 | 
				
			|||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it('closed immediately by autocmd after win_enter #15548', function()
 | 
					  it('closed immediately by autocmd after win_enter #15548', function()
 | 
				
			||||||
    eq('Error executing lua: [string "<nvim>"]:0: Window was closed immediately',
 | 
					    eq('Window was closed immediately',
 | 
				
			||||||
      pcall_err(exec_lua, [[
 | 
					      pcall_err(exec_lua, [[
 | 
				
			||||||
        vim.cmd "autocmd BufLeave * ++once quit!"
 | 
					        vim.cmd "autocmd BufLeave * ++once quit!"
 | 
				
			||||||
        local buf = vim.api.nvim_create_buf(true, true)
 | 
					        local buf = vim.api.nvim_create_buf(true, true)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -195,7 +195,7 @@ describe('listing functions using :function', function()
 | 
				
			|||||||
  it('does not crash if another function is deleted while listing', function()
 | 
					  it('does not crash if another function is deleted while listing', function()
 | 
				
			||||||
    local screen = Screen.new(80, 24)
 | 
					    local screen = Screen.new(80, 24)
 | 
				
			||||||
    screen:attach()
 | 
					    screen:attach()
 | 
				
			||||||
    matches('.*: Vim%(function%):E454: function list was modified', pcall_err(exec_lua, [=[
 | 
					    matches('Vim%(function%):E454: function list was modified', pcall_err(exec_lua, [=[
 | 
				
			||||||
      vim.cmd([[
 | 
					      vim.cmd([[
 | 
				
			||||||
        func Func1()
 | 
					        func Func1()
 | 
				
			||||||
        endfunc
 | 
					        endfunc
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -246,9 +246,9 @@ describe('mapset()', function()
 | 
				
			|||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it('does not leak memory if lhs is missing', function()
 | 
					  it('does not leak memory if lhs is missing', function()
 | 
				
			||||||
    eq('Error executing lua: Vim:E460: entries missing in mapset() dict argument',
 | 
					    eq('Vim:E460: entries missing in mapset() dict argument',
 | 
				
			||||||
       pcall_err(exec_lua, [[vim.fn.mapset('n', false, {rhs = 'foo'})]]))
 | 
					       pcall_err(exec_lua, [[vim.fn.mapset('n', false, {rhs = 'foo'})]]))
 | 
				
			||||||
    eq('Error executing lua: Vim:E460: entries missing in mapset() dict argument',
 | 
					    eq('Vim:E460: entries missing in mapset() dict argument',
 | 
				
			||||||
       pcall_err(exec_lua, [[vim.fn.mapset('n', false, {callback = function() end})]]))
 | 
					       pcall_err(exec_lua, [[vim.fn.mapset('n', false, {callback = function() end})]]))
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
end)
 | 
					end)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -114,25 +114,13 @@ function module.assert_nolog(pat, logfile, nrlines)
 | 
				
			|||||||
  return module.assert_log(pat, logfile, nrlines, true)
 | 
					  return module.assert_log(pat, logfile, nrlines, true)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Invokes `fn` and returns the error string (with truncated paths), or raises
 | 
					function module.pcall(fn, ...)
 | 
				
			||||||
-- an error if `fn` succeeds.
 | 
					 | 
				
			||||||
--
 | 
					 | 
				
			||||||
-- Replaces line/column numbers with zero:
 | 
					 | 
				
			||||||
--     shared.lua:0: in function 'gsplit'
 | 
					 | 
				
			||||||
--     shared.lua:0: in function <shared.lua:0>'
 | 
					 | 
				
			||||||
--
 | 
					 | 
				
			||||||
-- Usage:
 | 
					 | 
				
			||||||
--    -- Match exact string.
 | 
					 | 
				
			||||||
--    eq('e', pcall_err(function(a, b) error('e') end, 'arg1', 'arg2'))
 | 
					 | 
				
			||||||
--    -- Match Lua pattern.
 | 
					 | 
				
			||||||
--    matches('e[or]+$', pcall_err(function(a, b) error('some error') end, 'arg1', 'arg2'))
 | 
					 | 
				
			||||||
--
 | 
					 | 
				
			||||||
function module.pcall_err_withfile(fn, ...)
 | 
					 | 
				
			||||||
  assert(type(fn) == 'function')
 | 
					  assert(type(fn) == 'function')
 | 
				
			||||||
  local status, rv = pcall(fn, ...)
 | 
					  local status, rv = pcall(fn, ...)
 | 
				
			||||||
  if status == true then
 | 
					  if status then
 | 
				
			||||||
    error('expected failure, but got success')
 | 
					    return status, rv
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  -- From:
 | 
					  -- From:
 | 
				
			||||||
  --    C:/long/path/foo.lua:186: Expected string, got number
 | 
					  --    C:/long/path/foo.lua:186: Expected string, got number
 | 
				
			||||||
  -- to:
 | 
					  -- to:
 | 
				
			||||||
@@ -150,13 +138,37 @@ function module.pcall_err_withfile(fn, ...)
 | 
				
			|||||||
  --    We remove this so that the tests are not lua dependent.
 | 
					  --    We remove this so that the tests are not lua dependent.
 | 
				
			||||||
  errmsg = errmsg:gsub('%s*%(tail call%): %?', '')
 | 
					  errmsg = errmsg:gsub('%s*%(tail call%): %?', '')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return errmsg
 | 
					  return status, errmsg
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- Invokes `fn` and returns the error string (with truncated paths), or raises
 | 
				
			||||||
 | 
					-- an error if `fn` succeeds.
 | 
				
			||||||
 | 
					--
 | 
				
			||||||
 | 
					-- Replaces line/column numbers with zero:
 | 
				
			||||||
 | 
					--     shared.lua:0: in function 'gsplit'
 | 
				
			||||||
 | 
					--     shared.lua:0: in function <shared.lua:0>'
 | 
				
			||||||
 | 
					--
 | 
				
			||||||
 | 
					-- Usage:
 | 
				
			||||||
 | 
					--    -- Match exact string.
 | 
				
			||||||
 | 
					--    eq('e', pcall_err(function(a, b) error('e') end, 'arg1', 'arg2'))
 | 
				
			||||||
 | 
					--    -- Match Lua pattern.
 | 
				
			||||||
 | 
					--    matches('e[or]+$', pcall_err(function(a, b) error('some error') end, 'arg1', 'arg2'))
 | 
				
			||||||
 | 
					--
 | 
				
			||||||
 | 
					function module.pcall_err_withfile(fn, ...)
 | 
				
			||||||
 | 
					  assert(type(fn) == 'function')
 | 
				
			||||||
 | 
					  local status, rv = module.pcall(fn, ...)
 | 
				
			||||||
 | 
					  if status == true then
 | 
				
			||||||
 | 
					    error('expected failure, but got success')
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					  return rv
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function module.pcall_err_withtrace(fn, ...)
 | 
					function module.pcall_err_withtrace(fn, ...)
 | 
				
			||||||
  local errmsg = module.pcall_err_withfile(fn, ...)
 | 
					  local errmsg = module.pcall_err_withfile(fn, ...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return errmsg:gsub('.../helpers.lua:0: ', '')
 | 
					  return errmsg:gsub('^%.%.%./helpers%.lua:0: ', '')
 | 
				
			||||||
 | 
					               :gsub('^Error executing lua:- ' ,'')
 | 
				
			||||||
 | 
					               :gsub('^%[string "<nvim>"%]:0: ' ,'')
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function module.pcall_err(...)
 | 
					function module.pcall_err(...)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user