summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2012-03-10 17:26:34 +0200
committerZahary Karadjov <zahary@gmail.com>2012-03-10 17:26:34 +0200
commitf9876d379dab388e8ef706682a29dae9aeb9c40e (patch)
treee6c724a82afd4f3551f6feb9ab0033baa3c0ac9a
parente88123fb17141ce6134411269c71bdbf8cba46d3 (diff)
downloadNim-f9876d379dab388e8ef706682a29dae9aeb9c40e.tar.gz
unit test for #100
unittest: the check macro will print only the non-literal part of the checked expression
tests/run: added tunittests.nim as a single central executable where unittests could be added for quicker compilation/execution of the test suite
-rwxr-xr-xlib/core/macros.nim7
-rw-r--r--lib/pure/unittest.nim11
-rw-r--r--tests/run/tunittests.nim2
-rw-r--r--tests/run/uclosures.nim11
4 files changed, 26 insertions, 5 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index 8712761b4..19869c8eb 100755
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -89,7 +89,12 @@ type
   

   PNimrodNode* = expr

     ## represents a Nimrod AST node. Macros operate on this type.

-    

+

+const

+  nnkLiterals* = {nnkCharLit..nnkNilLit}

+  nnkCallKinds* = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand,

+                      nnkCallStrLit}

+

 # Nodes should be reference counted to make the `copy` operation very fast!

 # However, this is difficult to achieve: modify(n[0][1]) should propagate to

 # its father. How to do this without back references? Hm, BS, it works without 

diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim
index d7abb8cfa..7aca9929e 100644
--- a/lib/pure/unittest.nim
+++ b/lib/pure/unittest.nim
@@ -106,7 +106,8 @@ macro check*(conditions: stmt): stmt =
     case conditions[1].kind

     of nnkInfix:

       proc rewriteBinaryOp(op: expr): stmt =

-        template rewrite(op, left, right, lineInfoLit: expr, opLit, leftLit, rightLit: string): stmt =

+        template rewrite(op, left, right, lineInfoLit: expr, opLit,

+          leftLit, rightLit: string, printLhs, printRhs: bool): stmt =

           block:

             var 

               lhs = left

@@ -114,8 +115,8 @@ macro check*(conditions: stmt): stmt =
 

             if not `op`(lhs, rhs):

               checkpoint(lineInfoLit & ": Check failed: " & opLit)

-              checkpoint("  " & leftLit & " was " & $lhs)

-              checkpoint("  " & rightLit & " was " & $rhs)

+              when printLhs: checkpoint("  " & leftLit & " was " & $lhs)

+              when printRhs: checkpoint("  " & rightLit & " was " & $rhs)

               fail()

 

         result = getAst(rewrite(

@@ -123,7 +124,9 @@ macro check*(conditions: stmt): stmt =
           op.lineinfo,

           op.toStrLit,

           op[1].toStrLit,

-          op[2].toStrLit))

+          op[2].toStrLit,

+          op[1].kind notin nnkLiterals,

+          op[2].kind notin nnkLiterals))

         

       result = rewriteBinaryOp(conditions[1])

   

diff --git a/tests/run/tunittests.nim b/tests/run/tunittests.nim
new file mode 100644
index 000000000..fa7fe5075
--- /dev/null
+++ b/tests/run/tunittests.nim
@@ -0,0 +1,2 @@
+import uclosures
+
diff --git a/tests/run/uclosures.nim b/tests/run/uclosures.nim
new file mode 100644
index 000000000..d54b88285
--- /dev/null
+++ b/tests/run/uclosures.nim
@@ -0,0 +1,11 @@
+import unittest
+
+test "loop variables are captured by copy":
+  var funcs: seq[proc (): int {.closure.}] = @[]
+  
+  for i in 0..10:
+    funcs.add do -> int: return i * i
+
+  check funcs[0]() == 0
+  check funcs[3]() == 9
+