diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2019-09-06 18:16:57 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-06 18:16:57 -0700 |
commit | 158b78062e9daa65203b4591dae733dd6c11ad2c (patch) | |
tree | e6988da22f16062b57d50daeda084bcc489a6a1d /test/helpers.lua | |
parent | 638f2b6dee7439de303bea12dec80240617e8034 (diff) | |
parent | 7e1c9598617a140e40a0a22676c0631294617246 (diff) | |
download | rneovim-158b78062e9daa65203b4591dae733dd6c11ad2c.tar.gz rneovim-158b78062e9daa65203b4591dae733dd6c11ad2c.tar.bz2 rneovim-158b78062e9daa65203b4591dae733dd6c11ad2c.zip |
Merge #10932 'test: Eliminate expect_err'
Diffstat (limited to 'test/helpers.lua')
-rw-r--r-- | test/helpers.lua | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/test/helpers.lua b/test/helpers.lua index c2a708197f..ebc0a7d811 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -73,12 +73,23 @@ 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, ...) - local fn_args = {...} - table.remove(fn_args, 1) - assert.error_matches(function() return fn(unpack(fn_args)) end, pat) + +-- Invokes `fn` and returns the error string, or raises an error if `fn` succeeds. +-- +-- Usage: +-- -- Match exact string. +-- eq('e', pcall_err(function(a, b) error('e') end, 'arg1', 'arg2')) +-- -- Match Lua pattern. +-- matches('e[or]+$', pcall_err(function(a, b) error('some error') end, 'arg1', 'arg2')) +-- +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 |