unittest questions

Johannes Pfau spam at example.com
Thu Aug 19 11:08:33 PDT 2010


Hi, I wrote some unittests using the built-in d unittest and a came
across 2 problems:

1) I have some code that accepts both delegates and functions. How can a
unittest explicitly check the function part? Whenever I add a function
in an unittest block it becomes a delegate.
---------------------------------------------------
void add(T)(T handler) if (is(T == void function())){}
void add(T)(T handler) if (is(T == void delegate())){}

unittest
{
    //always a delegate
    void handler() {};
    add(&handler);
}
----------------------------------------------------

2) I know Errors should not be caught. But when I expect a function to
throw in debug mode, but not necessarily in release mode (assert), I
have to check for both Errors and Exceptions --> Throwable. Is it OK to
catch Throwables in this case?
----------------------------------------------------
unittest
{
    void handler() {};
    bool thrown = false;
    try
        add(&handler);
    catch(Throwable)
        thrown = true;

    assert(thrown);
}
----------------------------------------------------

-- 
Johannes Pfau


More information about the Digitalmars-d-learn mailing list