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
This commit is contained in:
parent
e88123fb17
commit
f9876d379d
4 changed files with 26 additions and 5 deletions
|
|
@ -90,6 +90,11 @@ type
|
||||||
PNimrodNode* = expr
|
PNimrodNode* = expr
|
||||||
## represents a Nimrod AST node. Macros operate on this type.
|
## 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!
|
# 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
|
# 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
|
# its father. How to do this without back references? Hm, BS, it works without
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,8 @@ macro check*(conditions: stmt): stmt =
|
||||||
case conditions[1].kind
|
case conditions[1].kind
|
||||||
of nnkInfix:
|
of nnkInfix:
|
||||||
proc rewriteBinaryOp(op: expr): stmt =
|
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:
|
block:
|
||||||
var
|
var
|
||||||
lhs = left
|
lhs = left
|
||||||
|
|
@ -114,8 +115,8 @@ macro check*(conditions: stmt): stmt =
|
||||||
|
|
||||||
if not `op`(lhs, rhs):
|
if not `op`(lhs, rhs):
|
||||||
checkpoint(lineInfoLit & ": Check failed: " & opLit)
|
checkpoint(lineInfoLit & ": Check failed: " & opLit)
|
||||||
checkpoint(" " & leftLit & " was " & $lhs)
|
when printLhs: checkpoint(" " & leftLit & " was " & $lhs)
|
||||||
checkpoint(" " & rightLit & " was " & $rhs)
|
when printRhs: checkpoint(" " & rightLit & " was " & $rhs)
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
result = getAst(rewrite(
|
result = getAst(rewrite(
|
||||||
|
|
@ -123,7 +124,9 @@ macro check*(conditions: stmt): stmt =
|
||||||
op.lineinfo,
|
op.lineinfo,
|
||||||
op.toStrLit,
|
op.toStrLit,
|
||||||
op[1].toStrLit,
|
op[1].toStrLit,
|
||||||
op[2].toStrLit))
|
op[2].toStrLit,
|
||||||
|
op[1].kind notin nnkLiterals,
|
||||||
|
op[2].kind notin nnkLiterals))
|
||||||
|
|
||||||
result = rewriteBinaryOp(conditions[1])
|
result = rewriteBinaryOp(conditions[1])
|
||||||
|
|
||||||
|
|
|
||||||
2
tests/run/tunittests.nim
Normal file
2
tests/run/tunittests.nim
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
import uclosures
|
||||||
|
|
||||||
11
tests/run/uclosures.nim
Normal file
11
tests/run/uclosures.nim
Normal file
|
|
@ -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
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue