Added better object printing and fixed some bugs with complex checks.

This commit is contained in:
Joey Payne 2015-07-10 21:11:45 -06:00
commit 221afbd2bc
2 changed files with 44 additions and 9 deletions

View file

@ -15,7 +15,7 @@
import macros import macros
import strutils import strutils
import tables import tables
import typetraits
when not defined(ECMAScript): when not defined(ECMAScript):
import terminal import terminal
@ -29,6 +29,9 @@ proc `$`*[T](ar: openarray[T]): string=
result &= "]" result &= "]"
return result return result
proc `$`*(s: ref object): string=
result = "ref " & ($s[]).replace(":ObjectType", "")
proc `==`*[T](ar: openarray[T], ar2: openarray[T]): bool= proc `==`*[T](ar: openarray[T], ar2: openarray[T]): bool=
if len(ar) != len(ar2): if len(ar) != len(ar2):
return false return false
@ -105,8 +108,8 @@ template checkRaises*(self: TestSuite, error: Exception,
try: try:
code code
var var
snip = astToStr(code).strip() snip = "$1, $2".format(astToStr(error), astToStr(code).strip())
vals = {snip: "No Exception Raised"}.toTable() vals = {astToStr(code).strip(): "No Exception Raised"}.toTable()
testName = self.currentTestName testName = self.currentTestName
returnException("checkRaises", testName, snip, vals, pos, posRel) returnException("checkRaises", testName, snip, vals, pos, posRel)
@ -117,8 +120,9 @@ template checkRaises*(self: TestSuite, error: Exception,
raise raise
except Exception: except Exception:
var var
snip = astToStr(code).strip() e = getCurrentException()
vals = {snip: "not equal to $1".format(astToStr(error))}.toTable() snip = "$1, $2".format(astToStr(error), astToStr(code).strip())
vals = {astToStr(code).strip(): e.name}.toTable()
testName = self.currentTestName testName = self.currentTestName
returnException("checkRaises", testName, snip, vals, pos, posRel) returnException("checkRaises", testName, snip, vals, pos, posRel)
@ -134,10 +138,10 @@ template recursive(node, action): expr {.dirty.} =
discard helper(node) discard helper(node)
template strRep(n: NimNode): untyped= template strRep(n: NimNode): untyped=
newNimNode(nnkPrefix).add(ident("$"), newNimNode(nnkPar).add(n)) $(n)
template tableEntry(n: NimNode): untyped= template tableEntry(n: NimNode): untyped=
newNimNode(nnkExprColonExpr).add(n.toStrLit(), strRep(n)) newNimNode(nnkExprColonExpr).add(n.toStrLit(), getAst(strRep(n)))
macro getSyms(code:untyped): untyped= macro getSyms(code:untyped): untyped=
var var
@ -184,7 +188,8 @@ template check*(self: TestSuite, code: untyped){.immediate.}=
testName = self.currentTestName testName = self.currentTestName
var vals = getSyms(code) var vals = getSyms(code)
snip = astToStr(code) # get ast string with extra spaces ignored
snip = astToStr(code).replace(" ", " ")
returnException("check", testName, snip, vals, pos, posRel) returnException("check", testName, snip, vals, pos, posRel)
@ -246,6 +251,7 @@ macro testSuite*(head: untyped, body: untyped): untyped =
template importRequiredLibs()= template importRequiredLibs()=
import strutils import strutils
import tables import tables
import typetraits
when not defined(ECMAScript): when not defined(ECMAScript):
import terminal import terminal
@ -405,7 +411,8 @@ macro testSuite*(head: untyped, body: untyped): untyped =
template setupDecl(self, baseMethod)= template setupDecl(self, baseMethod)=
method setup()= method setup()=
self.baseMethod() when compiles(self.baseMethod()):
self.baseMethod()
var setupProcTypename = ident("setup" & $typeName.toStrLit()) var setupProcTypename = ident("setup" & $typeName.toStrLit())
var baseMethodName = ident("setup" & $baseName.toStrLit()) var baseMethodName = ident("setup" & $baseName.toStrLit())

View file

@ -105,6 +105,34 @@ testSuite MoreInheritance of TestInherit:
method testNewObj()= method testNewObj()=
self.check(self.testObj == 12345) self.check(self.testObj == 12345)
proc doStuff(arg: int, arg2: string): string=
result = $arg & arg2
method testComplex()=
type
TestObj = ref object
t: int
Person = tuple[name: string, age: int]
proc `==`(d: TestObj, d2: TestObj): bool=
return d.t == d2.t
var
a = 5
s = "stuff"
y = 45
t: Person = (name: "Peter", age: 30)
r: Person = (name: "P", age: 3)
d = TestObj(t: 3)
k = TestObj(t: 30)
self.check(t != r)
self.check(d != k)
self.check(self.doStuff(a, s) == "5stuff" and self.doStuff(a, self.doStuff(a, self.doStuff(y, s))) == "something?")
when isMainModule: when isMainModule:
runTests() runTests()