-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luatest.lua
executable file
·281 lines (248 loc) · 6.56 KB
/
luatest.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env lua
local io = io
running_in_testmode = true
disable_debug = true
local some_tests_failed = false
local format = string.format
local count_assertions = 0
local count_tests = 0
local count_errors = 0
local current_file = nil
local current_function = nil
local function passed()
count_assertions = count_assertions + 1
io.write(".")
end
local function failed()
count_assertions = count_assertions + 1
count_errors = count_errors + 1
io.write("F")
some_tests_failed = true
end
local function flattensequence(arg)
local ret = {}
if type(arg) ~= "table" then return arg end
for i = 1, #arg do
if type(arg[i]) == "table" then
local argi = arg[i]
if argi[".__type"] == "element" then
ret[#ret + 1] = argi
elseif argi[".__type"] == "attribute" then
ret[#ret + 1] = tostring(argi)
else
local tmp = flattensequence(arg[i])
if type(tmp) == "table" then
for j = 1, #tmp do
ret[#ret+1] = tmp[j]
end
else
ret[#ret+1] = tmp
end
end
else
ret[#ret+1] = arg[i]
end
end
if #ret == 1 then return ret[1] end
return ret
end
local function compare_tables(a, b) -- http://lua-users.org/lists/lua-l/2008-12/msg00442.html
local bi = nil
for k, v in pairs(a) do
bi = next(b, bi)
if type(v) == "table" and type(b[k]) == "table" then
setmetatable(v, {__tostring = array_to_string, __eq = compare_tables})
setmetatable(b[k], {__tostring = array_to_string, __eq = compare_tables})
end
if v ~= b[k] then
return false
end
if bi == nil then
return false
end
end
if next(b, bi) ~= nil then
return false
end
return true
end
--
function array_to_string(a)
local ret = {}
for _, v in ipairs(a) do
ret[#ret + 1] = string.format("'%s'", tostring(v))
end
return "{ " .. table.concat(ret, ", ") .. " }"
end
local function row()
local dt = debug.traceback()
lines = {}
for s in dt:gmatch("[^\r\n]+") do
table.insert(lines, s)
end
return lines[4]:gsub("^.*:(%d+):.*$", "%1")
end
local function isNaN( v ) return type( v ) == "number" and v ~= v end
-- assert that the result of a is NaN (not a number)
function assert_nan(a, msg)
a = flattensequence(a)
if isNaN(a) then passed() return end
if msg then
print(msg)
else
print(
string.format(
"'%s' expected to be NaN.\nFile: %s, function: %s:%d",
tostring(a),
current_file,
current_function,
row()
)
)
end
failed()
end
function assert_equal(a, b, msg)
a = flattensequence(a)
b = flattensequence(b)
if type(a) == "table" and type(b) == "table" then
setmetatable(a, {__tostring = array_to_string, __eq = compare_tables})
setmetatable(b, {__tostring = array_to_string, __eq = compare_tables})
end
if a == b then
passed()
else
if msg then
print(msg)
else
print(
string.format(
"'%s' expected to be equal '%s'.\nFile: %s:%d (%s)",
tostring(a),
tostring(b),
current_file,
row(),
current_function
)
)
end
failed()
end
end
function assert_not_nil(a, msg)
a = flattensequence(a)
if a == nil then
if msg then
print(msg)
else
print(format("'%s' expected to be non nil.\nFile: %s:%d", tostring(a), current_file, row()))
end
failed()
else
passed()
end
end
function assert_nil(a, msg)
a = flattensequence(a)
if a ~= nil then
if msg then
print(msg)
else
print(format("'%s' expected to be nil.\nFile: %s:%d", tostring(a), current_file, row()))
end
failed()
else
passed()
end
end
function assert_true(a, msg)
a = flattensequence(a)
if a ~= true then
if msg then
print(format("In %s: %s", current_function, msg))
else
print(format("'%s' expected to be true.\nFile: %s:%d", tostring(a), current_file, row()))
end
failed()
else
passed()
end
end
function assert_false(a, msg)
a = flattensequence(a)
if a ~= false then
if msg then
print(msg)
else
print(format("'%s' expected to be false.\nFile: %s:%d", tostring(a), current_file, row()))
end
failed()
else
passed()
end
end
function assert_fail(...)
local ok, msg = pcall(...)
if ok == false then
passed()
if msg:match("assertion failed!") then
return true
end
end
print(string.format("assertion did not fail.\nFile: %s, function: %s:%d", current_file, current_function, row()))
failed()
end
function assert_not_fail(...)
local ok, msg = pcall(...)
if ok == true then
passed()
return true
end
print(string.format("assertion failed.\nFile: %s, function: %s:%d", current_file, current_function, row()))
failed()
end
local mod
for _, modname in ipairs(arg) do
if string.sub(modname, -4, -1) == ".lua" then
modname = string.sub(modname, 1, -5)
end
current_file = modname
mod = require(modname)
local setup
local teardown
if mod.setup and type(mod.setup) == "function" then
setup = mod.setup
else
setup = function()
end
end
if mod.teardown and type(mod.teardown) == "function" then
teardown = mod.teardown
else
teardown = function()
end
end
local keys = {}
for k,_ in pairs(mod) do
keys[#keys + 1] = k
end
table.sort(keys)
for i, k in ipairs(keys) do
local j = mod[k]
if string.sub(k, 1, 5) == "test_" and type(j) == "function" then
count_tests = count_tests + 1
current_function = k
setup()
j()
teardown()
end
end
end
print(string.format("\n%d tests, %d assertions - with errors: %d", count_tests, count_assertions, count_errors))
print("Finished")
if some_tests_failed == true then
print("tests failed.")
os.exit(1)
end
print("All tests passed. Great!")
os.exit(0)