optionally verbose assertions

spir denis.spir at gmail.com
Thu Apr 14 11:38:32 PDT 2011


On 04/14/2011 06:35 PM, Daniel Gibson wrote:
> Am 14.04.2011 17:47, schrieb Steven Schveighoffer:
>> On Thu, 14 Apr 2011 11:28:39 -0400, spir<denis.spir at gmail.com>  wrote:
>>
>>> 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:
>>
>> [snip]
>>
>> I don't think we can change assert syntax now.  What I was looking was
>> for something more like:
>>
>> assert(x == y);
>>
>> prints out
>>
>> "asserting x == y: true"
>>
>> for asserts that pass when you have the 'verbose assert' flag turned
>> on.  This should be easy to do in the compiler by just translating
>>
>> assert(expr);
>>
>> to something like:
>>
>> auto result = evaluateAssert(expr);
>> print("asserting expr: ", result ? "true" : "false");
>> assert(result);

The problem is how do /you/ get expr's original expression? In other words, is 
there a way to do it without compiler magic?
(I think even passing expr as string and using string mixins would not do it, 
because the string must be constant.)

>> -Steve
>
> Another possibility are named unittests and printing out "running test
> 'foobar' was successful".
> One message for every assert is too verbose IMHO.

There may be a third, intermediate mode. But many times having the list of 
check result is very helpful to support one's thought, or is just what you need 
in any case. Wouldn't it be stupid to require you to write one writeln per 
assertion just because we omitted this verbode mode?

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d mailing list