Skip to content

Commit

Permalink
Add methode fail to check failing code and pass error messages to o…
Browse files Browse the repository at this point in the history
…utput

- fail can be called with a function that should fail and a string which should be contained in the errormessage.
- Pass failed check reasons to output.
  • Loading branch information
HHHartmann committed Jan 20, 2020
1 parent a063d7d commit 733e00f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 21 deletions.
73 changes: 55 additions & 18 deletions lua_tests/gambiarra/gambiarra.lua
@@ -1,16 +1,23 @@
local function TERMINAL_HANDLER(e, test, msg)
local function TERMINAL_HANDLER(e, test, msg, errormsg)
if errormsg then
errormsg = ": "..errormsg
else
errormsg = ""
end
if e == 'pass' then
print(e.." "..test..': '..msg)
print(" "..e.." "..test..': '..msg)
elseif e == 'fail' then
print(e.." "..test..': '..msg)
print(" ==> "..e.." "..test..': '..msg..errormsg)
elseif e == 'except' then
print(e.." "..test..': '..msg)
print(" ==> "..e.." "..test..': '..msg..errormsg)
else
print(e.." "..test)
end
end

local function deepeq(a, b)
-- Different types: false
if type(a) ~= type(b) then return false end
if type(a) ~= type(b) then return {false, "type 1 is "..type(a)..", type 2 is "..type(b)} end
-- Functions
if type(a) == 'function' then
return string.dump(a) == string.dump(b)
Expand Down Expand Up @@ -54,6 +61,43 @@ local function spy(f)
return s
end

local function assertok(handler, name, cond, msg)
if not msg then
-- debug.getinfo() does not exist in NodeMCU
-- msg = debug.getinfo(2, 'S').short_src..":"..debug.getinfo(2, 'l').currentline
msg = debug.traceback()
msg = msg:match("\n[^\n]*\n[^\n]*\n[^\n]*\n\t*([^\n]*): in")
end
local errormsg

if type(cond) == "table" then
errormsg = cond[2]
cond = cond[1]
end
if cond then
handler('pass', name, msg, errormsg)
else
handler('fail', name, msg, errormsg)
end
end

local function fail(handler, name, func, expected, msg)
local status, err = pcall(func)
if status then
local messagePart = ""
if expected then
messagePart = " containing \"" .. expected .. "\""
end
handler('fail', name, msg, "Expected to fail with Error" .. messagePart)
return
end
if (expected and not string.find(err, expected)) then
handler('fail', name, msg, "expected errormessage \"" .. err .. "\" to contain \"" .. expected .. "\"")
return
end
handler('pass', name, msg)
end

local pendingtests = {}
local env = _G
local gambiarrahandler = TERMINAL_HANDLER
Expand All @@ -67,6 +111,7 @@ local function copyenv(dest, src)
dest.spy = src.spy
dest.ok = src.ok
dest.nok = src.nok
dest.fail = src.fail
end

return function(name, f, async)
Expand All @@ -89,23 +134,15 @@ return function(name, f, async)
end

local handler = gambiarrahandler
local function wrap(f, ...)
f(handler, name, ...)
end

env.eq = deepeq
env.spy = spy
env.ok = function (cond, msg)
if not msg then
-- debug.getinfo() does not exist in NodeMCU
-- msg = debug.getinfo(2, 'S').short_src..":"..debug.getinfo(2, 'l').currentline
msg = debug.traceback()
msg = msg:match("\n[^\n]*\n\t*([^\n]*): in")
end
if cond then
handler('pass', name, msg)
else
handler('fail', name, msg)
end
end
env.ok = function (cond, msg) wrap(assertok, cond, msg) end
env.nok = function(cond, msg) env.ok(not cond, msg) end
env.fail = function (func, expected, msg) wrap(fail, func, expected, msg) end

handler('begin', name);
local ok, err = pcall(f, restore)
Expand Down
24 changes: 21 additions & 3 deletions lua_tests/gambiarra/gambiarra_test.lua
Expand Up @@ -4,7 +4,7 @@ local actual = {}
local expected = {}

-- Set meta test handler
test(function(e, test, msg)
test(function(e, test, msg, errormsg)
if e == 'begin' then
currentTest = {
name = test,
Expand All @@ -15,10 +15,12 @@ test(function(e, test, msg)
table.insert(actual, currentTest)
elseif e == 'pass' then
table.insert(currentTest.pass, msg)
if errormsg then table.insert(currentTest.pass, errormsg) end
elseif e == 'fail' then
table.insert(currentTest.fail, msg)
if errormsg then table.insert(currentTest.fail, errormsg) end
elseif e == 'except' then
print('*** PANIC ***: ', test, msg)
print('*** PANIC ***: ', test, msg, errormsg)
end
end)

Expand Down Expand Up @@ -71,7 +73,7 @@ end, {'1~=2'}, {})
metatest('ok without a message', function()
ok(1 == 1)
ok(1 == 2)
end, {'gambiarra_test.lua:72'}, {'gambiarra_test.lua:73'})
end, {'gambiarra_test.lua:74'}, {'gambiarra_test.lua:75'})

--
-- Equality tests
Expand Down Expand Up @@ -125,6 +127,11 @@ metatest('eq functions', function()
ok(eq(function(x) return x end, function(x) return x+2 end), 'wrong code')
end, {'equal'}, {'wrong variable', 'wrong code'})

metatest('eq different types', function()
ok(eq({a=1,b=2}, "text"), 'object/string')
ok(eq(function(x) return x end, 12), 'function/int')
end, {}, {"object/string", "type 1 is table, type 2 is string", "function/int", "type 1 is function, type 2 is number"})

--
-- Spies
--
Expand Down Expand Up @@ -183,6 +190,17 @@ metatest('spy should return a value', function()
ok(g() == nil, 'default spy returns undefined')
end, {'spy returns a value', 'default spy returns undefined'}, {})

--
-- fail tests
--
metatest('fail should work', function()
fail(function() error("my error") end, "my error", "Failed with correct error")
fail(function() error("my error") end, "not my error", "Failed with incorrect error")
fail(function() end, "my error", "Failed without error")
end, {'Failed with correct error'}, {'Failed with incorrect error',
'expected errormessage "gambiarra_test.lua:199: my error" to contain "not my error"',
"Failed without error", 'Expected to fail with Error containing "my error"'})

--
-- Async tests
--
Expand Down

0 comments on commit 733e00f

Please sign in to comment.