test: Eliminate expect_err

Eliminate `expect_err` in favor of `pcall_err` + `eq` or `matches`.
This commit is contained in:
Justin M. Keyes
2019-09-06 17:17:37 -07:00
parent af946046b9
commit 7e1c959861
21 changed files with 214 additions and 216 deletions

View File

@@ -74,15 +74,14 @@ function module.matches(pat, actual)
error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual))
end
-- Expects an error matching Lua pattern `pat`.
--
function module.expect_err(pat, fn, ...)
assert(type(fn) == 'function')
local fn_args = {...}
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.
--
-- 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, ...)