std.unittests for (final?) review [Update]

Jonathan M Davis jmdavisProg at gmx.com
Mon Jan 10 16:29:42 PST 2011


On Monday, January 10, 2011 14:41:51 Jonathan M Davis wrote:
> On Monday, January 10, 2011 13:48:50 Tomek Sowiński wrote:
> > Another example:
> > 
> > {
> > 
> >         bool thrown = false;
> >         try
> >         
> >             assertNotThrown!AssertError(throwEx(new AssertError("It's an
> > 
> > AssertError", __FILE__, __LINE__)), "It's a message"); catch(AssertError)
> > 
> >             thrown = true;
> >         
> >         assert(thrown);
> >     
> >     }
> > 
> > can be:
> >     try {
> >     
> >         assertNotThrown!AssertError(throwEx(new AssertError("It's an
> > 
> > AssertError", __FILE__, __LINE__)), "It's a message"); assert(false);
> > 
> >     } catch(AssertError) { /*OK*/ }
> > 
> > and you don't have to introduce a new scope every time.
> 
> Doesn't work actually - at least not in the general case (for this
> particular test, it's arguably okay). It doesn't take into account the
> case where an exception other than AssertError is thrown. The exception
> escapes instead of hitting the assertion. I believe that they are the way
> they are because I was essentialy re-writing assertThrown to test
> assertThrown. Regardless, we're talking about the unit tests here, not the
> actual code, so I don't think that it's as big a deal.

Actually, I now remember the big reason that your version doesn't work at all. 
The AssertError thrown by assert(false); inside of the try block would just be 
caught by the catch block. So, it doesn't work at all. You have to do it pretty 
much the way that I did it. It was one of those nuances in writing 
assertNotThrown that initially missed when coming up with it in the first place. 
So, the ugly way is necessary.

- Jonathan M Davis


More information about the Digitalmars-d mailing list