unittest questions

Jonathan M Davis jmdavisprog at gmail.com
Thu Aug 19 16:17:19 PDT 2010


On Thursday, August 19, 2010 11:08:33 Johannes Pfau wrote:
> Hi, I wrote some unittests using the built-in d unittest and a came
> across 2 problems:
> 
> 1) I have some code that accepts both delegates and functions. How can a
> unittest explicitly check the function part? Whenever I add a function
> in an unittest block it becomes a delegate.
> ---------------------------------------------------
> void add(T)(T handler) if (is(T == void function())){}
> void add(T)(T handler) if (is(T == void delegate())){}
> 
> unittest
> {
>     //always a delegate
>     void handler() {};
>     add(&handler);
> }
> ----------------------------------------------------
> 
> 2) I know Errors should not be caught. But when I expect a function to
> throw in debug mode, but not necessarily in release mode (assert), I
> have to check for both Errors and Exceptions --> Throwable. Is it OK to
> catch Throwables in this case?
> ----------------------------------------------------
> unittest
> {
>     void handler() {};
>     bool thrown = false;
>     try
>         add(&handler);
>     catch(Throwable)
>         thrown = true;
> 
>     assert(thrown);
> }
> ----------------------------------------------------

If you declare a nested function as static, it shouldn't be a delegate. Also, I 
don't believe that you need the semicolon after the function declaration.

- Jonathan m Davis


More information about the Digitalmars-d-learn mailing list