Improved test runner usage

This commit is contained in:
Sam Surtees 2018-06-15 00:51:36 +10:00
parent b5471154a6
commit c85e8203d0
3 changed files with 103 additions and 34 deletions

View File

@ -41,12 +41,27 @@
m.loadTestsFromManifests() m.loadTestsFromManifests()
m.detectDuplicateTests = false m.detectDuplicateTests = false
local test, err = m.getTestWithIdentifier(_OPTIONS["test-only"]) local tests = {}
if err then local isAction = true
error(err, 0) for i, arg in ipairs(_ARGS) do
local _tests, err = m.getTestsWithIdentifier(arg)
if err then
error(err, 0)
end
tests = table.join(tests, _tests)
end
if #tests == 0 or _OPTIONS["test-only"] ~= nil then
local _tests, err = m.getTestsWithIdentifier(_OPTIONS["test-only"])
if err then
error(err, 0)
end
tests = table.join(tests, _tests)
end end
local passed, failed = m.runTest(test) local passed, failed = m.runTest(tests)
if failed > 0 then if failed > 0 then
printf("\n %d FAILED TEST%s", failed, iif(failed > 1, "S", "")) printf("\n %d FAILED TEST%s", failed, iif(failed > 1, "S", ""))

View File

@ -112,31 +112,79 @@
--- ---
-- Fetch a test object via its string identifier. -- Fetch test objects via the string identifier.
-- --
-- @param identifier -- @param identifier
-- An optional test or suite identifier, indicating which tests should be -- An optional test or suite identifier, indicating which tests should be
-- run, in the form "suiteName" or "suiteName.testName". If not specified, -- run, in the form "suiteName" or "suiteName.testName". If not specified,
-- the global test object, representing all test suites, will be returned. -- the global test object, representing all test suites, will be returned.
-- Use "*" to match any part of a suite or test name
-- @return -- @return
-- On success, returns a test object, which should be considered opaque. -- On success, returns an array of test objects, which should be considered opaque.
-- On failure, returns `nil` and an error. -- On failure, returns `nil` and an error.
--- ---
function m.getTestWithIdentifier(identifier) function m.getTestsWithIdentifier(identifier)
local suiteName, testName = m.parseTestIdentifier(identifier) local suiteName, testName = m.parseTestIdentifier(identifier)
local suite, test, err = _.checkTestIdentifier(_.suites, suiteName, testName) if suiteName ~= nil and string.contains(suiteName, "*") then
if err then local tests = {}
return nil, err
end
return { local pattern = string.gsub(suiteName, "*", ".*")
suiteName = suiteName, for _suiteName, suite in pairs(_.suites) do
suite = suite, local length = string.len(_suiteName)
testName = testName, local start, finish = string.find(_suiteName, pattern)
testFunction = test if start == 1 and finish == length then
} if testName ~= nil then
if string.contains(testName, "*") then
local testPattern = string.gsub(testName, "*", ".*")
for _testName, test in pairs(suite) do
length = string.len(_testName)
start, finish = string.find(_testName, testPattern)
if start == 1 and finish == length then
table.insert(tests, {
suiteName = _suiteName,
suite = suite,
testName = _testName,
testFunction = test,
})
end
end
else
table.insert(tests, {
suiteName = _suiteName,
suite = suite,
testName = testName,
testFunction = suite[testName],
})
end
else
table.insert(tests, {
suiteName = _suiteName,
suite = suite,
testName = nil,
testFunction = nil,
})
end
end
end
return tests
else
local suite, test, err = _.checkTestIdentifier(_.suites, suiteName, testName)
if err then
return nil, err
end
return {
{
suiteName = suiteName,
suite = suite,
testName = testName,
testFunction = test
}
}
end
end end

View File

@ -15,12 +15,12 @@
function m.runTest(test) function m.runTest(tests)
local failed = 0 local failed = 0
local failedTests = {} local failedTests = {}
local suites = m.getSuites() local suites = m.getSuites()
local suitesKeys, suiteTestsKeys, totalTestCount = _.preprocessTests(suites, test) local suitesKeys, suiteTestsKeys, totalTestCount = _.preprocessTests(suites, tests)
_.log(term.lightGreen, "[==========]", string.format(" Running %d tests from %d test suites.", totalTestCount, #suitesKeys)) _.log(term.lightGreen, "[==========]", string.format(" Running %d tests from %d test suites.", totalTestCount, #suitesKeys))
local startTime = os.clock() local startTime = os.clock()
@ -128,30 +128,36 @@
function _.preprocessTests(suites, filter) function _.preprocessTests(suites, filters)
local suitesKeys = {} local suitesKeys = {}
local suiteTestsKeys = {} local suiteTestsKeys = {}
local totalTestCount = 0 local totalTestCount = 0
for i, filter in ipairs(filters) do
for suiteName, suite in pairs(suites) do
if not m.isSuppressed(suiteName) and suite ~= nil and (not filter.suiteName or filter.suiteName == suiteName) then
local test = {}
for suiteName, suite in pairs(suites) do test.suiteName = suiteName
if not m.isSuppressed(suiteName) and suite ~= nil and (not filter.suiteName or filter.suiteName == suiteName) then test.suite = suite
local test = {}
table.insertsorted(suitesKeys, suiteName) if not table.contains(suitesKeys, suiteName) then
table.insertsorted(suitesKeys, suiteName)
suiteTestsKeys[suiteName] = {}
end
test.suiteName = suiteName for testName, testFunction in pairs(suite) do
test.suite = suite test.testName = testName
test.testFunction = testFunction
suiteTestsKeys[suiteName] = {} if m.isValid(test) and not m.isSuppressed(test.suiteName .. "." .. test.testName) and (not filter.testName or filter.testName == testName) then
for testName, testFunction in pairs(suite) do if not table.contains(suiteTestsKeys[suiteName], testName) then
test.testName = testName table.insertsorted(suiteTestsKeys[suiteName], testName)
test.testFunction = testFunction totalTestCount = totalTestCount + 1
end
if m.isValid(test) and not m.isSuppressed(test.suiteName .. "." .. test.testName) and (not filter.testName or filter.testName == testName) then end
table.insertsorted(suiteTestsKeys[suiteName], testName)
end end
end end
totalTestCount = totalTestCount + #suiteTestsKeys[suiteName]
end end
end end