-
Notifications
You must be signed in to change notification settings - Fork 1
/
least.lua
250 lines (217 loc) · 6.62 KB
/
least.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
local least = {}
local active_suites = {}
local esc = string.char(27)
local color = {
reset = esc.."[0m",
bold = esc.."[1m",
dim = esc.."[2m",
under = esc.."[4m",
blink = esc.."[5m",
rev = esc.."[7m",
hiddn = esc.."[8m",
black = esc.."[30m",
r = esc.."[31m",
g = esc.."[32m",
y = esc.."[33m",
b = esc.."[34m",
m = esc.."[35m",
c = esc.."[36m",
w = esc.."[37m"
}
local bull = "•"
local fbull = color.r..color.bold..bull..color.reset
local pbull = color.g..bull..color.reset
if not ((package.config:sub(1,1)=='\\' and os.getenv("ANSICON")) or package.config:sub(1,1)=='/') then --we lack console color support
for k,v in pairs(color) do
color[k] = ""
end
end
local active_test
local function getstackplace()
local stack = debug.traceback()
local res
for line in stack:gmatch("[^\r\n]+") do
for cap in line:gmatch(".*.lua:%d+: in function <.*.lua:%d+>") do
res = line
end
end
return res
end
least.quiet = false
least.assert = {}
least.assert.isNil = function(statement)
local result = {}
result.line = getstackplace()
if statement==nil then
table.insert(active_test.sucesses,result)
else
table.insert(active_test.fails,result)
end
end
least.assert.falsey = function(statement)
local result = {}
result.line = getstackplace()
if not statement then
table.insert(active_test.sucesses,result)
else
table.insert(active_test.fails,result)
end
end
least.assert.truthy = function(statement)
local result = {}
result.line = getstackplace()
if statement then
table.insert(active_test.sucesses,result)
else
table.insert(active_test.fails,result)
end
return result
end
local __assert = {
__call = function(self, statement)
return least.assert.truthy(statement)
end
}
setmetatable(least.assert,__assert)
least.test = {}
least.test.should = {}
least.test.should.pass = function(desc, func)
self = {}
self.desc = desc
self.sucesses = {}
self.fails = {}
active_test = self
local good, err = pcall(func)
if not good then
self.error = err
end
local parent = active_suites[#active_suites]
if parent and parent ~= self then
if (not self.fails[1]) and (not self.error) then
table.insert(parent.sucesses,self)
active_suites[1].dots = active_suites[1].dots .. pbull
active_suites[1].sucesses[-1] = active_suites[1].sucesses[-1] + 1
else
table.insert(parent.fails,self)
active_suites[1].dots = active_suites[1].dots .. fbull
active_suites[1].fails[-1] = active_suites[1].fails[-1] + 1
end
end
active_test = nil
end
least.test.should.fail = function(desc, func)
self = {}
self.desc = desc
self.sucesses = {}
self.fails = {}
active_test = self
local good, err = pcall(func)
if not good then
self.error = err
end
local parent = active_suites[#active_suites]
if parent and parent ~= self then
if (self.fails[1]) and (not self.error) then
table.insert(parent.sucesses,self)
active_suites[1].dots = active_suites[1].dots .. pbull
active_suites[1].sucesses[-1] = active_suites[1].sucesses[-1] + 1
else
table.insert(parent.fails,self)
active_suites[1].dots = active_suites[1].dots .. fbull
active_suites[1].fails[-1] = active_suites[1].fails[-1] + 1
end
end
active_test = nil
end
local __test = {
__call = function(self, desc, func)
return least.test.should.pass(desc, func)
end
}
setmetatable(least.test.should,__test)
setmetatable(least.test,__test)
local function hook(suite) --push!
table.insert(active_suites, suite)
end
local function unhook(suite) --pop!
table.remove(active_suites)
end
least.suite = function(desc, func)
local self = {}
self.desc = desc
self.sucesses = {}
self.sucesses[-1] = 0
self.fails = {}
self.fails[-1] = 0
self.dots = ""
hook(self)
if setfenv then --Lua 5.1, yay~
local fake = {}
setmetatable(fake,{
__index = _G
})
for k,v in pairs(least) do
fake[k] = v
end
setfenv(func,fake)
func()
else --We need the topmost level to have _ENV as a named argument.. bleh
local fake = {}
setmetatable(fake,{
__index = _G
})
for k,v in pairs(least) do
fake[k] = v
end
func(fake)
end
unhook(self)
if self.fails[1] then
print(color.r.."Suite Failed"..color.reset.." - "..self.desc.."\n"..color.y.."Failed Test(s):"..color.reset)
for k,v in ipairs(self.fails) do
local out = "\t"..color.b..v.desc.."\n\t\t"..color.y.."Failed Asserts:"..color.reset
for k,test in ipairs(v.fails) do
if test.line then
if out then
print(out)
out = nil
end
print("\t\t"..test.line)
else
print(color.r.."\t\tSub-suite failure, see above: "..color.reset..color.b..v.desc:sub(1,20).."..."..color.reset)
break;
end
end
if v.error then
print("\t"..color.r..color.bold.."Errors: "..color.reset..v.error)
end
end
end
local parent = active_suites[#active_suites]
if (not parent) or parent == self then
local out = color.c.."Top-level completion:\n\t"..color.reset
out = self.dots .. (" ("..(self.sucesses[-1]).."/"..(self.sucesses[-1]+self.fails[-1])..")")
if not least.quiet then
print(out)
end
end
for k,v in ipairs(active_suites) do
if v~=self then
if self.fails[1] then
table.insert(v.fails, self)
else
table.insert(v.sucesses, self)
end
end
end
return self
end
least.describe = least.suite
least.it = least.test
local __suite = {
__call = function(self, desc, func)
return least.suite(desc, func)
end
}
setmetatable(least,__suite)
return least