Skip to content

Commit

Permalink
Merge #178
Browse files Browse the repository at this point in the history
178: minor refactor of unittest implementation r=saem a=krux02

generally speaking unittesting is very important, but the unittest
modules causes nothing but problems. The problem is, unittest pretty
much uses all problematic metaprogramming APIs at the same time. This
should be changed, step by step.

 * `template print(a,b: untyped)` -> `proc print[T](name: string, value: T)`
 * `newIdentNode(":c" & $counter)` -> `genSym`
 * `template assign` -> `newVarStmt`



Co-authored-by: Arne Döring <a.doering@digiblue.de>
  • Loading branch information
bors[bot] and Arne Döring authored Jan 20, 2022
2 parents 66f04ef + 62dbaf6 commit a6fed7b
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions lib/pure/unittest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,13 @@ template skip* =
testStatusIMPL = TestStatus.SKIPPED
checkpoints = @[]

proc print[T: not typedesc](name: string, value: T) =
when compiles(string($value)):
checkpoint(name & " was " & $value)

proc print[T](name: string, typ: typedesc[T]) =
checkpoint(name & " was " & $typ)

macro check*(conditions: untyped): untyped =
## Verify if a statement or a list of statements is true.
## A helpful error message and set checkpoints are printed out on
Expand All @@ -649,47 +656,36 @@ macro check*(conditions: untyped): untyped =

let checked = callsite()[1]

template asgn(a: untyped, value: typed) =
var a = value # XXX: we need "var: var" here in order to
# preserve the semantics of var params

template print(name: untyped, value: typed) =
when compiles(string($value)):
checkpoint(name & " was " & $value)

proc inspectArgs(exp: NimNode): tuple[assigns, check, printOuts: NimNode] =
result.check = copyNimTree(exp)
result.assigns = newNimNode(nnkStmtList)
result.printOuts = newNimNode(nnkStmtList)

var counter = 0

if exp[0].kind in {nnkIdent, nnkOpenSymChoice, nnkClosedSymChoice, nnkSym} and
$exp[0] in ["not", "in", "notin", "==", "<=",
">=", "<", ">", "!=", "is", "isnot"]:

for i in 1 ..< exp.len:
if exp[i].kind notin nnkLiterals:
inc counter
let argStr = exp[i].toStrLit
let paramAst = exp[i]
if exp[i].kind == nnkIdent:
result.printOuts.add getAst(print(argStr, paramAst))
result.printOuts.add newCall(bindSym"print", argStr, paramAst)
if exp[i].kind in nnkCallKinds + {nnkDotExpr, nnkBracketExpr, nnkPar} and
(exp[i].typeKind notin {ntyTypeDesc} or $exp[0] notin ["is", "isnot"]):
let callVar = newIdentNode(":c" & $counter)
result.assigns.add getAst(asgn(callVar, paramAst))
let callVar = genSym(nskVar, "c")
result.assigns.add newVarStmt(callVar, paramAst)
result.check[i] = callVar
result.printOuts.add getAst(print(argStr, callVar))
result.printOuts.add newCall(bindSym"print", argStr, callVar)
if exp[i].kind == nnkExprEqExpr:
# ExprEqExpr
# Ident "v"
# IntLit 2
result.check[i] = exp[i][1]
if exp[i].typeKind notin {ntyTypeDesc}:
let arg = newIdentNode(":p" & $counter)
result.assigns.add getAst(asgn(arg, paramAst))
result.printOuts.add getAst(print(argStr, arg))
let arg = genSym(nskVar, "p")
result.assigns.add newVarStmt(arg, paramAst)
result.printOuts.add newCall(bindSym"print", argStr, arg)
if exp[i].kind != nnkExprEqExpr:
result.check[i] = arg
else:
Expand Down

0 comments on commit a6fed7b

Please sign in to comment.