-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTester.lua
More file actions
81 lines (70 loc) · 2.23 KB
/
Tester.lua
File metadata and controls
81 lines (70 loc) · 2.23 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
74
75
76
77
78
79
80
81
--[[
-- Tester keeps track and runs the tests.
-- It is a simple minimalistic test module I made specifically for this project.
--]]
local Tester = {
__TestList = {},
__BeforeList = {},
__AfterList = {},
}
--[[
-- Spawns a new thread to run a test on.
-- Takes the test.
-- Returns whether or not the test failed along with the error message.
-- If the test succeeds, it will come with the time it took to run the test.
--]]
local function RunTest(t)
if type(t) ~= "table" or type(t.Callback) ~= "function" then
error("Expected a valid test to run.")
end
for _, f in ipairs(Tester.__BeforeList) do f() end
local s, e = coroutine.resume(coroutine.create(function()
local start_time = os.time()
local s, e = pcall(t.Callback)
if not s then return e end
return os.time() - start_time
end))
for _, f in ipairs(Tester.__AfterList) do f() end
return s, e
end
--[[ Runs the setup tests ]]--
function Tester.Run()
for n, test in pairs(Tester.__TestList) do
local s, e = RunTest(test)
local Char = "S"
if not s then Char = "F" end
print("["..Char.."]: "..n.." | "..tostring(e))
end
end
--[[ Clears all the data from the Tester ]]--
function Tester.Reset()
Tester.__TestList = {}
Tester.__BeforeList = {}
Tester.__AfterList = {}
end
--[[
-- Describes a test.
-- Takes in a name and a callback.
--]]
function Tester.Test(name, callback)
if Tester.__TestList[name] ~= nil then error("Duplicate test name found. "..name) end
local Test = { Callback = callback }
Tester.__TestList[name] = Test
end
--[[
-- Runs the callback everytime before calling the test.
-- Warning, using this could result in tests stepping on eachother and having race conditions.
--]]
function Tester.Before(callback)
if type(callback) ~= "function" then error("Expected a function to run before test. Instead found: "..type(callback)) end
table.insert(Tester.__BeforeList, callback)
end
--[[
-- Runs the callback everytime after calling the test.
-- Warning, using this could result in tests stepping on eachother and having race conditions.
--]]
function Tester.After(callback)
if type(callback) ~= "function" then error("Expected a function to run after test. Instead found: "..type(callback)) end
table.insert(Tester.__AfterList, callback)
end
return Tester