diff options
Diffstat (limited to 'test/functional/lua')
-rw-r--r-- | test/functional/lua/api_spec.lua | 4 | ||||
-rw-r--r-- | test/functional/lua/buffer_updates_spec.lua | 96 | ||||
-rw-r--r-- | test/functional/lua/diagnostic_spec.lua | 472 | ||||
-rw-r--r-- | test/functional/lua/ffi_spec.lua | 62 | ||||
-rw-r--r-- | test/functional/lua/json_spec.lua | 133 | ||||
-rw-r--r-- | test/functional/lua/luaeval_spec.lua | 7 | ||||
-rw-r--r-- | test/functional/lua/ui_spec.lua | 46 | ||||
-rw-r--r-- | test/functional/lua/uri_spec.lua | 17 | ||||
-rw-r--r-- | test/functional/lua/vim_spec.lua | 78 |
9 files changed, 835 insertions, 80 deletions
diff --git a/test/functional/lua/api_spec.lua b/test/functional/lua/api_spec.lua index fdf79d55b2..8551c3d2a0 100644 --- a/test/functional/lua/api_spec.lua +++ b/test/functional/lua/api_spec.lua @@ -194,6 +194,10 @@ describe('luaeval(vim.api.…)', function() 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})")]])) + + eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua table', + exc_exec([[call luaeval("vim.api.nvim_set_keymap('', '', '', '')")]])) + -- TODO: check for errors with Tabpage argument -- TODO: check for errors with Window argument -- TODO: check for errors with Buffer argument diff --git a/test/functional/lua/buffer_updates_spec.lua b/test/functional/lua/buffer_updates_spec.lua index 073927bf22..c83a50b78b 100644 --- a/test/functional/lua/buffer_updates_spec.lua +++ b/test/functional/lua/buffer_updates_spec.lua @@ -1050,6 +1050,102 @@ describe('lua: nvim_buf_attach on_bytes', function() } end) + it("sends updates on U", function() + feed("ggiAAA<cr>BBB") + feed("<esc>gg$a CCC") + + local check_events = setup_eventcheck(verify, nil) + + feed("ggU") + + check_events { + { "test1", "bytes", 1, 6, 0, 7, 7, 0, 0, 0, 0, 3, 3 }; + } + end) + + it("delete in completely empty buffer", function() + local check_events = setup_eventcheck(verify, nil) + + command "delete" + check_events { } + end) + + it("delete the only line of a buffer", function() + local check_events = setup_eventcheck(verify, {"AAA"}) + + command "delete" + check_events { + { "test1", "bytes", 1, 3, 0, 0, 0, 1, 0, 4, 1, 0, 1 }; + } + end) + + it("delete the last line of a buffer with two lines", function() + local check_events = setup_eventcheck(verify, {"AAA", "BBB"}) + + command "2delete" + check_events { + { "test1", "bytes", 1, 3, 1, 0, 4, 1, 0, 4, 0, 0, 0 }; + } + end) + + it(":sort lines", function() + local check_events = setup_eventcheck(verify, {"CCC", "BBB", "AAA"}) + + command "%sort" + check_events { + { "test1", "bytes", 1, 3, 0, 0, 0, 3, 0, 12, 3, 0, 12 }; + } + end) + + it("handles already sorted lines", function() + local check_events = setup_eventcheck(verify, {"AAA", "BBB", "CCC"}) + + command "%sort" + check_events { } + end) + + local function test_lockmarks(mode) + local description = (mode ~= "") and mode or "(baseline)" + it("test_lockmarks " .. description .. " %delete _", function() + local check_events = setup_eventcheck(verify, {"AAA", "BBB", "CCC"}) + + command(mode .. " %delete _") + check_events { + { "test1", "bytes", 1, 3, 0, 0, 0, 3, 0, 12, 1, 0, 1 }; + } + end) + + it("test_lockmarks " .. description .. " append()", function() + local check_events = setup_eventcheck(verify) + + command(mode .. " call append(0, 'CCC')") + check_events { + { "test1", "bytes", 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 4 }; + } + + command(mode .. " call append(1, 'BBBB')") + check_events { + { "test1", "bytes", 1, 3, 1, 0, 4, 0, 0, 0, 1, 0, 5 }; + } + + command(mode .. " call append(2, '')") + check_events { + { "test1", "bytes", 1, 4, 2, 0, 9, 0, 0, 0, 1, 0, 1 }; + } + + command(mode .. " $delete _") + check_events { + { "test1", "bytes", 1, 5, 3, 0, 10, 1, 0, 1, 0, 0, 0 }; + } + + eq("CCC|BBBB|", table.concat(meths.buf_get_lines(0, 0, -1, true), "|")) + end) + end + + -- check that behavior is identical with and without "lockmarks" + test_lockmarks "" + test_lockmarks "lockmarks" + teardown(function() os.remove "Xtest-reload" os.remove "Xtest-undofile" diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 29dd5c60da..1cbfa224cc 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -14,48 +14,32 @@ describe('vim.diagnostic', function() exec_lua [[ require('vim.diagnostic') - function make_error(msg, x1, y1, x2, y2) + function make_diagnostic(msg, x1, y1, x2, y2, severity, source) return { lnum = x1, col = y1, end_lnum = x2, end_col = y2, message = msg, - severity = vim.diagnostic.severity.ERROR, + severity = severity, + source = source, } end - function make_warning(msg, x1, y1, x2, y2) - return { - lnum = x1, - col = y1, - end_lnum = x2, - end_col = y2, - message = msg, - severity = vim.diagnostic.severity.WARN, - } + function make_error(msg, x1, y1, x2, y2, source) + return make_diagnostic(msg, x1, y1, x2, y2, vim.diagnostic.severity.ERROR, source) end - function make_info(msg, x1, y1, x2, y2) - return { - lnum = x1, - col = y1, - end_lnum = x2, - end_col = y2, - message = msg, - severity = vim.diagnostic.severity.INFO, - } + function make_warning(msg, x1, y1, x2, y2, source) + return make_diagnostic(msg, x1, y1, x2, y2, vim.diagnostic.severity.WARN, source) end - function make_hint(msg, x1, y1, x2, y2) - return { - lnum = x1, - col = y1, - end_lnum = x2, - end_col = y2, - message = msg, - severity = vim.diagnostic.severity.HINT, - } + function make_info(msg, x1, y1, x2, y2, source) + return make_diagnostic(msg, x1, y1, x2, y2, vim.diagnostic.severity.INFO, source) + end + + function make_hint(msg, x1, y1, x2, y2, source) + return make_diagnostic(msg, x1, y1, x2, y2, vim.diagnostic.severity.HINT, source) end function count_diagnostics(bufnr, severity, namespace) @@ -489,6 +473,78 @@ describe('vim.diagnostic', function() end) describe('config()', function() + it('works with global, namespace, and ephemeral options', function() + eq(1, exec_lua [[ + vim.diagnostic.config({ + virtual_text = false, + }) + + vim.diagnostic.config({ + virtual_text = true, + underline = false, + }, diagnostic_ns) + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_error('Some Error', 4, 4, 4, 4), + }) + + return count_extmarks(diagnostic_bufnr, diagnostic_ns) + ]]) + + eq(1, exec_lua [[ + vim.diagnostic.config({ + virtual_text = false, + }) + + vim.diagnostic.config({ + virtual_text = false, + underline = false, + }, diagnostic_ns) + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_error('Some Error', 4, 4, 4, 4), + }, {virtual_text = true}) + + return count_extmarks(diagnostic_bufnr, diagnostic_ns) + ]]) + + eq(0, exec_lua [[ + vim.diagnostic.config({ + virtual_text = false, + }) + + vim.diagnostic.config({ + virtual_text = {severity=vim.diagnostic.severity.ERROR}, + underline = false, + }, diagnostic_ns) + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_warning('Some Warning', 4, 4, 4, 4), + }, {virtual_text = true}) + + return count_extmarks(diagnostic_bufnr, diagnostic_ns) + ]]) + + eq(1, exec_lua [[ + vim.diagnostic.config({ + virtual_text = false, + }) + + vim.diagnostic.config({ + virtual_text = {severity=vim.diagnostic.severity.ERROR}, + underline = false, + }, diagnostic_ns) + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_warning('Some Warning', 4, 4, 4, 4), + }, { + virtual_text = {} -- An empty table uses default values + }) + + return count_extmarks(diagnostic_bufnr, diagnostic_ns) + ]]) + end) + it('can use functions for config values', function() exec_lua [[ vim.diagnostic.config({ @@ -590,6 +646,118 @@ describe('vim.diagnostic', function() eq({'Error', 'Warn', 'Info'}, result[1]) eq({'Info', 'Warn', 'Error'}, result[2]) end) + + it('can show diagnostic sources in virtual text', function() + local result = exec_lua [[ + local diagnostics = { + make_error('Some error', 0, 0, 0, 0, 'source x'), + } + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { + underline = false, + virtual_text = { + prefix = '', + source = 'always', + } + }) + + local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + local virt_text = extmarks[1][4].virt_text[2][1] + return virt_text + ]] + eq(' source x: Some error', result) + + result = exec_lua [[ + vim.diagnostic.config({ + underline = false, + virtual_text = { + prefix = '', + source = 'if_many', + } + }, diagnostic_ns) + + local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + local virt_text = extmarks[1][4].virt_text[2][1] + return virt_text + ]] + eq(' Some error', result) + + result = exec_lua [[ + local diagnostics = { + make_error('Some error', 0, 0, 0, 0, 'source x'), + make_error('Another error', 1, 1, 1, 1, 'source y'), + } + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { + underline = false, + virtual_text = { + prefix = '', + source = 'if_many', + } + }) + + local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + local virt_text = {extmarks[1][4].virt_text[2][1], extmarks[2][4].virt_text[2][1]} + return virt_text + ]] + eq(' source x: Some error', result[1]) + eq(' source y: Another error', result[2]) + end) + + it('supports a format function for diagnostic messages', function() + local result = exec_lua [[ + vim.diagnostic.config({ + underline = false, + virtual_text = { + prefix = '', + format = function(diagnostic) + if diagnostic.severity == vim.diagnostic.severity.ERROR then + return string.format("🔥 %s", diagnostic.message) + end + return string.format("👀 %s", diagnostic.message) + end, + } + }) + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_warning('Warning', 0, 0, 0, 0), + make_error('Error', 1, 0, 1, 0), + }) + + local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} + ]] + eq(" 👀 Warning", result[1][2][1]) + eq(" 🔥 Error", result[2][2][1]) + end) + + it('includes source for formatted diagnostics', function() + local result = exec_lua [[ + vim.diagnostic.config({ + underline = false, + virtual_text = { + prefix = '', + source = 'always', + format = function(diagnostic) + if diagnostic.severity == vim.diagnostic.severity.ERROR then + return string.format("🔥 %s", diagnostic.message) + end + return string.format("👀 %s", diagnostic.message) + end, + } + }) + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_warning('Warning', 0, 0, 0, 0, 'some_linter'), + 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}) + return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} + ]] + eq(" some_linter: 👀 Warning", result[1][2][1]) + eq(" another_linter: 🔥 Error", result[2][2][1]) + end) end) describe('set()', function() @@ -778,10 +946,124 @@ describe('vim.diagnostic', function() return count_extmarks(diagnostic_bufnr, diagnostic_ns) ]]) end) + + it('sets signs', function() + local result = exec_lua [[ + vim.diagnostic.config({ + signs = true, + }) + + local diagnostics = { + make_error('Error', 1, 1, 1, 2), + make_warning('Warning', 3, 3, 3, 3), + } + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + + return vim.fn.sign_getplaced(diagnostic_bufnr, {group = '*'})[1].signs + ]] + + eq({2, 'DiagnosticSignError'}, {result[1].lnum, result[1].name}) + eq({4, 'DiagnosticSignWarn'}, {result[2].lnum, result[2].name}) + end) end) - describe('show_line_diagnostics()', function() - it('creates floating window and returns popup bufnr and winnr if current line contains diagnostics', function() + describe('open_float()', function() + it('can show diagnostics from the whole buffer', function() + eq({'1. Syntax error', '2. Some warning'}, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 0, 1, 0, 3), + make_warning("Some warning", 1, 1, 1, 3), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header = false}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + end) + + it('can show diagnostics from a single line', function() + -- Using cursor position + eq({'1. Some warning'}, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 0, 1, 0, 3), + make_warning("Some warning", 1, 1, 1, 3), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, {2, 1}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="line"}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + + -- With specified position + eq({'1. Some warning'}, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 0, 1, 0, 3), + make_warning("Some warning", 1, 1, 1, 3), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, {1, 1}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="line", pos=1}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + end) + + it('can show diagnostics from a specific position', function() + -- Using cursor position + eq({'1. Syntax error'}, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 1, 1, 1, 2), + make_warning("Some warning", 1, 3, 1, 4), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, {2, 2}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor"}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + + -- With specified position + eq({'1. Some warning'}, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 1, 1, 1, 2), + make_warning("Some warning", 1, 3, 1, 4), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, {1, 1}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor", pos={1,3}}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + + -- With column position past the end of the line. #16062 + eq({'1. Syntax error'}, exec_lua [[ + local first_line_len = #vim.api.nvim_buf_get_lines(diagnostic_bufnr, 0, 1, true)[1] + local diagnostics = { + make_error("Syntax error", 0, first_line_len + 1, 1, 0), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, {1, 1}) + local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor", pos={0,first_line_len}}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + end) + + it('creates floating window and returns float bufnr and winnr if current line contains diagnostics', function() -- Two lines: -- Diagnostic: -- 1. <msg> @@ -791,8 +1073,10 @@ describe('vim.diagnostic', function() } vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local popup_bufnr, winnr = vim.diagnostic.show_line_diagnostics() - return #vim.api.nvim_buf_get_lines(popup_bufnr, 0, -1, false) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {scope="line"}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines ]]) end) @@ -808,8 +1092,10 @@ describe('vim.diagnostic', function() vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, buf_1_diagnostics) vim.diagnostic.set(other_ns, other_bufnr, buf_2_diagnostics) - local popup_bufnr, winnr = vim.diagnostic.show_line_diagnostics() - return #vim.api.nvim_buf_get_lines(popup_bufnr, 0, -1, false) + local float_bufnr, winnr = vim.diagnostic.open_float() + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines ]]) end) @@ -824,12 +1110,14 @@ describe('vim.diagnostic', function() vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diagnostics) vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diagnostics) - local popup_bufnr, winnr = vim.diagnostic.show_line_diagnostics({namespace = diagnostic_ns}) - return #vim.api.nvim_buf_get_lines(popup_bufnr, 0, -1, false) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {namespace = diagnostic_ns}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines ]]) end) - it('creates floating window and returns popup bufnr and winnr without header, if requested', function() + it('creates floating window and returns float bufnr and winnr without header, if requested', function() -- One line (since no header): -- 1. <msg> eq(1, exec_lua [[ @@ -838,39 +1126,105 @@ describe('vim.diagnostic', function() } vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local popup_bufnr, winnr = vim.diagnostic.show_line_diagnostics {show_header = false} - return #vim.api.nvim_buf_get_lines(popup_bufnr, 0, -1, false) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {show_header = false}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines ]]) end) - end) - describe('set_signs()', function() - -- TODO(tjdevries): Find out why signs are not displayed when set from Lua...?? - pending('sets signs by default', function() - exec_lua [[ - vim.diagnostic.config({ - update_in_insert = true, - signs = true, + it('clamps diagnostic line numbers within the valid range', function() + eq(1, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 6, 0, 6, 0), + } + vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {show_header = false, scope = "line", pos = 5}) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines + ]]) + end) + + it('can show diagnostic source', function() + exec_lua [[vim.api.nvim_win_set_buf(0, diagnostic_bufnr)]] + + eq({"1. Syntax error"}, exec_lua [[ + local diagnostics = { + make_error("Syntax error", 0, 1, 0, 3, "source x"), + } + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { + show_header = false, + source = "if_many", }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + + eq({"1. source x: Syntax error"}, exec_lua [[ + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { + show_header = false, + source = "always", + }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + eq({"1. source x: Syntax error", "2. source y: Another error"}, exec_lua [[ local diagnostics = { - make_error('Delayed Diagnostic', 1, 1, 1, 2), - make_error('Delayed Diagnostic', 3, 3, 3, 3), + make_error("Syntax error", 0, 1, 0, 3, "source x"), + make_error("Another error", 0, 1, 0, 3, "source y"), + } + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { + show_header = false, + source = "if_many", + }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + end) + + it('respects severity_sort', function() + exec_lua [[vim.api.nvim_win_set_buf(0, diagnostic_bufnr)]] + + eq({"1. Syntax error", "2. Info", "3. Error", "4. Warning"}, exec_lua [[ + local diagnostics = { + 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), } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.diagnostic._set_signs(diagnostic_ns, diagnostic_bufnr, diagnostics) - -- return vim.fn.sign_getplaced() - ]] + vim.diagnostic.config({severity_sort = false}) - nvim("input", "o") - nvim("input", "<esc>") + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) + + eq({"1. Syntax error", "2. Error", "3. Warning", "4. Info"}, exec_lua [[ + vim.diagnostic.config({severity_sort = true}) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + ]]) - -- TODO(tjdevries): Find a way to get the signs to display in the test... - eq(nil, exec_lua [[ - return im.fn.sign_getplaced()[1].signs + eq({"1. Info", "2. Warning", "3. Error", "4. Syntax error"}, exec_lua [[ + vim.diagnostic.config({severity_sort = { reverse = true } }) + local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines ]]) end) end) diff --git a/test/functional/lua/ffi_spec.lua b/test/functional/lua/ffi_spec.lua new file mode 100644 index 0000000000..80c01a2b8c --- /dev/null +++ b/test/functional/lua/ffi_spec.lua @@ -0,0 +1,62 @@ +local helpers = require('test.functional.helpers')(after_each) +local eq = helpers.eq +local exec_lua = helpers.exec_lua +local clear = helpers.clear + +before_each(clear) + +describe('ffi.cdef', function() + it('can use Neovim core functions', function() + if not exec_lua("return pcall(require, 'ffi')") then + pending('missing LuaJIT FFI') + end + + eq(12, exec_lua[[ + local ffi = require('ffi') + + ffi.cdef('int curwin_col_off(void);') + + vim.cmd('set number numberwidth=4 signcolumn=yes:4') + + return ffi.C.curwin_col_off() + ]]) + + eq(20, exec_lua[=[ + local ffi = require('ffi') + + ffi.cdef[[ + typedef unsigned char char_u; + typedef struct window_S win_T; + typedef struct {} stl_hlrec_t; + typedef struct {} StlClickRecord; + typedef struct {} Error; + + win_T *find_window_by_handle(int Window, Error *err); + + int build_stl_str_hl( + win_T *wp, + char_u *out, + size_t outlen, + char_u *fmt, + int use_sandbox, + char_u fillchar, + int maxwidth, + stl_hlrec_t **hltab, + StlClickRecord **tabtab + ); + ]] + + return ffi.C.build_stl_str_hl( + ffi.C.find_window_by_handle(0, ffi.new('Error')), + ffi.new('char_u[1024]'), + 1024, + ffi.cast('char_u*', 'StatusLineOfLength20'), + 0, + 0, + 0, + nil, + nil + ) + ]=]) + end) +end) diff --git a/test/functional/lua/json_spec.lua b/test/functional/lua/json_spec.lua new file mode 100644 index 0000000000..fbb21bfd57 --- /dev/null +++ b/test/functional/lua/json_spec.lua @@ -0,0 +1,133 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local NIL = helpers.NIL +local exec_lua = helpers.exec_lua +local eq = helpers.eq + +describe('vim.json.decode function', function() + before_each(function() + clear() + end) + + it('parses null, true, false', function() + eq(NIL, exec_lua([[return vim.json.decode('null')]])) + eq(true, exec_lua([[return vim.json.decode('true')]])) + eq(false, exec_lua([[return vim.json.decode('false')]])) + end) + + it('parses integer numbers', function() + eq(100000, exec_lua([[return vim.json.decode('100000')]])) + eq(-100000, exec_lua([[return vim.json.decode('-100000')]])) + eq(100000, exec_lua([[return vim.json.decode(' 100000 ')]])) + eq(-100000, exec_lua([[return vim.json.decode(' -100000 ')]])) + eq(0, exec_lua([[return vim.json.decode('0')]])) + eq(0, exec_lua([[return vim.json.decode('-0')]])) + end) + + it('parses floating-point numbers', function() + -- This behavior differs from vim.fn.json_decode, which return '100000.0' + eq('100000', exec_lua([[return tostring(vim.json.decode('100000.0'))]])) + eq(100000.5, exec_lua([[return vim.json.decode('100000.5')]])) + eq(-100000.5, exec_lua([[return vim.json.decode('-100000.5')]])) + eq(-100000.5e50, exec_lua([[return vim.json.decode('-100000.5e50')]])) + eq(100000.5e50, exec_lua([[return vim.json.decode('100000.5e50')]])) + eq(100000.5e50, exec_lua([[return vim.json.decode('100000.5e+50')]])) + eq(-100000.5e-50, exec_lua([[return vim.json.decode('-100000.5e-50')]])) + eq(100000.5e-50, exec_lua([[return vim.json.decode('100000.5e-50')]])) + eq(100000e-50, exec_lua([[return vim.json.decode('100000e-50')]])) + eq(0.5, exec_lua([[return vim.json.decode('0.5')]])) + eq(0.005, exec_lua([[return vim.json.decode('0.005')]])) + eq(0.005, exec_lua([[return vim.json.decode('0.00500')]])) + eq(0.5, exec_lua([[return vim.json.decode('0.00500e+002')]])) + eq(0.00005, exec_lua([[return vim.json.decode('0.00500e-002')]])) + + eq(-0.0, exec_lua([[return vim.json.decode('-0.0')]])) + eq(-0.0, exec_lua([[return vim.json.decode('-0.0e0')]])) + eq(-0.0, exec_lua([[return vim.json.decode('-0.0e+0')]])) + eq(-0.0, exec_lua([[return vim.json.decode('-0.0e-0')]])) + eq(-0.0, exec_lua([[return vim.json.decode('-0e-0')]])) + eq(-0.0, exec_lua([[return vim.json.decode('-0e-2')]])) + eq(-0.0, exec_lua([[return vim.json.decode('-0e+2')]])) + + eq(0.0, exec_lua([[return vim.json.decode('0.0')]])) + eq(0.0, exec_lua([[return vim.json.decode('0.0e0')]])) + eq(0.0, exec_lua([[return vim.json.decode('0.0e+0')]])) + eq(0.0, exec_lua([[return vim.json.decode('0.0e-0')]])) + eq(0.0, exec_lua([[return vim.json.decode('0e-0')]])) + eq(0.0, exec_lua([[return vim.json.decode('0e-2')]])) + eq(0.0, exec_lua([[return vim.json.decode('0e+2')]])) + end) + + it('parses containers', function() + eq({1}, exec_lua([[return vim.json.decode('[1]')]])) + eq({NIL, 1}, exec_lua([[return vim.json.decode('[null, 1]')]])) + eq({['1']=2}, exec_lua([[return vim.json.decode('{"1": 2}')]])) + eq({['1']=2, ['3']={{['4']={['5']={{}, 1}}}}}, + exec_lua([[return vim.json.decode('{"1": 2, "3": [{"4": {"5": [ [], 1]}}]}')]])) + end) + + it('parses strings properly', function() + eq('\n', exec_lua([=[return vim.json.decode([["\n"]])]=])) + eq('', exec_lua([=[return vim.json.decode([[""]])]=])) + eq('\\/"\t\b\n\r\f', exec_lua([=[return vim.json.decode([["\\\/\"\t\b\n\r\f"]])]=])) + eq('/a', exec_lua([=[return vim.json.decode([["\/a"]])]=])) + -- Unicode characters: 2-byte, 3-byte + eq('«',exec_lua([=[return vim.json.decode([["«"]])]=])) + eq('ફ',exec_lua([=[return vim.json.decode([["ફ"]])]=])) + end) + + it('parses surrogate pairs properly', function() + eq('\240\144\128\128', exec_lua([[return vim.json.decode('"\\uD800\\uDC00"')]])) + end) + + it('accepts all spaces in every position where space may be put', function() + local s = ' \t\n\r \t\r\n \n\t\r \n\r\t \r\t\n \r\n\t\t \n\r\t \r\n\t\n \r\t\n\r \t\r \n\t\r\n \n \t\r\n \r\t\n\t \r\n\t\r \n\r \t\n\r\t \r \t\n\r \n\t\r\t \n\r\t\n \r\n \t\r\n\t' + local str = ('%s{%s"key"%s:%s[%s"val"%s,%s"val2"%s]%s,%s"key2"%s:%s1%s}%s'):gsub('%%s', s) + eq({key={'val', 'val2'}, key2=1}, exec_lua([[return vim.json.decode(...)]], str)) + end) + +end) + +describe('vim.json.encode function', function() + before_each(function() + clear() + end) + + it('dumps strings', function() + eq('"Test"', exec_lua([[return vim.json.encode('Test')]])) + eq('""', exec_lua([[return vim.json.encode('')]])) + eq('"\\t"', exec_lua([[return vim.json.encode('\t')]])) + eq('"\\n"', exec_lua([[return vim.json.encode('\n')]])) + -- vim.fn.json_encode return \\u001B + eq('"\\u001b"', exec_lua([[return vim.json.encode('\27')]])) + eq('"þÿþ"', exec_lua([[return vim.json.encode('þÿþ')]])) + end) + + it('dumps numbers', function() + eq('0', exec_lua([[return vim.json.encode(0)]])) + eq('10', exec_lua([[return vim.json.encode(10)]])) + eq('-10', exec_lua([[return vim.json.encode(-10)]])) + end) + + it('dumps floats', function() + eq('10.5', exec_lua([[return vim.json.encode(10.5)]])) + eq('-10.5', exec_lua([[return vim.json.encode(-10.5)]])) + eq('-1e-05', exec_lua([[return vim.json.encode(-1e-5)]])) + end) + + it('dumps lists', function() + eq('[]', exec_lua([[return vim.json.encode({})]])) + eq('[[]]', exec_lua([[return vim.json.encode({{}})]])) + eq('[[],[]]', exec_lua([[return vim.json.encode({{}, {}})]])) + end) + + it('dumps dictionaries', function() + eq('{}', exec_lua([[return vim.json.encode(vim.empty_dict())]])) + eq('{"d":[]}', exec_lua([[return vim.json.encode({d={}})]])) + end) + + it('dumps vim.NIL', function() + eq('null', exec_lua([[return vim.json.encode(vim.NIL)]])) + end) + +end) diff --git a/test/functional/lua/luaeval_spec.lua b/test/functional/lua/luaeval_spec.lua index 255e99032f..0675ec9abd 100644 --- a/test/functional/lua/luaeval_spec.lua +++ b/test/functional/lua/luaeval_spec.lua @@ -86,14 +86,15 @@ describe('luaeval()', function() -- meaningful later. it('correctly evaluates scalars', function() + -- Also test method call (->) syntax eq(1, funcs.luaeval('1')) - eq(0, eval('type(luaeval("1"))')) + eq(0, eval('"1"->luaeval()->type()')) eq(1.5, funcs.luaeval('1.5')) - eq(5, eval('type(luaeval("1.5"))')) + eq(5, eval('"1.5"->luaeval()->type()')) eq("test", funcs.luaeval('"test"')) - eq(1, eval('type(luaeval("\'test\'"))')) + eq(1, eval('"\'test\'"->luaeval()->type()')) eq('', funcs.luaeval('""')) eq('\000', funcs.luaeval([['\0']])) diff --git a/test/functional/lua/ui_spec.lua b/test/functional/lua/ui_spec.lua new file mode 100644 index 0000000000..94f1b5840b --- /dev/null +++ b/test/functional/lua/ui_spec.lua @@ -0,0 +1,46 @@ +local helpers = require('test.functional.helpers')(after_each) +local eq = helpers.eq +local exec_lua = helpers.exec_lua +local clear = helpers.clear + +describe('vim.ui', function() + before_each(function() + clear() + end) + + + describe('select', function() + it('can select an item', function() + local result = exec_lua[[ + local items = { + { name = 'Item 1' }, + { name = 'Item 2' }, + } + local opts = { + format_item = function(entry) + return entry.name + end + } + local selected + local cb = function(item) + selected = item + end + -- inputlist would require input and block the test; + local choices + vim.fn.inputlist = function(x) + choices = x + return 1 + end + vim.ui.select(items, opts, cb) + vim.wait(100, function() return selected ~= nil end) + return {selected, choices} + ]] + eq({ name = 'Item 1' }, result[1]) + eq({ + 'Select one of:', + '1: Item 1', + '2: Item 2', + }, result[2]) + end) + end) +end) diff --git a/test/functional/lua/uri_spec.lua b/test/functional/lua/uri_spec.lua index 052a8a1ecd..81f1820986 100644 --- a/test/functional/lua/uri_spec.lua +++ b/test/functional/lua/uri_spec.lua @@ -2,6 +2,7 @@ local helpers = require('test.functional.helpers')(after_each) local clear = helpers.clear local exec_lua = helpers.exec_lua local eq = helpers.eq +local write_file = require('test.helpers').write_file describe('URI methods', function() before_each(function() @@ -158,6 +159,22 @@ describe('URI methods', function() end) + describe('uri from bufnr', function() + it('Windows paths should not be treated as uris', function() + if not helpers.iswin() then return end + + local file = helpers.tmpname() + write_file(file, 'Test content') + local test_case = string.format([[ + local file = '%s' + return vim.uri_from_bufnr(vim.fn.bufadd(file)) + ]], file) + local expected_uri = 'file:///' .. file:gsub("\\", "/") + eq(expected_uri, exec_lua(test_case)) + os.remove(file) + end) + end) + describe('uri to bufnr', function() it('uri_to_bufnr & uri_from_bufnr returns original uri for non-file uris', function() local uri = 'jdt://contents/java.base/java.util/List.class?=sql/%5C/home%5C/user%5C/.jabba%5C/jdk%5C/openjdk%5C@1.14.0%5C/lib%5C/jrt-fs.jar%60java.base=/javadoc_location=/https:%5C/%5C/docs.oracle.com%5C/en%5C/java%5C/javase%5C/14%5C/docs%5C/api%5C/=/%3Cjava.util(List.class' diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index f3d265cb92..3123063b8c 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -237,29 +237,34 @@ describe('lua stdlib', function() end) it("vim.split", function() - local split = function(str, sep, plain) - return exec_lua('return vim.split(...)', str, sep, plain) + local split = function(str, sep, kwargs) + return exec_lua('return vim.split(...)', str, sep, kwargs) end local tests = { - { "a,b", ",", false, { 'a', 'b' } }, - { ":aa::bb:", ":", false, { '', 'aa', '', 'bb', '' } }, - { "::ee::ff:", ":", false, { '', '', 'ee', '', 'ff', '' } }, - { "ab", ".", false, { '', '', '' } }, - { "a1b2c", "[0-9]", false, { 'a', 'b', 'c' } }, - { "xy", "", false, { 'x', 'y' } }, - { "here be dragons", " ", false, { "here", "be", "dragons"} }, - { "axaby", "ab?", false, { '', 'x', 'y' } }, - { "f v2v v3v w2w ", "([vw])2%1", false, { 'f ', ' v3v ', ' ' } }, - { "", "", false, {} }, - { "", "a", false, { '' } }, - { "x*yz*oo*l", "*", true, { 'x', 'yz', 'oo', 'l' } }, + { "a,b", ",", false, false, { 'a', 'b' } }, + { ":aa::bb:", ":", false, false, { '', 'aa', '', 'bb', '' } }, + { ":aa::bb:", ":", false, true, { 'aa', '', 'bb' } }, + { "::ee::ff:", ":", false, false, { '', '', 'ee', '', 'ff', '' } }, + { "::ee::ff:", ":", false, true, { 'ee', '', 'ff' } }, + { "ab", ".", false, false, { '', '', '' } }, + { "a1b2c", "[0-9]", false, false, { 'a', 'b', 'c' } }, + { "xy", "", false, false, { 'x', 'y' } }, + { "here be dragons", " ", false, false, { "here", "be", "dragons"} }, + { "axaby", "ab?", false, false, { '', 'x', 'y' } }, + { "f v2v v3v w2w ", "([vw])2%1", false, false, { 'f ', ' v3v ', ' ' } }, + { "", "", false, false, {} }, + { "", "a", false, false, { '' } }, + { "x*yz*oo*l", "*", true, false, { 'x', 'yz', 'oo', 'l' } }, } for _, t in ipairs(tests) do - eq(t[4], split(t[1], t[2], t[3])) + eq(t[5], split(t[1], t[2], {plain=t[3], trimempty=t[4]})) end + -- Test old signature + eq({'x', 'yz', 'oo', 'l'}, split("x*yz*oo*l", "*", true)) + local loops = { { "abc", ".-" }, } @@ -283,9 +288,8 @@ describe('lua stdlib', function() vim/shared.lua:0: in function <vim/shared.lua:0>]]), pcall_err(split, 'string', 1)) eq(dedent([[ - Error executing lua: vim/shared.lua:0: plain: expected boolean, got number + Error executing lua: vim/shared.lua:0: kwargs: expected table, got number stack traceback: - vim/shared.lua:0: in function 'gsplit' vim/shared.lua:0: in function <vim/shared.lua:0>]]), pcall_err(split, 'string', 'string', 1)) end) @@ -992,6 +996,9 @@ describe('lua stdlib', function() vim.g.to_delete = nil ]] eq(NIL, funcs.luaeval "vim.g.to_delete") + + matches([[^Error executing lua: .*: attempt to index .* nil value]], + pcall_err(exec_lua, 'return vim.g[0].testing')) end) it('vim.b', function() @@ -1001,18 +1008,25 @@ describe('lua stdlib', function() vim.api.nvim_buf_set_var(0, "floaty", 5120.1) vim.api.nvim_buf_set_var(0, "nullvar", vim.NIL) vim.api.nvim_buf_set_var(0, "to_delete", {hello="world"}) + BUF = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_var(BUF, "testing", "bye") ]] eq('hi', funcs.luaeval "vim.b.testing") + eq('bye', funcs.luaeval "vim.b[BUF].testing") eq(123, funcs.luaeval "vim.b.other") eq(5120.1, funcs.luaeval "vim.b.floaty") eq(NIL, funcs.luaeval "vim.b.nonexistant") + eq(NIL, funcs.luaeval "vim.b[BUF].nonexistant") eq(NIL, funcs.luaeval "vim.b.nullvar") -- lost over RPC, so test locally: eq({false, true}, exec_lua [[ return {vim.b.nonexistant == vim.NIL, vim.b.nullvar == vim.NIL} ]]) + matches([[^Error executing lua: .*: attempt to index .* nil value]], + pcall_err(exec_lua, 'return vim.b[BUF][0].testing')) + eq({hello="world"}, funcs.luaeval "vim.b.to_delete") exec_lua [[ vim.b.to_delete = nil @@ -1033,11 +1047,22 @@ describe('lua stdlib', function() vim.api.nvim_win_set_var(0, "testing", "hi") vim.api.nvim_win_set_var(0, "other", 123) vim.api.nvim_win_set_var(0, "to_delete", {hello="world"}) + BUF = vim.api.nvim_create_buf(false, true) + WIN = vim.api.nvim_open_win(BUF, false, { + width=10, height=10, + relative='win', row=0, col=0 + }) + vim.api.nvim_win_set_var(WIN, "testing", "bye") ]] eq('hi', funcs.luaeval "vim.w.testing") + eq('bye', funcs.luaeval "vim.w[WIN].testing") eq(123, funcs.luaeval "vim.w.other") eq(NIL, funcs.luaeval "vim.w.nonexistant") + eq(NIL, funcs.luaeval "vim.w[WIN].nonexistant") + + matches([[^Error executing lua: .*: attempt to index .* nil value]], + pcall_err(exec_lua, 'return vim.w[WIN][0].testing')) eq({hello="world"}, funcs.luaeval "vim.w.to_delete") exec_lua [[ @@ -1064,6 +1089,12 @@ describe('lua stdlib', function() eq('hi', funcs.luaeval "vim.t.testing") eq(123, funcs.luaeval "vim.t.other") eq(NIL, funcs.luaeval "vim.t.nonexistant") + eq('hi', funcs.luaeval "vim.t[0].testing") + eq(123, funcs.luaeval "vim.t[0].other") + eq(NIL, funcs.luaeval "vim.t[0].nonexistant") + + matches([[^Error executing lua: .*: attempt to index .* nil value]], + pcall_err(exec_lua, 'return vim.t[0][0].testing')) eq({hello="world"}, funcs.luaeval "vim.t.to_delete") exec_lua [[ @@ -1092,6 +1123,8 @@ describe('lua stdlib', function() eq(funcs.luaeval "vim.api.nvim_get_vvar('progpath')", funcs.luaeval "vim.v.progpath") eq(false, funcs.luaeval "vim.v['false']") eq(NIL, funcs.luaeval "vim.v.null") + matches([[^Error executing lua: .*: attempt to index .* nil value]], + pcall_err(exec_lua, 'return vim.v[0].progpath')) end) it('vim.bo', function() @@ -2255,7 +2288,7 @@ end) describe('lua: require("mod") from packages', function() before_each(function() - command('set rtp+=test/functional/fixtures') + command('set rtp+=test/functional/fixtures pp+=test/functional/fixtures') end) it('propagates syntax error', function() @@ -2266,4 +2299,13 @@ describe('lua: require("mod") from packages', function() matches("unexpected symbol", syntax_error_msg) end) + + it('uses the right order of mod.lua vs mod/init.lua', function() + -- lua/fancy_x.lua takes precedence over lua/fancy_x/init.lua + eq('I am fancy_x.lua', exec_lua [[ return require'fancy_x' ]]) + -- but lua/fancy_y/init.lua takes precedence over after/lua/fancy_y.lua + eq('I am init.lua of fancy_y!', exec_lua [[ return require'fancy_y' ]]) + -- safety check: after/lua/fancy_z.lua is still loaded + eq('I am fancy_z.lua', exec_lua [[ return require'fancy_z' ]]) + end) end) |