detect dfhack.printerr usage at the test level

not the expect level. this allows us to properly nest expect calls
inside of expect.printerr_match() as originally intended.
develop
myk002 2021-04-03 15:40:40 -07:00
parent 5af1b627cb
commit c8abecd082
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
1 changed files with 30 additions and 24 deletions

@ -141,30 +141,14 @@ local function load_test_config(config_file)
return config return config
end end
local function run_expect_func(func, ...) -- we have to save and use the original dfhack.printerr here so our test harness
local args = {...} -- output doesn't trigger its own dfhack.printerr usage detection (see
local saved_printerr = dfhack.printerr -- detect_printerr below)
local printerr_called = false local orig_printerr = dfhack.printerr
dfhack.printerr = function(msg) printerr_called = true end
return dfhack.with_finalize(
function() dfhack.printerr = saved_printerr end,
function()
local ret = {func(table.unpack(args))}
if printerr_called then
return {false,
"dfhack.printerr was called outside of" ..
" expect.printerr_match(). please wrap your test" ..
" with expect.printerr_match()."}
end
return ret
end
)
end
local function wrap_expect(func, private) local function wrap_expect(func, private)
return function(...) return function(...)
private.checks = private.checks + 1 private.checks = private.checks + 1
local ret = run_expect_func(func, ...) local ret = {func(...)}
local ok = table.remove(ret, 1) local ok = table.remove(ret, 1)
if ok then if ok then
private.checks_ok = private.checks_ok + 1 private.checks_ok = private.checks_ok + 1
@ -177,9 +161,9 @@ local function wrap_expect(func, private)
end end
end end
msg = msg:sub(3) -- strip leading ': ' msg = msg:sub(3) -- strip leading ': '
dfhack.printerr('Check failed! ' .. (msg or '(no message)')) orig_printerr('Check failed! ' .. (msg or '(no message)'))
local info = debug.getinfo(2) local info = debug.getinfo(2)
dfhack.printerr((' at %s:%d'):format(info.short_src, info.currentline)) orig_printerr((' at %s:%d'):format(info.short_src, info.currentline))
print('') print('')
end end
end end
@ -282,12 +266,34 @@ local function sort_tests(tests)
end) end)
end end
local function detect_printerr(func)
local saved_printerr = dfhack.printerr
local printerr_called = false
dfhack.printerr = function(msg)
saved_printerr(msg)
printerr_called = true
end
return dfhack.with_finalize(
function() dfhack.printerr = saved_printerr end,
function()
local ok, err = pcall(func)
if printerr_called then
return false,
"dfhack.printerr was called outside of" ..
" expect.printerr_match(). please wrap your test" ..
" with expect.printerr_match()."
end
return ok, err
end
)
end
local function run_test(test, status, counts) local function run_test(test, status, counts)
test.private.checks = 0 test.private.checks = 0
test.private.checks_ok = 0 test.private.checks_ok = 0
counts.tests = counts.tests + 1 counts.tests = counts.tests + 1
dfhack.internal.IN_TEST = true dfhack.internal.IN_TEST = true
local ok, err = pcall(test.func) local ok, err = detect_printerr(test.func)
dfhack.internal.IN_TEST = false dfhack.internal.IN_TEST = false
local passed = false local passed = false
if not ok then if not ok then