From 04ffcaccfd517e347a8d5e456ea2c7fbdaedf91b Mon Sep 17 00:00:00 2001 From: Joey Payne Date: Sat, 18 Jun 2016 09:15:27 -0600 Subject: [PATCH] Fix proc definition style --- einheit.nim | 70 +++++++++++++++++++++++++++-------------------------- test.nim | 63 +++++++++++++++++++++++++++-------------------- 2 files changed, 73 insertions(+), 60 deletions(-) diff --git a/einheit.nim b/einheit.nim index c0a0981..4b65f88 100644 --- a/einheit.nim +++ b/einheit.nim @@ -8,7 +8,7 @@ ## .. code:: nim ## ## testSuite UnitTests: -## proc thisIsATest()= +## proc thisIsATest() = ## self.check(1 == 1) ## self.checkRaises(OSError, newException(OSError, "OS is exploding!")) ## @@ -19,9 +19,11 @@ import typetraits when not defined(ECMAScript): import terminal + + # ----------------- Helper Procs and Macros ----------------------------------- -proc `$`*[T](ar: openarray[T]): string= +proc `$`*[T](ar: openarray[T]): string = ## Converts an array into a string result = "[" if ar.len() > 0: @@ -74,11 +76,11 @@ macro toString*(obj: typed): untyped = let kind = obj.getType().typeKind case kind: of ntyTuple, ntyObject: - template toStrAst(obj): string= + template toStrAst(obj): string = einheit.objToStr(obj) result = getAst(toStrAst(obj)) else: - template toStrAst(obj): string= + template toStrAst(obj): string = $(obj) result = getAst(toStrAst(obj)) @@ -113,21 +115,21 @@ type # -- Methods for the TestSuite base -- -method setup*(suite: TestSuite)= +method setup*(suite: TestSuite) = ## Base method for setup code discard -method tearDown*(suite: TestSuite)= +method tearDown*(suite: TestSuite) = ## Base method for tearDown code discard -method runTests*(suite: TestSuite)= +method runTests*(suite: TestSuite) = ## Base method for running tests discard # ------------------------------------ -template returnException(name, testName, snip, vals, pos, posRel)= +template returnException(name, testName, snip, vals, pos, posRel) = ## private template for raising an exception var filename = posRel.filename @@ -195,13 +197,13 @@ template recursive(node, action): expr {.dirty.} = result.add helper(c) discard helper(node) -template strRep(n: NimNode): untyped= +template strRep(n: NimNode): untyped = toString(n) -template tableEntry(n: NimNode): untyped= +template tableEntry(n: NimNode): untyped = newNimNode(nnkExprColonExpr).add(n.toStrLit(), getAst(strRep(n))) -macro getSyms(code:untyped): untyped= +macro getSyms(code:untyped): untyped = ## This macro gets all symbols and values of an expression ## into a table ## @@ -238,7 +240,7 @@ macro getSyms(code:untyped): untyped= tableCall.add(tableConstr) result = tableCall else: - template emptyTable()= + template emptyTable() = initTable[string, string]() result = getAst(emptyTable()) @@ -296,11 +298,11 @@ macro testSuite*(head: untyped, body: untyped): untyped = ## var ## suiteVar: string ## - ## method setup()= + ## method setup() = ## ## do setup code here ## self.suiteVar = "Testing" ## - ## method testAddingString()= + ## method testAddingString() = ## ## adds a string to the suiteVar ## self.suiteVar &= " 123" ## self.check(self.suiteVar == "Testing 123") @@ -315,7 +317,7 @@ macro testSuite*(head: untyped, body: untyped): untyped = let objReference = "self" var exportClass: bool = false - template importRequiredLibs()= + template importRequiredLibs() = import strutils import tables import typetraits @@ -410,7 +412,7 @@ macro testSuite*(head: untyped, body: untyped): untyped = # return `baseName`(self) # result.add(super) - template setNodeName(n2, procName, typeName)= + template setNodeName(n2, procName, typeName) = if n2.name.kind == nnkIdent: procName = $(n2.name.toStrLit()) n2.name = ident(procName & typeName) @@ -427,12 +429,12 @@ macro testSuite*(head: untyped, body: untyped): untyped = result.add(n2) - template runTestsProc(self, typeName, baseMethod, typeMethod)= - method typeMethod(self: typeName)= + template runTestsProc(self, typeName, baseMethod, typeMethod) = + method typeMethod(self: typeName) = when compiles(self.baseMethod()): self.baseMethod() - method runTests(self: typeName)= + method runTests(self: typeName) = self.typeMethod() var baseMethodName = ident("runTests" & $baseName.toStrLit()) @@ -472,12 +474,12 @@ macro testSuite*(head: untyped, body: untyped): untyped = discard if not foundSetup: - template setupProc(self, typeName, setupProc)= + template setupProc(self, typeName, setupProc) = method setup(self: typeName) method setupProc(self: typeName) - template setupDecl(self, baseMethod)= - method setup()= + template setupDecl(self, baseMethod) = + method setup() = when compiles(self.baseMethod()): self.baseMethod() @@ -488,12 +490,12 @@ macro testSuite*(head: untyped, body: untyped): untyped = body.add(setupBaseAst[0]) if not foundTeardown: - template teardownProc(self, typeName, tdProc)= + template teardownProc(self, typeName, tdProc) = method tearDown(self: typeName) method tdProc(self: typeName) - template teardownDecl(self, baseMethod)= - method tearDown()= + template teardownDecl(self, baseMethod) = + method tearDown() = when compiles(self.baseMethod()): self.baseMethod() @@ -506,10 +508,10 @@ macro testSuite*(head: untyped, body: untyped): untyped = baseTearMethodName)) body.add(teardownBaseAst[0]) - template setTestName(self, procName)= + template setTestName(self, procName) = self.currentTestName = procName - template tryBlock(self, testCall)= + template tryBlock(self, testCall) = self.numTests += 1 try: testCall @@ -645,9 +647,9 @@ macro testSuite*(head: untyped, body: untyped): untyped = var typeDecl: NimNode - template declareTypeExport(tname, bname)= + template declareTypeExport(tname, bname) = type tname* = ref object of bname - template declareType(tname, bname)= + template declareType(tname, bname) = type tname = ref object of bname if baseName == nil: @@ -686,13 +688,13 @@ macro testSuite*(head: untyped, body: untyped): untyped = result.add(runTests) - template addTestSuite(typeName)= + template addTestSuite(typeName) = testSuites.add(typeName()) result.add(getAst(addTestSuite(typeName))) -proc printRunning(suite: TestSuite)= +proc printRunning(suite: TestSuite) = var numTicks = 80 - 12 - len(suite.name) ticks = "" @@ -710,7 +712,7 @@ proc printRunning(suite: TestSuite)= echo "\n[Running] $1 $2\n".format(suite.name, ticks) -proc printPassedTests(suite: TestSuite)= +proc printPassedTests(suite: TestSuite) = # Output red if tests didn't pass, green otherwise var color = fgGreen @@ -735,7 +737,7 @@ proc printPassedTests(suite: TestSuite)= else: echo "\n$1 tests passed for $2.$3\n".format(passedStr, suite.name, ticks) -proc printSummary(totalTestsPassed: int, totalTests: int)= +proc printSummary(totalTestsPassed: int, totalTests: int) = var summaryColor = fgGreen if totalTestsPassed != totalTests: @@ -761,7 +763,7 @@ proc printSummary(totalTestsPassed: int, totalTests: int)= echo "\n[Summary]" echo "\n $1 tests passed.".format(passedStr) -proc runTests*()= +proc runTests*() = ## The method that runs the tests. Invoke ## after setting up all of the tests and ## usually inside a "when isMainModule" block diff --git a/test.nim b/test.nim index 3ff6bfc..44b957b 100644 --- a/test.nim +++ b/test.nim @@ -1,32 +1,34 @@ import einheit + + testSuite UnitTests: var testObj: int testArray: array[4, int] - proc doThings()= + proc doThings() = # This proc won't be invoked as a test self.testObj = 400 self.check(self.testObj == 400) - method setup()= + method setup() = self.testObj = 90 for i in 0 ..< self.testArray.len(): self.testArray[i] = i - method tearDown()= + method tearDown() = self.testObj = 0 - method testForB()= + method testForB() = var b = 4 self.doThings() self.check(b == 4) - method testArrayAssert()= + method testArrayAssert() = self.check(self.testArray == [0,1,2]) - method testForC()= + method testForC() = var c = 0 # supposed to fail self.check(c == 1) @@ -36,37 +38,46 @@ testSuite UnitTestsNew: var testObj: int - method setup()= + method setup() = self.testObj = 90 - method tearDown()= + method tearDown() = self.testObj = 0 - method testTestObj()= + method testTestObj() = self.check(self.testObj == 90) - method testStuff()= + method testStuff() = self.check("Stuff" == "Stuff") proc returnTrue(): bool= result = false - method testMore()= + method testMore() = var more = 23 self.check(more == 1) - method testMoreMore()= + method testMoreMore() = self.check(self.returnTrue()) + method testValues() = + proc foo : int = + return 1 + + proc bar : int = + return 2 + let (a, b) = (123, 321) + self.check(a == b and foo() == bar()) + # Inheritance! testSuite TestInherit of UnitTestsNew: ## This will call every test defined in UnitTestsNew - proc raisesOs()= + proc raisesOs() = # This proc won't be invoked as a test raise newException(SystemError, "Oh no! OS malfunction!") - method testRaises()= + method testRaises() = # Two ways of checking self.checkRaises OSError: @@ -77,7 +88,7 @@ testSuite TestInherit of UnitTestsNew: testSuite MoreInheritance of TestInherit: - method setup()= + method setup() = # This must be called if overriding setup if you want # base class setup functionality. You can also call # self.setupTestInherit() to call the direct parent's @@ -88,12 +99,12 @@ testSuite MoreInheritance of TestInherit: # fail. This is expected. self.testObj = 12345 - method tearDown()= + method tearDown() = # Calling the direct parent's tearDown method self.tearDownTestInherit() self.testObj = 0 - method testTestObj()= + method testTestObj() = # This method is overwritten. To call the base method, # simply use # self.testTestObj_UnitTestsNew() @@ -102,13 +113,13 @@ testSuite MoreInheritance of TestInherit: # This one will pass, the other will fail self.check(self.testObj == 12345) - method testNewObj()= + method testNewObj() = self.check(self.testObj == 12345) - proc doStuff(arg: int, arg2: string): string= + proc doStuff(arg: int, arg2: string): string = result = $arg & arg2 - method testRefObject()= + method testRefObject() = type TestObj = ref object t: int @@ -117,12 +128,12 @@ testSuite MoreInheritance of TestInherit: d = TestObj(t: 3) k = TestObj(t: 30) - proc `==`(d: TestObj, d2: TestObj): bool= + proc `==`(d: TestObj, d2: TestObj): bool = result = d.t == d2.t self.check(d == k) - method testObject()= + method testObject() = type TestObj = object t: int @@ -131,13 +142,13 @@ testSuite MoreInheritance of TestInherit: d = TestObj(t: 3) k = TestObj(t: 30) - proc `==`(d: TestObj, d2: TestObj): bool= + proc `==`(d: TestObj, d2: TestObj): bool = result = d.t == d2.t self.check(d != k) self.check(d == k) - method testComplexObject()= + method testComplexObject() = type Obj1 = object e: string @@ -149,7 +160,7 @@ testSuite MoreInheritance of TestInherit: result = false self.check(x.isObj(p)) - method testTuple()= + method testTuple() = type Person = tuple[name: string, age: int] @@ -160,7 +171,7 @@ testSuite MoreInheritance of TestInherit: self.check(t != r) self.check(t == r) - method testComplex()= + method testComplex() = var a = 5 s = "stuff"