-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
218 lines (195 loc) · 4.16 KB
/
Copy pathinit.lua
File metadata and controls
218 lines (195 loc) · 4.16 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
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
---@class Test
---@field func function
---@field cleanup function?
local M = {}
---@type table<string, Test>
M.tests = {}
M.assert_eq = function(actual, expected, msg)
if actual ~= expected then
error(
string.format(
"%s\n expected %s\n got:\t%s",
msg or "assertion failed",
tostring(expected),
tostring(actual)
),
2
)
end
end
M.assert_neq = function(actual, expected, msg)
if actual == expected then
error(
string.format(
"%s\nexpected: not: %s\ngot:\t%s",
msg or "assertion failed",
tostring(expected),
tostring(actual)
),
2
)
end
end
M.assert_is_true = function(val, msg)
if val ~= true then
error(
string.format(
"%s\nExpected: true (boolean)\nGot:\t%s",
msg or "assertion failed",
tostring(val)
),
2
)
end
end
M.assert_is_false = function(val, msg)
if val ~= false then
error(
string.format(
"%s\nExpected: false (boolean)\nGot:\t%s",
msg or "assertion failed",
tostring(val)
),
2
)
end
end
M.assert_false = function(val, msg)
if val then
error(
string.format(
"%s\nExpected: falsy\nGot:\t%s",
msg or "assertion failed",
tostring(val)
),
2
)
end
end
M.assert_true = function(val, msg)
if not val then
error(
string.format(
"%s\nExpected: truthy\nGot:\t%s",
msg or "assertion failed",
tostring(val)
),
2
)
end
end
local dcmp
dcmp = function(a, b)
if a == b then return true end
if type(a) ~= "table" or type(b) ~= "table" then return false end
for k, v in pairs(a) do
if not dcmp(v, b[k]) then return false end
end
for k in pairs(b) do
if a[k] == nil then return false end
end
return true
end
local function inspect(t, indent)
indent = indent or 0
local pad = string.rep("\t", indent)
local innerPad = string.rep("\t", indent + 1)
if type(t) ~= "table" then return tostring(t) end
local result = "{\n"
for k, v in pairs(t) do
local key = type(k) == "string" and string.format("[%q]", k)
or "[" .. tostring(k) .. "]"
result = result
.. innerPad
.. key
.. " = "
.. inspect(v, indent + 1)
.. ",\n"
end
return result .. pad .. "}"
end
M.assert_dpeq = function(actual, expected, msg)
if not dcmp(actual, expected) then
error(
string.format(
"%s\nDeep Equality check failed.\nExpected:\n%s\nGot:\n%s",
msg or "Assertion Failed",
inspect(expected),
inspect(actual)
),
2
)
end
end
---@param name string # The name of the test as a string
---@param test Test
M.register = function(name, test)
assert(
type(test.func) == "function",
string.format('test "%s" missing func', name)
)
M.tests[name] = test
end
M.add_test = M.register
M.test_all = function()
local passed = 0
local total = 0
local running = true
for t_name, test in pairs(M.tests) do
total = total + 1
local success, err = pcall(test.func)
-- prevent state leakage from invalidating entire test set by
-- stopping execution of a failed cleanup call
-- (ugly, but should work)
if test.cleanup then
local cs, cerr = pcall(test.cleanup)
if not cs then
print(
string.format(
"\n\nTest cleanup failed:\n%s\nError\t:%s",
t_name,
cerr
)
)
running = false
end
end
if success then
passed = passed + 1
print(string.format("[PASS] %s", t_name))
else
print(string.format("[FAIL] %s: %s", t_name, err))
end
if not running then
print("Test Execution halted due to cleanup failure.")
break
end
end
print(string.format("\nResults: %d/%d passed", passed, total))
end
---@param name string string name of the test to run
M.run_test = function(name)
if not M.tests[name] then
error('there is no test named "' .. name .. '"', 2)
return
end
local test = M.tests[name]
local success, err = pcall(test.func)
-- prevent state leakage from invalidating entire test set by
-- stopping execution of a failed cleanup call
-- (ugly, but should work)
if test.cleanup then
local cs, cerr = pcall(test.cleanup)
assert(cs, cerr)
end
if success then
print(string.format("[PASS] %s", name))
else
print(string.format("[FAIL] %s: %s", name, err))
end
end
-- should be on user side
-- if arg and arg[1] == "--all" then
-- M.test_all()
-- end
return M