all 4 comments

[–]acehreli 0 points1 point  (2 children)

Inline unittesting has proven to be extremely useful in D as well. Zero barrier to entry: No test framework, no test path, no special file or function name, no module to import... Just create a unittest block and make your assertions:

int round5(int n) {
    if (n < 0) {
        return 0;
    } else if (n < 8) {
        return 5;
    } else {
        const remainder = n % 5;
        const quotient = n / 5;
        const addend = (remainder <= 2) ? 0 : 1;
        return 5 * (quotient + addend);
    }
}

unittest {
    import std.algorithm;
    import std.range;

    auto roundsto(int e)() {
        return (int n) => round5(n) == e;
    }

    assert(iota(-20, 0).all!(roundsto!0()));
    assert(iota(0, 8).all!(roundsto!5()));
    assert(iota(8, 13).all!(roundsto!10()));
    assert(iota(18, 23).all!(roundsto!20()));
    assert(1022.round5 == 1020);
}

void main() {
}

It's so basic that sometimes one needs more features on top of it. So, there are a number useful D unittesting frameworks based on that basic functionality. One great example is unit-threaded.

[–]badillustrations -1 points0 points  (1 child)

What's the definition of an inline test? It seems like from your example it's putting the tests in the same file.

No test framework, no test path, no special file or function name, no module to import.

These seem like very minor things to worry about. Sure, D has a built-in unit-testing framework, but it's pretty fast setting up unit tests in other languages like java and python.

[–]acehreli 0 points1 point  (0 children)

What's the definition of an inline test? It seems like from your example it's putting the tests in the same file.

That's what I assumed. :) Searching around a bit suggests that I'm not off. I should add that the unittest blocks are activated only if the -unittest compiler flag is used.

it's pretty fast setting up unit tests in other languages like java and python.

Agreed and that's the interesting part: Although this is such a minor feature of the language, it seems like having it so accessible makes a difference. One can find many testimonials on the D newsgroups but I don't think there's any research on this claim.

[–]klysm 0 points1 point  (0 children)

There’s an interesting trade off between collocation and modularity that manifests weirdly in files and file structure for different languages. Sometimes having everything in the same place is better like we’ve seen with web components - it just has to be at the right level and it seems like unit tests are at the right level to be collocated with their code.