throws(statement, ErrorType)

Jonathan M Davis jmdavisProg at gmx.com
Sun Jan 23 03:36:57 PST 2011


On Sunday 23 January 2011 03:14:48 spir wrote:
> On 01/23/2011 06:32 AM, Jonathan M Davis wrote:
> > On Saturday 22 January 2011 20:45:14 Andrej Mitrovic wrote:
> >> *There are several of those, like assertExcThrown, etc. Try searching
> >> the newsgroups for std.unittest or std.datetime and there should be a
> >> link to the source if you want it right now.
> > 
> > It's currently being reviewed and eventually vote on in the
> > "std.unittests for (final?) review" thread (which reminds me that I need
> > to finish my updates to put the next version up - though IIRC, it's
> > primarily a documentation change, not an API change). From the looks of
> > it, I expect it to pass the vote (for which Andrei set the deadline as
> > Feb 7), at which point it looks like those functions will be going into
> > std.exception. So, assuming that it passes the vote and there's no
> > release between now and then, then it'll be in the next release.
> > 
> > assertThrown is the current name of the function that spir is looking
> > for.
> 
> Thank all for your answers. Seems I'll have to wait a bit.
> Pointer to the current code?
> (I'm very curious of how assertThrown works, actually asked about
> throws(statement, ErrorType) because I have no idea how to craft it myself.
> Seems to me needs compiler support to pass around the statement. Please, do
> not tell me it uses string mixins ;-)

It uses lazy, not string mixins. It was a fair bit of work to figure out though, 
and it has had several incarnations (one of which _did_ use string mixins). 
However, the current version is quite clean IMHO. The current code can be found 
here: http://is.gd/jZEVl

However, I'll be posting an updated version for further review relatively soon. 
If all you really want is to see the code for assertThrown though, it's fairly 
short and simple:

void assertThrown(T : Throwable = Exception, F)
                 (lazy F funcToCall, string msg = null, string file = __FILE__, 
size_t line = __LINE__)
{
    bool thrown = false;

    try
        funcToCall();
    catch(T t)
        thrown = true;

    if(!thrown)
    {
        immutable tail = msg.empty ? "." : ": " ~ msg;

        throw new AssertError(format("assertThrown failed: No %s was thrown%s", 
T.stringof, tail), file, line);
    }
}

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list