How do you test pre-/post-conditions and invariants?

Jesse Phillips jessekphillips+D at gmail.com
Fri Feb 25 10:18:40 PST 2011


Magnus Lie Hetland Wrote:

> Or, more generally, how do you test asserts (which is what I'm using in 
> my preconditions etc.)?
> 
> As far as I can see, collectException() won't collect errors, which is 
> what assert() throws -- so what's the standard way of writing unit 
> tests for preconditions that use assert? (I.e., test that they will, in 
> fact, throw when you break them.)

There has been talk of a std.unittest that would help with this, but for now:

unittest {
    try {
         callMeWrong(wrong);
         assert(0);
    catch(AssertError e) {
    }
}

}


More information about the Digitalmars-d-learn mailing list