From 9405472565cb4b0cb0c339d65babdef4d4cb7abd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 18 Aug 2019 17:29:46 -0300 Subject: Improvement in warn-mode '@store' (for testing) When using warn-mode '@store', from the test library, the tests ensure not only that the expected warnings were issued, but also that there was no extra warnings. --- testes/coroutine.lua | 11 ++++++++--- testes/gc.lua | 6 +++--- testes/locals.lua | 35 +++++++++++++++++++++-------------- testes/main.lua | 6 +++--- 4 files changed, 35 insertions(+), 23 deletions(-) (limited to 'testes') diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 79c72a9d..4fc23261 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -177,10 +177,15 @@ do end) coroutine.resume(co) assert(x == 0) - _WARN = nil; warn("@off"); warn("@store") + -- with test library, use 'store' mode to check warnings + warn(not T and "@off" or "@store") local st, msg = coroutine.close(co) - warn("@on"); warn("@normal") - assert(_WARN == nil or string.find(_WARN, "200")) + if not T then + warn("@on") + else -- test library + assert(string.find(_WARN, "200")); _WARN = nil + warn("@normal") + end assert(st == false and coroutine.status(co) == "dead" and msg == 111) assert(x == 200) diff --git a/testes/gc.lua b/testes/gc.lua index 6bdc98ca..34854d6f 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -372,7 +372,7 @@ if T then warn("@store") collectgarbage() assert(string.find(_WARN, "error in __gc metamethod")) - assert(string.match(_WARN, "@(.-)@") == "expected") + assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = nil for i = 8, 10 do assert(s[i]) end for i = 1, 5 do @@ -481,6 +481,7 @@ if T then u = setmetatable({}, {__gc = function () error "@expected error" end}) u = nil collectgarbage() + assert(string.find(_WARN, "@expected error")); _WARN = nil warn("@normal") end @@ -663,9 +664,8 @@ if T then else assert(lastmsg == _WARN) -- subsequent error messages are equal end - warn("@store") + warn("@store"); _WARN = nil error"@expected warning" - warn("@normal") end} for i = 10, 1, -1 do -- create object and preserve it until the end diff --git a/testes/locals.lua b/testes/locals.lua index 595e107a..b4de523d 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -288,9 +288,8 @@ end -- auxiliary functions for testing warnings in '__close' local function prepwarn () - warn("@off") -- do not show (lots of) warnings - if not T then - _WARN = "OFF" -- signal that warnings are not being captured + if not T then -- no test library? + warn("@off") -- do not show (lots of) warnings else warn("@store") -- to test the warnings end @@ -298,15 +297,20 @@ end local function endwarn () - assert(T or _WARN == "OFF") - warn("@on") -- back to normal - warn("@normal") - _WARN = nil + if not T then + warn("@on") -- back to normal + else + assert(_WARN == nil) + warn("@normal") + end end local function checkwarn (msg) - assert(_WARN == "OFF" or string.find(_WARN, msg)) + if T then + assert(string.find(_WARN, msg)) + _WARN = nil -- reset variable to check next warning + end end @@ -333,7 +337,8 @@ do print("testing errors in __close") local y = func2close(function (self, msg) - assert(string.find(msg, "@z")) -- error in 'z' + assert(string.find(msg, "@z")) -- first error in 'z' + checkwarn("@z") -- second error in 'z' generated a warning error("@y") end) @@ -377,14 +382,14 @@ do print("testing errors in __close") local y = func2close(function (self, msg) - assert(msg == 4) -- error in body + assert(msg == 4) -- error in body + checkwarn("@z") error("@y") end) local first = true local z = func2close(function (self, msg) - checkwarn("@z") -- 'z' close is called once assert(first and msg == 4) first = false @@ -418,16 +423,18 @@ do print("testing errors in __close") local st, msg = xpcall(foo, debug.traceback) assert(string.match(msg, "^[^ ]* @X")) assert(string.find(msg, "in metamethod 'close'")) + checkwarn("@Y") -- error in toclose in vararg function local function foo (...) - local x123 = func2close(function () error("@X") end) + local x123 = func2close(function () error("@x123") end) end local st, msg = xpcall(foo, debug.traceback) - assert(string.match(msg, "^[^ ]* @X")) - + assert(string.match(msg, "^[^ ]* @x123")) assert(string.find(msg, "in metamethod 'close'")) + checkwarn("@x123") -- from second call to close 'x123' + endwarn() end diff --git a/testes/main.lua b/testes/main.lua index 0ef4822d..36220362 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -379,12 +379,12 @@ if T then -- test library? -- testing 'warn' warn("@store") warn("@123", "456", "789") - assert(_WARN == "@123456789") + assert(_WARN == "@123456789"); _WARN = nil warn("zip", "", " ", "zap") - assert(_WARN == "zip zap") + assert(_WARN == "zip zap"); _WARN = nil warn("ZIP", "", " ", "ZAP") - assert(_WARN == "ZIP ZAP") + assert(_WARN == "ZIP ZAP"); _WARN = nil warn("@normal") end -- cgit v1.2.3-55-g6feb