-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLibBusted-2.0.lua
481 lines (383 loc) · 13.6 KB
/
LibBusted-2.0.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
local MAJOR,MINOR = "Lib:Busted-2.0", 6
-- Get a reference to the package information if any
local APkg = Apollo.GetPackage(MAJOR)
-- If there was an older version loaded we need to see if this is newer
if APkg and (APkg.nVersion or 0) >= MINOR then
return -- no upgrade needed
end
-- Set a reference to the actual package or create an empty table
local LibBusted = APkg and APkg.tPackage or {}
local s, busted, pretty -- Olivine-Say, Olivine-Busted, Penlight-Pretty
local tags, excludeTags
local BustedTests = setmetatable({}, { __index = function(tbl, key) tbl[key] = {} return tbl[key] end })
local tLibError = Apollo.GetPackage("Gemini:LibError-1.0")
local fnErrorHandler = tLibError and tLibError.tPackage and tLibError.tPackage.Error or Print
-- first load the submodules
local function loadModule(dir, folder, file)
local func = assert(loadfile(dir..folder.."\\"..file..".lua"))
if func then
return xpcall(func, fnErrorHandler)
end
end
-- This gets the current directory of this file, so it also works when embedded
local strsub, strgsub, debug = string.sub, string.gsub, debug
local dir = string.sub(string.gsub(debug.getinfo(1).source, "^(.+[\\/])[^\\/]+$", "%1"), 2, -1)
loadModule(dir, "Say", "Say-1.0")
loadModule(dir, "Luassert", "util")
loadModule(dir, "Luassert", "spy")
loadModule(dir, "Luassert", "stub")
loadModule(dir, "Luassert", "mock")
loadModule(dir, "Mediator", "mediator")
loadModule(dir, "Penlight", "operator")
loadModule(dir, "Penlight", "utils")
loadModule(dir, "Penlight", "types")
loadModule(dir, "Penlight", "tablex")
loadModule(dir, "Penlight", "pretty")
loadModule(dir, "Busted", "core")
-------------------------------------------------------------------------------
--- Olivine-Labs Busted - Output Handler - Plain Terminal
-------------------------------------------------------------------------------
local function outputPlainTerminal(options, busted)
-- options.language, options.deferPrint, options.suppressPending, options.verbose
local handler = { }
local tests = 0
local successes = 0
local failures = 0
local pendings = 0
local successString = '+'
local failureString = '-'
local pendingString = '.'
local failureInfos = { }
local pendingInfos = { }
local startTime, endTime
local getFullName = function(context)
local parent = context.parent
local names = { (context.name or context.descriptor) }
while parent and (parent.name or parent.descriptor) and
parent.descriptor ~= 'file' do
table.insert(names, 1, parent.name or parent.descriptor)
parent = busted.context.parent(parent)
end
return table.concat(names, ' ')
end
local pendingDescription = function(pending)
local name = getFullName(pending)
local string = '\n\n' .. s('output.pending') .. ': ' ..
pending.elementTrace.short_src .. ' @ ' ..
pending.elementTrace.currentline ..
'\n' .. name
return string
end
local failureDescription = function(failure)
local string = s('output.failure') .. ': '
if failure.elementTrace then
string = string .. failure.elementTrace.short_src .. ' @ ' ..
failure.elementTrace.currentline
else
string = string .. failure.debug
end
string = string .. '\n' .. getFullName(failure) .. '\n\n'
if type(failure.message) == 'string' then
string = string .. failure.message
elseif failure.message == nil then
string = string .. 'Nil error'
else
string = string .. pretty.write(failure.message)
end
if options.verbose then
string = string .. failure.debug.traceback
end
return string
end
local statusString = function(successes, failures, pendings, ms)
local successString = s('output.success_plural')
local failureString = s('output.failure_plural')
local pendingString = s('output.pending_plural')
if successes == 0 then
successString = s('output.success_zero')
elseif successes == 1 then
successString = s('output.success_single')
end
if failures == 0 then
failureString = s('output.failure_zero')
elseif failures == 1 then
failureString = s('output.failure_single')
end
if pendings == 0 then
pendingString = s('output.pending_zero')
elseif pendings == 1 then
pendingString = s('output.pending_single')
end
local formattedTime = ('%.6f'):format(ms):gsub('([0-9])0+$', '%1')
return successes .. ' ' .. successString .. ' / ' ..
failures .. ' ' .. failureString .. ' / ' ..
pendings .. ' ' .. pendingString .. ' : ' ..
formattedTime .. ' ' .. s('output.seconds')
end
handler.testStart = function(name, parent)
tests = tests + 1
return nil, true
end
handler.testEnd = function(element, parent, status, debug)
local string = successString
if status == 'success' then
successes = successes + 1
elseif status == 'pending' then
if not options.suppressPending then
Print(pendingString)
pendings = pendings + 1
table.insert(pendingInfos, {
name = element.name,
elementTrace = element.trace,
parent = parent
})
end
elseif status == 'failure' then
string = failureString
failures = failures + 1
end
if not options.deferPrint then
Print(string)
--io.write(string)
end
return nil, true
end
handler.pending = function(element, parent, message, debug)
return nil, true
end
handler.fileStart = function(name, parent)
return nil, true
end
handler.fileEnd = function(name, parent)
return nil, true
end
handler.suiteStart = function(name, parent)
startTime = os.clock()
return nil, true
end
handler.suiteEnd = function(name, parent)
endTime = os.clock()
-- print an extra newline of defer print
if not options.deferPrint then
Print('')
end
Print(statusString(successes, failures, pendings, endTime - startTime, {}))
if #pendingInfos > 0 then print('') end
for i, pending in pairs(pendingInfos) do
Print(pendingDescription(pending))
end
if #failureInfos > 0 then print('') end
for i, err in pairs(failureInfos) do
Print(failureDescription(err))
end
tests, successes, failures, pendings = 0, 0, 0, 0
failureInfos, pendingInfos = {}, {}
return nil, true
end
handler.error = function(element, parent, message, debug)
table.insert(failureInfos, {
elementTrace = element.trace,
name = element.name,
descriptor = element.descriptor,
message = message,
debug = debug,
parent = parent
})
return nil, true
end
return handler
end
-------------------------------------------------------------------------------
--- Olivine-Labs Busted - Sound Output
-------------------------------------------------------------------------------
local function outputSound(options, busted)
local handler = {}
local isFailure = false
handler.testEnd = function(element, parent, status)
if status == 'failure' then
isFailure = true
end
return nil, true
end
handler.suiteEnd = function(name, parent)
local messages, soundNum
if isFailure then
messages = busted.failure_messages
soundNum = Sound.PlayUIChallengeQuestFailed
else
messages = busted.success_messages
soundNum = Sound.PlayUIChallengeQuestComplete
end
Sound.Play(soundNum)
Print(messages[math.random(1, #messages)])
isFailure = false
return nil, true
end
handler.error = function(element, parent, message, debug)
isFailure = true
return nil, true
end
return handler
end
-------------------------------------------------------------------------------
--- Olivine-Labs Busted - Lua Test File
-------------------------------------------------------------------------------
local LuaTests
do
local ret = {}
local getTrace = function(filename, info)
local index = info.traceback:find('\n%s*%[C]')
info.traceback = info.traceback:sub(1, index)
return info, false
end
ret.load = function(busted, filename)
local file
local success, err = pcall(function()
file, err = loadfile(filename)
if not file then
busted.publish({ 'error', 'file' }, filename, nil, nil, err)
end
end)
if not success then
busted.publish({ 'error', 'file' }, filename, nil, nil, err)
end
return file, getTrace
end
LuaTests = ret
end
-------------------------------------------------------------------------------
--- Olivine-Labs Busted - v2
-------------------------------------------------------------------------------
local checkTag = function(name, tag, modifier)
local found = name:find('#' .. tag)
return (modifier == (found ~= nil))
end
local checkTags = function(name)
if #tags == 0 and #excludeTags == 0 then return nil, true end
for i, tag in pairs(tags) do
if not checkTag(name, tag, true) then
return nil, false
end
end
for i, tag in pairs(excludeTags) do
if not checkTag(name, tag, false) then
return nil, false
end
end
return nil, true
end
local function ExecuteTests()
local register = LibBusted.Register
LibBusted.Register = function() end
busted.publish({ 'suite', 'start' })
busted.execute()
busted.publish({ 'suite', 'end' })
busted.context.reset()
LibBusted.Register = register
end
local function RunTest(self, strTest, config)
local strFile = BustedTests[self][strTest]
if not strFile then return end
local testFile, getTrace = LuaTests.load(busted, strFile)
if testFile then
tags = config and config.tags or {}
excludeTags = config and config.excludeTags or {}
local file = setmetatable({
getTrace = getTrace
}, {
__call = testFile
})
busted.getEnvironment().set('self', self)
busted.executors.file(strTest, file)
ExecuteTests()
end
end
local function RunTests(self, config)
local bLoadedTests
tags = config and config.tags or {}
excludeTags = config and config.excludeTags or {}
for k,v in pairs(BustedTests[self]) do
local testFile, getTrace = LuaTests.load(busted, v)
if testFile then
bLoadedTests = true
local file = setmetatable({
getTrace = getTrace
}, {
__call = testFile
})
busted.executors.file(k, file)
end
end
if bLoadedTests then
busted.getEnvironment().set('self', self)
ExecuteTests()
end
end
local function IterateTests(self)
return pairs(BustedTests[self])
end
local tMixins = {
RunTest = RunTest,
RunTests = RunTests,
IterateTests = IterateTests,
}
function LibBusted:Register(oAddon)
local bFoundTests
local strAssetFolder = Apollo.GetAssetFolder()
local tocXML = XmlDoc.CreateFromFile("toc.xml"):ToTable()
for k,v in pairs(tocXML) do
if v.__XmlNode == "Test" then
BustedTests[oAddon][v.Name] = strAssetFolder .. "\\" .. v.File
bFoundTests = true
end
end
if bFoundTests then
for k, v in pairs(tMixins) do
oAddon[k] = v
end
end
end
function LibBusted:OnDependencyError(strDep, strError)
return false
end
function LibBusted:OnLoad()
s = Apollo.GetPackage("Olivine:Say-1.0").tPackage
pretty = Apollo.GetPackage("Lib:Penlight:Pretty-1.0").tPackage
busted = Apollo.GetPackage("Olivine:Busted:Core-2.0").tPackage
-- Set up output handler to listen to events TODO: Allow config, support tags
local outputHandlerOptions = {
verbose = true,
suppressPending = true,
deferPrint = false,
}
local outputHandler = outputPlainTerminal(outputHandlerOptions, busted) -- (only choice for now)
busted.subscribe({'register', 'it'}, checkTags, { priority = 1 })
busted.subscribe({'register', 'pending'}, checkTags, { priority = 1 })
busted.subscribe({ 'test', 'start' }, outputHandler.testStart)
busted.subscribe({ 'test', 'end' }, outputHandler.testEnd)
busted.subscribe({ 'file', 'start' }, outputHandler.fileStart)
busted.subscribe({ 'file', 'end' }, outputHandler.fileEnd)
busted.subscribe({ 'suite', 'start' }, outputHandler.suiteStart)
busted.subscribe({ 'suite', 'end' }, outputHandler.suiteEnd)
busted.subscribe({ 'error' }, outputHandler.error)
if true then -- TODO: Config
local sound = outputSound(outputHandlerOptions, busted)
busted.subscribe({ 'test', 'end' }, sound.testEnd)
busted.subscribe({ 'suite', 'end' }, sound.suiteEnd)
busted.subscribe({ 'error' }, sound.error)
end
end
if _TESTRUNNER then
function LibBusted:RunAllTests()
for addon, tests in pairs(BustedTests) do
addon:RunTests()
end
end
end
local tDependencies = {
"Gemini:LibError-1.0",
"Lib:Assert-1.0",
"Olivine:Say-1.0",
"Olivine:Busted:Core-2.0",
}
Apollo.RegisterPackage(LibBusted, MAJOR, MINOR, tDependencies)