diff options
author | Lewis Russell <lewis6991@gmail.com> | 2022-11-14 10:01:35 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-14 10:01:35 +0000 |
commit | e8cc489accc435076afb4fdf89778b64f0a48473 (patch) | |
tree | 78b5a74c13cefb916752f25cf42e301a400fcab2 | |
parent | 5c5187c6f809c50e2cfd0ba04961ae8e0d2dabd0 (diff) | |
download | rneovim-e8cc489accc435076afb4fdf89778b64f0a48473.tar.gz rneovim-e8cc489accc435076afb4fdf89778b64f0a48473.tar.bz2 rneovim-e8cc489accc435076afb4fdf89778b64f0a48473.zip |
feat(test): add Lua forms for API methods (#20152)
-rw-r--r-- | test/functional/api/buffer_spec.lua | 6 | ||||
-rw-r--r-- | test/functional/api/vim_spec.lua | 6 | ||||
-rw-r--r-- | test/functional/helpers.lua | 145 | ||||
-rw-r--r-- | test/functional/lua/diagnostic_spec.lua | 2 | ||||
-rw-r--r-- | test/functional/lua/spell_spec.lua | 2 | ||||
-rw-r--r-- | test/functional/lua/thread_spec.lua | 2 | ||||
-rw-r--r-- | test/functional/lua/vim_spec.lua | 6 | ||||
-rw-r--r-- | test/functional/lua/xdiff_spec.lua | 18 | ||||
-rw-r--r-- | test/functional/plugin/lsp_spec.lua | 4 | ||||
-rw-r--r-- | test/functional/treesitter/language_spec.lua | 10 | ||||
-rw-r--r-- | test/functional/ui/float_spec.lua | 4 | ||||
-rw-r--r-- | test/functional/vimscript/eval_spec.lua | 2 | ||||
-rw-r--r-- | test/functional/vimscript/map_functions_spec.lua | 4 | ||||
-rw-r--r-- | test/helpers.lua | 48 |
14 files changed, 157 insertions, 102 deletions
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index 8f6fc666c9..598bfeb754 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -3,6 +3,7 @@ local Screen = require('test.functional.ui.screen') local clear, nvim, buffer = helpers.clear, helpers.nvim, helpers.buffer local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq local curbufmeths, ok = helpers.curbufmeths, helpers.ok +local describe_lua_and_rpc = helpers.describe_lua_and_rpc(describe) local meths = helpers.meths local funcs = helpers.funcs local request = helpers.request @@ -579,9 +580,8 @@ describe('api/buf', function() end) end) - describe('nvim_buf_get_text', function() - local get_text = curbufmeths.get_text - + describe_lua_and_rpc('nvim_buf_get_text', function(api) + local get_text = api.curbufmeths.get_text before_each(function() insert([[ hello foo! diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 7c24f32f74..9df73c8d69 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -605,10 +605,10 @@ describe('API', function() eq([[Error loading lua: [string "<nvim>"]:0: unexpected symbol]], 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()', {})) - eq('Error executing lua: [string "<nvim>"]:0: did\nthe\nfail', + eq('did\nthe\nfail', pcall_err(meths.exec_lua, 'error("did\\nthe\\nfail")', {})) end) @@ -1149,7 +1149,7 @@ describe('API', function() end) it('vim.paste() failure', function() 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)) end) end) diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index f0a85785b3..1cd632f00b 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -112,6 +112,10 @@ function module.request(method, ...) return rv end +function module.request_lua(method, ...) + return module.exec_lua([[return vim.api[...](select(2, ...))]], method, ...) +end + function module.next_msg(timeout) return session:next_message(timeout and timeout or 10000) end @@ -299,12 +303,18 @@ function module.eval(expr) return module.request('nvim_eval', expr) end --- Executes a VimL function. +-- Executes a VimL function via RPC. -- Fails on VimL error, but does not update v:errmsg. function module.call(name, ...) return module.request('nvim_call_function', name, {...}) 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. -- Does not fail on VimL error, but v:errmsg will be updated. local function nvim_feed(input) @@ -575,8 +585,16 @@ function module.set_shell_powershell(fake) return found end -function module.nvim(method, ...) - return module.request('nvim_'..method, ...) +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 local function ui(method, ...) @@ -587,23 +605,83 @@ function module.nvim_async(method, ...) session:notify('nvim_'..method, ...) end -function module.buffer(method, ...) - return module.request('nvim_buf_'..method, ...) -end +module.async_meths = module.create_callindex(module.nvim_async) +module.uimeths = module.create_callindex(ui) -function module.window(method, ...) - return module.request('nvim_win_'..method, ...) -end +local function create_api(request, call) + local m = {} + function m.nvim(method, ...) + return request('nvim_'..method, ...) + end + + 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 module.tabpage(method, ...) - return module.request('nvim_tabpage_'..method, ...) + 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 -function module.curbuf(method, ...) - if not method then - return module.nvim('get_current_buf') +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 - return module.buffer(method, 0, ...) +end + +for name, fn in pairs(module.rpc.api) do + module[name] = fn end function module.poke_eventloop() @@ -622,20 +700,6 @@ function module.curbuf_contents() return table.concat(module.curbuf('get_lines', 0, -1, true), '\n') 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) return eq(dedent(contents), module.curbuf_contents()) end @@ -751,18 +815,6 @@ function module.exc_exec(cmd) return ret 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) if cond then local pending = getfenv(2).pending @@ -789,17 +841,6 @@ function module.skip_fragile(pending_fn, cond) return false 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) return module.meths.exec(code, false) end diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 28a8679205..565243d5a0 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -1776,7 +1776,7 @@ end) 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 }) ]])) end) diff --git a/test/functional/lua/spell_spec.lua b/test/functional/lua/spell_spec.lua index 7e831f16a7..b3de6a0912 100644 --- a/test/functional/lua/spell_spec.lua +++ b/test/functional/lua/spell_spec.lua @@ -15,7 +15,7 @@ describe('vim.spell', function() end 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)]])) end) diff --git a/test/functional/lua/thread_spec.lua b/test/functional/lua/thread_spec.lua index e183ce3a57..c7f2783cf3 100644 --- a/test/functional/lua/thread_spec.lua +++ b/test/functional/lua/thread_spec.lua @@ -272,7 +272,7 @@ describe('threadpool', function() 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) end) diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index 38f772c5cf..6d0d87746c 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -168,8 +168,8 @@ describe('lua stdlib', function() for i,k in pairs(indicies16) do eq(k, exec_lua("return vim.str_byteindex(_G.test_text, ..., true)", i), i) end - matches(": 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, ...)", #indicies32 + 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 len = 51 for k = 0,len do @@ -184,7 +184,7 @@ describe('lua stdlib', function() end eq({i32, i16}, exec_lua("return {vim.str_utfindex(_G.test_text, ...)}", k), k) 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) it("vim.str_utf_start", function() diff --git a/test/functional/lua/xdiff_spec.lua b/test/functional/lua/xdiff_spec.lua index d55268fc78..3121ac051f 100644 --- a/test/functional/lua/xdiff_spec.lua +++ b/test/functional/lua/xdiff_spec.lua @@ -74,11 +74,13 @@ describe('xdiff bindings', function() end) 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') - 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})]])) end) @@ -135,19 +137,19 @@ describe('xdiff bindings', function() end) 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')]])) - 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)]])) - 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)]])) - 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 })]])) - 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 })]])) end) diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 572d7a7d97..2ab1e34de4 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -238,9 +238,9 @@ describe('LSP', function() end) 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')) - 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})) end) end) diff --git a/test/functional/treesitter/language_spec.lua b/test/functional/treesitter/language_spec.lua index ed84dedb5a..1db8381a93 100644 --- a/test/functional/treesitter/language_spec.lua +++ b/test/functional/treesitter/language_spec.lua @@ -13,21 +13,21 @@ before_each(clear) describe('treesitter language API', function() -- error tests not requiring a parser library 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')")) -- 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')")) -- 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', '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')")) - 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")')) end) @@ -76,7 +76,7 @@ describe('treesitter language API', function() eq('c', exec_lua("return vim.treesitter.get_parser(0):lang()")) command("set filetype=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)")) end) diff --git a/test/functional/ui/float_spec.lua b/test/functional/ui/float_spec.lua index cb90a902d7..c7ee4ee87d 100644 --- a/test/functional/ui/float_spec.lua +++ b/test/functional/ui/float_spec.lua @@ -93,7 +93,7 @@ describe('float window', function() end) 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, [[ local a = vim.api local function crashes(contents) @@ -118,7 +118,7 @@ describe('float window', function() end) 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, [[ vim.cmd "autocmd BufLeave * ++once quit!" local buf = vim.api.nvim_create_buf(true, true) diff --git a/test/functional/vimscript/eval_spec.lua b/test/functional/vimscript/eval_spec.lua index 9b0ace0882..d50c252569 100644 --- a/test/functional/vimscript/eval_spec.lua +++ b/test/functional/vimscript/eval_spec.lua @@ -195,7 +195,7 @@ describe('listing functions using :function', function() it('does not crash if another function is deleted while listing', function() local screen = Screen.new(80, 24) 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([[ func Func1() endfunc diff --git a/test/functional/vimscript/map_functions_spec.lua b/test/functional/vimscript/map_functions_spec.lua index 8645b1e506..ba1b4d7a76 100644 --- a/test/functional/vimscript/map_functions_spec.lua +++ b/test/functional/vimscript/map_functions_spec.lua @@ -246,9 +246,9 @@ describe('mapset()', function() end) 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'})]])) - 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})]])) end) end) diff --git a/test/helpers.lua b/test/helpers.lua index 225a4d35b4..68c1b17dbc 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -114,25 +114,13 @@ function module.assert_nolog(pat, logfile, nrlines) return module.assert_log(pat, logfile, nrlines, true) 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, ...) +function module.pcall(fn, ...) assert(type(fn) == 'function') local status, rv = pcall(fn, ...) - if status == true then - error('expected failure, but got success') + if status then + return status, rv end + -- From: -- C:/long/path/foo.lua:186: Expected string, got number -- to: @@ -150,13 +138,37 @@ function module.pcall_err_withfile(fn, ...) -- We remove this so that the tests are not lua dependent. 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 function module.pcall_err_withtrace(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 function module.pcall_err(...) |