[Issue 4644] assertExceptionThrown to assert that a particular exception was thrown

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Aug 15 14:00:08 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4644


Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich at gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2010-08-15 14:00:01 PDT ---
Here, I made something similar to your request:

import std.stream, std.stdio;

auto assertExceptionThrown(alias func, alias exc, T...)(T args)
{
    auto funcName = __traits(identifier, func);

    try
    {
        func(args);
    }
    catch (exc e)
    {
    }
    catch (Exception e)
    {
        writefln("Error: Call to %s did not throw %s", funcName, typeid(exc));
        writeln(e);
    }
}

unittest
{
    assertExceptionThrown!(willThrow, core.exception.AssertError)(4);
}

void willThrow(int i)
{
    //~ assert(0);
    writefln("willThrow using its passed argument %s..", i);
    throw new SeekException("Oops a file exploded!");
}

void main()
{
}

If you uncomment the assert(0) statement in the willThrow function, the
unittest will run fine, since it catches an AssertError exception, the one you
passed with the call to assertExceptionThrown.

It's just a prototype, but try it out and let me know if it works (I'm an
exception newbie myself tbh..)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list