Added ability to specify if colors are used and the verbosity using defined switches: -d:quiet -d:noColors
This commit is contained in:
parent
49e9ff9789
commit
0845d49110
3 changed files with 127 additions and 29 deletions
12
README.md
12
README.md
|
|
@ -29,6 +29,18 @@ Install with nimble!
|
||||||
nimble install einheit
|
nimble install einheit
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Running
|
||||||
|
|
||||||
|
```bash
|
||||||
|
nim c -r testing_script.nim
|
||||||
|
|
||||||
|
# Or no colors
|
||||||
|
nim c -r -d:noColors testing_script.nim
|
||||||
|
|
||||||
|
# No colors, quiet output
|
||||||
|
nim c -r -d:quiet -d:noColors testing_script.nim
|
||||||
|
```
|
||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
```nim
|
```nim
|
||||||
|
|
|
||||||
142
einheit.nim
142
einheit.nim
|
|
@ -29,8 +29,10 @@ proc `$`*[T](ar: openarray[T]): string=
|
||||||
result &= "]"
|
result &= "]"
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
proc `$`*[T](some:typedesc[T]): string = name(T)
|
||||||
|
|
||||||
proc `$`*(s: ref object): string=
|
proc `$`*(s: ref object): string=
|
||||||
result = "ref " & ($s[]).replace(":ObjectType", "")
|
result = ($type(s)).replace(":ObjectType", "") & $s[]
|
||||||
|
|
||||||
proc `==`*[T](ar: openarray[T], ar2: openarray[T]): bool=
|
proc `==`*[T](ar: openarray[T], ar2: openarray[T]): bool=
|
||||||
if len(ar) != len(ar2):
|
if len(ar) != len(ar2):
|
||||||
|
|
@ -480,26 +482,60 @@ macro testSuite*(head: untyped, body: untyped): untyped =
|
||||||
self.numTests += 1
|
self.numTests += 1
|
||||||
try:
|
try:
|
||||||
testCall
|
testCall
|
||||||
styledEcho(styleBright, fgGreen, "[OK]",
|
when defined(quiet):
|
||||||
fgWhite, " ", self.currentTestName)
|
when defined(noColors):
|
||||||
|
stdout.write(".")
|
||||||
|
else:
|
||||||
|
setForegroundColor(fgGreen)
|
||||||
|
writeStyled(".", {styleBright})
|
||||||
|
setForegroundColor(fgWhite)
|
||||||
|
else:
|
||||||
|
when not defined(noColors):
|
||||||
|
styledEcho(styleBright, fgGreen, "[OK]",
|
||||||
|
fgWhite, " ", self.currentTestName)
|
||||||
|
else:
|
||||||
|
echo "[OK] $1".format(self.currentTestName)
|
||||||
|
|
||||||
self.testsPassed += 1
|
self.testsPassed += 1
|
||||||
except TestAssertError:
|
except TestAssertError:
|
||||||
let e = (ref TestAssertError)(getCurrentException())
|
let e = (ref TestAssertError)(getCurrentException())
|
||||||
styledEcho(styleBright,
|
|
||||||
fgRed, "[Failed]",
|
when defined(quiet):
|
||||||
fgWhite, " ", self.currentTestName)
|
when defined(noColors):
|
||||||
let
|
stdout.write("F")
|
||||||
name = e.checkFuncName
|
else:
|
||||||
snip = e.codeSnip
|
setForegroundColor(fgRed)
|
||||||
line = e.lineNumber
|
writeStyled("F", {styleBright})
|
||||||
filename = e.fileName
|
setForegroundColor(fgWhite)
|
||||||
vals = e.valTable
|
else:
|
||||||
styledEcho(styleDim, fgWhite, " Condition: $2($1)\n".format(snip, name), " Where:")
|
when not defined(noColors):
|
||||||
for k, v in vals.pairs:
|
styledEcho(styleBright,
|
||||||
styledEcho(styleDim, fgCyan, " ", k,
|
fgRed, "[Failed]",
|
||||||
fgWhite, " -> ",
|
fgWhite, " ", self.currentTestName)
|
||||||
fgGreen, v)
|
else:
|
||||||
styledEcho(styleDim, fgWhite, " Location: $1; line $2\n".format(filename, line))
|
echo "[Failed] $1".format(self.currentTestName)
|
||||||
|
|
||||||
|
let
|
||||||
|
name = e.checkFuncName
|
||||||
|
snip = e.codeSnip
|
||||||
|
line = e.lineNumber
|
||||||
|
filename = e.fileName
|
||||||
|
vals = e.valTable
|
||||||
|
|
||||||
|
when not defined(noColors):
|
||||||
|
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))
|
||||||
|
else:
|
||||||
|
echo " Condition: $2($1)".format(snip, name)
|
||||||
|
echo " Where:"
|
||||||
|
for k, v in vals.pairs:
|
||||||
|
echo " ", k, " -> ", v
|
||||||
|
echo " 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)))
|
||||||
|
|
@ -600,21 +636,41 @@ proc runTests*()=
|
||||||
## The method that runs the tests. Invoke
|
## The method that runs the tests. Invoke
|
||||||
## after setting up all of the tests and
|
## after setting up all of the tests and
|
||||||
## usually inside a "when isMainModule" block
|
## usually inside a "when isMainModule" block
|
||||||
|
var
|
||||||
|
totalTests = 0
|
||||||
|
totalTestsPassed = 0
|
||||||
|
|
||||||
|
when defined(quiet):
|
||||||
|
echo ""
|
||||||
|
|
||||||
for suite in testSuites:
|
for suite in testSuites:
|
||||||
suite.setup()
|
suite.setup()
|
||||||
var numTicks = 80 - 12 - len(suite.name)
|
|
||||||
var ticks = ""
|
var
|
||||||
|
numTicks = 80 - 12 - len(suite.name)
|
||||||
|
ticks = ""
|
||||||
|
|
||||||
for i in 0..<numTicks:
|
for i in 0..<numTicks:
|
||||||
ticks &= "-"
|
ticks &= "-"
|
||||||
styledEcho(styleBright,
|
|
||||||
fgYellow, "\n[Running]",
|
when not defined(quiet):
|
||||||
fgWhite, " $1 ".format(suite.name),
|
when not defined(noColors):
|
||||||
fgYellow, " ", ticks, "\n")
|
styledEcho(styleBright,
|
||||||
|
fgYellow, "\n[Running]",
|
||||||
|
fgWhite, " $1 ".format(suite.name),
|
||||||
|
fgYellow, " ", ticks, "\n")
|
||||||
|
else:
|
||||||
|
echo "\n[Running] $1 $2\n".format(suite.name, ticks)
|
||||||
|
|
||||||
suite.runTests()
|
suite.runTests()
|
||||||
suite.tearDown()
|
suite.tearDown()
|
||||||
|
|
||||||
|
totalTests += suite.numTests
|
||||||
|
totalTestsPassed += suite.testsPassed
|
||||||
|
|
||||||
# Output red if tests didn't pass, green otherwise
|
# Output red if tests didn't pass, green otherwise
|
||||||
var color = fgGreen
|
var color = fgGreen
|
||||||
|
|
||||||
if suite.testsPassed != suite.numTests:
|
if suite.testsPassed != suite.numTests:
|
||||||
color = fgRed
|
color = fgRed
|
||||||
|
|
||||||
|
|
@ -626,8 +682,38 @@ proc runTests*()=
|
||||||
for i in 0..<numTicks:
|
for i in 0..<numTicks:
|
||||||
ticks &= "-"
|
ticks &= "-"
|
||||||
|
|
||||||
styledEcho(styleBright, color,
|
when not defined(quiet):
|
||||||
"\n", passedStr,
|
when not defined(noColors):
|
||||||
fgWhite, " tests passed for ", suite.name, ".",
|
styledEcho(styleBright, color,
|
||||||
fgYellow, ticks, "\n")
|
"\n", passedStr,
|
||||||
|
fgWhite, " tests passed for ", suite.name, ".",
|
||||||
|
fgYellow, ticks, "\n")
|
||||||
|
else:
|
||||||
|
echo "\n$1 tests passed for $2.$3\n".format(passedStr, suite.name, ticks)
|
||||||
|
|
||||||
|
var summaryColor = fgGreen
|
||||||
|
|
||||||
|
if totalTestsPassed != totalTests:
|
||||||
|
summaryColor = fgRed
|
||||||
|
|
||||||
|
var passedStr = "[" & $totalTestsPassed & "/" & $totalTests & "]"
|
||||||
|
|
||||||
|
when defined(quiet):
|
||||||
|
when not defined(noColors):
|
||||||
|
styledEcho(styleBright, summaryColor,
|
||||||
|
"\n\n", passedStr,
|
||||||
|
fgWhite, " tests passed.")
|
||||||
|
else:
|
||||||
|
echo "\n\n$1 tests passed.".format(passedStr)
|
||||||
|
else:
|
||||||
|
when not defined(noColors):
|
||||||
|
styledEcho(styleBright, fgYellow, "\n[Summary]")
|
||||||
|
|
||||||
|
styledEcho(styleBright, summaryColor,
|
||||||
|
"\n ", passedStr,
|
||||||
|
fgWhite, " tests passed.")
|
||||||
|
else:
|
||||||
|
echo "\n[Summary]"
|
||||||
|
echo "\n $1 tests passed.".format(passedStr)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
2
test.nim
2
test.nim
|
|
@ -129,7 +129,7 @@ testSuite MoreInheritance of TestInherit:
|
||||||
k = TestObj(t: 30)
|
k = TestObj(t: 30)
|
||||||
|
|
||||||
self.check(t != r)
|
self.check(t != r)
|
||||||
self.check(d != k)
|
self.check(d == k)
|
||||||
|
|
||||||
self.check(self.doStuff(a, s) == "5stuff" and self.doStuff(a, self.doStuff(a, self.doStuff(y, s))) == "something?")
|
self.check(self.doStuff(a, s) == "5stuff" and self.doStuff(a, self.doStuff(a, self.doStuff(y, s))) == "something?")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue