On Thursday, 24 November 2016 at 14:47:32 UTC, Jonathan M Davis wrote:
On Thursday, November 24, 2016 13:42:25 Kagamin via Digitalmars-d-learn wrote:
Linux? Probably another bug.
Try this:
unittest
{
     import core.exception : UnicodeException;
     void f()
     {
         string ret;
         int i = -1;
         ret ~= i;
     }

     try
     {
         f();
     }
     catch(UnicodeException e)
     {
         assert(e.msg == "Invalid UTF-8 sequence");
     }
}

If you're doing that, you might as well use std.exception.assertThrown or collectException. e.g.

assertThrown!UnicodeException(f());

or

assert(collectException!UnicodeException(f()).msg ==
       "Invalid UTF-8 sequence.");

You could also do the same with a lambda to avoid the explicit function.

Regardless, I don't know why the OP is hitting this problem, and unfortunately, I can't test it on my local machine at the moment to try and see what's going on.

- Jonathan M Davis

Thanks Kagamin and Jonathan for your suggestions!

I was able to hack my way around it. In druntime there are other modules which need the exact same thing and have their own assertThrown template:
https://github.com/dlang/druntime/search?utf8=%E2%9C%93&q=assertThrown

So I did the same thing and defined assertThrown in my unittest. Now it works fine:
unittest
{
    import core.exception : UnicodeException;

static void assertThrown(T : Throwable = Exception, E)(lazy E expr, string msg)
    {
        try
            expr;
        catch (T e) {
            assert(e.msg == msg);
            return;
        }
    }

    static void f()
    {
        string ret;
        int i = -1;
        ret ~= i;
    }

    assertThrown!UnicodeException(f(), "Invalid UTF-8 sequence");
}

It is still unclear why this approach is necessary.

Thanks,
Lucia

Reply via email to