Add dynamic terminal separators based on terminal width

This commit is contained in:
Joey Payne 2016-06-18 12:48:07 -06:00
commit e01a6fc83d
2 changed files with 51 additions and 37 deletions

View file

@ -18,8 +18,7 @@ import tables
import typetraits import typetraits
when not defined(ECMAScript): when not defined(ECMAScript):
import terminal import terminal
import utils
# ----------------- Helper Procs and Macros ----------------------------------- # ----------------- Helper Procs and Macros -----------------------------------
@ -101,6 +100,7 @@ type
currentTestName: string currentTestName: string
testsPassed: int testsPassed: int
numTests: int numTests: int
lastTestFailed: bool
TestAssertError = object of Exception TestAssertError = object of Exception
## check and other check* statements will raise ## check and other check* statements will raise
@ -134,11 +134,11 @@ template returnException(name, testName, snip, vals, pos, posRel) =
var var
filename = posRel.filename filename = posRel.filename
line = pos.line line = pos.line
var message = "\n" var message = "\l"
message &= " Condition: $2($1)\n".format(snip, name) message &= " Condition: $2($1)\l".format(snip, name)
message &= " Where:\n" message &= " Where:\l"
for k, v in vals.pairs: for k, v in vals.pairs:
message &= " $1 -> $2\n".format(k, v) message &= " $1 -> $2\l".format(k, v)
message &= " Location: $1; line $2".format(filename, line) message &= " Location: $1; line $2".format(filename, line)
@ -523,13 +523,18 @@ macro testSuite*(head: untyped, body: untyped): untyped =
writeStyled(".", {styleBright}) writeStyled(".", {styleBright})
setForegroundColor(fgWhite) setForegroundColor(fgWhite)
else: else:
var okStr = "[OK]"
if self.lastTestFailed:
okStr = "\l" & okStr
when not defined(noColors): when not defined(noColors):
styledEcho(styleBright, fgGreen, "[OK]", styledEcho(styleBright, fgGreen, okStr,
fgWhite, " ", self.currentTestName) fgWhite, " ", self.currentTestName)
else: else:
echo "[OK] $1".format(self.currentTestName) echo "$1 $2".format(okStr, self.currentTestName)
self.testsPassed += 1 self.testsPassed += 1
self.lastTestFailed = false
except TestAssertError: except TestAssertError:
let e = (ref TestAssertError)(getCurrentException()) let e = (ref TestAssertError)(getCurrentException())
@ -543,10 +548,10 @@ macro testSuite*(head: untyped, body: untyped): untyped =
else: else:
when not defined(noColors): when not defined(noColors):
styledEcho(styleBright, styledEcho(styleBright,
fgRed, "[Failed]", fgRed, "\l[Failed]",
fgWhite, " ", self.currentTestName) fgWhite, " ", self.currentTestName)
else: else:
echo "[Failed] $1".format(self.currentTestName) echo "\l[Failed] $1".format(self.currentTestName)
let let
name = e.checkFuncName name = e.checkFuncName
@ -556,18 +561,19 @@ macro testSuite*(head: untyped, body: untyped): untyped =
vals = e.valTable vals = e.valTable
when not defined(noColors): when not defined(noColors):
styledEcho(styleDim, fgWhite, " Condition: $2($1)\n".format(snip, name), " Where:") styledEcho(styleDim, fgWhite, " Condition: $2($1)\l".format(snip, name), " Where:")
for k, v in vals.pairs: for k, v in vals.pairs:
styledEcho(styleDim, fgCyan, " ", k, styledEcho(styleDim, fgCyan, " ", k,
fgWhite, " -> ", fgWhite, " -> ",
fgGreen, v) fgGreen, v)
styledEcho(styleDim, fgWhite, " Location: $1; line $2\n".format(filename, line)) styledEcho(styleDim, fgWhite, " Location: $1; line $2".format(filename, line))
else: else:
echo " Condition: $2($1)".format(snip, name) echo " Condition: $2($1)".format(snip, name)
echo " Where:" echo " Where:"
for k, v in vals.pairs: for k, v in vals.pairs:
echo " ", k, " -> ", v echo " ", k, " -> ", v
echo " Location: $1; line $2\n".format(filename, line) echo " Location: $1; line $2".format(filename, line)
self.lastTestFailed = true
# Iterate over the statements, adding `self: T` # Iterate over the statements, adding `self: T`
# to the parameters of functions # to the parameters of functions
@ -593,6 +599,9 @@ macro testSuite*(head: untyped, body: untyped): untyped =
let dotName = newDotExpr(ident(objReference), ident("name")) let dotName = newDotExpr(ident(objReference), ident("name"))
let setName = newAssignment(dotName, newLit(typeName)) let setName = newAssignment(dotName, newLit(typeName))
n2.body.add(setName) n2.body.add(setName)
let dotRan = newDotExpr(ident(objReference), ident("lastTestFailed"))
let setRan = newAssignment(dotRan, ident("true"))
n2.body.add(setRan)
elif procName.toLower() == "teardown": elif procName.toLower() == "teardown":
discard discard
elif procName.startswith("test"): elif procName.startswith("test"):
@ -695,8 +704,9 @@ macro testSuite*(head: untyped, body: untyped): untyped =
proc printRunning(suite: TestSuite) = proc printRunning(suite: TestSuite) =
let termSize = getTermSize()
var var
numTicks = 80 - 12 - len(suite.name) numTicks = termSize[1]
ticks = "" ticks = ""
for i in 0..<numTicks: for i in 0..<numTicks:
@ -705,11 +715,12 @@ proc printRunning(suite: TestSuite) =
when not defined(quiet): when not defined(quiet):
when not defined(noColors): when not defined(noColors):
styledEcho(styleBright, styledEcho(styleBright,
fgYellow, "\n[Running]", fgYellow, "\l"&ticks,
fgWhite, " $1 ".format(suite.name), fgYellow, "\l\l[Running]",
fgYellow, " ", ticks, "\n") fgWhite, " $1 ".format(suite.name))
else: else:
echo "\n[Running] $1 $2\n".format(suite.name, ticks) echo "\l$1\l".format(ticks)
echo "[Running] $1".format(suite.name)
proc printPassedTests(suite: TestSuite) = proc printPassedTests(suite: TestSuite) =
@ -721,21 +732,13 @@ proc printPassedTests(suite: TestSuite) =
var passedStr = "[" & $suite.testsPassed & "/" & $suite.numTests & "]" var passedStr = "[" & $suite.testsPassed & "/" & $suite.numTests & "]"
var
ticks = " "
numTicks = 80 - len(passedStr) - 20 - len(suite.name)
for i in 0..<numTicks:
ticks &= "-"
when not defined(quiet): when not defined(quiet):
when not defined(noColors): when not defined(noColors):
styledEcho(styleBright, color, styledEcho(styleBright, color,
"\n", passedStr, "\l", passedStr,
fgWhite, " tests passed for ", suite.name, ".", fgWhite, " tests passed for ", suite.name, ".")
fgYellow, ticks, "\n")
else: else:
echo "\n$1 tests passed for $2.$3\n".format(passedStr, suite.name, ticks) echo "\l$1 tests passed for $2.".format(passedStr, suite.name)
proc printSummary(totalTestsPassed: int, totalTests: int) = proc printSummary(totalTestsPassed: int, totalTests: int) =
var summaryColor = fgGreen var summaryColor = fgGreen
@ -748,20 +751,33 @@ proc printSummary(totalTestsPassed: int, totalTests: int) =
when defined(quiet): when defined(quiet):
when not defined(noColors): when not defined(noColors):
styledEcho(styleBright, summaryColor, styledEcho(styleBright, summaryColor,
"\n\n", passedStr, "\l\l", passedStr,
fgWhite, " tests passed.") fgWhite, " tests passed.")
else: else:
echo "\n\n$1 tests passed.".format(passedStr) echo "\l\l$1 tests passed.".format(passedStr)
else: else:
let termSize = getTermSize()
var
ticks = ""
numTicks = termSize[1]
for i in 0..<numTicks:
ticks &= "-"
when not defined(noColors): when not defined(noColors):
styledEcho(styleBright, fgYellow, "\n[Summary]") styledEcho(styleBright,
fgYellow, "\l$1\l".format(ticks),
fgYellow, "\l[Summary]")
styledEcho(styleBright, summaryColor, styledEcho(styleBright, summaryColor,
"\n ", passedStr, "\l ", passedStr,
fgWhite, " tests passed.") fgWhite, " tests passed.")
else: else:
echo "\n[Summary]" echo "\l$1\l".format(ticks)
echo "\n $1 tests passed.".format(passedStr) echo "\l[Summary]"
echo "\l $1 tests passed.".format(passedStr)
proc runTests*() = proc runTests*() =
## The method that runs the tests. Invoke ## The method that runs the tests. Invoke

View file

@ -1,7 +1,5 @@
import einheit import einheit
testSuite UnitTests: testSuite UnitTests:
var var
testObj: int testObj: int