From a1d8eb27431c02c4529be1efd92143ad65434f3a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Aug 2019 13:44:36 -0300 Subject: Added control messages to warnings Added the concept of control messages to the warning system, plus the implementation of the controls "@on"/"@off" to turn warnings on/off. Moreover, the warning system in the test library adds some other controls to ease the test of warnings. --- testes/all.lua | 4 ++++ testes/api.lua | 2 ++ testes/gc.lua | 8 +++++++- testes/main.lua | 29 +++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) (limited to 'testes') diff --git a/testes/all.lua b/testes/all.lua index bf27f106..5d698d4b 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -209,6 +209,10 @@ if #msgs > 0 then warn("#tests not performed:\n ", m, "\n") end +warn("@off") +warn("******** THIS WARNING SHOULD NOT APPEAR **********") +warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********") +warn("@on") print("(there should be two warnings now)") warn("#This is ", "an expected", " warning") warn("#This is", " another one") diff --git a/testes/api.lua b/testes/api.lua index f6915c3e..4f9d6717 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -977,6 +977,7 @@ assert(t[7] == nil) ------------------------------------------------------------------------- do -- testing errors during GC + warn("@off") collectgarbage("stop") local a = {} for i=1,20 do @@ -994,6 +995,7 @@ do -- testing errors during GC collectgarbage() assert(A == 10) -- number of normal collections collectgarbage("restart") + warn("@on") end ------------------------------------------------------------------------- -- test for userdata vals diff --git a/testes/gc.lua b/testes/gc.lua index 9ea054c1..6bdc98ca 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -369,6 +369,7 @@ if T then s[n] = i end + warn("@store") collectgarbage() assert(string.find(_WARN, "error in __gc metamethod")) assert(string.match(_WARN, "@(.-)@") == "expected") @@ -383,6 +384,7 @@ if T then for i = 1, 10 do assert(s[i]) end getmetatable(u).__gc = nil + warn("@normal") end print '+' @@ -475,9 +477,11 @@ end -- errors during collection if T then + warn("@store") u = setmetatable({}, {__gc = function () error "@expected error" end}) u = nil collectgarbage() + warn("@normal") end @@ -645,7 +649,7 @@ end -- create several objects to raise errors when collected while closing state if T then - local error, assert, find = error, assert, string.find + local error, assert, find, warn = error, assert, string.find, warn local n = 0 local lastmsg local mt = {__gc = function (o) @@ -659,7 +663,9 @@ if T then else assert(lastmsg == _WARN) -- subsequent error messages are equal end + warn("@store") error"@expected warning" + warn("@normal") end} for i = 10, 1, -1 do -- create object and preserve it until the end diff --git a/testes/main.lua b/testes/main.lua index b224b54e..0ef4822d 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -221,6 +221,28 @@ assert(string.find(getoutput(), "error calling 'print'")) RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) checkout("lua_debug> 1000lua_debug> ") +-- test warnings +RUN('echo "io.stderr:write(1); warn[[XXX]]" | lua -q 2> %s', out) +checkout("1") + +prepfile[[ +warn("@allow") -- unknown control, ignored +warn("@off", "XXX", "@off") -- these are not control messages +warn("@off") -- this one is +warn("@on", "YYY", "@on") -- not control, but warn is off +warn("@off") -- keep it off +warn("@on") -- restart warnings +warn("", "@on") -- again, no control, real warning +warn("@on") -- keep it "started" +warn("Z", "Z", "Z") -- common warning +]] +RUN('lua %s 2> %s', prog, out) +checkout[[ +Lua warning: @offXXX@off +Lua warning: @on +Lua warning: ZZZ +]] + -- test many arguments prepfile[[print(({...})[30])]] RUN('lua %s %s > %s', prog, string.rep(" a", 30), out) @@ -355,8 +377,15 @@ if T then -- test library? NoRun("not enough memory", "env MEMLIMIT=100 lua") -- testing 'warn' + warn("@store") warn("@123", "456", "789") assert(_WARN == "@123456789") + + warn("zip", "", " ", "zap") + assert(_WARN == "zip zap") + warn("ZIP", "", " ", "ZAP") + assert(_WARN == "ZIP ZAP") + warn("@normal") end do -- cgit v1.2.3-55-g6feb