summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorfenekku <fenekku@fenekku.com>2015-08-12 08:26:40 -0400
committerfenekku <fenekku@fenekku.com>2015-08-12 08:26:40 -0400
commit58b83815018fce2ec614b4a3624e5828a2f7db8f (patch)
treec27fc604f1379b846f431b64d71eb3f4cf776306
parentd230b75e0a7cd49cf5c4e4f687515c296cd2ce5b (diff)
downloadNim-58b83815018fce2ec614b4a3624e5828a2f7db8f.tar.gz
failed unittest check causes identifiers and calls to be printed out
-rwxr-xr-x[-rw-r--r--]lib/pure/unittest.nim27
1 files changed, 17 insertions, 10 deletions
diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim
index 064937ad8..3f36ed5a7 100644..100755
--- a/lib/pure/unittest.nim
+++ b/lib/pure/unittest.nim
@@ -92,8 +92,8 @@ template suite*(name: expr, body: stmt): stmt {.immediate, dirty.} =
   ##    test "2 + 2 = 4":
   ##      check(2+2 == result)
   ##
-  ##    test "2 + -2 != 4":
-  ##      check(2+2 != result)
+  ##    test "(2 + -2) != 4":
+  ##      check(2 + -2 != result)
   ##
   ##    # No teardown needed
   ##
@@ -234,41 +234,48 @@ macro check*(conditions: stmt): stmt {.immediate.} =
     when compiles(string($value)):
       checkpoint(name & " was " & $value)
 
-  proc inspectArgs(exp: NimNode) =
+  proc inspectArgs(exp: NimNode): NimNode =
+    result = copyNimTree(exp)
     for i in countup(1, exp.len - 1):
       if exp[i].kind notin nnkLiterals:
         inc counter
         var arg = newIdentNode(":p" & $counter)
         var argStr = exp[i].toStrLit
         var paramAst = exp[i]
-        if exp[i].kind in nnkCallKinds: inspectArgs(exp[i])
+        if exp[i].kind == nnkIdent:
+          argsPrintOuts.add getAst(print(argStr, paramAst))
+        if exp[i].kind in nnkCallKinds:
+          var callVar = newIdentNode(":c" & $counter)
+          argsAsgns.add getAst(asgn(callVar, paramAst))
+          result[i] = callVar
+          argsPrintOuts.add getAst(print(argStr, callVar))
         if exp[i].kind == nnkExprEqExpr:
           # ExprEqExpr
           #   Ident !"v"
           #   IntLit 2
-          paramAst = exp[i][1]
+          result[i] = exp[i][1]
         if exp[i].typekind notin {ntyTypeDesc}:
           argsAsgns.add getAst(asgn(arg, paramAst))
           argsPrintOuts.add getAst(print(argStr, arg))
           if exp[i].kind != nnkExprEqExpr:
-            exp[i] = arg
+            result[i] = arg
           else:
-            exp[i][1] = arg
+            result[i][1] = arg
 
   case checked.kind
   of nnkCallKinds:
     template rewrite(call, lineInfoLit: expr, callLit: string,
                      argAssgs, argPrintOuts: stmt): stmt =
       block:
-        argAssgs
+        argAssgs #all callables (and assignments) are run here
         if not call:
           checkpoint(lineInfoLit & ": Check failed: " & callLit)
           argPrintOuts
           fail()
 
     var checkedStr = checked.toStrLit
-    inspectArgs(checked)
-    result = getAst(rewrite(checked, checked.lineinfo, checkedStr,
+    let parameterizedCheck = inspectArgs(checked)
+    result = getAst(rewrite(parameterizedCheck, checked.lineinfo, checkedStr,
                             argsAsgns, argsPrintOuts))
 
   of nnkStmtList: