Added simple implementation of assert with simple reflection.

This commit is contained in:
Joey Payne 2015-07-08 20:06:06 -06:00
commit 21ef6228b7
3 changed files with 97 additions and 71 deletions

View file

@ -51,7 +51,7 @@ testSuite SuiteName of TestSuite:
method testAddingString()=
## adds a string to the suiteVar
self.suiteVar &= " 123"
self.assertEqual(self.suiteVar, "Testing 123")
self.assert(self.suiteVar == "Testing 123")
proc raisesOs()=
# This proc won't be invoked as a test, it must begin with "test" in lowercase
@ -65,10 +65,10 @@ testSuite SuiteName of TestSuite:
self.assertRaises(OSError, self.raisesOs())
method testTestObj()=
self.assertTrue(self.testObj == 90)
self.assert(self.testObj == 90)
method testMoreMore()=
self.assertFalse("String" != "String")
self.assert("String" == "String")
when isMainModule:
einheit.runTests()
@ -89,49 +89,71 @@ is this:
[Running] UnitTests
[OK] testForB
[Failed] testArrayAssert
Condition: assert(self.testArray == [0, 1, 2])
Reason: self.testArray == [0, 1, 2, 3]
Location: einheit.nim; line 160
[Failed] testForC
Condition: assertTrue(c, 1)
Reason: c == 0; 0 != 1
Location: test.nim; line 24
Condition: assert(c == 1)
Reason: c == 0
Location: einheit.nim; line 160
[1/2] tests passed.
[1/3] tests passed.
[Running] UnitTestsNew
[OK] testTestObj
[OK] testStuff
[OK] testMore
[OK] testMoreMore
[Failed] testMore
Condition: assert(more == 1)
Reason: more == 23
Location: einheit.nim; line 160
[Failed] testMoreMore
Condition: assert(self.returnTrue())
Reason: self.returnTrue() == false
Location: einheit.nim; line 160
[4/4] tests passed.
[2/4] tests passed.
[Running] TestInherit
[OK] testTestObj
[OK] testStuff
[OK] testMore
[OK] testMoreMore
[Failed] testMore
Condition: assert(more == 1)
Reason: more == 23
Location: einheit.nim; line 160
[Failed] testMoreMore
Condition: assert(self.returnTrue())
Reason: self.returnTrue() == false
Location: einheit.nim; line 160
[OK] testRaises
[5/5] tests passed.
[3/5] tests passed.
[Running] MoreInheritance
[Failed] testTestObj
Condition: assertTrue(self.testObj == 90)
Reason: (self.testObj == 90) == false
Location: test.nim; line 36
Condition: assert(self.testObj == 90)
Reason: self.testObj == 12345
Location: einheit.nim; line 160
[OK] testStuff
[OK] testMore
[OK] testMoreMore
[Failed] testMore
Condition: assert(more == 1)
Reason: more == 23
Location: einheit.nim; line 160
[Failed] testMoreMore
Condition: assert(self.returnTrue())
Reason: self.returnTrue() == false
Location: einheit.nim; line 160
[OK] testRaises
[OK] testTestObj
[OK] testNewObj
[6/7] tests passed.
[4/7] 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.

View file

