forked from Garoze/testLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibTest.lua
More file actions
74 lines (66 loc) · 2.48 KB
/
libTest.lua
File metadata and controls
74 lines (66 loc) · 2.48 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
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
function importAllTestFunctions()
return {
sendDebug = sendDebug,
functionHasReturn = functionHasReturn,
executeInstance = executeInstance,
createTests = createTests,
describe = describe,
expected = expected,
returnExecutionTime = returnExecutionTime,
}
end
function sendDebug(name, message, i)
return {
failed = function(name, i) print(debug.getinfo(i).short_src .. ':'..debug.getinfo(i).currentline .. ' ['..name..'] - Test has Failed!') end,
sucess = function(name, i) print(debug.getinfo(i).short_src .. ':'..debug.getinfo(i).currentline .. ' ['..name..'] - Test has Succeed!') end,
result = function(condition, name, i) if condition == true then sendDebug().sucess(name, i) else sendDebug().failed(name, i) end end,
}
end
function functionHasReturn(func)
if type(func) == 'function' then
local d = string.dump(func)
assert(d:sub(1,5) == "\27LuaQ")
d = d:match"^.-\30%z\128%z"
for pos = #d % 4 + 1, #d, 4 do
local b1, b2, b3, b4 = d:byte(pos, pos+3)
local dword = b1 + 256 * (b2 + 256 * (b3 + 256 * b4))
local OpCode, BC = dword % 64, math.floor(dword/16384)
local B, C = math.floor(BC/512), BC % 512
if OpCode == 30 and C == 0 and B ~= 1 then return true end
end
end
return false
end
function executeInstance(func, ...)
if type(func) == 'function' then
if functionHasReturn(func) then return func(...) end
end
return false
end
function createTests(name, array)
return {
__name = name,
__array = array,
runAllTests = function(self) for i, v in pairs(self.__array) do v:run() end end,
runTest = function(self, i) if self.__array[i] ~= nil then self.__array[i]:run() end end,
}
end
function describe(name, func)
return {
__name = name,
__func = func,
run = function(self) return self.__func() end,
getFunc = function(self) return self.__func end,
getName = function(self) return self.__name end,
expected = expected,
}
end
function expected(name, func, ...)
local f = functionHasReturn(func) and executeInstance(func, ...) or func
return {
toBe = function(n) sendDebug().result(f == n, name, 4) end,
toReturn = function(n) sendDebug().result(f == n, name, 4) end,
toReturnType = function(s) sendDebug().result(type(f) == s, name , 4) end,
}
end
return importAllTestFunctions