diff options
Diffstat (limited to 'test/functional/lua/vim_spec.lua')
-rw-r--r-- | test/functional/lua/vim_spec.lua | 203 |
1 files changed, 104 insertions, 99 deletions
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index a739992611..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,37 @@ 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 } + eq(expected_positions, exec_lua("return vim.str_utf_pos(_G.test_text)")) + end) + it("vim.schedule", function() exec_lua([[ test_table = {} @@ -202,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({ @@ -228,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) @@ -275,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) @@ -311,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) @@ -393,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) @@ -679,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 ]]) @@ -814,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>') @@ -883,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) @@ -996,6 +967,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() @@ -1005,18 +979,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 @@ -1037,11 +1018,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 [[ @@ -1068,6 +1060,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 [[ @@ -1096,6 +1094,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() @@ -2030,6 +2030,8 @@ describe('lua stdlib', function() -- Would wait ten seconds if results blocked. wait_result = vim.wait(10000, function() return vim.g.timer_result end) + timer:close() + return { time = (start_time + 5) > get_time(), wait_result = wait_result, @@ -2049,6 +2051,8 @@ describe('lua stdlib', function() wait_result = vim.wait(300, function() return vim.g.timer_result end, nil, true) + timer:close() + return { wait_result = wait_result, } @@ -2069,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() |