unittests and templates

Steven Schveighoffer schveiguy at yahoo.com
Thu May 13 04:15:16 PDT 2010


On Wed, 12 May 2010 18:54:20 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Steven Schveighoffer:
>
>> class ArrayList(V)
>> {
>>     V take() {...}
>>     unittest
>>     {
>>        auto al = new ArrayList!uint;
>> ...
>
>
> That unit test is the test of just take().

[snip]

> I don't have other ideas for you.

Right now, I'm doing fine with the static if for conditionally having unit  
tests.  I feel less strongly about having unit tests which test a function  
regardless of implementation than I originally did.  In order to unittest  
a function, you will inevitably end up instantiating with certain types,  
you can just use a static if to only build the unit tests when those types  
are present, or you could do it the same way I originally did, and have  
the unit tests run more than once.

Of course, it is odd to have to do this at the end of the class  
declaration:

unittest
{
    ArrayList!int al1;
    ArrayList!uint al2;
    ...
}

But there's no other way to do it.  If D could implicitly do this, I would  
like it, but it's not that big of a deal.  In fact, you could probably  
write a CTFE function that takes a list of types and instantiates them all  
in order to make sure unit tests run.

This post was more of a "hey, look what's cool about unit tests and  
templates!" than a complaint :)

-Steve


More information about the Digitalmars-d-learn mailing list