[Issue 20180] Deprecated unittests should not be deprecated functions

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 26 06:07:58 UTC 2020


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

--- Comment #4 from Mathias LANG <pro.mathias.lang at gmail.com> ---
Oh right. Yeah we probably need a way for framework to handle deprecated.
I wonder if it should be limited to unittests, or extended.
For example you could have a fuzzing framework that iterates over functions and
generate fuzzing code. You don't want to stop testing functions as soon as
they're deprecated, but you don't want to trigger the message either.

For the moment, a possible workaround is:
```
import std.traits;

deprecated @safe pure unittest
{

}

string funAttrToString (uint attrs)
{
    string result;
    if (attrs & FunctionAttribute.pure_)
        result ~= " pure";
    if (attrs & FunctionAttribute.nothrow_)
        result ~= " nothrow";
    if (attrs & FunctionAttribute.property)
        result ~= " @property";
    if (attrs & FunctionAttribute.trusted)
        result ~= " @trusted";
    if (attrs & FunctionAttribute.safe)
        result ~= " @safe";
    if (attrs & FunctionAttribute.nogc)
        result ~= " @nogc";
    if (attrs & FunctionAttribute.system)
        result ~= " @system";
    if (attrs & FunctionAttribute.const_)
        result ~= " const";
    if (attrs & FunctionAttribute.immutable_)
        result ~= " immutable";
    if (attrs & FunctionAttribute.inout_)
        result ~= " inout";
    if (attrs & FunctionAttribute.shared_)
        result ~= " shared";
    if (attrs & FunctionAttribute.return_)
        result ~= " return";
    return result;
}

void main () @safe pure
{
    static foreach (ut; __traits(getUnitTests, mixin(__MODULE__)))
    {
        static if (__traits(isDeprecated, ut))
        {
            mixin(`extern(C) void `, ut.mangleof, `()`,
funAttrToString(functionAttributes!ut),`;`);
            typeof(&ut) workaround = &mixin(__traits(identifier, ut));
            workaround();
        }
        else
            ut();
    }
}
```

However this relies on the fact that using `ut` in `__traits(identifier)` and
`typeof` does not trigger a deprecation.
Perhaps simply extending `__traits(isDeprecated)` to support:
`__traits(isDeprecated, ut, () { doThis(); })` would be enough.

--


More information about the Digitalmars-d-bugs mailing list