diff options
| author | OpportunityLiu <[email protected]> | 2019-06-08 13:34:02 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-06-08 13:34:02 +0800 |
| commit | 1af88102687e493cab40e62b0a2b8b7d42417ae6 (patch) | |
| tree | 272a80b424e68024a9ecda138159864d9659fe5a /tests/test_utils/check.lua | |
| parent | e165a8e43abed4eb2eca64c3b5adefb57cd65600 (diff) | |
improve test lib
Diffstat (limited to 'tests/test_utils/check.lua')
| -rw-r--r-- | tests/test_utils/check.lua | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/test_utils/check.lua b/tests/test_utils/check.lua new file mode 100644 index 000000000..bc9ddb6b6 --- /dev/null +++ b/tests/test_utils/check.lua @@ -0,0 +1,38 @@ + + +function _tablelength(table) + local count = 0 + for _ in pairs(table) do count = count + 1 end + return count +end + +function _get_rep(value) + if type(value) == "nil" then return "(nil)" end + if type(value) == "string" then return "`" .. value .. "`" end + return tostring(value) +end + +function same(actual, expacted) + return actual == expacted, _get_rep(actual), _get_rep(expacted) +end + +function equal(actual, expacted) + local same, ap, ep = same(actual, expacted) + if same then return true, ap, ep end + if type(expacted) == "table" and type(actual) == "table" then + local al = _tablelength(actual) + local el = _tablelength(expacted) + if al ~= el then + return false, vformat("{...(%d elements)}", al), vformat("{...(%d elements)}", el) + end + for k, v in pairs(expacted) do + local av = actual[k] + local eq, app, epp = equal(av, v) + if not eq then + return false, vformat("{[%s] = %s, ...}", k, app), vformat("{[%s] = %s, ...}", k, epp) + end + end + return true, "{...}", "{...}" + end + return false, ap, ep +end
\ No newline at end of file |
