Added simple implementation of assert with simple reflection.
This commit is contained in:
parent
98ccabf9b4
commit
21ef6228b7
3 changed files with 97 additions and 71 deletions
60
README.md
60
README.md
|
|
@ -51,7 +51,7 @@ testSuite SuiteName of TestSuite:
|
||||||
method testAddingString()=
|
method testAddingString()=
|
||||||
## adds a string to the suiteVar
|
## adds a string to the suiteVar
|
||||||
self.suiteVar &= " 123"
|
self.suiteVar &= " 123"
|
||||||
self.assertEqual(self.suiteVar, "Testing 123")
|
self.assert(self.suiteVar == "Testing 123")
|
||||||
|
|
||||||
proc raisesOs()=
|
proc raisesOs()=
|
||||||
# This proc won't be invoked as a test, it must begin with "test" in lowercase
|
# 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())
|
self.assertRaises(OSError, self.raisesOs())
|
||||||
|
|
||||||
method testTestObj()=
|
method testTestObj()=
|
||||||
self.assertTrue(self.testObj == 90)
|
self.assert(self.testObj == 90)
|
||||||
|
|
||||||
method testMoreMore()=
|
method testMoreMore()=
|
||||||
self.assertFalse("String" != "String")
|
self.assert("String" == "String")
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
einheit.runTests()
|
einheit.runTests()
|
||||||
|
|
@ -89,49 +89,71 @@ is this:
|
||||||
[Running] UnitTests
|
[Running] UnitTests
|
||||||
|
|
||||||
[OK] testForB
|
[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
|
[Failed] testForC
|
||||||
Condition: assertTrue(c, 1)
|
Condition: assert(c == 1)
|
||||||
Reason: c == 0; 0 != 1
|
Reason: c == 0
|
||||||
Location: test.nim; line 24
|
Location: einheit.nim; line 160
|
||||||
|
|
||||||
[1/2] tests passed.
|
[1/3] tests passed.
|
||||||
|
|
||||||
|
|
||||||
[Running] UnitTestsNew
|
[Running] UnitTestsNew
|
||||||
|
|
||||||
[OK] testTestObj
|
[OK] testTestObj
|
||||||
[OK] testStuff
|
[OK] testStuff
|
||||||
[OK] testMore
|
[Failed] testMore
|
||||||
[OK] testMoreMore
|
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
|
[Running] TestInherit
|
||||||
|
|
||||||
[OK] testTestObj
|
[OK] testTestObj
|
||||||
[OK] testStuff
|
[OK] testStuff
|
||||||
[OK] testMore
|
[Failed] testMore
|
||||||
[OK] testMoreMore
|
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] testRaises
|
||||||
|
|
||||||
[5/5] tests passed.
|
[3/5] tests passed.
|
||||||
|
|
||||||
|
|
||||||
[Running] MoreInheritance
|
[Running] MoreInheritance
|
||||||
|
|
||||||
[Failed] testTestObj
|
[Failed] testTestObj
|
||||||
Condition: assertTrue(self.testObj == 90)
|
Condition: assert(self.testObj == 90)
|
||||||
Reason: (self.testObj == 90) == false
|
Reason: self.testObj == 12345
|
||||||
Location: test.nim; line 36
|
Location: einheit.nim; line 160
|
||||||
[OK] testStuff
|
[OK] testStuff
|
||||||
[OK] testMore
|
[Failed] testMore
|
||||||
[OK] testMoreMore
|
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] testRaises
|
||||||
[OK] testTestObj
|
[OK] testTestObj
|
||||||
[OK] testNewObj
|
[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.
|
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.
|
||||||
|
|
|
||||||
79
einheit.nim
79
einheit.nim
|
|
@ -91,41 +91,6 @@ template returnException(name, testName, snip, vals)=
|
||||||
|
|
||||||
# ------------------------ Templates for assertion ----------------------------
|
# ------------------------ 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,
|
template assertRaises*(self: TestSuite, error: Exception,
|
||||||
code: untyped): untyped {.immediate.}=
|
code: untyped): untyped {.immediate.}=
|
||||||
## Raises a TestAssertError when the exception "error" is
|
## Raises a TestAssertError when the exception "error" is
|
||||||
|
|
@ -151,10 +116,48 @@ template assertRaises*(self: TestSuite, error: Exception,
|
||||||
|
|
||||||
returnException("assertRaises", testName, snip, vals)
|
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.}=
|
macro getLhs(code: untyped): untyped=
|
||||||
assertTrue(self, d)
|
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)
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
29
test.nim
29
test.nim
|
|
@ -8,7 +8,7 @@ testSuite UnitTests:
|
||||||
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.assertTrue(self.testObj == 400)
|
self.assert(self.testObj == 400)
|
||||||
|
|
||||||
method setup()=
|
method setup()=
|
||||||
self.testObj = 90
|
self.testObj = 90
|
||||||
|
|
@ -19,17 +19,17 @@ testSuite UnitTests:
|
||||||
self.testObj = 0
|
self.testObj = 0
|
||||||
|
|
||||||
method testForB()=
|
method testForB()=
|
||||||
var b = 0
|
var b = 4
|
||||||
self.doThings()
|
self.doThings()
|
||||||
self.assertTrue(4 == 4)
|
self.assert(b == 4)
|
||||||
|
|
||||||
method testArrayAssert()=
|
method testArrayAssert()=
|
||||||
self.assertTrue(self.testArray == [0,1,2])
|
self.assert(self.testArray == [0,1,2])
|
||||||
|
|
||||||
method testForC()=
|
method testForC()=
|
||||||
var c = 0
|
var c = 0
|
||||||
# supposed to fail
|
# supposed to fail
|
||||||
self.assertEqual(c, 1)
|
self.assert(c == 1)
|
||||||
|
|
||||||
|
|
||||||
testSuite UnitTestsNew:
|
testSuite UnitTestsNew:
|
||||||
|
|
@ -43,19 +43,20 @@ testSuite UnitTestsNew:
|
||||||
self.testObj = 0
|
self.testObj = 0
|
||||||
|
|
||||||
method testTestObj()=
|
method testTestObj()=
|
||||||
self.assertTrue(self.testObj == 90)
|
self.assert(self.testObj == 90)
|
||||||
|
|
||||||
method testStuff()=
|
method testStuff()=
|
||||||
self.assertEqual("Stuff", "Stuff")
|
self.assert("Stuff" == "Stuff")
|
||||||
|
|
||||||
|
proc returnTrue(): bool=
|
||||||
|
return false
|
||||||
|
|
||||||
method testMore()=
|
method testMore()=
|
||||||
self.assertEqual("String", "String")
|
var more = 23
|
||||||
self.assert 0 == 1
|
self.assert(more == 1)
|
||||||
|
|
||||||
method testMoreMore()=
|
method testMoreMore()=
|
||||||
self.assertFalse("String" != "String")
|
self.assert(self.returnTrue())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Inheritance!
|
# Inheritance!
|
||||||
testSuite TestInherit of UnitTestsNew:
|
testSuite TestInherit of UnitTestsNew:
|
||||||
|
|
@ -99,10 +100,10 @@ testSuite MoreInheritance of TestInherit:
|
||||||
# However, currently this method will be run
|
# However, currently this method will be run
|
||||||
# IN ADDITION to the base class's method.
|
# IN ADDITION to the base class's method.
|
||||||
# This one will pass, the other will fail
|
# This one will pass, the other will fail
|
||||||
self.assertTrue(self.testObj == 12345)
|
self.assert(self.testObj == 12345)
|
||||||
|
|
||||||
method testNewObj()=
|
method testNewObj()=
|
||||||
self.assertEqual(self.testObj, 12345)
|
self.assert(self.testObj == 12345)
|
||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue