Ah, simple solution to unittests inside templates

Andrej Mitrovic via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 17 14:23:58 PDT 2016


On Saturday, 17 September 2016 at 17:22:52 UTC, Andrei 
Alexandrescu wrote:
> The unittest documentation is nicely generated. The unittest 
> code itself is only generated for one instantiation.

I had a similar thought, but I didn't really like verbosity of 
the static if.

I think at some point someone suggested we could implement 
explicit support for such unittests via `static unittest`:

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

     ///
     static unittest
     {
         Awesome!int awesome;
         awesome.awesome;
     }
}

A static unittest would not have implicit access to anything 
internal in the aggregation (this is different to your solution 
where the unittest has access to all fields of the Awesome!int 
instance).

Note the difference inside the unittest: it uses `Awesome!int 
awesome` instead of the previous `Awesome awesome`. In your case 
it implicitly refers to the Awesome!int instantiation, however 
you're actually presenting uncompilable code to the user! The 
user might copy-paste it into a main function but it would fail 
to build.

So the feature might have some merit as it would avoid generating 
docs with code which may not even work.


More information about the Digitalmars-d mailing list