Fix proc definition style

This commit is contained in:
Joey Payne 2016-06-18 09:15:27 -06:00
commit 04ffcaccfd
2 changed files with 73 additions and 60 deletions

View file

@ -8,7 +8,7 @@
## .. code:: nim ## .. code:: nim
## ##
## testSuite UnitTests: ## testSuite UnitTests:
## proc thisIsATest()= ## proc thisIsATest() =
## self.check(1 == 1) ## self.check(1 == 1)
## self.checkRaises(OSError, newException(OSError, "OS is exploding!")) ## self.checkRaises(OSError, newException(OSError, "OS is exploding!"))
## ##
@ -19,9 +19,11 @@ import typetraits
when not defined(ECMAScript): when not defined(ECMAScript):
import terminal import terminal
# ----------------- Helper Procs and Macros ----------------------------------- # ----------------- Helper Procs and Macros -----------------------------------
proc `$`*[T](ar: openarray[T]): string= proc `$`*[T](ar: openarray[T]): string =
## Converts an array into a string ## Converts an array into a string
result = "[" result = "["
if ar.len() > 0: if ar.len() > 0:
@ -74,11 +76,11 @@ macro toString*(obj: typed): untyped =
let kind = obj.getType().typeKind let kind = obj.getType().typeKind
case kind: case kind:
of ntyTuple, ntyObject: of ntyTuple, ntyObject:
template toStrAst(obj): string= template toStrAst(obj): string =
einheit.objToStr(obj) einheit.objToStr(obj)
result = getAst(toStrAst(obj)) result = getAst(toStrAst(obj))
else: else:
template toStrAst(obj): string= template toStrAst(obj): string =
$(obj) $(obj)
result = getAst(toStrAst(obj)) result = getAst(toStrAst(obj))
@ -113,21 +115,21 @@ type
# -- Methods for the TestSuite base -- # -- Methods for the TestSuite base --
method setup*(suite: TestSuite)= method setup*(suite: TestSuite) =
## Base method for setup code ## Base method for setup code
discard discard
method tearDown*(suite: TestSuite)= method tearDown*(suite: TestSuite) =
## Base method for tearDown code ## Base method for tearDown code
discard discard
method runTests*(suite: TestSuite)= method runTests*(suite: TestSuite) =
## Base method for running tests ## Base method for running tests
discard discard
# ------------------------------------ # ------------------------------------
template returnException(name, testName, snip, vals, pos, posRel)= template returnException(name, testName, snip, vals, pos, posRel) =
## private template for raising an exception ## private template for raising an exception
var var
filename = posRel.filename filename = posRel.filename
@ -195,13 +197,13 @@ template recursive(node, action): expr {.dirty.} =
result.add helper(c) result.add helper(c)
discard helper(node) discard helper(node)
template strRep(n: NimNode): untyped= template strRep(n: NimNode): untyped =
toString(n) toString(n)
template tableEntry(n: NimNode): untyped= template tableEntry(n: NimNode): untyped =
newNimNode(nnkExprColonExpr).add(n.toStrLit(), getAst(strRep(n))) 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 ## This macro gets all symbols and values of an expression
## into a table ## into a table
## ##
@ -238,7 +240,7 @@ macro getSyms(code:untyped): untyped=
tableCall.add(tableConstr) tableCall.add(tableConstr)
result = tableCall result = tableCall
else: else:
template emptyTable()= template emptyTable() =
initTable[string, string]() initTable[string, string]()
result = getAst(emptyTable()) result = getAst(emptyTable())
@ -296,11 +298,11 @@ macro testSuite*(head: untyped, body: untyped): untyped =
## var ## var
## suiteVar: string ## suiteVar: string
## ##
## method setup()= ## method setup() =
## ## do setup code here ## ## do setup code here
## self.suiteVar = "Testing" ## self.suiteVar = "Testing"
## ##
## method testAddingString()= ## method testAddingString() =
## ## adds a string to the suiteVar ## ## adds a string to the suiteVar
## self.suiteVar &= " 123" ## self.suiteVar &= " 123"
## self.check(self.suiteVar == "Testing 123") ## self.check(self.suiteVar == "Testing 123")
@ -315,7 +317,7 @@ macro testSuite*(head: untyped, body: untyped): untyped =
let objReference = "self" let objReference = "self"
var exportClass: bool = false var exportClass: bool = false
template importRequiredLibs()= template importRequiredLibs() =
import strutils import strutils
import tables import tables
import typetraits import typetraits
@ -410,7 +412,7 @@ macro testSuite*(head: untyped, body: untyped): untyped =
# return `baseName`(self) # return `baseName`(self)
# result.add(super) # result.add(super)
template setNodeName(n2, procName, typeName)= template setNodeName(n2, procName, typeName) =
if n2.name.kind == nnkIdent: if n2.name.kind == nnkIdent:
procName = $(n2.name.toStrLit()) procName = $(n2.name.toStrLit())
n2.name = ident(procName & typeName) n2.name = ident(procName & typeName)
@ -427,12 +429,12 @@ macro testSuite*(head: untyped, body: untyped): untyped =
result.add(n2) result.add(n2)
template runTestsProc(self, typeName, baseMethod, typeMethod)= template runTestsProc(self, typeName, baseMethod, typeMethod) =
method typeMethod(self: typeName)= method typeMethod(self: typeName) =
when compiles(self.baseMethod()): when compiles(self.baseMethod()):
self.baseMethod() self.baseMethod()
method runTests(self: typeName)= method runTests(self: typeName) =
self.typeMethod() self.typeMethod()
var baseMethodName = ident("runTests" & $baseName.toStrLit()) var baseMethodName = ident("runTests" & $baseName.toStrLit())
@ -472,12 +474,12 @@ macro testSuite*(head: untyped, body: untyped): untyped =
discard discard
if not foundSetup: if not foundSetup:
template setupProc(self, typeName, setupProc)= template setupProc(self, typeName, setupProc) =
method setup(self: typeName) method setup(self: typeName)
method setupProc(self: typeName) method setupProc(self: typeName)
template setupDecl(self, baseMethod)= template setupDecl(self, baseMethod) =
method setup()= method setup() =
when compiles(self.baseMethod()): when compiles(self.baseMethod()):
self.baseMethod() self.baseMethod()
@ -488,12 +490,12 @@ macro testSuite*(head: untyped, body: untyped): untyped =
body.add(setupBaseAst[0]) body.add(setupBaseAst[0])
if not foundTeardown: if not foundTeardown:
template teardownProc(self, typeName, tdProc)= template teardownProc(self, typeName, tdProc) =
method tearDown(self: typeName) method tearDown(self: typeName)
method tdProc(self: typeName) method tdProc(self: typeName)
template teardownDecl(self, baseMethod)= template teardownDecl(self, baseMethod) =
method tearDown()= method tearDown() =
when compiles(self.baseMethod()): when compiles(self.baseMethod()):
self.baseMethod() self.baseMethod()
@ -506,10 +508,10 @@ macro testSuite*(head: untyped, body: untyped): untyped =
baseTearMethodName)) baseTearMethodName))
body.add(teardownBaseAst[0]) body.add(teardownBaseAst[0])
template setTestName(self, procName)= template setTestName(self, procName) =
self.currentTestName = procName self.currentTestName = procName
template tryBlock(self, testCall)= template tryBlock(self, testCall) =
self.numTests += 1 self.numTests += 1
try: try:
testCall testCall
@ -645,9 +647,9 @@ macro testSuite*(head: untyped, body: untyped): untyped =
var typeDecl: NimNode var typeDecl: NimNode
template declareTypeExport(tname, bname)= template declareTypeExport(tname, bname) =
type tname* = ref object of bname type tname* = ref object of bname
template declareType(tname, bname)= template declareType(tname, bname) =
type tname = ref object of bname type tname = ref object of bname
if baseName == nil: if baseName == nil:
@ -686,13 +688,13 @@ macro testSuite*(head: untyped, body: untyped): untyped =
result.add(runTests) result.add(runTests)
template addTestSuite(typeName)= template addTestSuite(typeName) =
testSuites.add(typeName()) testSuites.add(typeName())
result.add(getAst(addTestSuite(typeName))) result.add(getAst(addTestSuite(typeName)))
proc printRunning(suite: TestSuite)= proc printRunning(suite: TestSuite) =
var var
numTicks = 80 - 12 - len(suite.name) numTicks = 80 - 12 - len(suite.name)
ticks = "" ticks = ""
@ -710,7 +712,7 @@ proc printRunning(suite: TestSuite)=
echo "\n[Running] $1 $2\n".format(suite.name, ticks) 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 # Output red if tests didn't pass, green otherwise
var color = fgGreen var color = fgGreen
@ -735,7 +737,7 @@ proc printPassedTests(suite: TestSuite)=
else: else:
echo "\n$1 tests passed for $2.$3\n".format(passedStr, suite.name, ticks) 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 var summaryColor = fgGreen
if totalTestsPassed != totalTests: if totalTestsPassed != totalTests:
@ -761,7 +763,7 @@ proc printSummary(totalTestsPassed: int, totalTests: int)=
echo "\n[Summary]" echo "\n[Summary]"
echo "\n $1 tests passed.".format(passedStr) echo "\n $1 tests passed.".format(passedStr)
proc runTests*()= proc runTests*() =
## The method that runs the tests. Invoke ## The method that runs the tests. Invoke
## after setting up all of the tests and ## after setting up all of the tests and
## usually inside a "when isMainModule" block ## usually inside a "when isMainModule" block

