vegardit / haxe-doctest

A haxelib inspired by Python's doctest command that generates unit tests based on assertions specified within the source code.

Home Page:http://vegardit.github.io/haxe-doctest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

does not allow new line, tricky to make test always readable.

nanjizal opened this issue · comments

Currently hard to make tests readable? Perhaps there is a built in trick I could use?

This seems to work fine, but it's not very readable.

     /**
     * <pre><code>
     * >>> ({ var a=new Matrix1x2({x:3.,y:1.}); var b=new Matrix1x2({x:4.,y:2.}); var c=new Matrix1x2({x:3.5,y:1.5}); var mid=Matrix1x2.mid(a,b); Equal.equals(mid,c); }) == true
     * </code></pre>
     */
    public static inline
    function mid( a: Matrix1x2, b:Matrix1x2 ): Matrix1x2 {
        return new Matrix1x2( { x: ( a.x + b.x )/2, y:( a.y + b.y )/2 } );
    }

But this fails.

     /**
     * <pre><code>
     * >>> ({ 
                      var a = new Matrix1x2({ x: 3., y: 1. });
                      var b = new Matrix1x2({ x: 4., y: 2. });
                      var c = new Matrix1x2({ x: 3.5, y: 1.5 });
                      var mid = Matrix1x2.mid( a, b );
                      Equal.equals( mid, c ); }) == true
     * </code></pre>
     */
    public static inline
    function mid( a: Matrix1x2, b:Matrix1x2 ): Matrix1x2 {
        return new Matrix1x2( { x: ( a.x + b.x )/2, y:( a.y + b.y )/2 } );
    }

cleaned up one of my abstracts in geom, may take me awhile to do the whole library as still in progress, but I wrote up using cppia for quick static tests, which if you have not tried cppia maybe of interest first time I have tried it outside of NME.
https://github.com/nanjizal/geom/blob/master/readmeSetupCppia.md

You can now do this:

    /**
     * <pre><code>
     * >>> ({
     * ...    var o=new MyObject("cat");
     * ...    o.setData("dog");
     * ...    o.data;  // return data property outside expression block for comparison
     * ... }) == "dog"
     * </code></pre>
     */
    public function setData(data:String):Void {
        this.data = data;
    }

The approach seems to require remembering the special characters and referencing back to original library, feels kind of arbitary, not really user friendly enough.

My thoughts would be that from an api perspective something like this might be more user friendly but I am not sure if there are technical reasons against and obviously the effort of not having different search terms.

    /**
     * <pre><code>
     *** ({
     ***    var o=new MyObject("cat");
     ***    o.setData("dog");
     ***    o.data;  // return data property outside expression block for comparison
     *** }) == "dog"
     * </code></pre>
     */
    public function setData(data:String):Void {
        this.data = data;
    }

or perhaps since dot's are less distracting.

    /**
     * <pre><code>
     *. ({
     *.    var o=new MyObject("cat");
     *.    o.setData("dog");
     *.    o.data;  // return data property outside expression block for comparison
     *. }) == "dog"
     * </code></pre>
     */
    public function setData(data:String):Void {
        this.data = data;
    }

I know symantics not really important, just thinking in terms of keeping the library simple enough for users to want to use this approach, based on quick fiddle today. I initially tried --- instead of ... I know I will never remember and always have to refer to manual and >>> and ... making sure they are always in right place, just extra grammar and I think it could be simpler.

The code follows the same conventions as Python's doctest, where ... is used to indicate following lines.

However, you are free to customize that during test generation:

@:build(hx.doctest.DocTestGenerator.generateDocTests("src", ".*\\.hx", "* >>>", "* ..."))
@:build(utest.utils.TestBuilder.build())
class MyUTestDocTests extends utest.Test {

The last two parameters to the generateDocTests define the doctest line identifiers.