throws(statement, ErrorType)

spir denis.spir at gmail.com
Sun Jan 23 03:55:48 PST 2011


On 01/23/2011 12:36 PM, Jonathan M Davis wrote:
> 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

:-) That's not was I was looking for, IIUC. (And explains the mystery about 
passing a statement around). Seems one cannot write
	assertThrown (1/0, ZeroDivisionError);
or
	assertThrown (divide(1,0), ZeroDivisinoError);
or can we? If I was looking for a variant of assert able to deal with throwing 
statements. (Check they throw, precisely). Obvious use case:

unittest {
     assert(divide(2,1) == 2);
     assert(divide(1,1) == 1);
     assert(throws(divide(1,0), ZeroDivisionError));
}

But my dream unittest tool looks like check(statement, outcome):

unittest {
     check(divide(2,1), 2);
     check(divide(1,1), 1);
     check(divide(1,0), ZeroDivisionError);
     check(divide(3,1), 2);
}

On verbose, would output:
     divide(2,1) --> 2
     divide(1,1) --> 1
     divide(0,1) --> ZeroDivisionError
******* unittest check error *************
     statement  : divide(3,1)
     expected   : 2
     outcome    : 3
******************************************

On silent, would output only the error.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list