diff --git a/einheit.nim b/einheit.nim index 5309501..638d3ed 100644 --- a/einheit.nim +++ b/einheit.nim @@ -19,6 +19,23 @@ import strutils when not defined(ECMAScript): import terminal +proc `$`*[T](ar: openarray[T]): string= + ## Converts an array into a string + result = "[" + if ar.len() > 0: + result &= $ar[0] + for i in 1..ar.len()-1: + result &= ", " & $ar[i] + result &= "]" + return result + +proc `==`*[T](ar: openarray[T], ar2: openarray[T]): bool= + if len(ar) != len(ar2): + return false + for i in countup(0, ar.len()): + if ar[i] != ar2[i]: + return false + return true type TestSuite = ref object of RootObj @@ -136,6 +153,9 @@ template assertRaises*(self: TestSuite, error: Exception, discard +template assert*(self: TestSuite, d: untyped, msg=""){.immediate.}= + assertTrue(self, d) + # ----------------------------------------------------------------------------- @@ -183,6 +203,8 @@ macro testSuite*(head: untyped, body: untyped): untyped = ## ## when isMainModule: ## einheit.runTests() + ## + # object reference name inside methods. # ie: self, self diff --git a/test.nim b/test.nim index 760933c..8928f26 100644 --- a/test.nim +++ b/test.nim @@ -3,6 +3,7 @@ import einheit testSuite UnitTests: var testObj: int + testArray: array[4, int] proc doThings()= # This proc won't be invoked as a test @@ -11,6 +12,8 @@ testSuite UnitTests: method setup()= self.testObj = 90 + for i in 0 ..< self.testArray.len(): + self.testArray[i] = i method tearDown()= self.testObj = 0 @@ -20,6 +23,9 @@ testSuite UnitTests: self.doThings() self.assertTrue(4 == 4) + method testArrayAssert()= + self.assertTrue(self.testArray == [0,1,2]) + method testForC()= var c = 0 # supposed to fail @@ -44,6 +50,7 @@ testSuite UnitTestsNew: method testMore()= self.assertEqual("String", "String") + self.assert 0 == 1 method testMoreMore()= self.assertFalse("String" != "String")