Unit tests and verifying pre/post-conditions and invariants

Steven Schveighoffer schveiguy at yahoo.com
Mon Aug 16 14:36:13 PDT 2010


On Sat, 14 Aug 2010 23:49:18 -0400, Jonathan M Davis  
<jmdavisprog at gmail.com> wrote:


> and I wanted to test to make sure that func() couldn't be called with  
> any int
> greater or equal to 8, what would I do? The best that I can think of is  
> to catch
> an AssertError and ignore it. e.g.
>
> unittest
> {
>     try
>     {
>         func(8);
>         assert(0);
>     }
>     catch(AssertionError ae)
>     {}
> }

BTW, I just realized, this code doesn't test anything.  You want something  
like this:

unittest
{
    bool caughtAssert = false;
    try
    {
       func(8);
    }
    catch(AssertionError ae)
    {
       caughtAssert = true;
    }
    assert(caughtAssert);
}

-Steve


More information about the Digitalmars-d-learn mailing list