Helpers for writing unittests

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 30 17:30:21 PDT 2015


On Friday, 31 July 2015 at 00:07:43 UTC, Idan Arye wrote:
> Thoughts?

Some unit test helpers for this sort of thing might be nice, but 
I don't think that it really buys us much with this particular 
case. You could just as easily do

unittest
{
     foreach(T; TypeTuple!(ubyte, byte, ushort, short, uint, int, 
ulong, long))
         static assert(is(typeof(foo(T.init)));
}

and the code is basically as long as is with assertCompilesWith - 
shorter even. The above example is longer due to not using an 
alias for the integral types like you did, but if that same alias 
were used, then it becomes

unittest
{
     foreach(T; IntegerTypes)
         static assert(is(typeof(foo(T.init)));
}

which isn't all that different than

unittest
{
     assertCompilesWith!(IntegerTypes, (x) {
         foo(x);
     });
}

but it doesn't require a lambda or creating any helpers, and it 
cuts down on the number of unit testing functions that are 
templated (it can cost a lot in terms of compilation time and 
memory, if you use a lot of templates as helpers for unit tests). 
In addition, in most cases, I don't see much point in simply 
testing that the templated function or type compiles with various 
types. It's generally far better to do something like

foreach(T; IntegerTypes)
{
     // Tests which actually test foo with T rather than just that 
it compiles
}

And in that case, explicitly testing compilation is unnecessary. 
You're already testing far more than that.

So, I applaud the attitude and motivation behind your suggestion, 
but this particular helper doesn't really help IMHO.

- Jonathan M Davis


More information about the Digitalmars-d mailing list