Unit tests and verifying pre/post-conditions and invariants
Jonathan M Davis
jmdavisprog at gmail.com
Mon Aug 16 14:51:53 PDT 2010
On Monday, August 16, 2010 14:36:13 Steven Schveighoffer wrote:
> 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
Ah, that would be true, since assert throws an AssertException. Whoops. It does
work for normal exceptions though (assuming that you changed the type in the
catch to the appropriate exception type).
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list