I'm not sure how this would be implemented, but currently there is an IMO big flaw with the unittest ddoc feature: you cannot detach the unittest from the declaration that it's supposed to give an example for. This may not seem like a bad thing on the surface (we like to keep unittest and code close together), but consider this:
class A(T,U,V) {
/**
* My method.
*/
void myMethod() { ... }
/// Unittest ddoc
unittest {
// N.B.: we explicitly instantiate A with specific parameters.
// Why? Because declaring A without specific parameters means
// the example code is unhelpful, since the user can't actually
// just write "auto a = new A();" outside of A's context!
auto a = new A!(int, double, float)();
doSomeTests(a);
// Problem: this unittest will be duplicated across all
// instantiations of A.
}
}
This causes the unittest to be run for *every* instantiation of A, even though it is identical in each case, since we are instantiating A explicitly. This clashes with the idiom of putting per-instantiation unittests inside the class, and explicit instantiation unittests *outside* (where it only gets run once, as it should).
Maybe one way of solving this is to make use of the content of the ddoc comment header of the unittest to refer to the fully-qualified method signature of the method to document, for example:
class A(T,U,V) {
/**
* My method
*/
void myMethod(int x) { ... }
/**
* My other method
*/
void myMethod(float x) { ... }
}
/// @ExampleFor: A.myMethod(int)
/// This example gets attached to A.myMethod(int)
unittest {
auto a = new A!(int,float,real)();
a.myMethod(123);
}
/// @ExampleFor: A.myMethod(float)
/// This example gets attached to A.myMethod(float)
unittest {
auto a = new A!(string,string,bool)();
a.myMethod(1.61803);
}
Comment #1 by andrej.mitrovich — 2013-03-19T19:40:07Z
(In reply to comment #0)
> class A(T,U,V) {
> ///
> void myMethod() { ... }
>
> /// Unittest ddoc
> unittest {
>
> }
> }
Another alternative is to add the ability for template-nested unittests to only run for specific instantiations. I'm thinking of using UDAs for this. What I mean is:
(In reply to comment #0)
> class A(T,U,V) {
> ///
> void myMethod() { ... }
>
> /// Unittest that only runs for a specific instance
> @A!(int, int, int)
> unittest {
>
> }
> }
This kills two birds with one stone: Avoids instantiating a lot of unittests, and allows us to test-document methods inline.
Comment #2 by hsteoh — 2013-03-20T07:23:40Z
I like this idea. Now that we have UDAs, let's make good use of them!
Comment #3 by andrej.mitrovich — 2013-06-16T16:43:38Z
This feature is also important if we want to move our tests into a separate module, but at the same time allow these tests to appear in the documentation.
For example:
-----
module mod.math;
///
int sum(int x, int y);
-----
-----
module mod.test;
/// @(mod.math.sum)
unittest
{
assert(sum(2, 2) == 4);
}
-----
In non-trivial modules which span hundreds (or thousands) of lines, it would be useful to be able to extract the tests into another directory, while still being able to use the documented unittests feature.
Comment #4 by robert.schadek — 2024-12-13T18:04:56Z