diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2015-08-21 21:30:10 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2015-08-21 21:30:10 +0200 |
commit | 47919e219bc97600d5918b6e3626cbd7ae093180 (patch) | |
tree | 870ee060a2ceff8201e6d99e8114b288ea2f40a9 | |
parent | abccaa15d8888ea2657c9b82319a80a20cf63820 (diff) | |
parent | c62698b2969f67cdfdcf18c6416bdba9e5b50197 (diff) | |
download | Nim-47919e219bc97600d5918b6e3626cbd7ae093180.tar.gz |
Merge pull request #3205 from fenekku/unittest-work-2
other unittest pull-request
-rwxr-xr-x[-rw-r--r--] | lib/pure/unittest.nim | 36 | ||||
-rw-r--r-- | tests/stdlib/tunittest.nim | 45 |
2 files changed, 66 insertions, 15 deletions
diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index 064937ad8..a0f7b955e 100644..100755 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -70,9 +70,6 @@ var ## Global unittest settings! checkpoints = @[] -template testSetupIMPL*: stmt {.immediate, dirty.} = discard #Should this be public or even exist? -template testTeardownIMPL*: stmt {.immediate, dirty.} = discard - proc shouldRun(testName: string): bool = result = true @@ -92,8 +89,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 ## @@ -106,9 +103,11 @@ template suite*(name: expr, body: stmt): stmt {.immediate, dirty.} = ## [OK] (2 + -2) != 4 block: template setup(setupBody: stmt): stmt {.immediate, dirty.} = + var testSetupIMPLFlag = true template testSetupIMPL: stmt {.immediate, dirty.} = setupBody template teardown(teardownBody: stmt): stmt {.immediate, dirty.} = + var testTeardownIMPLFlag = true template testTeardownIMPL: stmt {.immediate, dirty.} = teardownBody body @@ -149,7 +148,7 @@ template test*(name: expr, body: stmt): stmt {.immediate, dirty.} = var testStatusIMPL {.inject.} = OK try: - testSetupIMPL() + when declared(testSetupIMPLFlag): testSetupIMPL() body except: @@ -159,7 +158,7 @@ template test*(name: expr, body: stmt): stmt {.immediate, dirty.} = fail() finally: - testTeardownIMPL() + when declared(testTeardownIMPLFlag): testTeardownIMPL() testDone name, testStatusIMPL proc checkpoint*(msg: string) = @@ -234,41 +233,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: diff --git a/tests/stdlib/tunittest.nim b/tests/stdlib/tunittest.nim index 1389214ea..4d2a2a340 100644 --- a/tests/stdlib/tunittest.nim +++ b/tests/stdlib/tunittest.nim @@ -38,3 +38,48 @@ proc defectiveRobot() = test "unittest expect": expect IOError, OSError, ValueError, AssertionError: defectiveRobot() + +var + a = 1 + b = -1 + c = 1 + +#unittests are sequential right now +suite "suite with only teardown": + teardown: + b = 2 + + test "unittest with only teardown 1": + check a == c + + test "unittest with only teardown 2": + check b > a + +suite "suite with only setup": + setup: + var testVar = "from setup" + + test "unittest with only setup 1": + check testVar == "from setup" + check b > a + b = -1 + + test "unittest with only setup 2": + check b < a + +suite "suite with none": + test "unittest with none": + check b < a + +suite "suite with both": + setup: + a = -2 + + teardown: + c = 2 + + test "unittest with both 1": + check b > a + + test "unittest with both 2": + check c == 2 |