[Issue 11255] Support for inner unittests

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 28 00:09:17 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=11255

Witold Baryluk <witold.baryluk+d at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |witold.baryluk+d at gmail.com

--- Comment #1 from Witold Baryluk <witold.baryluk+d at gmail.com> ---
I agree, this would be useful. I often created inner functions or inner structs
in functions, as mini helpers. They are self contained, and only used in one
place, so they can be tested on their own, but I don't want to pollute the
module level symbols with it, which makes unittesting them harder.

I think it should only be allowed to be attached to static nested functions,
or when running otherwise, should only have access to static functions.

This probably also should apply to the nested structs and nested static
classes, and nested static structs.

Using it for non-static nested functions or aggregates, doesn't make sense,
because they can have access to some dynamic state, that is not available
before the program starts or executes these functions and initializes the
dynamic state.

The only issue I can think of it how to deal with instance of unittests in
templated functions and classes. This could create many "duplicates" of the
unittest, but it actually could be something good and intended.

So for example maybe these two things only for the start:


void main() {
    static int sqr(int x) { return x * x; }
    unittest {
        assert(sqr(3) == 9);
    }
}

class A {
    static class HelperClass {
       int y = 2;
       int m(int x) { return x * y; }
    }
    unittest {
        auto c = new HelperClass();
        assert(c.m(3) == 6);
    }
}


In the second case, maybe the unittest should be inside the nested class itself
maybe, or maybe it should have `static` prefix or something.

Named unittests should also work, and probably full name should be a scoped
path (i.e. `mod1.main.sqr.unittest.myunittest1`,
`mod1.A.HelperClass.myunittest1`).

--


More information about the Digitalmars-d-bugs mailing list