Non-trivial assert should provide as much context as possible, especially when there's no easy-to-use debugger.
Here are my suggestions:
assert(a > 1) # show what the value of a is
assert(foo() > bar.baz()) # show return value of foo() and return value of bar.baz()
assert(a > 1, b, c, d) # show content of b, c and d, whatever type they are
assert(a > 1):
var x = foo()
if bar(x): echo "..."
You can do that with unittest:
import unittest
let (a, b) = (123, 321)
check a == b
# prog.nim(4,8): Check failed: a == b
# a was 123
# b was 321
http://nim-lang.org/docs/unittest.htmlimport unittest
proc foo : int =
return 1
proc bar : int =
return 2
let (a, b) = (123, 321)
check a == b and foo() == bar()
shows
a.nim(12,13): Check failed: a == b and foo() == bar()
a == b was false
foo() == bar() was false
which is not as informative as it could be. The real code problem is usually complicated and requires lot of context for understanding.
I was inspired by John Torjo's Smart Assert library:
http://www.drdobbs.com/cpp/enhancing-assertions/184403745
(He made even more powerfull version for Boost, but it didn't get through.)
This library employs preprocessor tricks. Using it, however, caused compilation time to grow significantly, so it was unusable in practice for me.
It would be awesome to see this integrated into the unittest lib! Perhaps inspiration could be taken from my einheit testing library :) It seems to do what you want.
For example, the following code:
import einheit
testSuite TestingValues:
method setup()=
discard
method tearDown()=
discard
method testValues()=
proc foo : int =
return 1
proc bar : int =
return 2
let (a, b) = (123, 321)
self.check(a == b and foo() == bar())
when isMainModule:
runTests()
Outputs:
[Running] TestingValues -------------------------------------------------------
[Failed] testValues
Condition: check(a == b and foo() == bar())
Where:
a -> 123
bar() -> 2
b -> 321
foo() -> 1
Location: x.nim; line 19
[0/1] tests passed for TestingValues. ------------------------------------------
[Summary]
[0/1] tests passed.