optionally verbose assertions
spir
denis.spir at gmail.com
Thu Apr 14 08:28:39 PDT 2011
On 04/14/2011 04:03 PM, Steven Schveighoffer wrote:
> Sometimes, I worry that my unit tests or asserts aren't running. Every once in
> a while, I have to change one to fail to make sure that code is compiling (this
> is especially true when I'm doing version statements or templates). It would
> be nice if there was a -assertprint mode which showed asserts actually running
> (only for the module compiled with that switch, of course).
Man, I'm very pleased to read someone else advocating for optionally verbose
assertions.
This could use 2 arguments instead of a predicate:
assert(expressions, value);
Example use:
unittest {
auto args = [0, 1, -1, 999, -999];
auto vals = [a, b, c, d, e]; // letters here represent expected values
foreach (i ; 0..args.length)
assert(f(args[i]), vals[i]);
}
During a test session, assert-mode is set to 'verbose'. This would print for
instance, in case f(999) fails:
"
assert: f(0) --> a
assert: f(1) --> b
assert: f(-1) --> c
********************************
Assertion Error:
expression : f(999)
expected : d
outcome : whatever
********************************
assert: f(-999) --> e
"
Then, we get all we need to comfortably debug, I guess. Also note /other/
unittests (eg one testing g called by f) may also bring in valuable feedback
data on how the program actually runs.
As is discussed in this thread, this verbose mode is also very nice for code
samples (introduction to D, manuals, tutorials, howtos, and even exchanges on
mailing lists...).
For a plain regression test, after some change has been made to a piece of
code, assert-mode is set to 'silent'. This would only print out failure cases:
"
********************************
Assertion Error:
expression : f(999)
expected : d
outcome : whatever
********************************
"
Then, we just have to turn assert-mode back to verbose, and happily start
debugging ;-)
Note: 2 friendly complementary features are (1) expected outcome may be an
exception, (2) outcome may be expression via a string representation. This is
the reason why output shows as '-->', not '=='.
Denis
--
_________________
vita es estrany
spir.wikidot.com
More information about the Digitalmars-d
mailing list