-
Notifications
You must be signed in to change notification settings - Fork 3
/
assert.lua
58 lines (44 loc) · 1.03 KB
/
assert.lua
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
48
49
50
51
52
53
54
55
56
57
58
local assert = {}
setmetatable(assert, assert)
function assert.__call(expr, msg)
if expr then
return
end
msg = msg or "assertion failed"
error(msg, 2)
end
function assert.equal(a, b, msg)
if a == b then
return
end
msg = msg or ("not equal: " .. tostring(a) .. ", " .. tostring(b))
error(msg, 2)
end
function assert.not_equal(a, b, msg)
if a ~= b then
return
end
msg = msg or ("equal: " .. tostring(a) .. ", " .. tostring(b))
error(msg, 2)
end
local function deep_equal(a, b)
if a == b then return true end
if type(a) ~= "table" or type(b) ~= "table" then return false end
-- Iterate over the longest table to ensure we check all keys
if #a < #b then
a, b = b, a
end
for k,v in pairs(a) do
local v2 = b[k]
if v2 == nil or not deep_equal(v, v2) then return false end
end
return true
end
function assert.deep_equal(a, b, msg)
if deep_equal(a, b) then
return
end
msg = msg or ("not deep equal: " .. tostring(a) .. ", " .. tostring(b))
error(msg, 2)
end
return assert