Improved colors for error reporting.

This commit is contained in:
Joey Payne 2015-07-10 23:49:58 -06:00
commit 03f84ad22e

View file

@ -49,12 +49,15 @@ type
numTests: int numTests: int
TestAssertError = object of Exception TestAssertError = object of Exception
## checkTrue and other check_* statements will raise ## check and other check* statements will raise
## this exception when the condition fails ## this exception when the condition fails
lineNumber: int lineNumber: int
fileName: string fileName: string
codeSnip: string codeSnip: string
testName: string testName: string
checkFuncName: string
valTable: Table[string, string]
# -- Methods for the TestSuite base -- # -- Methods for the TestSuite base --
@ -78,13 +81,10 @@ template returnException(name, testName, snip, vals, pos, posRel)=
filename = posRel.filename filename = posRel.filename
line = pos.line line = pos.line
var message = "\n" var message = "\n"
message &= " Condition: $2($1)\n".format(snip.replace("\n","").strip(), name) message &= " Condition: $2($1)\n".format(snip, name)
try:
message &= " Where:\n" message &= " Where:\n"
for k, v in vals.pairs: for k, v in vals.pairs:
message &= " $1 -> $2\n".format(k, v) message &= " $1 -> $2\n".format(k, v)
except:
message &= " Reason: $1\n".format(vals)
message &= " Location: $1; line $2".format(filename, line) message &= " Location: $1; line $2".format(filename, line)
@ -93,6 +93,8 @@ template returnException(name, testName, snip, vals, pos, posRel)=
exc.lineNumber = line exc.lineNumber = line
exc.codeSnip = snip exc.codeSnip = snip
exc.testName = testName exc.testName = testName
exc.valTable = vals
exc.checkFuncName = name
raise exc raise exc
# ------------------------ Templates for checkion ---------------------------- # ------------------------ Templates for checkion ----------------------------
@ -108,10 +110,10 @@ template checkRaises*(self: TestSuite, error: Exception,
try: try:
code code
var var
snip = "$1, $2".format(astToStr(error), astToStr(code).strip()) codeStr = astToStr(code).split().join(" ")
vals = {astToStr(code).strip(): "No Exception Raised"}.toTable() snip = "$1, $2".format(astToStr(error), codeStr)
vals = {codeStr: "No Exception Raised"}.toTable()
testName = self.currentTestName testName = self.currentTestName
returnException("checkRaises", testName, snip, vals, pos, posRel) returnException("checkRaises", testName, snip, vals, pos, posRel)
except error: except error:
@ -121,8 +123,9 @@ template checkRaises*(self: TestSuite, error: Exception,
except Exception: except Exception:
var var
e = getCurrentException() e = getCurrentException()
snip = "$1, $2".format(astToStr(error), astToStr(code).strip()) codeStr = astToStr(code).split().join(" ")
vals = {astToStr(code).strip(): e.name}.toTable() snip = "$1, $2".format(astToStr(error), codeStr)
vals = {codeStr: $e.name}.toTable()
testName = self.currentTestName testName = self.currentTestName
returnException("checkRaises", testName, snip, vals, pos, posRel) returnException("checkRaises", testName, snip, vals, pos, posRel)
@ -171,7 +174,7 @@ macro getSyms(code:untyped): untyped=
result = tableCall result = tableCall
else: else:
template emptyTable()= template emptyTable()=
newTable[string, string]() initTable[string, string]()
result = getAst(emptyTable()) result = getAst(emptyTable())
@ -189,7 +192,7 @@ template check*(self: TestSuite, code: untyped){.immediate.}=
var vals = getSyms(code) var vals = getSyms(code)
# get ast string with extra spaces ignored # get ast string with extra spaces ignored
snip = astToStr(code).replace(" ", " ") snip = astToStr(code).split().join(" ")
returnException("check", testName, snip, vals, pos, posRel) returnException("check", testName, snip, vals, pos, posRel)
@ -484,7 +487,19 @@ macro testSuite*(head: untyped, body: untyped): untyped =
let e = (ref TestAssertError)(getCurrentException()) let e = (ref TestAssertError)(getCurrentException())
styledEcho(styleBright, styledEcho(styleBright,
fgRed, "[Failed]", fgRed, "[Failed]",
fgWhite, " ", self.currentTestName, e.msg, "\n") fgWhite, " ", self.currentTestName)
let
name = e.checkFuncName
snip = e.codeSnip
line = e.lineNumber
filename = e.fileName
vals = e.valTable
styledEcho(styleDim, fgWhite, " Condition: $2($1)\n".format(snip, name), " Where:")
for k, v in vals.pairs:
styledEcho(styleDim, fgCyan, " ", k,
fgWhite, " -> ",
fgGreen, v)
styledEcho(styleDim, fgWhite, " Location: $1; line $2\n".format(filename, line))
runTests[0][6].add(getAst(setTestName(ident(objReference), procName))) runTests[0][6].add(getAst(setTestName(ident(objReference), procName)))
runTests[0][6].add(getAst(tryBlock(ident(objReference), procCall))) runTests[0][6].add(getAst(tryBlock(ident(objReference), procCall)))