From 74846335b5e691217dad904ceb4d9d81e2d8fe25 Mon Sep 17 00:00:00 2001 From: Joey Payne Date: Mon, 13 Jul 2015 19:03:38 -0600 Subject: [PATCH] Added recursive object printing. Bumped version up! --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++----- einheit.nim | 29 ++++++++++++++++++++++++++--- einheit.nimble | 2 +- 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d5cdb7e..99de902 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ is this: Condition: check(self.returnTrue()) Where: self.returnTrue() -> false + self -> ref UnitTestsNew(testObj: 90, name: UnitTestsNew, currentTestName: testMoreMore, testsPassed: 2, numTests: 4) Location: test.nim; line 59 @@ -153,12 +154,13 @@ is this: Condition: check(self.returnTrue()) Where: self.returnTrue() -> false + self -> ref UnitTestsNew(testObj: 90, name: TestInherit, currentTestName: testMoreMore, testsPassed: 2, numTests: 4) Location: test.nim; line 59 [Failed] testRaises Condition: checkRaises(OSError, self.raisesOs()) Where: - self.raisesOs() -> Exception + self.raisesOs() -> SystemError Location: test.nim; line 72 @@ -184,30 +186,66 @@ is this: Condition: check(self.returnTrue()) Where: self.returnTrue() -> false + self -> ref UnitTestsNew(testObj: 12345, name: MoreInheritance, currentTestName: testMoreMore, testsPassed: 1, numTests: 4) Location: test.nim; line 59 [Failed] testRaises Condition: checkRaises(OSError, self.raisesOs()) Where: - self.raisesOs() -> Exception + self.raisesOs() -> SystemError Location: test.nim; line 72 [OK] testTestObj [OK] testNewObj +[Failed] testRefObject + Condition: check(d == k) + Where: + k -> ref TestObj(t: 30) + d -> ref TestObj(t: 3) + Location: test.nim; line 123 + +[Failed] testObject + Condition: check(d == k) + Where: + k -> TestObj(t: 30) + d -> TestObj(t: 3) + Location: test.nim; line 138 + +[Failed] testComplexObject + Condition: check(x.isObj(p)) + Where: + x.isObj(p) -> false + p -> 4 + x -> Obj2(d: Obj1(e: Hey)) + Location: test.nim; line 150 + +[Failed] testTuple + Condition: check(t == r) + Where: + r -> tuple Person(name: P, age: 3) + t -> tuple Person(name: Peter, age: 30) + Location: test.nim; line 161 + [Failed] testComplex - Condition: check(self.doStuff(a, s) == "5stuff" and self.doStuff(a, self.doStuff(a, self.doStuff(y, s))) == "something?") + Condition: check(self.doStuff(a, s) == "5stuff" and self.doStuff(a, self.doStuff(a, self.doStuff(y, s))) == "something?") Where: self.doStuff(a, s) -> 5stuff a -> 5 + self -> ref MoreInheritance(testObj: 12345, name: MoreInheritance, currentTestName: testComplex, testsPassed: 3, numTests: 12) self.doStuff(a, self.doStuff(a, self.doStuff(y, s))) -> 5545stuff y -> 45 s -> stuff self.doStuff(y, s) -> 45stuff self.doStuff(a, self.doStuff(y, s)) -> 545stuff - Location: test.nim; line 134 + Location: test.nim; line 169 -[3/8] tests passed for MoreInheritance. ---------------------------------------- +[3/12] tests passed for MoreInheritance. --------------------------------------- + + +[Summary] + + [8/24] tests passed. ``` Notice that on failure, the test runner gives some useful information about the test in question. This is useful for determining why the test failed. diff --git a/einheit.nim b/einheit.nim index 614de61..b86e6d1 100644 --- a/einheit.nim +++ b/einheit.nim @@ -35,11 +35,34 @@ proc `$`*[T](some:typedesc[T]): string = name(T) proc `$`*(s: ref object): string = result = ($type(s)).replace(":ObjectType", "") & $s[] -proc objToStr*[T: object](obj: T): string = - result = $type(obj) & system.`$`(obj) +template tupleObjToStr(obj): string {.dirty.} = + var res = $type(obj) + template helper(n) {.gensym.} = + res.add("(") + var firstElement = true + for name, value in n.fieldPairs(): + if not firstElement: + res.add(", ") + res.add(name) + res.add(": ") + when (value is object or value is tuple): + when (value is tuple): + res.add("tuple " & $type(value)) + else: + res.add($type(value)) + helper(value) + else: + res.add($value) + firstElement = false + res.add(")") + helper(obj) + res + +proc objToStr*[T: object](obj: var T): string = + tupleObjToStr(obj) proc objToStr*[T: tuple](obj: T): string = - result = "tuple " & $type(obj) & system.`$`(obj) + result = "tuple " & tupleObjToStr(obj) macro toString*(obj: typed): untyped = ## this macro is to work around not being diff --git a/einheit.nimble b/einheit.nimble index 0da4959..5b655cd 100644 --- a/einheit.nimble +++ b/einheit.nimble @@ -1,6 +1,6 @@ [Package] name = "einheit" -version = "0.1.3" +version = "0.1.4" author = "Joey Payne" description = "Tool for providing unit tests. Einheit is German for Unit." license = "MIT"