From 642d8cbe4b7883ef5d52738d0c2661ff8d67e61d Mon Sep 17 00:00:00 2001 From: myk002 Date: Mon, 5 Apr 2021 01:37:43 -0700 Subject: [PATCH] handle nil values sent to printerr --- ci/test.lua | 1 + library/lua/test_util/expect.lua | 2 +- test/library/test_util/expect_unit.lua | 39 +++++++++++++++++--------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/ci/test.lua b/ci/test.lua index 956ce07b6..2209f58b8 100644 --- a/ci/test.lua +++ b/ci/test.lua @@ -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 diff --git a/library/lua/test_util/expect.lua b/library/lua/test_util/expect.lua index 7ae1385c9..deca8c44f 100644 --- a/library/lua/test_util/expect.lua +++ b/library/lua/test_util/expect.lua @@ -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) diff --git a/test/library/test_util/expect_unit.lua b/test/library/test_util/expect_unit.lua index 6094149fb..6c71b59c1 100644 --- a/test/library/test_util/expect_unit.lua +++ b/test/library/test_util/expect_unit.lua @@ -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