documented unit tests as examples

Andrej Mitrovic via Digitalmars-d digitalmars-d at puremagic.com
Sat May 14 06:32:45 PDT 2016


On 5/13/16, Steven Schveighoffer via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> I'm wondering if we can have a mechanism for documented unit tests to
> have a slightly different showing inside the docs vs. the actual unit test.
>
> For example, let's say we have a function writelnAssert. Used like this:
>
> writelnAssert(someText, "Text You Expect To Output");

Hmm.. I like the idea, but I have an alternate proposal. Add a new
compiler switch (or better yet use a built-in version switch) which
makes all assertions chatty.

So for a unittest like this:

----
auto sum (a, b) => a + b;

///
unittest { assert(sum(2, 2) == 4); }
----

Running it with something like this:

----
$ dmd -unittest -version=printasserts -run test.d
----

Would produce:

----
> test.d(4): assert(4 == 4); was successful
----

We could nitpick about the actual output, but the general idea is to
avoid having to modify all the unittests by hand and instead use a
compiler switch for chatty assertions.

And actually if we used a -version switch then I think we could
implement this entirely in Druntime. That is unless I remember
incorrectly how assertions are emitted, I haven't looked at the code
in a while.


More information about the Digitalmars-d mailing list