From b6f8ab3254dd393121d7840186d7954d286b458a Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Fri, 14 Sep 2012 00:53:30 +0300 Subject: [PATCH] [unittest] even deeper inspection of expressions and variables for the check macro --- lib/pure/unittest.nim | 116 +++++++++++++++------------------------- tests/compile/tseq2.nim | 2 - 2 files changed, 43 insertions(+), 75 deletions(-) diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index fff84ee29b..0233b8bac4 100644 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -93,88 +93,58 @@ template fail* = checkpoints = @[] macro check*(conditions: stmt): stmt {.immediate.} = - let conditions = callsite() + let checked = callsite()[1] - case conditions.kind - of nnkCall, nnkCommand, nnkMacroStmt: - case conditions[1].kind - of nnkInfix: - proc rewriteBinaryOp(op: PNimrodNode): PNimrodNode = - template rewrite(op, left, right, lineInfoLit: expr, opLit, - leftLit, rightLit: string, printLhs, printRhs: bool): stmt = - block: - var - lhs = left - rhs = right + var + argsAsgns = newNimNode(nnkStmtList) + argsPrintOuts = newNimNode(nnkStmtList) + counter = 0 - if not `op`(lhs, rhs): - checkpoint(lineInfoLit & ": Check failed: " & opLit) - when printLhs: checkpoint(" " & leftLit & " was " & $lhs) - when printRhs: checkpoint(" " & rightLit & " was " & $rhs) - fail() - - result = getAst(rewrite( - op[0], op[1], op[2], - op.lineinfo, - op.toStrLit, - op[1].toStrLit, - op[2].toStrLit, - op[1].kind notin nnkLiterals, - op[2].kind notin nnkLiterals)) - - result = rewriteBinaryOp(conditions[1]) + template asgn(a, value: expr): stmt = + let a = value - of nnkCall, nnkCommand: - proc rewriteCall(op: PNimrodNode): PNimrodNode = - template rewrite(call, lineInfoLit: expr, expLit: string, - argAssgs, argPrintOuts: stmt): stmt = - block: - argAssgs - if not call: - checkpoint(lineInfoLit & ": Check failed: " & expLit) - argPrintOuts - fail() + template print(name, value: expr): stmt = + when compiles(string($value)): + checkpoint(name & " was " & $value) - template asgn(a, value: expr): stmt = - let a = value - - template print(name, value: expr): stmt = - checkpoint(name & " was " & $value) + proc inspectArgs(exp: PNimrodNode) = + for i in 1 ..