unittesting generic functions

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 14 09:14:41 PDT 2014


On 14/08/2014 05:02, H. S. Teoh via Digitalmars-d wrote:
> 4) How much can you realistically test on a generic type argument T,
> that you can't already cover with concrete types? I'm finding that the
> per-instantiation behaviour of unittest blocks inside templated
> structs/classes is rarely desired, esp. when you write ddoc unittests
> (because you want code examples in the docs to involve concrete types,
> not abstract types, otherwise they are of limited use to the reader).
> Because of this, I often move unittests outside the aggregate or enclose
> them in static if's.

Yes, which is bad as they can be some distance away from methods, so 
they might not be kept in sync. I think it's not possible to have a 
documented unittest for a method that isn't part of the parent template. 
This is necessary to prevent template instantiation recursion sometimes.

Perhaps we could have new syntax for that, e.g.:

struct S(T)
{
     void foo();

     // force module scope, not part of S
     module unittest()
     {
         S!int s;  // no instantiation recursion
         s.foo();
     }
}


More information about the Digitalmars-d mailing list