Ah, simple solution to unittests inside templates

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 17 10:22:52 PDT 2016


Recall the discussion a few days ago about unittests inside templates 
being instantiated with the template. Often that's desirable, but 
sometimes not - for example when you want to generate nice ddoc 
unittests and avoid bloating. For those cases, here's a simple solution 
that I don't think has been mentioned:

/** Awesome struct */
struct Awesome(T)
{
     /** Awesome function. */
     void awesome() {}

     ///
     static if (is(T == int)) unittest
     {
         Awesome awesome;
         awesome.awesome;
     }
}

The unittest documentation is nicely generated. The unittest code itself 
is only generated for one instantiation.


Andrei


More information about the Digitalmars-d mailing list