std.unittests [updated] for review

Tomek Sowiński just at ask.me
Mon Jan 24 15:23:29 PST 2011


Dnia 2011-01-24, o godz. 06:34:49
Jonathan M Davis <jmdavisProg at gmx.com> napisał(a):

> In case you didn't know, I have a set of unit test helper functions which have 
> been being reviewed for possible inclusion in phobos. Here's an update.
> 
> Most recent code: http://is.gd/F1OHat
> 
> Okay. I took the previous suggestions into consideration and adjusted the code a 
> bit more. However, most of the changes are to the documentation (though there 
> are some changes to the code). Some of the code duplication was removed, and the 
> way that some of the assertPred functions' errors are formatted has been altered 
> so that values line up vertically, making them easier to compare.

That's a solid improvement, thanks.

> The big change 
> is the docs though. There's now a fake version of assertPred at the top with an 
> overall description for assertPred followed by the individual versions with as 
> little documentation as seemed appropriate while still getting all of the 
> necessary information across. A couple of the functions still have irritatingly 
> long example sections, but anything less wouldn't get the functionality across.

I'm not sure...

Examples:

assertPred!"+"(7, 5, 12);
assertPred!"-"(7, 5, 2);
assertPred!"*"(7, 5, 35);
assertPred!"/"(7, 5, 1);
assertPred!"%"(7, 5, 2);
assertPred!"^^"(7, 5, 16_807);
assertPred!"&"(7, 5, 5);
assertPred!"|"(7, 5, 7);
assertPred!"^"(7, 5, 2);
assertPred!"<<"(7, 1, 14);
assertPred!">>"(7, 1, 3);
assertPred!">>>"(-7, 1, 2_147_483_644);
assertPred!"~"("hello ", "world", "hello world");

assert(collectExceptionMsg(assertPred!"+"(7, 5, 11)) ==
       "assertPred!\"+\" failed: [7] + [5]:\n" ~
       "[12] (actual)\n" ~
       "[11] (expected).");

assert(collectExceptionMsg(assertPred!"/"(11, 2, 6, "It failed!")) ==
       "assertPred!\"/\" failed: [11] / [2]:\n" ~
       "[5] (actual)\n" ~
       "[6] (expected): It failed!");

Picking only one or two from the above would be enough to "get it". It's the description that ought to explain the function's behavior in all cases, examples are for jump-starting the user to action.


Oh, one more thing. Previously you asked me why a generic collectThrown is useful and I forgot to answer. One use is the same as collectExceptionMsg() without being tied to the msg property.

auto e = collectThrown!MyException(expr);
assert(e);
assert(e.errorCode == expectedCode);
assert(cast(MyCauseException) e.next);

I'm not proposing to yank collectExceptionMsg or assertThrown in favor of collectThrown, they're useful idioms. But having also collectThrown (a generic replacement for existing collectException) would definitely be of value.

> In any case. Here's the updated code. Review away. Andrei set the vote deadline 
> for February 7th, at which point, if it passes majority vote, then it will go 
> into Phobos. The number of functions is small enough now (thanks to having 
> consolidated most of them into the fantastically versatile assertPred) that it 
> looks like it will likely go in std.exception if the vote passes rather than 
> becoming a new module. So, the std.unittests title has now become a bit of a 
> misnomer, but that's what I've been calling it, so it seemed appropriate to 
> continue to label it that way in the thread's title.

Good luck!

-- 
Tomek



More information about the Digitalmars-d mailing list