diff --git a/README.md b/README.md index 30ecc93..d5cdb7e 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,18 @@ Install with nimble! 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 ```nim diff --git a/einheit.nim b/einheit.nim index 3dac4f5..6e697ce 100644 --- a/einheit.nim +++ b/einheit.nim @@ -29,8 +29,10 @@ proc `$`*[T](ar: openarray[T]): string= result &= "]" return result +proc `$`*[T](some:typedesc[T]): string = name(T) + 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= if len(ar) != len(ar2): @@ -480,26 +482,60 @@ macro testSuite*(head: untyped, body: untyped): untyped = self.numTests += 1 try: testCall - styledEcho(styleBright, fgGreen, "[OK]", - fgWhite, " ", self.currentTestName) + when defined(quiet): + 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 except TestAssertError: let e = (ref TestAssertError)(getCurrentException()) - styledEcho(styleBright, - fgRed, "[Failed]", - 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)) + + when defined(quiet): + when defined(noColors): + stdout.write("F") + else: + setForegroundColor(fgRed) + writeStyled("F", {styleBright}) + setForegroundColor(fgWhite) + else: + when not defined(noColors): + styledEcho(styleBright, + fgRed, "[Failed]", + fgWhite, " ", self.currentTestName) + else: + 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(tryBlock(ident(objReference), procCall))) @@ -600,21 +636,41 @@ proc runTests*()= ## The method that runs the tests. Invoke ## after setting up all of the tests and ## usually inside a "when isMainModule" block + var + totalTests = 0 + totalTestsPassed = 0 + + when defined(quiet): + echo "" + for suite in testSuites: suite.setup() - var numTicks = 80 - 12 - len(suite.name) - var ticks = "" + + var + numTicks = 80 - 12 - len(suite.name) + ticks = "" + for i in 0..