From bc751345300d6a9c75824668ea63255c1e000fec Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Mon, 4 Nov 2019 14:07:19 -0700 Subject: [PATCH 1/2] Fix issues for compiler bugs #12595 and #12594 --- src/tani.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tani.nim b/src/tani.nim index c455d1a..47aec30 100644 --- a/src/tani.nim +++ b/src/tani.nim @@ -244,7 +244,7 @@ template genCheckRaises(tname) = let codeStr = astToStr(code).strip().split().join(" ") snip = "$1, $2".format(astToStr(error), codeStr) - vals = {codeStr: "No Exception Raised"}.toTable() + vals = [(codeStr, "No Exception Raised")].toTable() returnException("checkRaises", tname, snip, vals, pos, posRel) except error: @@ -256,7 +256,7 @@ template genCheckRaises(tname) = e = getCurrentException() codeStr = astToStr(code).strip().split().join(" ") snip = "$1, $2".format(astToStr(error), codeStr) - vals = {codeStr: $e.name}.toTable() + vals = [(codeStr, $e.name)].toTable() returnException("checkRaises", tname, snip, vals, pos, posRel) @@ -553,7 +553,7 @@ macro runTests*(site: varargs[untyped]): untyped = when defined(test): let currentDir = getProjectPath() - currentFile = site.lineInfoObj.fileName + currentFile = site.lineInfoObj.fileName.string return expandTests(currentDir, currentFile) macro runTestsMain*(site: varargs[untyped]): untyped = @@ -567,7 +567,7 @@ macro runTestsMain*(site: varargs[untyped]): untyped = when defined(test): let currentDir = getProjectPath() - currentFile = site.lineInfoObj.fileName + currentFile = site.lineInfoObj.fileName.string expTests = expandTests(currentDir, currentFile) template runAll(expTests) = -- 2.49.1 From 502ca5329be5de79641e0eac866688e8fc5f4dab Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Mon, 4 Nov 2019 15:03:28 -0700 Subject: [PATCH 2/2] Add test discovery for easy test writing --- src/tani.nim | 48 +++++++++++++++++++++++++++++++---------- tests/nim.cfg | 1 + tests/runner.nim | 3 +++ tests/stuff/testham.nim | 6 ++++++ tests/test.nim | 4 ++-- 5 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 tests/runner.nim create mode 100644 tests/stuff/testham.nim diff --git a/src/tani.nim b/src/tani.nim index 47aec30..5cabe45 100644 --- a/src/tani.nim +++ b/src/tani.nim @@ -1,5 +1,5 @@ import macros -import tables, strutils, os, typetraits +import tables, strutils, os, typetraits, strformat export tables, strutils, os, typetraits @@ -500,6 +500,11 @@ proc expandTests(currentDir, currentFile: string): NimNode {.used.} = totalTests = bindSym("totalTests") let name = currentFile + + # if there are not tests in a module, ignore it + if not testsModuleMap.hasKey(name): + return newEmptyNode() + let testsModule = testsModuleMap[name] infoSym = genSym(nskLet, "testsInfo") @@ -508,6 +513,9 @@ proc expandTests(currentDir, currentFile: string): NimNode {.used.} = result.add(getAst(createInfo(infoSym, name, numTests, currentDir))) + when defined(debug): + echo fmt"Found tests in {name}" + for test in testsModule.tests: let body = test.procDef @@ -556,9 +564,23 @@ macro runTests*(site: varargs[untyped]): untyped = currentFile = site.lineInfoObj.fileName.string return expandTests(currentDir, currentFile) -macro runTestsMain*(site: varargs[untyped]): untyped = - ## This macro must be run in the main testing module that imports - ## all of the other test modules. +macro runTestsMain(cd, cf: string): untyped = + let + currentDir = cd.strVal + currentFile = cf.strVal + expTests = expandTests(currentDir, currentFile) + + template runAll(expTests) = + expTests + printSummary() + echo "" + when not defined(noErrorCode): + quit(getResult()) + + return getAst(runAll(expTests)) + +macro discoverAndRunTests*(site: varargs[untyped]): untyped = + ## This macro must be run in a file that is in the directory containing all the tests ## ## After this is run, quitting with an error code of ``totalTests - totalPassedTests`` ## is the default behavior. To change this, define ``noErrorCode`` in the compiler @@ -568,12 +590,16 @@ macro runTestsMain*(site: varargs[untyped]): untyped = let currentDir = getProjectPath() currentFile = site.lineInfoObj.fileName.string - expTests = expandTests(currentDir, currentFile) - template runAll(expTests) = - expTests - printSummary() - when not defined(noErrorCode): - quit(getResult()) + let stmtList = newNimNode(nnkStmtList) - return getAst(runAll(expTests)) + for fname in os.walkDirRec(currentDir, relative=true): + if fname.splitFile().name.startsWith("test") and fname.splitFile().ext == ".nim": + if (currentDir / fname) != currentFile: + let moduleName = newLit(currentDir / fname.replace(".nim", "")) + let shortName = ident(fname.splitFile.name) + # Add the export here to avoid the complaining about unused imports + stmtList.add(quote do: import `moduleName`; export `shortName`) + + stmtList.add(quote do: runTestsMain(`currentDir`, `currentFile`)) + result = stmtList diff --git a/tests/nim.cfg b/tests/nim.cfg index 85bf6c4..2caff8c 100644 --- a/tests/nim.cfg +++ b/tests/nim.cfg @@ -1 +1,2 @@ +-d:test --path:"../src/" diff --git a/tests/runner.nim b/tests/runner.nim new file mode 100644 index 0000000..287822a --- /dev/null +++ b/tests/runner.nim @@ -0,0 +1,3 @@ +import tani + +discoverAndRunTests() diff --git a/tests/stuff/testham.nim b/tests/stuff/testham.nim new file mode 100644 index 0000000..fe7d35b --- /dev/null +++ b/tests/stuff/testham.nim @@ -0,0 +1,6 @@ +import tani + +test "this is crazy": + echo "balls" + +runTests() diff --git a/tests/test.nim b/tests/test.nim index ec682d2..f9ea948 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -1,4 +1,4 @@ -import tani, test2 +import tani import terminal import os @@ -20,4 +20,4 @@ proc y(): int = return 10 -runTestsMain() +runTests() -- 2.49.1