-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathcompare.lua
More file actions
47 lines (40 loc) · 1.16 KB
/
compare.lua
File metadata and controls
47 lines (40 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
local export = {}
local EXISTS = {}
export.EXISTS = EXISTS
local divider = "\n=================\n"
local header1 = ("%stest failed. got:%s"):format(divider, divider)
local header2 = ("%sexpected:%s"):format(divider, divider)
local template = header1 .. "%s" .. header2 .. "%s" .. divider
function export.eq(a, b)
if a == EXISTS and b ~= nil then
return true
end
if b == EXISTS and a ~= nil then
return true
end
local tp1, tp2 = type(a), type(b)
if tp1 ~= tp2 then
return false, template:format("type is: " .. tp1, "type is: " .. tp2)
end
if tp1 == 'table' then
local mark = {}
for k in pairs(a) do
local ok, err = export.eq(a[k], b[k])
if not ok then
return false, string.format(".%s%s", k, err)
end
mark[k] = true
end
for k in pairs(b) do
if not mark[k] then
return false, string.format(".%s: unexpected key in result", k)
end
end
return true
end
if a == b then
return true
end
return false, template:format(tostring(a), tostring(b))
end
return export