Fix bug with noColors and add return code option

This commit is contained in:
Joey Payne 2019-02-27 20:50:40 -07:00
commit d6a6bd95f1

View file

@ -333,7 +333,7 @@ template runTest(procCall, info, testName) =
styledEcho(styleBright, fgGreen, okStr, styledEcho(styleBright, fgGreen, okStr,
fgWhite, " ", testName) fgWhite, " ", testName)
else: else:
echo "$1 $2".format(okStr, t.name) echo "$1 $2".format(okStr, testName)
info.testsPassed += 1 info.testsPassed += 1
info.lastTestFailed = false info.lastTestFailed = false
@ -352,7 +352,7 @@ template runTest(procCall, info, testName) =
fgBlue, "\l[Not run]", fgBlue, "\l[Not run]",
fgWhite, " ", testName) fgWhite, " ", testName)
else: else:
echo "\l[Not run] $1".format(t.name) echo "\l[Not run] $1".format(testName)
let let
name = e.checkFuncName name = e.checkFuncName
@ -391,7 +391,7 @@ template runTest(procCall, info, testName) =
fgRed, "\l[Failed]", fgRed, "\l[Failed]",
fgWhite, " ", testName) fgWhite, " ", testName)
else: else:
echo "\l[Failed] $1".format(t.name) echo "\l[Failed] $1".format(testName)
let let
name = e.checkFuncName name = e.checkFuncName
@ -523,7 +523,7 @@ template createTotalVars(totalTestsPassed, totalTests) =
template printFinalSummary(totalTestsPassed, totalTests) = template printFinalSummary(totalTestsPassed, totalTests) =
printSummary(totalTestsPassed, totalTests) printSummary(totalTestsPassed, totalTests)
proc expandTests(currentDir: string): NimNode = proc expandTests(returnErrorCode: bool, currentDir: string): NimNode =
result = newNimNode(nnkStmtList) result = newNimNode(nnkStmtList)
@ -567,6 +567,8 @@ proc expandTests(currentDir: string): NimNode =
result.add(getAst(createRunTests(allTests, infoSym, totalTests, totalTestsPassed))) result.add(getAst(createRunTests(allTests, infoSym, totalTests, totalTestsPassed)))
result.add(getAst(printFinalSummary(totalTestsPassed, totalTests))) result.add(getAst(printFinalSummary(totalTestsPassed, totalTests)))
if returnErrorCode:
result.add(quote do: `totalTests` - `totalTestsPassed`)
macro test*(name: string, body: untyped): untyped = macro test*(name: string, body: untyped): untyped =
## The test macro. No runtime overhead is introduced using it and ## The test macro. No runtime overhead is introduced using it and
@ -576,8 +578,11 @@ macro test*(name: string, body: untyped): untyped =
testsInfo = getTestsModule(fileName) testsInfo = getTestsModule(fileName)
testsInfo.addTest(body, name) testsInfo.addTest(body, name)
macro runTests*(callsite: varargs[untyped]): untyped = macro runTests*(returnErrorCode=false): untyped =
## This macro must be run in the main testing module that imports ## This macro must be run in the main testing module that imports
## all of the other tests ## all of the other tests.
##
## If returnErrorCode is true, the number of failed tests will
## be returned.
let currentDir = getProjectPath() let currentDir = getProjectPath()
return expandTests(currentDir) return expandTests(returnErrorCode.boolVal, currentDir)