lua/stdlib: adjust some validation messages #11271

close #11271
This commit is contained in:
Hirokazu Hata
2019-10-21 23:46:28 +09:00
committed by Justin M. Keyes
parent 91b831da8b
commit 996a057fb9
2 changed files with 34 additions and 23 deletions

View File

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

View File

@@ -9,6 +9,8 @@ local eval = helpers.eval
local feed = helpers.feed local feed = helpers.feed
local pcall_err = helpers.pcall_err local pcall_err = helpers.pcall_err
local exec_lua = helpers.exec_lua local exec_lua = helpers.exec_lua
local matches = helpers.matches
local iswin = helpers.iswin
before_each(clear) before_each(clear)
@@ -147,11 +149,8 @@ describe('lua stdlib', function()
eq({"yy","xx"}, exec_lua("return test_table")) eq({"yy","xx"}, exec_lua("return test_table"))
-- type checked args -- type checked args
eq('Error executing lua: vim.schedule: expected function', eq('Error executing lua: vim.schedule: expected function', pcall_err(exec_lua, "vim.schedule('stringly')"))
pcall_err(exec_lua, "vim.schedule('stringly')")) eq('Error executing lua: vim.schedule: expected function', pcall_err(exec_lua, "vim.schedule()"))
eq('Error executing lua: vim.schedule: expected function',
pcall_err(exec_lua, "vim.schedule()"))
exec_lua([[ exec_lua([[
vim.schedule(function() vim.schedule(function()
@@ -195,8 +194,8 @@ describe('lua stdlib', function()
end) end)
it("vim.split", function() it("vim.split", function()
local split = function(str, sep) local split = function(str, sep, plain)
return exec_lua('return vim.split(...)', str, sep) return exec_lua('return vim.split(...)', str, sep, plain)
end end
local tests = { local tests = {
@@ -221,10 +220,15 @@ describe('lua stdlib', function()
} }
for _, t in ipairs(loops) do for _, t in ipairs(loops) do
local status, err = pcall(split, t[1], t[2]) matches(".*Infinite loop detected", pcall_err(split, t[1], t[2]))
eq(false, status)
assert(string.match(err, "Infinite loop detected"))
end end
-- type checked args
eq(true, pcall(split, 'string', 'string', nil))
local path_pattern = iswin() and '[a-zA-Z]:[^:]+:%d+:' or '[^:]+:%d+:'
matches("Error executing lua: "..path_pattern.." Expected string, got number", pcall_err(split, 1, 'string', nil))
matches("Error executing lua: "..path_pattern.." Expected string, got number", pcall_err(split, 'string', 1, nil))
matches("Error executing lua: "..path_pattern.." Expected boolean or nil, got number", pcall_err(split, 'string', 'string', 1))
end) end)
it('vim.trim', function() it('vim.trim', function()
@@ -243,9 +247,9 @@ describe('lua stdlib', function()
assert(t[2], trim(t[1])) assert(t[2], trim(t[1]))
end end
local status, err = pcall(trim, 2) -- type checked args
eq(false, status) local path_pattern = iswin() and '[a-zA-Z]:[^:]+:%d+:' or '[^:]+:%d+:'
assert(string.match(err, "Only strings can be trimmed")) matches("Error executing lua: "..path_pattern.." Expected string, got number", pcall_err(trim, 2))
end) end)
it('vim.inspect', function() it('vim.inspect', function()
@@ -285,7 +289,15 @@ describe('lua stdlib', function()
end) end)
it('vim.pesc', function() it('vim.pesc', function()
eq('foo%-bar', exec_lua([[return vim.pesc('foo-bar')]])) local pesc = function(s)
eq('foo%%%-bar', exec_lua([[return vim.pesc(vim.pesc('foo-bar'))]])) return exec_lua('return vim.pesc(...)', s)
end
eq('foo%-bar', pesc('foo-bar'))
eq('foo%%%-bar', pesc(pesc('foo-bar')))
-- type checked args
local path_pattern = iswin() and '[a-zA-Z]:[^:]+:%d+:' or '[^:]+:%d+:'
matches("Error executing lua: "..path_pattern.." Expected string, got number", pcall_err(pesc, 2))
end) end)
end) end)