View file

@ -1,32 +1,34 @@
import einheit import einheit
testSuite UnitTests: testSuite UnitTests:
var var
testObj: int testObj: int
testArray: array[4, int] testArray: array[4, int]
proc doThings()= proc doThings() =
# This proc won't be invoked as a test # This proc won't be invoked as a test
self.testObj = 400 self.testObj = 400
self.check(self.testObj == 400) self.check(self.testObj == 400)
method setup()= method setup() =
self.testObj = 90 self.testObj = 90
for i in 0 ..< self.testArray.len(): for i in 0 ..< self.testArray.len():
self.testArray[i] = i self.testArray[i] = i
method tearDown()= method tearDown() =
self.testObj = 0 self.testObj = 0
method testForB()= method testForB() =
var b = 4 var b = 4
self.doThings() self.doThings()
self.check(b == 4) self.check(b == 4)
method testArrayAssert()= method testArrayAssert() =
self.check(self.testArray == [0,1,2]) self.check(self.testArray == [0,1,2])
method testForC()= method testForC() =
var c = 0 var c = 0
# supposed to fail # supposed to fail
self.check(c == 1) self.check(c == 1)
@ -36,37 +38,46 @@ testSuite UnitTestsNew:
var var
testObj: int testObj: int
method setup()= method setup() =
self.testObj = 90 self.testObj = 90
method tearDown()= method tearDown() =
self.testObj = 0 self.testObj = 0
method testTestObj()= method testTestObj() =
self.check(self.testObj == 90) self.check(self.testObj == 90)
method testStuff()= method testStuff() =
self.check("Stuff" == "Stuff") self.check("Stuff" == "Stuff")
proc returnTrue(): bool= proc returnTrue(): bool=
result = false result = false
method testMore()= method testMore() =
var more = 23 var more = 23
self.check(more == 1) self.check(more == 1)
method testMoreMore()= method testMoreMore() =
self.check(self.returnTrue()) 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! # Inheritance!
testSuite TestInherit of UnitTestsNew: testSuite TestInherit of UnitTestsNew:
## This will call every test defined in UnitTestsNew ## This will call every test defined in UnitTestsNew
proc raisesOs()= proc raisesOs() =
# This proc won't be invoked as a test # This proc won't be invoked as a test
raise newException(SystemError, "Oh no! OS malfunction!") raise newException(SystemError, "Oh no! OS malfunction!")
method testRaises()= method testRaises() =
# Two ways of checking # Two ways of checking
self.checkRaises OSError: self.checkRaises OSError:
@ -77,7 +88,7 @@ testSuite TestInherit of UnitTestsNew:
testSuite MoreInheritance of TestInherit: testSuite MoreInheritance of TestInherit:
method setup()= method setup() =
# This must be called if overriding setup if you want # This must be called if overriding setup if you want
# base class setup functionality. You can also call # base class setup functionality. You can also call
# self.setupTestInherit() to call the direct parent's # self.setupTestInherit() to call the direct parent's
@ -88,12 +99,12 @@ testSuite MoreInheritance of TestInherit:
# fail. This is expected. # fail. This is expected.
self.testObj = 12345 self.testObj = 12345
method tearDown()= method tearDown() =
# Calling the direct parent's tearDown method # Calling the direct parent's tearDown method
self.tearDownTestInherit() self.tearDownTestInherit()
self.testObj = 0 self.testObj = 0
method testTestObj()= method testTestObj() =
# This method is overwritten. To call the base method, # This method is overwritten. To call the base method,
# simply use # simply use
# self.testTestObj_UnitTestsNew() # self.testTestObj_UnitTestsNew()
@ -102,13 +113,13 @@ testSuite MoreInheritance of TestInherit:
# This one will pass, the other will fail # This one will pass, the other will fail
self.check(self.testObj == 12345) self.check(self.testObj == 12345)
method testNewObj()= method testNewObj() =
self.check(self.testObj == 12345) self.check(self.testObj == 12345)
proc doStuff(arg: int, arg2: string): string= proc doStuff(arg: int, arg2: string): string =
result = $arg & arg2 result = $arg & arg2
method testRefObject()= method testRefObject() =
type type
TestObj = ref object TestObj = ref object
t: int t: int
@ -117,12 +128,12 @@ testSuite MoreInheritance of TestInherit:
d = TestObj(t: 3) d = TestObj(t: 3)
k = TestObj(t: 30) k = TestObj(t: 30)
proc `==`(d: TestObj, d2: TestObj): bool= proc `==`(d: TestObj, d2: TestObj): bool =
result = d.t == d2.t result = d.t == d2.t
self.check(d == k) self.check(d == k)
method testObject()= method testObject() =
type type
TestObj = object TestObj = object
t: int t: int
@ -131,13 +142,13 @@ testSuite MoreInheritance of TestInherit:
d = TestObj(t: 3) d = TestObj(t: 3)
k = TestObj(t: 30) k = TestObj(t: 30)
proc `==`(d: TestObj, d2: TestObj): bool= proc `==`(d: TestObj, d2: TestObj): bool =
result = d.t == d2.t result = d.t == d2.t
self.check(d != k) self.check(d != k)
self.check(d == k) self.check(d == k)
method testComplexObject()= method testComplexObject() =
type type
Obj1 = object Obj1 = object
e: string e: string
@ -149,7 +160,7 @@ testSuite MoreInheritance of TestInherit:
result = false result = false
self.check(x.isObj(p)) self.check(x.isObj(p))
method testTuple()= method testTuple() =
type type
Person = tuple[name: string, age: int] Person = tuple[name: string, age: int]
@ -160,7 +171,7 @@ testSuite MoreInheritance of TestInherit:
self.check(t != r) self.check(t != r)
self.check(t == r) self.check(t == r)
method testComplex()= method testComplex() =
var var
a = 5 a = 5
s = "stuff" s = "stuff"