Add better testing experience and fix compiler bugs #1

Merged
jyapayne merged 2 commits from wip into master 2019-11-05 16:39:55 +00:00
5 changed files with 53 additions and 17 deletions

View file

@ -1,5 +1,5 @@
import macros import macros
import tables, strutils, os, typetraits import tables, strutils, os, typetraits, strformat
export tables, strutils, os, typetraits export tables, strutils, os, typetraits
@ -244,7 +244,7 @@ template genCheckRaises(tname) =
let let
codeStr = astToStr(code).strip().split().join(" ") codeStr = astToStr(code).strip().split().join(" ")
snip = "$1, $2".format(astToStr(error), codeStr) 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) returnException("checkRaises", tname, snip, vals, pos, posRel)
except error: except error:
@ -256,7 +256,7 @@ template genCheckRaises(tname) =
e = getCurrentException() e = getCurrentException()
codeStr = astToStr(code).strip().split().join(" ") codeStr = astToStr(code).strip().split().join(" ")
snip = "$1, $2".format(astToStr(error), codeStr) snip = "$1, $2".format(astToStr(error), codeStr)
vals = {codeStr: $e.name}.toTable() vals = [(codeStr, $e.name)].toTable()
returnException("checkRaises", tname, snip, vals, pos, posRel) returnException("checkRaises", tname, snip, vals, pos, posRel)
@ -500,6 +500,11 @@ proc expandTests(currentDir, currentFile: string): NimNode {.used.} =
totalTests = bindSym("totalTests") totalTests = bindSym("totalTests")
let name = currentFile let name = currentFile
# if there are not tests in a module, ignore it
if not testsModuleMap.hasKey(name):
return newEmptyNode()
let let
testsModule = testsModuleMap[name] testsModule = testsModuleMap[name]
infoSym = genSym(nskLet, "testsInfo") infoSym = genSym(nskLet, "testsInfo")
@ -508,6 +513,9 @@ proc expandTests(currentDir, currentFile: string): NimNode {.used.} =
result.add(getAst(createInfo(infoSym, name, numTests, currentDir))) result.add(getAst(createInfo(infoSym, name, numTests, currentDir)))
when defined(debug):
echo fmt"Found tests in {name}"
for test in testsModule.tests: for test in testsModule.tests:
let let
body = test.procDef body = test.procDef
@ -553,12 +561,26 @@ macro runTests*(site: varargs[untyped]): untyped =
when defined(test): when defined(test):
let let
currentDir = getProjectPath() currentDir = getProjectPath()
currentFile = site.lineInfoObj.fileName currentFile = site.lineInfoObj.fileName.string
return expandTests(currentDir, currentFile) return expandTests(currentDir, currentFile)
macro runTestsMain*(site: varargs[untyped]): untyped = macro runTestsMain(cd, cf: string): untyped =
## This macro must be run in the main testing module that imports let
## all of the other test modules. 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`` ## After this is run, quitting with an error code of ``totalTests - totalPassedTests``
## is the default behavior. To change this, define ``noErrorCode`` in the compiler ## 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): when defined(test):
let let
currentDir = getProjectPath() currentDir = getProjectPath()
currentFile = site.lineInfoObj.fileName currentFile = site.lineInfoObj.fileName.string
expTests = expandTests(currentDir, currentFile)
template runAll(expTests) = let stmtList = newNimNode(nnkStmtList)
expTests
printSummary()
when not defined(noErrorCode):
quit(getResult())
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

View file

@ -1 +1,2 @@
-d:test
--path:"../src/" --path:"../src/"

3
tests/runner.nim Normal file
View file

@ -0,0 +1,3 @@
import tani
discoverAndRunTests()

6
tests/stuff/testham.nim Normal file
View file

@ -0,0 +1,6 @@
import tani
test "this is crazy":
echo "balls"
runTests()

View file

@ -1,4 +1,4 @@
import tani, test2 import tani
import terminal import terminal
import os import os
@ -20,4 +20,4 @@ proc y(): int =
return 10 return 10
runTestsMain() runTests()