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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Aug 16 13:05:02 PDT 2010


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



--- Comment #9 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2010-08-16 13:04:56 PDT ---
Good news, I've found a way to pass __LINE__ and __FILE__. It's a tad bit ugly
and uses a struct, I haven't been able to call a struct's custom constructor
without calling it with an extra parameter. I'm trying to call my struct
constructor without passing any arguments, but it doesn't seem to work. So I'm
using a workaround. Anyway, check it out:

void assertExceptionThrown(alias E, alias func, T...)(lineInfo info, T args)
    if(__traits(compiles, {try{}catch(E e){}}))
{
    try
        func(args);
    catch(E e)
        return;     // Expected exception was thrown

    throw new AssertError(format("assertExceptionThrown() failed: No %s was
thrown from %s()", E.stringof, __traits(identifier, func)), info._file,
info._line);
}


import std.stdio, std.stream, core.exception, std.string, std.typecons;

int myfunc(int i)
{
    return i;
}

struct lineInfo
{
    string _file;
    uint _line;

    this(int x, string file = __FILE__, uint line = __LINE__)
    {
        _file = file;
        _line = line;
    }
}


void main()
{
    assertExceptionThrown!(core.exception.AssertError, myfunc)(lineInfo(0) ,
5);
}

-- 
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