Thread overview
[Issue 9730] New: Allow ddoc unittests to remotely reference declaration
Mar 20, 2013
Andrej Mitrovic
Jun 16, 2013
Andrej Mitrovic
March 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9730

           Summary: Allow ddoc unittests to remotely reference declaration
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: hsteoh@quickfur.ath.cx


--- Comment #0 from hsteoh@quickfur.ath.cx 2013-03-15 10:05:48 PDT ---
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);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9730


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-03-19 19:40:07 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9730



--- Comment #2 from hsteoh@quickfur.ath.cx 2013-03-20 07:23:40 PDT ---
I like this idea. Now that we have UDAs, let's make good use of them!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 16, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9730



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-06-16 16:43:38 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------