51 lines
1.5 KiB
Lua
51 lines
1.5 KiB
Lua
-- functions to validate check better.
|
|
|
|
-- All methods have an optional level parameter, which represent which
|
|
-- level of stack to report the error (1 == the caller). If you create
|
|
-- a check method using those, remember to pass level+1 to it, so that
|
|
-- the error is reported on the right function.
|
|
|
|
-- raise an error if cond is false, including where the error happen.
|
|
function check(cond, msg, level)
|
|
msg = msg or "Check failed"
|
|
level = level or 1
|
|
if not cond then
|
|
error(msg, level + 1)
|
|
end
|
|
end
|
|
|
|
-- test utility to assert that two tables are equals
|
|
function check_tables(t1, t2, level)
|
|
level = level or 1
|
|
check(type(t1) == "table", "t1 is not a table", level + 1)
|
|
check(type(t2) == "table", "t2 is not a table", level + 1)
|
|
check(table.getn(t1) == table.getn(t2), "Not the same size", level + 1)
|
|
for k,v in pairs(t1) do
|
|
if type(v) == "table" then
|
|
compare_tables(v, t2[k], level + 1)
|
|
else
|
|
check(v == t2[k], "different value", level + 1)
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
IGNORED = {}
|
|
|
|
-- compare two tables but ignored field from expected marked
|
|
-- as IGNORED.
|
|
function check_partial_tables(expected, result, level)
|
|
level = level or 1
|
|
for k, v in pairs(expected) do
|
|
vres = result[k]
|
|
check(vres, "invalid key", level + 1)
|
|
if v ~= IGNORED then
|
|
if type(v) == "table" then
|
|
check_partial_tables(v, vres, level + 1)
|
|
else
|
|
check(v == vres, "different values", level + 1)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|