Created
December 28, 2016 15:10
-
-
Save xiaobin83/2792e6db39489423e6d9df505623ac05 to your computer and use it in GitHub Desktop.
Simple unit test script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local UnitTest = {} | |
local meta = { __index = UnitTest } | |
function UnitTest.New() | |
local inst = { | |
numTests = 0, | |
failed = {} | |
} | |
setmetatable(inst, meta) | |
return inst | |
end | |
function UnitTest:NewTest() | |
self.numTests = self.numTests + 1 | |
end | |
function UnitTest:Failed(message) | |
self.failed[#self.failed + 1] = debug.traceback(message or 'failed!') | |
end | |
function UnitTest:AreEqual(expected, actual) | |
self:NewTest() | |
if expected ~= actual then | |
self:Failed('Expected '..tostring(expected) .. ', but ' .. tostring(actual) .. ' got. ') | |
end | |
end | |
function UnitTest:True(expr) | |
self:NewTest() | |
if not expr then | |
self:Failed('Expected True failed.') | |
end | |
end | |
function UnitTest:False(expr) | |
self:NewTest() | |
if expr then | |
self:Failed('Expected False failed.') | |
end | |
end | |
function UnitTest:Done() | |
if #self.failed == 0 then | |
print('All Passed!') | |
else | |
print((self.numTests - #self.failed) .. '/' .. self.numTests .. ' passed.') | |
for _, f in ipairs(self.failed) do | |
print(f) | |
end | |
end | |
end | |
return UnitTest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment