How to measure static test coverage?

Viewed 173

So, DLang (effectively) comes with code coverage built in. That's cool.

My issue is, I do a lot of metaprogramming. I tend to test my templates, by static asserts:

template CompileTimeFoo(size_t i)
{
    enum CompileTimeFoo = i+3;
}

unittest
{
    static assert(CompileTimeFoo!5 == 8);
}

Unfortunately (and obviously), when running tests with coverage, body of CompileTimeFoo will not be counted as "hits" nor "hittable" lines.

Now, let's consider slightly more complicated template:

enum IdentifierLength(alias X) = __traits(identifier, X).length;

void foo(){}

static assert(IdentifierLength!foo == 3);

In this case there still are no "hits", but there is one hittable line (where foo is defined). Because of that my coverage falls to 0% (in this example). If you look at this submodule of my pet project and into it's coverage on codecov, you'll see this exact case - I've prepared a not-that-bad test suite, yet it looks like the whole module is a wildland, because coverage is 0% (or close).

Disclaimer: I want to keep my tests in different source set. This is a matter of taste, but I dislike mixing tests with production code. I don't exactly know what would happen if I wrap foo in version(unittest){...}, but I expect that (since this code will be pushed to compiler) it wouldn't change much.

Again: I DO understand why that happens. I was wondering if there is some trick to work around that? Specifically: is there a way to calculate coverage for things that get called ONLY during compile time?

I can hack testing for coverage sake when I do mixins and just test code-generating things by comparing strings in runtime, but: 1. this would be ugly, and 2. it doesn't cover the case above.

0 Answers
Related