@ -91,41 +91,6 @@ template returnException(name, testName, snip, vals)=
# ------------------------ Templates for assertion ----------------------------
template assertEqual*(self: TestSuite, lhs: untyped,
rhs: untyped): untyped {.immediate.}=
## Raises a TestAssertError when the lhs and the rhs are not equal.
if (lhs) != (rhs):
var snip = astToStr(lhs) & ", " & astToStr(rhs)
var
vals = "$3 == $1; $1 != $2".format(lhs, rhs, astToStr(lhs))
testName = self.currentTestName
returnException("assertTrue", testName, snip, vals)
template assertTrue*(self: TestSuite, code: untyped): untyped {.immediate.}=
## Raises a TestAssertError when the code is false.
if not code:
var snip = astToStr(code)
var
vals = "($1) == $2".format(snip, code)
testName = self.currentTestName
returnException("assertTrue", testName, snip, vals)
template assertFalse*(self: TestSuite, code: untyped): untyped {.immediate.}=
## Raises a TestAssertError when the code is true.
if code:
var snip = astToStr(code)
var
vals = "($1) == $2".format(snip, code)
testName = self.currentTestName
returnException("assertFalse", testName, snip, vals)
template assertRaises*(self: TestSuite, error: Exception,
code: untyped): untyped {.immediate.}=
## Raises a TestAssertError when the exception "error" is
@ -151,10 +116,48 @@ template assertRaises*(self: TestSuite, error: Exception,
returnException("assertRaises", testName, snip, vals)
discard
macro isInfix(code: untyped): untyped=
if code.kind == nnkInfix:
return newLit(true)
return newLit(false)
template assert*(self: TestSuite, d: untyped, msg=""){.immediate.}=
assertTrue(self, d)
macro getLhs(code: untyped): untyped=
return code[1].toStrLit()
macro getLhsVal(code: untyped): untyped=
return code[1]
macro getRhs(code: untyped): untyped=
return code[2]
macro getRhsVal(code: untyped): untyped=
return code[2]
template assert*(self: TestSuite, code: untyped){.immediate.}=
## Assertions for tests
if not code:
var
snip = ""
vals = ""
testName = self.currentTestName
when isInfix(code) == 0:
snip = astToStr(code)
vals = "$1 == $2".format(snip, code)
else:
snip = astToStr(code)
var
lhs = getLhs(code)
lhsVal = getLhsVal(code)
rhs = getRhs(code)
rhsVal = getRhsVal(code)
if $lhs != $lhsVal:
vals &= "$1 == $2".format(lhs, lhsVal)
if $rhs != $rhsVal:
vals &= "$1 == $2".format(rhs, rhsVal)
returnException("assert", testName, snip, vals)
# -----------------------------------------------------------------------------

View file

@ -8,7 +8,7 @@ testSuite UnitTests:
proc doThings()=
# This proc won't be invoked as a test
self.testObj = 400
self.assertTrue(self.testObj == 400)
self.assert(self.testObj == 400)
method setup()=
self.testObj = 90
@ -19,17 +19,17 @@ testSuite UnitTests:
self.testObj = 0
method testForB()=
var b = 0
var b = 4
self.doThings()
self.assertTrue(4 == 4)
self.assert(b == 4)
method testArrayAssert()=
self.assertTrue(self.testArray == [0,1,2])
self.assert(self.testArray == [0,1,2])
method testForC()=
var c = 0
# supposed to fail
self.assertEqual(c, 1)
self.assert(c == 1)
testSuite UnitTestsNew:
@ -43,19 +43,20 @@ testSuite UnitTestsNew:
self.testObj = 0
method testTestObj()=
self.assertTrue(self.testObj == 90)
self.assert(self.testObj == 90)
method testStuff()=
self.assertEqual("Stuff", "Stuff")
self.assert("Stuff" == "Stuff")
proc returnTrue(): bool=
return false
method testMore()=
self.assertEqual("String", "String")
self.assert 0 == 1
var more = 23
self.assert(more == 1)
method testMoreMore()=
self.assertFalse("String" != "String")
self.assert(self.returnTrue())
# Inheritance!
testSuite TestInherit of UnitTestsNew:
@ -99,10 +100,10 @@ testSuite MoreInheritance of TestInherit:
# However, currently this method will be run
# IN ADDITION to the base class's method.
# This one will pass, the other will fail
self.assertTrue(self.testObj == 12345)
self.assert(self.testObj == 12345)
method testNewObj()=
self.assertEqual(self.testObj, 12345)
self.assert(self.testObj == 12345)
when isMainModule: