add support for custom test wrapper functions

to support @BeforeEach and @AfterEach semantics. however, much of our
code is scope-based (lambda-based?) so it seemed to make sense to
use a single wrapper function instead of separate before_each and
after_each functions.
develop
myk002 2021-06-30 07:10:59 -07:00
parent 0b2e26d095
commit c1df1ceff7
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
1 changed files with 17 additions and 0 deletions

@ -280,8 +280,21 @@ end
local function build_test_env() local function build_test_env()
local env = { local env = {
test = utils.OrderedTable(), test = utils.OrderedTable(),
-- config values can be overridden in the test file to define
-- requirements for the tests in that file
config = { config = {
-- override with the required game mode
mode = 'none', mode = 'none',
-- override with a test wrapper function with common setup and
-- teardown for every test, if needed. for example:
-- local function test_wrapper(test_fn)
-- mock.patch(dfhack.filesystem,
-- 'listdir_recursive',
-- mock.func({}),
-- test_fn)
-- end
-- config.wrapper = test_wrapper
wrapper = nil,
}, },
expect = {}, expect = {},
mock = mock, mock = mock,
@ -351,6 +364,10 @@ local function load_tests(file, tests)
return false return false
end end
for name, test_func in pairs(env.test) do for name, test_func in pairs(env.test) do
if env.config.wrapper then
local fn = test_func
test_func = function() env.config.wrapper(fn) end
end
local test_data = { local test_data = {
full_name = short_filename .. ':' .. name, full_name = short_filename .. ':' .. name,
func = test_func, func = test_func,