-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.lua
243 lines (216 loc) · 5.9 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
-- (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 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 get_caller()
local traceback = debug.traceback("", 3)
local loc = split(traceback, "\n")[3]
loc = string.gsub(loc, "(%d+):.*", "%1") -- drop message
loc = string.gsub(loc, "\t", "") -- drop tabulator
return loc
end
local function serialize_arg(v)
if v == nil then
return
end
local serialized = pod(v)
if serialized == nil then
if type(v) == "table" then
return "[cyclic table]"
else
return "[not serializable]"
end
end
return serialized
end
local function serialize_message(msg)
if msg == nil then
return nil
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 compare if two tables points to the same address in memory
---please use assert_same 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.
function assert_eq(expected, actual, msg)
if not equal(expected, actual) then
local err = {
assert = "eq",
expected = serialize_arg(expected),
actual = serialize_arg(actual),
msg = serialize_message(msg),
file = get_caller(),
}
error(err)
end
end
---@param not_expected any
---@param actual any
---@param msg? any message which will be presented in the unitron ui.
function assert_not_eq(not_expected, actual, msg)
if equal(not_expected, actual) then
local err = {
assert = "not_eq",
actual = serialize_arg(actual),
msg = serialize_message(msg),
file = get_caller(),
}
error(err)
end
end
---@param expected any
---@param actual any
---@param msg? any message which will be presented in the unitron ui.
function assert_same(expected, actual, msg)
if expected != actual then
local err = {
assert = "same",
-- tostring() is more useful than serialize because pointers have
-- more value than values here:
expected = tostring(expected),
actual = tostring(actual),
msg = serialize_message(msg),
file = get_caller(),
}
error(err)
end
end
---@param not_expected any
---@param actual any
---@param msg? any message which will be presented in the unitron ui.
function assert_not_same(not_expected, actual, msg)
if not_expected == actual then
local err = {
assert = "not_same",
actual = tostring(actual),
msg = serialize_message(msg),
file = get_caller(),
}
error(err)
end
end
---@param expected number
---@param actual number
---@param delta number
---@param msg? any message which will be presented in the unitron ui.
function assert_close(expected, actual, delta, msg)
if abs(expected - actual) > delta then
local err = {
assert = "close",
expected = tostring(expected), -- TODO Picotron has a bug that small numbers are not properly serialized
actual = tostring(actual), -- TODO Picotron has a bug that small numbers are not properly serialized
delta = tostring(delta), -- TODO Picotron has a bug that small numbers are not properly serialized
msg = serialize_message(msg),
file = get_caller(),
}
error(err)
end
end
---@param not_expected number
---@param actual number
---@param delta number
---@param msg? any message which will be presented in the unitron ui.
function assert_not_close(not_expected, actual, delta, msg)
if abs(not_expected - actual) <= delta then
local err = {
assert = "not_close",
not_expected = tostring(not_expected), -- TODO Picotron has a bug that small numbers are not properly serialized
actual = tostring(actual), -- TODO Picotron has a bug that small numbers are not properly serialized
delta = tostring(delta), -- TODO Picotron has a bug that small numbers are not properly serialized
msg = serialize_message(msg),
file = get_caller(),
}
error(err)
end
end
---@param actual any
---@param msg? any message which will be presented in the unitron ui.
function assert_not_nil(actual, msg)
if actual == nil then
local err = {
assert = "not_nil",
msg = serialize_message(msg),
file = get_caller(),
}
error(err)
end
end
---@param actual any
---@param msg? any message which will be presented in the unitron ui.
function assert_nil(actual, msg)
if actual != nil then
local err = {
assert = "nil",
actual = tostring(actual),
msg = serialize_message(msg),
file = get_caller(),
}
error(err)
end
end