executor,functests: Add print() tests, some fixes

This commit is contained in:
ZyX 2017-01-29 23:22:50 +03:00
parent ebad046220
commit 9fd2bf67aa
4 changed files with 101 additions and 11 deletions

View File

@ -384,9 +384,6 @@ static int nlua_print(lua_State *const lstate)
lua_pushvalue(lstate, curargidx); // arg
if (lua_pcall(lstate, 1, 1, 0)) {
errmsg = lua_tolstring(lstate, -1, &errmsg_len);
if (!errmsg) {
PRINT_ERROR("<Unknown error: lua_tolstring returned NULL for error>");
}
goto nlua_print_error;
}
size_t len;
@ -408,19 +405,32 @@ static int nlua_print(lua_State *const lstate)
const size_t len = (size_t)msg_ga.ga_len - 1;
char *const str = (char *)msg_ga.ga_data;
for (size_t i = 0; i < len - 1;) {
for (size_t i = 0; i < len;) {
const size_t start = i;
while (str[i] != NL && i < len - 1) {
if (str[i] == NUL) {
str[i] = NL;
while (i < len) {
switch (str[i]) {
case NUL: {
str[i] = NL;
i++;
continue;
}
case NL: {
str[i] = NUL;
i++;
break;
}
default: {
i++;
continue;
}
}
i++;
}
if (str[i] == NL) {
str[i] = NUL;
break;
}
msg((char_u *)str + start);
}
if (str[len - 1] == NUL) { // Last was newline
msg((char_u *)"");
}
}
ga_clear(&msg_ga);
return 0;

View File

@ -46,6 +46,10 @@ describe(':lua command', function()
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()
eq([=[Vim(lua):E5105: Error while calling lua chunk: [NULL]]=],
exc_exec('lua error(nil)'))
end)
it('accepts embedded NLs without heredoc', function()
-- Such code is usually used for `:execute 'lua' {generated_string}`:
-- heredocs do not work in this case.
@ -106,6 +110,10 @@ describe(':luado command', function()
eq([[Vim(luado):E5111: Error while calling lua function: [string "<VimL compiled string>"]:1: attempt to perform arithmetic on global 'liness' (a nil value)]],
exc_exec('luado return liness + 1'))
end)
it('works with NULL errors', function()
eq([=[Vim(luado):E5111: Error while calling lua function: [NULL]]=],
exc_exec('luado error(nil)'))
end)
it('fails in sandbox when needed', function()
curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
eq('\nE48: Not allowed in sandbox: sandbox luado runs = (runs or 0) + 1',
@ -148,4 +156,9 @@ describe(':luafile', function()
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))
end)
it('works with NULL errors', function()
write_file(fname, 'error(nil)')
eq([=[Vim(luafile):E5113: Error while calling lua chunk: [NULL]]=],
exc_exec('luafile ' .. fname))
end)
end)

View File

@ -241,6 +241,8 @@ describe('luaeval()', function()
exc_exec([[call luaeval("vim.xxx_nonexistent_key_xxx()")]]))
eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: ERROR',
exc_exec([[call luaeval("error('ERROR')")]]))
eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [NULL]',
exc_exec([[call luaeval("error(nil)")]]))
end)
it('does not leak memory when called with too long line',

View File

@ -0,0 +1,65 @@
-- Test for Vim overrides of lua built-ins
local helpers = require('test.functional.helpers')(after_each)
local eq = helpers.eq
local NIL = helpers.NIL
local clear = helpers.clear
local funcs = helpers.funcs
local meths = helpers.meths
local command = helpers.command
local write_file = helpers.write_file
local redir_exec = helpers.redir_exec
local fname = 'Xtest-functional-lua-overrides-luafile'
before_each(clear)
after_each(function()
os.remove(fname)
end)
describe('print', function()
it('returns nothing', function()
eq(NIL, funcs.luaeval('print("abc")'))
eq(0, funcs.luaeval('select("#", print("abc"))'))
end)
it('allows catching printed text with :execute', function()
eq('\nabc', funcs.execute('lua print("abc")'))
eq('\nabc', funcs.execute('luado print("abc")'))
eq('\nabc', funcs.execute('call luaeval("print(\'abc\')")'))
write_file(fname, 'print("abc")')
eq('\nabc', funcs.execute('luafile ' .. fname))
eq('\nabc', redir_exec('lua print("abc")'))
eq('\nabc', redir_exec('luado print("abc")'))
eq('\nabc', redir_exec('call luaeval("print(\'abc\')")'))
write_file(fname, 'print("abc")')
eq('\nabc', redir_exec('luafile ' .. fname))
end)
it('handles errors in __tostring', function()
write_file(fname, [[
local meta_nilerr = { __tostring = function() error(nil) end }
local meta_abcerr = { __tostring = function() error("abc") end }
local meta_tblout = { __tostring = function() return {"TEST"} end }
v_nilerr = setmetatable({}, meta_nilerr)
v_abcerr = setmetatable({}, meta_abcerr)
v_tblout = setmetatable({}, meta_tblout)
]])
eq('', redir_exec('luafile ' .. fname))
eq('\nE5114: Error while converting print argument #2: [NULL]',
redir_exec('lua print("foo", v_nilerr, "bar")'))
eq('\nE5114: Error while converting print argument #2: Xtest-functional-lua-overrides-luafile:2: abc',
redir_exec('lua print("foo", v_abcerr, "bar")'))
eq('\nE5114: Error while converting print argument #2: <Unknown error: lua_tolstring returned NULL for tostring result>',
redir_exec('lua print("foo", v_tblout, "bar")'))
end)
it('prints strings with NULs and NLs correctly', function()
meths.set_option('more', true)
eq('\nabc ^@ def\nghi^@^@^@jkl\nTEST\n\n\nT\n',
redir_exec([[lua print("abc \0 def\nghi\0\0\0jkl\nTEST\n\n\nT\n")]]))
eq('\nabc ^@ def\nghi^@^@^@jkl\nTEST\n\n\nT^@',
redir_exec([[lua print("abc \0 def\nghi\0\0\0jkl\nTEST\n\n\nT\0")]]))
eq('\nT^@', redir_exec([[lua print("T\0")]]))
eq('\nT\n', redir_exec([[lua print("T\n")]]))
end)
end)