-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.lua
186 lines (158 loc) · 4.27 KB
/
api.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
-- (c) 2024 Jacek Olszak
-- This code is licensed under MIT license (see LICENSE for details)
-- table_len returns number of elements in a table t.
-- Keys with nil value are not counted
local function table_len(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
local function equal(expected, actual, visited_values)
if expected == nil or actual == nil then
return expected == actual
end
if visited_values == nil then
visited_values = {}
visited_values[expected] = true
elseif visited_values[expected] == true then
-- do not compare already visited values
-- (avoid stack overflow for cycle references)
return true
end
local function tables_equal(expected, actual)
if table_len(expected) != table_len(actual) then
return false
end
for k, v in pairs(expected) do
if not equal(v, actual[k], visited_values) then
return false
end
end
return true
end
local function userdata_type(u)
_, _, t = u:attribs()
return t
end
if type(expected) != type(actual) then
return false
end
if type(expected) == "userdata" then
if userdata_type(expected) != userdata_type(actual) then
return false
end
if expected:width() != actual:width() then
return false
end
if expected:height() != actual:height() then
return false
end
for i = 0, #expected do
if expected[i] != actual[i] then
return false
end
end
return true
end
if type(expected) == "table" then
return tables_equal(expected, actual)
end
return expected == actual
end
local function msg_or(msg, default)
if msg == nil then
return default
end
return tostring(msg)
end
---Asserts that expected and actual are equal. Values must have the same type.
---
---For strings, numbers and booleans '==' operator is used.
---
---For tables, all keys and values are compared deeply.
---If you want to check if two variables reference to the same table in memory
---please use assert(a==b) instead.
---Tables could have cycles.
---
---For userdata, all data is compared and userdata must be of the same type,
---width and height.
---
---@param expected any
---@param actual any
---@param msg? any message which will be presented in the unitron ui, instead of standard message
function assert_eq(expected, actual, msg)
test_helper()
if not equal(expected, actual) then
test_fail {
msg = msg_or(msg, "Args not equal"),
expect = expected,
actual = actual
}
end
end
---@param not_expected any
---@param actual any
---@param msg? any message which will be presented in the unitron ui, instead of standard message
function assert_not_eq(not_expected, actual, msg)
test_helper()
if equal(not_expected, actual) then
test_fail {
msg = msg_or(msg, "Args are equal"),
not_expect = not_expected,
actual = actual,
}
end
end
---@param expected number
---@param actual number
---@param delta number
---@param msg? any message which will be presented in the unitron ui, instead of standard message
function assert_close(expected, actual, delta, msg)
test_helper()
local invalid_args = expected == nil or actual == nil or delta == nil
if invalid_args or abs(expected - actual) > delta then
test_fail {
msg = msg_or(msg, "Args not close"),
expect = expected,
actual = actual,
delta = delta,
}
end
end
---@param not_expected number
---@param actual number
---@param delta number
---@param msg? any message which will be presented in the unitron ui, instead of standard message
function assert_not_close(not_expected, actual, delta, msg)
test_helper()
local invalid_args = not_expected == nil or actual == nil or delta == nil
if invalid_args or abs(not_expected - actual) <= delta then
test_fail {
msg = msg_or(msg, "Args too close"),
not_expect = not_expected,
actual = actual,
delta = delta,
}
end
end
---@param actual any
---@param msg? any message which will be presented in the unitron ui, instead of standard message
function assert_not_nil(actual, msg)
test_helper()
if actual == nil then
test_fail {
msg = msg_or(msg, "Arg is nil")
}
end
end
---@param actual any
---@param msg? any message which will be presented in the unitron ui, instead of standard message
function assert_nil(actual, msg)
test_helper()
if actual != nil then
test_fail {
msg = msg_or(msg, "Arg is not nil"),
actual = actual
}
end
end