handle nil values sent to printerr

develop
myk002 2021-04-05 01:37:43 -07:00
parent 81bfdf4182
commit 642d8cbe4b
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
3 changed files with 27 additions and 15 deletions

@ -270,6 +270,7 @@ local function detect_printerr(func)
local saved_printerr = dfhack.printerr
local printerr_called = false
dfhack.printerr = function(msg)
if msg == nil then return end
saved_printerr(msg)
printerr_called = true
end

@ -164,7 +164,7 @@ function expect.printerr_match(matcher, func, comment)
function() dfhack.printerr = saved_printerr end,
func)
if type(matcher) ~= 'table' then matcher = {matcher} end
while messages[1] do
while #messages > 0 do
local msg = messages[1]
if matches(msg, matcher[1]) then
table.remove(matcher, 1)

@ -59,20 +59,6 @@ function test.printerr_match()
local oneprint = function()
dfhack.printerr('a')
end
local twoprint = function()
dfhack.printerr('a')
dfhack.printerr('b')
end
local threeprint = function()
dfhack.printerr('a')
dfhack.printerr('b')
dfhack.printerr('c')
end
local multiprint = function()
dfhack.printerr('a')
dfhack.printerr('b')
dfhack.printerr('a')
end
expect.true_(expect_raw.printerr_match(nil, noprint))
expect.true_(expect_raw.printerr_match({}, noprint))
@ -90,6 +76,10 @@ function test.printerr_match()
expect.false_(expect_raw.printerr_match({b=1}, oneprint))
expect.false_(expect_raw.printerr_match({a=1,b=1}, oneprint))
local twoprint = function()
dfhack.printerr('a')
dfhack.printerr('b')
end
expect.true_(expect_raw.printerr_match({'a','b'}, twoprint))
expect.true_(expect_raw.printerr_match({a=1,b=1}, twoprint))
expect.false_(expect_raw.printerr_match({'b','a'}, twoprint))
@ -98,8 +88,29 @@ function test.printerr_match()
expect.false_(expect_raw.printerr_match({a=1,b=2}, twoprint))
expect.false_(expect_raw.printerr_match({a=1,b=1,c=1}, twoprint))
local threeprint = function()
dfhack.printerr('a')
dfhack.printerr('b')
dfhack.printerr('c')
end
expect.true_(expect_raw.printerr_match({a=1,b=1,c=1}, threeprint))
local multiprint = function()
dfhack.printerr('a')
dfhack.printerr('b')
dfhack.printerr('a')
end
expect.true_(expect_raw.printerr_match({a=2,b=1}, multiprint))
expect.false_(expect_raw.printerr_match({a=1,b=1}, multiprint))
local nilprint = function()
dfhack.printerr()
end
expect.true_(expect_raw.printerr_match({}, nilprint))
local nilaprint = function()
dfhack.printerr()
dfhack.printerr('a')
end
expect.true_(expect_raw.printerr_match({'a'}, nilaprint))
end