-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.lua
702 lines (600 loc) · 21.5 KB
/
suite.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
--[=[
`Probo` Lua unit test framework
]=]
--[=[ Test suite assertions ]=]
local Assert = {
attempts = 0,
attemptsSuccess = 0,
attemptsFailed = 0,
}
Assert.__index = Assert
function Assert.New()
local instance = {
-- keys here overwrites the keys in `Assert`
}
return setmetatable(instance, Assert)
end
---@private
---@return nil
function Assert:Attempt()
self.attempts = self.attempts + 1
end
---@private
---@return boolean true
function Assert:AttemptSuccessful()
self.attemptsSuccess = self.attemptsSuccess + 1
if not self.silent then io.write(".") end
return true
end
---@private
---@param message string custom error message
---@param level number error traceback level
---@return void
function Assert:AttemptFailed(message, level)
self.attemptsFailed = self.attemptsFailed + 1
if not self.silent then io.write("!") end
return error(message, level or 2) -- 0 no line, 1 this line, 2 test case
end
--[=[ Asserts
Returns true or false depending on the given variables
Should not directly be used
]=]
-- Type checks
function Assert:IsNil(object) return type(object) == "nil" end
function Assert:IsBoolean(object) return type(object) == "boolean" end
function Assert:IsNumber(object) return type(object) == "number" end
function Assert:IsString(object) return type(object) == "string" end
function Assert:IsTable(object) return type(object) == "table" end
function Assert:IsThread(object) return type(object) == "thread" end -- coroutine
function Assert:IsFunction(object) return type(object) == "function" end
function Assert:IsUserdata(object) return type(object) == "userdata" end
function Assert:IsOpenfile(ioObj) return io.type(ioObj) == "file" end
function Assert:IsClosedfile(ioObj) return io.type(ioObj) == "closed file" end
-- Coroutine checks
local coroStates = { ["dead"]=1, ["normal"]=1, ["running"]=1, ["suspended"]=1, ["normal"]=1 }
function Assert:IsCoroutine(coro) return coroStates[coroutine.status(coro)] ~= nil end
function Assert:IsCoroutineDead(coro) return coroutine.status(coro) == "dead" end
function Assert:IsCoroutineNormal(coro) return coroutine.status(coro) == "normal" end
function Assert:IsCoroutineRunning(coro) return coroutine.status(coro) == "running" end
function Assert:IsCoroutineSuspended(coro) return coroutine.status(coro) == "suspended" end
function Assert:IsCoroutineYieldable(coro) return coroutine.isyieldable(coro) end
---Assert that the given conditional statement that results in boolean true
---@public
---@param expression boolean the conditional statement
---@param message string custom error message
---@return boolean
function Assert:Condition(expression, message)
self:Attempt()
if not expression then
message = message or (
"Assert:Condition failed, value: %s"
):format(tostring(expression))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Assert that the given invokable does not create an error
---@public
---@param invokable function
---@param ... any invokable parameters
---@return boolean
function Assert:CreatesError(invokable, ...)
self:Attempt()
local boolean = pcall(invokable, ...)
if boolean == true then
local message = (
"Assert:CreatesError failed, %s doesn't create error"
):format(tostring(invokable))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Assert that the given invokable creates an error
---@public
---@param invokable function
---@param ... any invokable parameters
---@return boolean
function Assert:CreatesNoError(invokable, ...)
self:Attempt()
local boolean = pcall(invokable, ...)
if boolean == false then
local message = (
"Assert:CreatesNoError failed, %s does create an error"
):format(tostring(invokable))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Assert that the actual value is equal to the expected value
---@public
---@param actual any
---@param expected any
---@param message string custom error message
---@return boolean
function Assert:Equal(actual, expected, message)
self:Attempt()
if actual ~= expected then
message = message or (
"Assert:Equal failed, %s is not equal to %s"
):format(tostring(actual), tostring(expected))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Force a failure
---@public
---@return boolean false
function Assert:Fail(message)
self:Attempt()
message = message or "Test Failed"
return self:AttemptFailed(message)
end
---Asser that the given boolean is false
---@public
---@param boolean boolean
---@param message string custom error message
---@return boolean
function Assert:False(boolean, message)
self:Attempt()
if not self:IsBoolean(boolean) or boolean ~= false then
message = message or (
"Assert:False failed, received %s"
):format(tostring(boolean))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Assert that the given object can be invoked
---@public
---@param object any
---@param message string custom error message
---@return boolean
function Assert:Invokable(object, message)
self:Attempt()
local meta = getmetatable(object)
local metaTest = (meta and meta.__call and (type(meta.__call) == "function"))
if not self:IsFunction(object) and not metaTest then
message = message or (
"Assert:Invokable failed, %s is invokable"
):format(tostring(object))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Assert that the given object is a nil
---@public
---@param nilObject nil
---@param message string custom error message
---@return boolean
function Assert:Nil(nilObject, message)
self:Attempt()
if not self:IsNil(nilObject) then
message = message or (
"Assert:Nil failed, received %s"
):format(tostring(nilObject))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Assert that the actual value is not equal to the expected value
---@public
---@param actual any
---@param expected any
---@param message string custom error message
---@return boolean
function Assert:NotEqual(actual, expected, message)
self:Attempt()
if actual == expected then
message = message or (
"Assert:NotEqual failed, %s is equal to %s"
):format(tostring(actual), tostring(expected))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Assert that the given object is not a nil
---@public
---@param object any
---@param message string custom error message
---@return boolean
function Assert:NotNil(object, message)
self:Attempt()
if self:IsNil(object) then
message = message or (
"Assert:NotNil failed, received %s"
):format(tostring(object))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Force a pass
---@public
---@return boolean true
function Assert:Pass()
self:Attempt()
return self:AttemptSuccessful()
end
---Assert that the given table is empty
---@public
---@param tableObject table
---@param message string custom error message
---@return boolean
function Assert:TableEmpty(tableObject, message)
self:Attempt()
if not self:IsTable(tableObject) then
message = message or (
"Assert:TableEmpty failed, expected a table, but got %s"
):format(type(tableObject))
return self:AttemptFailed(message)
elseif next(tableObject) ~= nil then
message = message or (
"Assert:TableEmpty failed, table has items: %s"
):format(tostring(tableObject))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Assert that both given tables contains the same values
---@public
---@param tableA table
---@param tableB table
---@param message string custom error message
---@return boolean
function Assert:TableEquals(tableA, tableB, message)
self:Attempt()
local function CompareTables(tableA, tableB)
if tableA == tableB then
return true
end
local msg = "Assert:TableEquals failed, tables are different"
if type(tableA) ~= "table" then
return false, msg
end
if type(tableB) ~= "table" then
return false, msg
end
local meta1, meta2 = getmetatable(tableA), getmetatable(tableB)
if not CompareTables(meta1, meta2) then
return false, msg
end
for key1, val1 in pairs(tableA) do
local val2 = tableB[key1]
if not CompareTables(val1, val2) then
return false, msg
end
end
for key2, val2 in pairs(tableB) do
local val1 = tableA[key2]
if not CompareTables(val1, val2) then
return false, msg
end
end
return true
end
local boolean, err = CompareTables(tableA, tableB)
CompareTables = nil
if not boolean then
return self:AttemptFailed(message or err)
end
return self:AttemptSuccessful()
end
---Assert that both given tables contains the same keys
---@public
---@param tableA table
---@param tableB table
---@param message string custom error message
---@return boolean
function Assert:TableHasSameKeys(tableA, tableB, message)
self:Attempt()
message = message or (
"Assert:TableHasSameKeys failed, key '%s' of table #%d is not persent in table #%d"
)
for key1, _ in pairs(tableA) do -- compare table 1 keys to to table 2
if tableB[key1] == nil then
return self:AttemptFailed((message):format(tostring(key1), 1, 2))
end
end
for key2, _ in pairs(tableB) do -- compare table 2 keys to to table 1
if tableA[key2] == nil then
return self:AttemptFailed((message):format(tostring(key2), 2, 1))
end
end
return self:AttemptSuccessful()
end
---Assert that the given table not is empty
---@public
---@param tableObject table
---@param message string custom error message
---@return boolean
function Assert:TableNotEmpty(tableObject, message)
self:Attempt()
if not self:IsTable(tableObject) then
message = message or (
"Assert:TableNotEmpty failed, expected a table, but got a %s"
):format(type(tableObject))
return self:AttemptFailed(message)
elseif next(tableObject) == nil then
message = message or (
"Assert:TableNotEmpty failed, table is empty: %s"
):format(tostring(tableObject))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Asser that the given boolean is true
---@public
---@param boolean boolean
---@param message string custom error message
---@return boolean
function Assert:True(boolean, message)
self:Attempt()
if not self:IsBoolean(boolean) or boolean ~= true then
message = message or (
"Assert:True failed, received %s"
):format(tostring(boolean))
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
---Assert that the given object is of the expected type
---@public
---@param object any
---@param expectedType string result of built-in type call
---@param message string custom error message
---@return boolean
function Assert:Type(object, expectedType, message)
self:Attempt()
if type(object) ~= expectedType then
message = message or (
"Assert:Type failed, object %s is of type %s, expected %s"
):format(tostring(object), type(object), expectedType)
return self:AttemptFailed(message)
end
return self:AttemptSuccessful()
end
--[=[ Test suite ]=]
---Place newly created test function in a separate table
---@private
---@return nil
local function NewDefinedTests(self, key, value)
if type(value) == "function" then
-- assure the test function is not already defined
if self.definedTests[key] ~= nil then
local message = (
"Test '%s' in %s is previously defined"
):format(key, self.name or "the test suite")
error(message, 2)
end
-- key is the `testName` value is the `testFunc`
-- place it in a separate table
self.definedTests[key] = value
else
-- ignore __newindex
rawset(self, key, value)
end
end
---Decorator for tests so testnames can have spaces
---@private
---@param self table Suite instance table
---@param testName string test name
local function SetName(self, testName)
testName = tostring(testName)
local function innerFunc(testFunc) -- `testFunc` is the decorated function
self[testName] = testFunc -- set by `NewDefinedTests`
end
return innerFunc
end
---The test runner
---@private
---@param self table the table of the defined test suite
---@param options table the options table
---@param runInfo table the runInfoTable
local function DoRun(self, options, runInfo)
local testNames = {}
for testName in pairs(runInfo.definedTests) do
table.insert(testNames, testName)
end
if options.sortedByName then
table.sort(testNames)
end
self.SuiteSetup(self, options, "SuiteSetup")
for _, testName in ipairs(testNames) do
local testFunc = runInfo.definedTests[testName]
self.Setup(self, options, "Setup")
-- pcall returns the message from `error` in `Assert:AttemptFailed`
local success, message = pcall(testFunc)
table.insert(runInfo.executedTests, testName)
runInfo.amountExecuted = (runInfo.amountExecuted + 1)
if success then
runInfo.amountPassed = (runInfo.amountPassed + 1)
runInfo.passedTests[testName] = message or ("%s passed"):format(testName)
self.PassedHook(self, options, "PassedHook")
else
runInfo.amountFailed = (runInfo.amountFailed + 1)
runInfo.failedTests[testName] = message or ("%s failed"):format(testName)
self.FailedHook(self, options, "FailedHook")
end
self.Teardown(self, options, "Teardown")
if not success and options.stopOnFail then break end
end
self.SuiteTeardown(self, options, "SuiteTeardown")
end
---Run the tests that are defined in the test suite
---@public
---@param options table
---@return table information about the test run
local function Run(self, options)
options = options or self.defaultOptions or {}
local runInfo = {}
runInfo.options = options
runInfo.suiteName = self.suiteName
runInfo.definedTests = self.definedTests
runInfo.amountExecuted = 0
runInfo.amountPassed = 0
runInfo.amountFailed = 0
runInfo.executedTests = {}
runInfo.passedTests = {}
runInfo.failedTests = {}
runInfo.rerunInfo = {}
self.silent = options.silent -- set Assert.silent
-- start time recording
runInfo.startTime = os.time(os.date("!*t"))
local clockStart = os.clock()
DoRun(self, options, runInfo) -- mutates runInfo
if options.rerunFailedTests then
local rerunInfo = {}
rerunInfo.definedTests = {}
rerunInfo.executedTests = {}
rerunInfo.passedTests = {}
rerunInfo.failedTests = {}
rerunInfo.amountExecuted = 0
rerunInfo.amountPassed = 0
rerunInfo.amountFailed = 0
for testName, _ in pairs(runInfo.failedTests) do
local testFunc = runInfo.definedTests[testName]
if type(testFunc) == "function" then
rerunInfo.definedTests[testName] = testFunc
end
end
DoRun(self, options, rerunInfo) -- mutates rerunInfo
runInfo.rerunInfo = rerunInfo
end
-- stop time recording
runInfo.endTime = os.time(os.date("!*t"))
runInfo.runTime = (os.clock() - clockStart)
runInfo.totalExecuted = runInfo.amountExecuted + (runInfo.rerunInfo.amountExecuted or 0)
runInfo.totalPassed = runInfo.amountPassed + (runInfo.rerunInfo.amountPassed or 0)
runInfo.totalFailed = runInfo.amountFailed + (runInfo.rerunInfo.amountFailed or 0)
runInfo.runSuccess = (runInfo.totalFailed <= 0)
-- after the run, write the errors to stderr
if not options.silent and next(runInfo.failedTests) ~= nil then
print(--[[ new line ]])
for testName, message in pairs(runInfo.failedTests) do
io.stderr:write(("%s\n"):format(message))
end
if options.rerunFailedTests and next(runInfo.rerunInfo.failedTests) ~= nil then
for testName, message in pairs(runInfo.rerunInfo.failedTests) do
io.stderr:write(("Rerun: %s\n"):format(message))
end
end
end
return runInfo, runInfo.runSuccess
end
local Suite = {
-- if `<close>` is defined, __close is called at the end of the 'do-end' scope
__close = (function() collectgarbage() end),
__call = SetName,
__newindex = NewDefinedTests,
}
Suite.__index = Suite
Suite = setmetatable(Suite, Assert) -- inherit from Assert
---Create a new instance of a test suite
---@param suiteName string name of the test suite
---@return table new test suite instance
function Suite.New(suiteName)
local SuiteHook = function() end
local instance = {
suiteName = tostring(suiteName) or "Probo Suite",
definedTests = {}, -- table containing the testcases defined in the suite
--[[ Test hooks ]]
SuiteSetup = SuiteHook, -- before all the tests
SuiteTeardown = SuiteHook, -- after all the tests
Setup = SuiteHook, -- before every tests
Teardown = SuiteHook, -- after all the tests
PassedHook = SuiteHook, -- after a test has passed
FailedHook = SuiteHook, -- after a test has failed
--[[ Suite runner ]]
Run = Run,
--[[ default Run options ]]
defaultOptions = {
stopOnFail = false, -- stop the tests after the first failure
silent = false, -- no output during tests
rerunFailedTests = false, -- rerun the failed tests if failures has happened
sortedByName = false -- sorts the tests by name before the run of the tests
},
}
return setmetatable(instance, Suite)
end
--[=[
-- Test suite example:
local runInfo = {} -- define a `runInfo` table outside the do-end scope
do
-- create a new test suite instance
-- with <close> defined a garbage-collection cycle is performed at the end this scope
local test <close> = Suite.New("Probo Suite example")
local assert = test -- more readable separation between tests and asserts
test.run = 1 -- test suite variable
function test.AlwaysPasses() -- this is a defined test
assert:Invokable(test) -- multiple different asserts are available
end
test([[Always Fails]]) -- test is a decorator
(function() -- with the decorator the name of the test can have spaces
assert:Fail()
end)
function test.FlakyTest() -- failed tests in the first run can be rerun
test.run = test.run + 1 -- if the option `rerunFailedTests` is set to true
assert:Condition(test.run > 2)
end
local suiteOptions = { -- a table with options
stopOnFail = false,
silent = false,
rerunFailedTests = true,
sortedByName = false
}
-- run the above defined tests with the given options
runInfo = test:Run(suiteOptions) -- runInfo, a table with info about the run
end
]=]
--[=[
-- `runInfo` structure after test:Run
runInfo = {
suiteName = "Probo Suite example",
runSuccess = false,
startTime = 725893261,
endTime = 725893265,
runTime = 4.000175,
amountExecuted = 3.0,
amountFailed = 2.0,
amountPassed = 1.0,
totalExecuted = 5.0,
totalFailed = 3.0,
totalPassed = 2.0,
definedTests = {
["Always Fails"] = <function 1>,
AlwaysPasses = <function 2>,
FlakyTest = <function 3>
},
executedTests = { ["Always Fails"], "FlakyTest", "AlwaysPasses" },
failedTests = {
["Always Fails"] = "Test Failed",
FlakyTest = "Assert:Condition failed, value: false"
},
options = {
rerunFailedTests = true,
silent = false,
stopOnFail = false,
sortedByName = false
},
passedTests = {
AlwaysPasses = "AlwaysPasses passed"
},
rerunInfo = {
amountExecuted = 2.0,
amountFailed = 1.0,
amountPassed = 1.0,
definedTests = {
["Always Fails"] = <function 1>,
FlakyTest = <function 3>
},
executedTests = { ["Always Fails"], "FlakyTest" },
failedTests = {
["Always Fails"] = "Test Failed"
},
passedTests = {
FlakyTest = "FlakyTest passed"
}
}
}
]=]
--[[ make `Suite` and optionally `Assert` available when this file is imported with require ]]
return Suite, Assert