diff options
Diffstat (limited to 'test/functional/lua')
-rw-r--r-- | test/functional/lua/api_spec.lua | 33 | ||||
-rw-r--r-- | test/functional/lua/commands_spec.lua | 27 | ||||
-rw-r--r-- | test/functional/lua/diagnostic_spec.lua | 151 | ||||
-rw-r--r-- | test/functional/lua/loop_spec.lua | 8 | ||||
-rw-r--r-- | test/functional/lua/luaeval_spec.lua | 7 | ||||
-rw-r--r-- | test/functional/lua/mpack_spec.lua | 8 | ||||
-rw-r--r-- | test/functional/lua/overrides_spec.lua | 12 | ||||
-rw-r--r-- | test/functional/lua/vim_spec.lua | 164 |
8 files changed, 248 insertions, 162 deletions
diff --git a/test/functional/lua/api_spec.lua b/test/functional/lua/api_spec.lua index 8551c3d2a0..81e00bba6d 100644 --- a/test/functional/lua/api_spec.lua +++ b/test/functional/lua/api_spec.lua @@ -2,6 +2,7 @@ local helpers = require('test.functional.helpers')(after_each) local exc_exec = helpers.exc_exec +local remove_trace = helpers.remove_trace local funcs = helpers.funcs local clear = helpers.clear local eval = helpers.eval @@ -159,44 +160,44 @@ describe('luaeval(vim.api.…)', function() it('errors out correctly when working with API', function() -- Conversion errors eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua type', - exc_exec([[call luaeval("vim.api.nvim__id(vim.api.nvim__id)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id(vim.api.nvim__id)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua table', - exc_exec([[call luaeval("vim.api.nvim__id({1, foo=42})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id({1, foo=42})")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua type', - exc_exec([[call luaeval("vim.api.nvim__id({42, vim.api.nvim__id})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id({42, vim.api.nvim__id})")]]))) -- Errors in number of arguments eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected 1 argument', - exc_exec([[call luaeval("vim.api.nvim__id()")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id()")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected 1 argument', - exc_exec([[call luaeval("vim.api.nvim__id(1, 2)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id(1, 2)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected 2 arguments', - exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2, 3)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2, 3)")]]))) -- Error in argument types eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua string', - exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua number', - exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 'test', 1, false)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 'test', 1, false)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Number is not integral', - exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 1.5, 1, false)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 1.5, 1, false)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua table', - exc_exec([[call luaeval("vim.api.nvim__id_float('test')")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_float('test')")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Unexpected type', - exc_exec([[call luaeval("vim.api.nvim__id_float({[vim.type_idx]=vim.types.dictionary})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_float({[vim.type_idx]=vim.types.dictionary})")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua table', - exc_exec([[call luaeval("vim.api.nvim__id_array(1)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_array(1)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Unexpected type', - exc_exec([[call luaeval("vim.api.nvim__id_array({[vim.type_idx]=vim.types.dictionary})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_array({[vim.type_idx]=vim.types.dictionary})")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua table', - exc_exec([[call luaeval("vim.api.nvim__id_dictionary(1)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_dictionary(1)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Unexpected type', - exc_exec([[call luaeval("vim.api.nvim__id_dictionary({[vim.type_idx]=vim.types.array})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_dictionary({[vim.type_idx]=vim.types.array})")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua table', - exc_exec([[call luaeval("vim.api.nvim_set_keymap('', '', '', '')")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_set_keymap('', '', '', '')")]]))) -- TODO: check for errors with Tabpage argument -- TODO: check for errors with Window argument diff --git a/test/functional/lua/commands_spec.lua b/test/functional/lua/commands_spec.lua index 2e0d0ea899..9b9ba531b0 100644 --- a/test/functional/lua/commands_spec.lua +++ b/test/functional/lua/commands_spec.lua @@ -17,6 +17,7 @@ local pcall_err = helpers.pcall_err local write_file = helpers.write_file local exec_capture = helpers.exec_capture local curbufmeths = helpers.curbufmeths +local remove_trace = helpers.remove_trace before_each(clear) @@ -46,9 +47,9 @@ describe(':lua command', function() eq([[Vim(lua):E5107: Error loading lua [string ":lua"]:0: unexpected symbol near ')']], pcall_err(command, 'lua ()')) eq([[Vim(lua):E5108: Error executing lua [string ":lua"]:1: TEST]], - exc_exec('lua error("TEST")')) + remove_trace(exc_exec('lua error("TEST")'))) eq([[Vim(lua):E5108: Error executing lua [string ":lua"]:1: Invalid buffer id: -10]], - exc_exec('lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})')) + remove_trace(exc_exec('lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})'))) eq({''}, curbufmeths.get_lines(0, 100, false)) end) it('works with NULL errors', function() @@ -95,15 +96,15 @@ describe(':lua command', function() feed(':lua error("fail\\nmuch error\\nsuch details")<cr>') screen:expect{grid=[[ - | - {1:~ }| - {1:~ }| - {1:~ }| {2: }| {3:E5108: Error executing lua [string ":lua}| {3:"]:1: fail} | {3:much error} | {3:such details} | + {3:stack traceback:} | + {3: [C]: in function 'error'} | + {3: [string ":lua"]:1: in main chunk}| + | {4:Press ENTER or type command to continue}^ | ]]} feed('<cr>') @@ -119,24 +120,24 @@ describe(':lua command', function() {1:~ }| | ]]} - eq('E5108: Error executing lua [string ":lua"]:1: fail\nmuch error\nsuch details', eval('v:errmsg')) + eq('E5108: Error executing lua [string ":lua"]:1: fail\nmuch error\nsuch details', remove_trace(eval('v:errmsg'))) local status, err = pcall(command,'lua error("some error\\nin a\\nAPI command")') local expected = 'Vim(lua):E5108: Error executing lua [string ":lua"]:1: some error\nin a\nAPI command' eq(false, status) - eq(expected, string.sub(err, -string.len(expected))) + eq(expected, string.sub(remove_trace(err), -string.len(expected))) feed(':messages<cr>') screen:expect{grid=[[ - | - {1:~ }| - {1:~ }| - {1:~ }| {2: }| {3:E5108: Error executing lua [string ":lua}| {3:"]:1: fail} | {3:much error} | {3:such details} | + {3:stack traceback:} | + {3: [C]: in function 'error'} | + {3: [string ":lua"]:1: in main chunk}| + | {4:Press ENTER or type command to continue}^ | ]]} end) @@ -219,7 +220,7 @@ describe(':luafile', function() exc_exec('luafile ' .. fname)) write_file(fname, 'vimm.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})') eq(("Vim(luafile):E5113: Error while calling lua chunk: %s:1: attempt to index global 'vimm' (a nil value)"):format(fname), - exc_exec('luafile ' .. fname)) + remove_trace(exc_exec('luafile ' .. fname))) end) it('works with NULL errors', function() write_file(fname, 'error(nil)') diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 1cbfa224cc..e8bbee671e 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -6,6 +6,8 @@ local clear = helpers.clear local exec_lua = helpers.exec_lua local eq = helpers.eq local nvim = helpers.nvim +local matches = helpers.matches +local pcall_err = helpers.pcall_err describe('vim.diagnostic', function() before_each(function() @@ -47,7 +49,21 @@ describe('vim.diagnostic', function() end function count_extmarks(bufnr, namespace) - return #vim.api.nvim_buf_get_extmarks(bufnr, namespace, 0, -1, {}) + local ns = vim.diagnostic.get_namespace(namespace) + local extmarks = 0 + if ns.user_data.virt_text_ns then + extmarks = extmarks + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.virt_text_ns, 0, -1, {}) + end + if ns.user_data.underline_ns then + extmarks = extmarks + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.underline_ns, 0, -1, {}) + end + return extmarks + end + + function get_virt_text_extmarks(ns) + local ns = vim.diagnostic.get_namespace(ns) + local virt_text_ns = ns.user_data.virt_text_ns + return vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, virt_text_ns, 0, -1, {details = true}) end ]] @@ -567,7 +583,7 @@ describe('vim.diagnostic', function() ]] eq(1, exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]]) - eq(1, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) + -- eq(1, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) end) it('allows filtering by severity', function() @@ -615,7 +631,7 @@ describe('vim.diagnostic', function() severity_sort = severity_sort, }) - local virt_text = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true})[1][4].virt_text + local virt_text = get_virt_text_extmarks(diagnostic_ns)[1][4].virt_text local virt_texts = {} for i = 2, #virt_text do @@ -661,7 +677,7 @@ describe('vim.diagnostic', function() } }) - local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + local extmarks = get_virt_text_extmarks(diagnostic_ns) local virt_text = extmarks[1][4].virt_text[2][1] return virt_text ]] @@ -676,7 +692,7 @@ describe('vim.diagnostic', function() } }, diagnostic_ns) - local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + local extmarks = get_virt_text_extmarks(diagnostic_ns) local virt_text = extmarks[1][4].virt_text[2][1] return virt_text ]] @@ -696,7 +712,7 @@ describe('vim.diagnostic', function() } }) - local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + local extmarks = get_virt_text_extmarks(diagnostic_ns) local virt_text = {extmarks[1][4].virt_text[2][1], extmarks[2][4].virt_text[2][1]} return virt_text ]] @@ -724,7 +740,7 @@ describe('vim.diagnostic', function() make_error('Error', 1, 0, 1, 0), }) - local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + local extmarks = get_virt_text_extmarks(diagnostic_ns) return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} ]] eq(" 👀 Warning", result[1][2][1]) @@ -752,7 +768,7 @@ describe('vim.diagnostic', function() make_error('Error', 1, 0, 1, 0, 'another_linter'), }) - local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + local extmarks = get_virt_text_extmarks(diagnostic_ns) return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} ]] eq(" some_linter: 👀 Warning", result[1][2][1]) @@ -800,13 +816,11 @@ describe('vim.diagnostic', function() virtual_text = true, }) - -- Count how many times we call display. - SetVirtualTextOriginal = vim.diagnostic._set_virtual_text - DisplayCount = 0 - vim.diagnostic._set_virtual_text = function(...) + local set_virtual_text = vim.diagnostic.handlers.virtual_text.show + vim.diagnostic.handlers.virtual_text.show = function(...) DisplayCount = DisplayCount + 1 - return SetVirtualTextOriginal(...) + return set_virtual_text(...) end vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { @@ -850,13 +864,12 @@ describe('vim.diagnostic', function() virtual_text = false, }) - -- Count how many times we call display. - SetVirtualTextOriginal = vim.diagnostic._set_virtual_text DisplayCount = 0 - vim.diagnostic._set_virtual_text = function(...) + local set_virtual_text = vim.diagnostic.handlers.virtual_text.show + vim.diagnostic.handlers.virtual_text.show = function(...) DisplayCount = DisplayCount + 1 - return SetVirtualTextOriginal(...) + return set_virtual_text(...) end vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { @@ -1227,6 +1240,41 @@ describe('vim.diagnostic', function() return lines ]]) end) + + it('can filter by severity', function() + local count_diagnostics_with_severity = function(min_severity, max_severity) + return exec_lua([[ + local min_severity, max_severity = ... + vim.diagnostic.config({ + float = { + severity = {min=min_severity, max=max_severity}, + }, + }) + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_error("Syntax error", 0, 1, 0, 3), + make_info('Info', 0, 3, 0, 4), + make_error('Error', 0, 2, 0, 2), + make_warning('Warning', 0, 0, 0, 1), + }) + + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false }) + if not float_bufnr then + return 0 + end + + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines + ]], min_severity, max_severity) + end + + eq(2, count_diagnostics_with_severity("ERROR")) + eq(3, count_diagnostics_with_severity("WARN")) + eq(1, count_diagnostics_with_severity("WARN", "WARN")) + eq(4, count_diagnostics_with_severity("HINT")) + eq(0, count_diagnostics_with_severity("HINT", "HINT")) + end) end) describe('setloclist()', function() @@ -1347,4 +1395,73 @@ describe('vim.diagnostic', function() eq(result[1], result[2]) end) end) + + describe('handlers', function() + it('checks that a new handler is a table', function() + matches([[.*handler: expected table, got string.*]], pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = "bar" ]])) + matches([[.*handler: expected table, got function.*]], pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = function() end ]])) + end) + + it('can add new handlers', function() + eq(true, exec_lua [[ + local handler_called = false + vim.diagnostic.handlers.test = { + show = function(namespace, bufnr, diagnostics, opts) + assert(namespace == diagnostic_ns) + assert(bufnr == diagnostic_bufnr) + assert(#diagnostics == 1) + assert(opts.test.some_opt == 42) + handler_called = true + end, + } + + vim.diagnostic.config({test = {some_opt = 42}}) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_warning("Warning", 0, 0, 0, 0), + }) + return handler_called + ]]) + end) + + it('can disable handlers by setting the corresponding option to false', function() + eq(false, exec_lua [[ + local handler_called = false + vim.diagnostic.handlers.test = { + show = function(namespace, bufnr, diagnostics, opts) + handler_called = true + end, + } + + vim.diagnostic.config({test = false}) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_warning("Warning", 0, 0, 0, 0), + }) + return handler_called + ]]) + end) + + it('always calls a handler\'s hide function if defined', function() + eq({false, true}, exec_lua [[ + local hide_called = false + local show_called = false + vim.diagnostic.handlers.test = { + show = function(namespace, bufnr, diagnostics, opts) + show_called = true + end, + hide = function(namespace, bufnr) + assert(namespace == diagnostic_ns) + assert(bufnr == diagnostic_bufnr) + hide_called = true + end, + } + + vim.diagnostic.config({test = false}) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_warning("Warning", 0, 0, 0, 0), + }) + vim.diagnostic.hide(diagnostic_ns, diagnostic_bufnr) + return {show_called, hide_called} + ]]) + end) + end) end) diff --git a/test/functional/lua/loop_spec.lua b/test/functional/lua/loop_spec.lua index 992d1666f6..7f3787d7bf 100644 --- a/test/functional/lua/loop_spec.lua +++ b/test/functional/lua/loop_spec.lua @@ -84,14 +84,14 @@ describe('vim.loop', function() screen:expect([[ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| {2: }| {3:Error executing luv callback:} | {3:[string "<nvim>"]:5: E5560: nvim_set_var must not }| {3:be called in a lua loop callback} | + {3:stack traceback:} | + {3: [C]: in function 'nvim_set_var'} | + {3: [string "<nvim>"]:5: in function <[string }| + {3:"<nvim>"]:2>} | {4:Press ENTER or type command to continue}^ | ]]) feed('<cr>') diff --git a/test/functional/lua/luaeval_spec.lua b/test/functional/lua/luaeval_spec.lua index ab76e71a12..c543dd1995 100644 --- a/test/functional/lua/luaeval_spec.lua +++ b/test/functional/lua/luaeval_spec.lua @@ -4,6 +4,7 @@ local Screen = require('test.functional.ui.screen') local pcall_err = helpers.pcall_err local exc_exec = helpers.exc_exec +local remove_trace = helpers.remove_trace local exec_lua = helpers.exec_lua local command = helpers.command local meths = helpers.meths @@ -424,11 +425,11 @@ describe('luaeval()', function() it('errors out correctly when doing incorrect things in lua', function() -- Conversion errors eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: attempt to call field \'xxx_nonexistent_key_xxx\' (a nil value)', - exc_exec([[call luaeval("vim.xxx_nonexistent_key_xxx()")]])) + remove_trace(exc_exec([[call luaeval("vim.xxx_nonexistent_key_xxx()")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: ERROR', - exc_exec([[call luaeval("error('ERROR')")]])) + remove_trace(exc_exec([[call luaeval("error('ERROR')")]]))) eq('Vim(call):E5108: Error executing lua [NULL]', - exc_exec([[call luaeval("error(nil)")]])) + remove_trace(exc_exec([[call luaeval("error(nil)")]]))) end) it('does not leak memory when called with too long line', diff --git a/test/functional/lua/mpack_spec.lua b/test/functional/lua/mpack_spec.lua index ef693f01f3..cc788ed8bb 100644 --- a/test/functional/lua/mpack_spec.lua +++ b/test/functional/lua/mpack_spec.lua @@ -7,16 +7,16 @@ local exec_lua = helpers.exec_lua describe('lua vim.mpack', function() before_each(clear) - it('can pack vim.NIL', function() + it('encodes vim.NIL', function() eq({true, true, true, true}, exec_lua [[ - local var = vim.mpack.unpack(vim.mpack.pack({33, vim.NIL, 77})) + local var = vim.mpack.decode(vim.mpack.encode({33, vim.NIL, 77})) return {var[1]==33, var[2]==vim.NIL, var[3]==77, var[4]==nil} ]]) end) - it('can pack vim.empty_dict()', function() + it('encodes vim.empty_dict()', function() eq({{{}, "foo", {}}, true, false}, exec_lua [[ - local var = vim.mpack.unpack(vim.mpack.pack({{}, "foo", vim.empty_dict()})) + local var = vim.mpack.decode(vim.mpack.encode({{}, "foo", vim.empty_dict()})) return {var, vim.tbl_islist(var[1]), vim.tbl_islist(var[3])} ]]) end) diff --git a/test/functional/lua/overrides_spec.lua b/test/functional/lua/overrides_spec.lua index 636479b81f..b0712ff366 100644 --- a/test/functional/lua/overrides_spec.lua +++ b/test/functional/lua/overrides_spec.lua @@ -161,15 +161,15 @@ describe('debug.debug', function() {0:~ }| {0:~ }| {0:~ }| - {0:~ }| - {0:~ }| - {0:~ }| nil | lua_debug> print("TEST") | TEST | | {E:E5108: Error executing lua [string ":lua"]:5: attempt}| {E: to perform arithmetic on local 'a' (a nil value)} | + {E:stack traceback:} | + {E: [string ":lua"]:5: in function 'Test'} | + {E: [string ":lua"]:1: in main chunk} | Interrupt: {cr:Press ENTER or type command to continue}^ | ]]} feed('<C-l>:lua Test()\n') @@ -197,13 +197,13 @@ describe('debug.debug', function() {0:~ }| {0:~ }| {0:~ }| - {0:~ }| - {0:~ }| - {0:~ }| nil | lua_debug> | {E:E5108: Error executing lua [string ":lua"]:5: attempt}| {E: to perform arithmetic on local 'a' (a nil value)} | + {E:stack traceback:} | + {E: [string ":lua"]:5: in function 'Test'} | + {E: [string ":lua"]:1: in main chunk} | {cr:Press ENTER or type command to continue}^ | ]]} end) diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index 8c691b1121..3832d27a22 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -4,7 +4,6 @@ local Screen = require('test.functional.ui.screen') local funcs = helpers.funcs local meths = helpers.meths -local dedent = helpers.dedent local command = helpers.command local insert = helpers.insert local clear = helpers.clear @@ -19,6 +18,7 @@ local source = helpers.source local NIL = helpers.NIL local retry = helpers.retry local next_msg = helpers.next_msg +local remove_trace = helpers.remove_trace before_each(clear) @@ -121,11 +121,6 @@ describe('lua stdlib', function() eq(1, funcs.luaeval('vim.stricmp("\\0C\\0", "\\0B\\0")')) end) - -- for brevity, match only the error header (not the traceback) - local function pcall_header(...) - return string.gsub(string.gsub(pcall_err(exec_lua, ...), '[\r\n].*', ''), '^Error executing lua: ', '') - end - it('vim.startswith', function() eq(true, funcs.luaeval('vim.startswith("123", "1")')) eq(true, funcs.luaeval('vim.startswith("123", "")')) @@ -136,8 +131,10 @@ describe('lua stdlib', function() eq(false, funcs.luaeval('vim.startswith("123", "2")')) eq(false, funcs.luaeval('vim.startswith("123", "1234")')) - eq("vim/shared.lua:0: prefix: expected string, got nil", pcall_header 'return vim.startswith("123", nil)') - eq("vim/shared.lua:0: s: expected string, got nil", pcall_header 'return vim.startswith(nil, "123")') + eq("Error executing lua: vim/shared.lua:0: prefix: expected string, got nil", + pcall_err(exec_lua, 'return vim.startswith("123", nil)')) + eq("Error executing lua: vim/shared.lua:0: s: expected string, got nil", + pcall_err(exec_lua, 'return vim.startswith(nil, "123")')) end) it('vim.endswith', function() @@ -150,8 +147,10 @@ describe('lua stdlib', function() eq(false, funcs.luaeval('vim.endswith("123", "2")')) eq(false, funcs.luaeval('vim.endswith("123", "1234")')) - eq("vim/shared.lua:0: suffix: expected string, got nil", pcall_header 'return vim.endswith("123", nil)') - eq("vim/shared.lua:0: s: expected string, got nil", pcall_header 'return vim.endswith(nil, "123")') + eq("Error executing lua: vim/shared.lua:0: suffix: expected string, got nil", + pcall_err(exec_lua, 'return vim.endswith("123", nil)')) + eq("Error executing lua: vim/shared.lua:0: s: expected string, got nil", + pcall_err(exec_lua, 'return vim.endswith(nil, "123")')) end) it("vim.str_utfindex/str_byteindex", function() @@ -179,6 +178,31 @@ describe('lua stdlib', function() end end) + it("vim.str_utf_start", function() + exec_lua([[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]]) + local expected_positions = {0,0,0,0,-1,0,-1,0,-1,0,0,-1,0,0,-1,-2,0,-1,-2,0,0,-1,-2,0,0,-1,-2,-3,0,0,-1,-2,-3,0,0,0,-1,0,0,-1,0,-1,0,-1,0,-1,0,-1} + eq(expected_positions, exec_lua([[ + local start_codepoint_positions = {} + for idx = 1, #_G.test_text do + table.insert(start_codepoint_positions, vim.str_utf_start(_G.test_text, idx)) + end + return start_codepoint_positions + ]])) + end) + + it("vim.str_utf_end", function() + exec_lua([[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]]) + local expected_positions = {0,0,0,1,0,1,0,1,0,0,1,0,0,2,1,0,2,1,0,0,2,1,0,0,3,2,1,0,0,3,2,1,0,0,0,1,0,0,1,0,1,0,1,0,1,0,1,0 } + eq(expected_positions, exec_lua([[ + local end_codepoint_positions = {} + for idx = 1, #_G.test_text do + table.insert(end_codepoint_positions, vim.str_utf_end(_G.test_text, idx)) + end + return end_codepoint_positions + ]])) + end) + + it("vim.str_utf_pos", function() exec_lua([[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]]) local expected_positions = { 1,2,3,4,6,8,10,11,13,14,17,20,21,24,25,29,30,34,35,36,38,39,41,43,45,47 } @@ -208,7 +232,7 @@ describe('lua stdlib', function() ]]) feed("<cr>") - eq('Error executing vim.schedule lua callback: [string "<nvim>"]:2: big failure\nvery async', eval("v:errmsg")) + eq('Error executing vim.schedule lua callback: [string "<nvim>"]:2: big failure\nvery async', remove_trace(eval("v:errmsg"))) local screen = Screen.new(60,5) screen:set_default_attr_ids({ @@ -234,10 +258,10 @@ describe('lua stdlib', function() end) ]]) screen:expect{grid=[[ - | - {2: }| - {3:Error executing vim.schedule lua callback: [string "<nvim>"]}| - {3::2: Vim(echo):E115: Missing quote: 'err} | + {3:stack traceback:} | + {3: [C]: in function 'nvim_command'} | + {3: [string "<nvim>"]:2: in function <[string "<nvim>"]:}| + {3:1>} | {4:Press ENTER or type command to continue}^ | ]]} end) @@ -281,22 +305,11 @@ describe('lua stdlib', function() -- Validates args. eq(true, pcall(split, 'string', 'string')) - eq(dedent([[ - Error executing lua: vim/shared.lua:0: s: expected string, got number - stack traceback: - vim/shared.lua:0: in function 'gsplit' - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: s: expected string, got number', pcall_err(split, 1, 'string')) - eq(dedent([[ - Error executing lua: vim/shared.lua:0: sep: expected string, got number - stack traceback: - vim/shared.lua:0: in function 'gsplit' - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: sep: expected string, got number', pcall_err(split, 'string', 1)) - eq(dedent([[ - Error executing lua: vim/shared.lua:0: kwargs: expected table, got number - stack traceback: - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: kwargs: expected table, got number', pcall_err(split, 'string', 'string', 1)) end) @@ -317,10 +330,7 @@ describe('lua stdlib', function() end -- Validates args. - eq(dedent([[ - Error executing lua: vim/shared.lua:0: s: expected string, got number - stack traceback: - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: s: expected string, got number', pcall_err(trim, 2)) end) @@ -399,10 +409,7 @@ describe('lua stdlib', function() eq('foo%%%-bar', exec_lua([[return vim.pesc(vim.pesc('foo-bar'))]])) -- Validates args. - eq(dedent([[ - Error executing lua: vim/shared.lua:0: s: expected string, got number - stack traceback: - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: s: expected string, got number', pcall_err(exec_lua, [[return vim.pesc(2)]])) end) @@ -685,10 +692,7 @@ describe('lua stdlib', function() it('vim.list_extend', function() eq({1,2,3}, exec_lua [[ return vim.list_extend({1}, {2,3}) ]]) - eq(dedent([[ - Error executing lua: vim/shared.lua:0: src: expected table, got nil - stack traceback: - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: src: expected table, got nil', pcall_err(exec_lua, [[ return vim.list_extend({1}, nil) ]])) eq({1,2}, exec_lua [[ return vim.list_extend({1}, {2;a=1}) ]]) eq(true, exec_lua [[ local a = {1} return vim.list_extend(a, {2;a=1}) == a ]]) @@ -820,12 +824,12 @@ describe('lua stdlib', function() end) ]]) screen:expect{grid=[[ - foo | - {1:~ }| - {2: }| - {3:Error executing luv callback:} | {3:[string "<nvim>"]:6: E5560: rpcrequest must not be}| {3: called in a lua loop callback} | + {3:stack traceback:} | + {3: [C]: in function 'rpcrequest'} | + {3: [string "<nvim>"]:6: in function <[string }| + {3:"<nvim>"]:2>} | {4:Press ENTER or type command to continue}^ | ]]} feed('<cr>') @@ -889,76 +893,37 @@ describe('lua stdlib', function() exec_lua("vim.validate{arg1={{}, 't' }, arg2={ 'foo', 's' }}") exec_lua("vim.validate{arg1={2, function(a) return (a % 2) == 0 end, 'even number' }}") - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: opt[1]: expected table, got number - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: opt[1]: expected table, got number', pcall_err(exec_lua, "vim.validate{ 1, 'x' }")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: invalid type name: x - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: invalid type name: x', pcall_err(exec_lua, "vim.validate{ arg1={ 1, 'x' }}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: invalid type name: 1 - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: invalid type name: 1', pcall_err(exec_lua, "vim.validate{ arg1={ 1, 1 }}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: invalid type name: nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: invalid type name: nil', pcall_err(exec_lua, "vim.validate{ arg1={ 1 }}")) -- Validated parameters are required by default. - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil', pcall_err(exec_lua, "vim.validate{ arg1={ nil, 's' }}")) -- Explicitly required. - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil', pcall_err(exec_lua, "vim.validate{ arg1={ nil, 's', false }}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected table, got number - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected table, got number', pcall_err(exec_lua, "vim.validate{arg1={1, 't'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg2: expected string, got number - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg2: expected string, got number', pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={1, 's'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil', pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil', pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected even number, got 3 - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected even number, got 3', pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1 end, 'even number'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3 - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3', pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1 end}}")) -- Pass an additional message back. - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3. Info: TEST_MSG - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3. Info: TEST_MSG', pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1, 'TEST_MSG' end}}")) end) @@ -2108,9 +2073,10 @@ describe('lua stdlib', function() end) it('should not crash when callback errors', function() - eq({false, '[string "<nvim>"]:1: As Expected'}, exec_lua [[ + local result = exec_lua [[ return {pcall(function() vim.wait(1000, function() error("As Expected") end) end)} - ]]) + ]] + eq({false, '[string "<nvim>"]:1: As Expected'}, {result[1], remove_trace(result[2])}) end) it('if callback is passed, it must be a function', function() |