on the path to get unittest running from JS

This commit is contained in:
Simon Hafner 2013-02-09 15:19:09 -06:00
commit 260ddd735a
2 changed files with 28 additions and 13 deletions

View file

@ -17,7 +17,13 @@
## It is loosely based on C++'s boost.test and Haskell's QuickTest ## It is loosely based on C++'s boost.test and Haskell's QuickTest
import import
macros, terminal, os macros
when defined(stdout):
import os
when not defined(ECMAScript):
import terminal
type type
TTestStatus* = enum OK, FAILED TTestStatus* = enum OK, FAILED
@ -52,12 +58,15 @@ proc testDone(name: string, s: TTestStatus) =
program_result += 1 program_result += 1
if OutputLevel != PRINT_NONE and (OutputLevel == PRINT_ALL or s == FAILED): if OutputLevel != PRINT_NONE and (OutputLevel == PRINT_ALL or s == FAILED):
proc rawPrint() = echo("[", $s, "] ", name, "\n")
when not defined(ECMAScript):
if ColorOutput and not defined(ECMAScript):
var color = (if s == OK: fgGreen else: fgRed) var color = (if s == OK: fgGreen else: fgRed)
if ColorOutput:
styledEcho styleBright, color, "[", $s, "] ", fgWhite, name, "\n" styledEcho styleBright, color, "[", $s, "] ", fgWhite, name, "\n"
else: else:
echo "[", $s, "] ", name, "\n" rawPrint()
else:
rawPrint()
template test*(name: expr, body: stmt): stmt {.immediate, dirty.} = template test*(name: expr, body: stmt): stmt {.immediate, dirty.} =
bind shouldRun, checkpoints, testDone bind shouldRun, checkpoints, testDone
@ -87,6 +96,7 @@ template fail* =
for msg in items(checkpoints): for msg in items(checkpoints):
echo msg echo msg
when not defined(ECMAScript):
if AbortOnError: quit(1) if AbortOnError: quit(1)
TestStatusIMPL = FAILED TestStatusIMPL = FAILED
@ -171,14 +181,19 @@ macro expect*(exceptions: varargs[expr], body: stmt): stmt {.immediate.} =
result = getAst(expectBody(errorTypes, exp.lineinfo, body)) result = getAst(expectBody(errorTypes, exp.lineinfo, body))
when defined(stdout):
## Reading settings ## Reading settings
var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string
AbortOnError = existsEnv("NIMTEST_ABORT_ON_ERROR")
ColorOutput = not existsEnv("NIMTEST_NO_COLOR")
else:
var envOutLvl = "" # TODO
ColorOutput = false
if envOutLvl.len > 0: if envOutLvl.len > 0:
for opt in countup(low(TOutputLevel), high(TOutputLevel)): for opt in countup(low(TOutputLevel), high(TOutputLevel)):
if $opt == envOutLvl: if $opt == envOutLvl:
OutputLevel = opt OutputLevel = opt
break break
AbortOnError = existsEnv("NIMTEST_ABORT_ON_ERROR")
ColorOutput = not existsEnv("NIMTEST_NO_COLOR")

View file

@ -42,7 +42,7 @@ proc nimCharToStr(x: char): string {.compilerproc.} =
result = newString(1) result = newString(1)
result[0] = x result[0] = x
proc getCurrentExceptionMsg(): string = proc getCurrentExceptionMsg*(): string =
if excHandler != nil: return $excHandler.exc.msg if excHandler != nil: return $excHandler.exc.msg
return "" return ""