aboutsummaryrefslogtreecommitdiff
path: root/test/helpers.lua
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2019-09-03 22:51:45 +0200
committerJustin M. Keyes <justinkz@gmail.com>2019-09-06 17:19:07 -0700
commitaf946046b922dc5d5285a70a23d11916d8389a5d (patch)
tree090d68a1dd675b5ad6e99abc829be0daafe2a7d9 /test/helpers.lua
parent638f2b6dee7439de303bea12dec80240617e8034 (diff)
downloadrneovim-af946046b922dc5d5285a70a23d11916d8389a5d.tar.gz
rneovim-af946046b922dc5d5285a70a23d11916d8389a5d.tar.bz2
rneovim-af946046b922dc5d5285a70a23d11916d8389a5d.zip
test: Rename meth_pcall to pcall_err
- Rename `meth_pcall`. - Make `pcall_err` raise an error if the function does not fail. - Add `vim.pesc()` to treat a string as literal where a Lua pattern is expected.
Diffstat (limited to 'test/helpers.lua')
-rw-r--r--test/helpers.lua20
1 files changed, 16 insertions, 4 deletions
diff --git a/test/helpers.lua b/test/helpers.lua
index c2a708197f..b6fd6d7e3f 100644
--- a/test/helpers.lua
+++ b/test/helpers.lua
@@ -73,14 +73,26 @@ function module.matches(pat, actual)
end
error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual))
end
--- Expect an error matching pattern `pat`.
-function module.expect_err(pat, ...)
- local fn = select(1, ...)
+
+-- Expects an error matching Lua pattern `pat`.
+--
+function module.expect_err(pat, fn, ...)
+ assert(type(fn) == 'function')
local fn_args = {...}
- table.remove(fn_args, 1)
assert.error_matches(function() return fn(unpack(fn_args)) end, pat)
end
+-- Invokes `fn` and returns the error string, or raises an error if `fn` succeeds.
+function module.pcall_err(fn, ...)
+ assert(type(fn) == 'function')
+ local status, rv = pcall(fn, ...)
+ if status == true then
+ error('expected failure, but got success')
+ end
+ local errmsg = tostring(rv):gsub('^[^:]+:%d+: ', '')
+ return errmsg
+end
+
-- initial_path: directory to recurse into
-- re: include pattern (string)
-- exc_re: exclude pattern(s) (string or table)