Ah, simple solution to unittests inside templates
H. S. Teoh via Digitalmars-d
digitalmars-d at puremagic.com
Mon Sep 19 09:02:05 PDT 2016
On Sat, Sep 17, 2016 at 01:22:52PM -0400, Andrei Alexandrescu via Digitalmars-d wrote:
> 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:
I've already mentioned this exact idea recently in the d-learn forum.
Nobody responded then.
> /** 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.
[...]
And you also have to make sure Awesome!int is actually instantiated,
otherwise the unittest won't actually run!
T
--
In theory, there is no difference between theory and practice.
More information about the Digitalmars-d
mailing list