diff --git a/src/tani.nim b/src/tani.nim index c455d1a..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 @@ -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) @@ -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 @@ -553,12 +561,26 @@ 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 = - ## 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 @@ -567,13 +589,17 @@ macro runTestsMain*(site: varargs[untyped]): untyped = when defined(test): let currentDir = getProjectPath() - currentFile = site.lineInfoObj.fileName - expTests = expandTests(currentDir, currentFile) + currentFile = site.lineInfoObj.fileName.string - 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()