diff options
author | Lewis Russell <lewis6991@gmail.com> | 2022-11-14 10:01:35 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-14 10:01:35 +0000 |
commit | e8cc489accc435076afb4fdf89778b64f0a48473 (patch) | |
tree | 78b5a74c13cefb916752f25cf42e301a400fcab2 /test/helpers.lua | |
parent | 5c5187c6f809c50e2cfd0ba04961ae8e0d2dabd0 (diff) | |
download | rneovim-e8cc489accc435076afb4fdf89778b64f0a48473.tar.gz rneovim-e8cc489accc435076afb4fdf89778b64f0a48473.tar.bz2 rneovim-e8cc489accc435076afb4fdf89778b64f0a48473.zip |
feat(test): add Lua forms for API methods (#20152)
Diffstat (limited to 'test/helpers.lua')
-rw-r--r-- | test/helpers.lua | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/test/helpers.lua b/test/helpers.lua index 225a4d35b4..68c1b17dbc 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -114,25 +114,13 @@ function module.assert_nolog(pat, logfile, nrlines) return module.assert_log(pat, logfile, nrlines, true) end --- Invokes `fn` and returns the error string (with truncated paths), or raises --- an error if `fn` succeeds. --- --- Replaces line/column numbers with zero: --- shared.lua:0: in function 'gsplit' --- shared.lua:0: in function <shared.lua:0>' --- --- 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_withfile(fn, ...) +function module.pcall(fn, ...) assert(type(fn) == 'function') local status, rv = pcall(fn, ...) - if status == true then - error('expected failure, but got success') + if status then + return status, rv end + -- From: -- C:/long/path/foo.lua:186: Expected string, got number -- to: @@ -150,13 +138,37 @@ function module.pcall_err_withfile(fn, ...) -- We remove this so that the tests are not lua dependent. errmsg = errmsg:gsub('%s*%(tail call%): %?', '') - return errmsg + return status, errmsg +end + +-- Invokes `fn` and returns the error string (with truncated paths), or raises +-- an error if `fn` succeeds. +-- +-- Replaces line/column numbers with zero: +-- shared.lua:0: in function 'gsplit' +-- shared.lua:0: in function <shared.lua:0>' +-- +-- 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_withfile(fn, ...) + assert(type(fn) == 'function') + local status, rv = module.pcall(fn, ...) + if status == true then + error('expected failure, but got success') + end + return rv end function module.pcall_err_withtrace(fn, ...) local errmsg = module.pcall_err_withfile(fn, ...) - return errmsg:gsub('.../helpers.lua:0: ', '') + return errmsg:gsub('^%.%.%./helpers%.lua:0: ', '') + :gsub('^Error executing lua:- ' ,'') + :gsub('^%[string "<nvim>"%]:0: ' ,'') end function module.pcall_err(...) |