summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-11-12 01:21:10 +0100
committerAraq <rumpf_a@web.de>2011-11-12 01:21:10 +0100
commit3b6d831549d5e5214a1712605e7b375444ad342f (patch)
treee163ea55583380d01604594a302db131ebd12f6b /lib
parent8fc15ca0d5b46f1ecd3bd46f201f4ede3225be30 (diff)
parent032ff877801ba6c9c6e40a4a1bbea25fe4961559 (diff)
downloadNim-3b6d831549d5e5214a1712605e7b375444ad342f.tar.gz
Merge branch 'master' of github.com:Araq/Nimrod
Diffstat (limited to 'lib')
-rwxr-xr-xlib/nimbase.h2
-rw-r--r--lib/pure/unittest.nim49
-rwxr-xr-xlib/system.nim33
3 files changed, 72 insertions, 12 deletions
diff --git a/lib/nimbase.h b/lib/nimbase.h
index cc0419f55..11278ccd2 100755
--- a/lib/nimbase.h
+++ b/lib/nimbase.h
@@ -331,6 +331,8 @@ typedef long long int NI64;
 typedef unsigned int NU32;
 #endif
 
+extern NI nim_program_result;
+
 typedef float NF32;
 typedef double NF64;
 typedef double NF;
diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim
index 87c2b6ed7..e2906dd1a 100644
--- a/lib/pure/unittest.nim
+++ b/lib/pure/unittest.nim
@@ -17,15 +17,18 @@
 ## It is loosely based on C++'s boost.test and Haskell's QuickTest

 

 import

-  macros, terminal

+  macros, terminal, os

 

 type

-  TestStatus* = enum OK, FAILED

-  # ETestFailed* = object of ESynch

+  TTestStatus* = enum OK, FAILED

+  TOutputLevel* = enum PRINT_ALL, PRINT_FAILURES, PRINT_NONE

 

 var 

   # XXX: These better be thread-local

-  AbortOnError* = false

+  AbortOnError*: bool

+  OutputLevel*: TOutputLevel

+  ColorOutput*: bool

+  

   checkpoints: seq[string] = @[]

 

 template TestSetupIMPL*: stmt = nil

@@ -36,20 +39,29 @@ proc shouldRun(testName: string): bool =
 

 template suite*(name: expr, body: stmt): stmt =

   block:

-    template setup(setupBody: stmt): stmt =

+    template setup*(setupBody: stmt): stmt =

       template TestSetupIMPL: stmt = setupBody

 

-    template teardown(teardownBody: stmt): stmt =

+    template teardown*(teardownBody: stmt): stmt =

       template TestTeardownIMPL: stmt = teardownBody

 

     body

 

-proc printStatus*(s: TestStatus, name: string) =

-  var color = (if s == OK: fgGreen else: fgRed)

-  styledEcho styleBright, color, "[", $s, "] ", fgWhite, name, "\n"

+proc testDone(name: string, s: TTestStatus) =

+  if s == FAILED:

+    program_result += 1

+

+  if OutputLevel != PRINT_NONE and (OutputLevel == PRINT_ALL or s == FAILED):

+    var color = (if s == OK: fgGreen else: fgRed)

+    

+    if ColorOutput:

+      styledEcho styleBright, color, "[", $s, "] ", fgWhite, name, "\n"

+    else:

+      echo "[", $s, "] ", name, "\n"

   

 template test*(name: expr, body: stmt): stmt =

-  bind shouldRun, checkPoints

+  bind shouldRun, checkpoints, testDone

+

   if shouldRun(name):

     checkpoints = @[]

     var TestStatusIMPL = OK

@@ -60,7 +72,7 @@ template test*(name: expr, body: stmt): stmt =
 

     finally:

       TestTeardownIMPL()

-      printStatus(TestStatusIMPL, name)

+      testDone name, TestStatusIMPL

 

 proc checkpoint*(msg: string) =

   checkpoints.add(msg)

@@ -124,7 +136,8 @@ macro check*(conditions: stmt): stmt =
       result = standardRewrite(conditions[1])

 

   else:

-    error conditions.lineinfo & ": Malformed check statement"

+    var ast = conditions.treeRepr

+    error conditions.lineinfo & ": Malformed check statement:\n" & ast

 

 template require*(conditions: stmt): stmt =

   block:

@@ -149,3 +162,15 @@ macro expect*(exp: stmt): stmt =
 

   result = getAst(expectBody(errorTypes, exp.lineinfo, body))

 

+

+## Reading settings

+var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string

+

+if envOutLvl.len > 0:

+  for opt in countup(low(TOutputLevel), high(TOutputLevel)):

+    if $opt == envOutLvl:

+      OutputLevel = opt

+      break

+

+AbortOnError = existsEnv("NIMTEST_ABORT_ON_ERROR")

+ColorOutput  = not existsEnv("NIMTEST_NO_COLOR")

diff --git a/lib/system.nim b/lib/system.nim
index 8657efe2a..460bca5c0 100755
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1541,6 +1541,11 @@ const
     ## is the value that should be passed to ``quit`` to indicate
     ## failure.
 
+var program_result* {.exportc: "nim_$1".} = QuitSuccess
+  ## modify this varialbe to specify the exit code of the program
+  ## under normal circumstances. when the program is terminated
+  ## prematurelly using ``quit``, this value is ignored.
+
 proc quit*(errorcode: int = QuitSuccess) {.
   magic: "Exit", importc: "exit", noDecl, noReturn.}
   ## stops the program immediately; before stopping the program the
@@ -2029,3 +2034,31 @@ proc slurp*(filename: string): string {.magic: "Slurp".}
   ##   const myResource = slurp"mydatafile.bin"
   ##
 
+proc `+=`*[T](x, y: ordinal[T]) {.magic: "Inc", noSideEffect.}
+  ## Increments an ordinal
+
+proc `-=`*[T](x, y: ordinal[T]) {.magic: "Dec", noSideEffect.}
+  ## Decrements an ordinal
+
+proc `*=`*[T](x: var ordinal[T], y: ordinal[T]) {.inline noSideEffect.} =
+  ## Binary `*=` operator for oridinals
+  x = x * y
+
+proc `+=` *(x: var float, y:float) {.inline noSideEffect.} =
+  ## Increments in placee a floating point number
+  x = x + y
+
+proc `-=` *(x: var float, y:float) {.inline noSideEffect.} =
+  ## Decrements in place a floating point number
+  x = x - y
+
+proc `*=` *(x: var float, y:float) {.inline noSideEffect.} =
+  ## Multiplies in place a floating point number
+  x = x * y
+
+proc `/=` *(x: var float, y:float) {.inline noSideEffect.} =
+  ## Divides in place a floating point number
+  x = x / y
+
+proc `&=`* (x: var string, y: string) {.magic: "AppendStrStr", noSideEffect.}
+