Merge #11271 from h-michael/add-more-info

lua/vim.shared: improve some validation messages
This commit is contained in:
Justin M. Keyes 2019-10-26 02:30:40 -07:00 committed by GitHub
commit 19ba36d0e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 22 deletions

View File

@ -44,9 +44,9 @@ end
--@param plain If `true` use `sep` literally (passed to String.find)
--@returns Iterator over the split components
local function gsplit(s, sep, plain)
assert(type(s) == "string")
assert(type(sep) == "string")
assert(type(plain) == "boolean" or type(plain) == "nil")
assert(type(s) == "string", string.format("Expected string, got %s", type(s)))
assert(type(sep) == "string", string.format("Expected string, got %s", type(sep)))
assert(type(plain) == "boolean" or type(plain) == "nil", string.format("Expected boolean or nil, got %s", type(plain)))
local start = 1
local done = false
@ -103,9 +103,8 @@ end
--@param value Value to compare
--@returns true if `t` contains `value`
local function tbl_contains(t, value)
if type(t) ~= 'table' then
error('t must be a table')
end
assert(type(t) == 'table', string.format("Expected table, got %s", type(t)))
for _,v in ipairs(t) do
if v == value then
return true
@ -174,7 +173,7 @@ end
--@param s String to trim
--@returns String with whitespace removed from its beginning and end
local function trim(s)
assert(type(s) == 'string', 'Only strings can be trimmed')
assert(type(s) == 'string', string.format("Expected string, got %s", type(s)))
return s:match('^%s*(.*%S)') or ''
end
@ -184,7 +183,7 @@ end
--@param s String to escape
--@returns %-escaped pattern string
local function pesc(s)
assert(type(s) == 'string')
assert(type(s) == 'string', string.format("Expected string, got %s", type(s)))
return s:gsub('[%(%)%.%%%+%-%*%?%[%]%^%$]', '%%%1')
end

View File

@ -15,9 +15,7 @@ before_each(clear)
describe('treesitter API', function()
-- error tests not requiring a parser library
it('handles missing language', function()
local path_pat = 'Error executing lua: '..(iswin() and '.+\\vim\\' or '.+/vim/')
matches(path_pat..'treesitter.lua:39: no such language: borklang',
eq('Error executing lua: .../treesitter.lua: no such language: borklang',
pcall_err(exec_lua, "parser = vim.treesitter.create_parser(0, 'borklang')"))
-- actual message depends on platform

View File

@ -9,6 +9,7 @@ local eval = helpers.eval
local feed = helpers.feed
local pcall_err = helpers.pcall_err
local exec_lua = helpers.exec_lua
local matches = helpers.matches
before_each(clear)
@ -146,10 +147,9 @@ describe('lua stdlib', function()
]])
eq({"yy","xx"}, exec_lua("return test_table"))
-- type checked args
-- Validates args.
eq('Error executing lua: vim.schedule: expected function',
pcall_err(exec_lua, "vim.schedule('stringly')"))
eq('Error executing lua: vim.schedule: expected function',
pcall_err(exec_lua, "vim.schedule()"))
@ -195,8 +195,8 @@ describe('lua stdlib', function()
end)
it("vim.split", function()
local split = function(str, sep)
return exec_lua('return vim.split(...)', str, sep)
local split = function(str, sep, plain)
return exec_lua('return vim.split(...)', str, sep, plain)
end
local tests = {
@ -221,10 +221,17 @@ describe('lua stdlib', function()
}
for _, t in ipairs(loops) do
local status, err = pcall(split, t[1], t[2])
eq(false, status)
assert(string.match(err, "Infinite loop detected"))
matches(".*Infinite loop detected", pcall_err(split, t[1], t[2]))
end
-- Validates args.
eq(true, pcall(split, 'string', 'string', nil))
eq('Error executing lua: .../shared.lua: Expected string, got number',
pcall_err(split, 1, 'string', nil))
eq('Error executing lua: .../shared.lua: Expected string, got number',
pcall_err(split, 'string', 1, nil))
eq('Error executing lua: .../shared.lua: Expected boolean or nil, got number',
pcall_err(split, 'string', 'string', 1))
end)
it('vim.trim', function()
@ -243,9 +250,9 @@ describe('lua stdlib', function()
assert(t[2], trim(t[1]))
end
local status, err = pcall(trim, 2)
eq(false, status)
assert(string.match(err, "Only strings can be trimmed"))
-- Validates args.
eq('Error executing lua: .../shared.lua: Expected string, got number',
pcall_err(trim, 2))
end)
it('vim.inspect', function()
@ -287,5 +294,9 @@ describe('lua stdlib', function()
it('vim.pesc', function()
eq('foo%-bar', exec_lua([[return vim.pesc('foo-bar')]]))
eq('foo%%%-bar', exec_lua([[return vim.pesc(vim.pesc('foo-bar'))]]))
-- Validates args.
eq("Error executing lua: .../shared.lua: Expected string, got number",
pcall_err(exec_lua, [[return vim.pesc(2)]]))
end)
end)

View File

@ -74,7 +74,8 @@ function module.matches(pat, actual)
error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual))
end
-- Invokes `fn` and returns the error string, or raises an error if `fn` succeeds.
-- Invokes `fn` and returns the error string (may truncate full paths), or
-- raises an error if `fn` succeeds.
--
-- Usage:
-- -- Match exact string.
@ -88,7 +89,17 @@ function module.pcall_err(fn, ...)
if status == true then
error('expected failure, but got success')
end
-- From this:
-- /home/foo/neovim/runtime/lua/vim/shared.lua:186: Expected string, got number
-- to this:
-- Expected string, got number
local errmsg = tostring(rv):gsub('^[^:]+:%d+: ', '')
-- From this:
-- Error executing lua: /very/long/foo.lua:186: Expected string, got number
-- to this:
-- Error executing lua: .../foo.lua:186: Expected string, got number
errmsg = errmsg:gsub([[lua: [a-zA-Z]?:?[^:]-[/\]([^:/\]+):%d+: ]], 'lua: .../%1: ')
-- ^ Windows drive-letter (C:)
return errmsg
end