[Issue 4653] More unit test functions should be added - like assertEqual() and assertNotEqual()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Feb 25 14:48:46 PST 2013


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



--- Comment #8 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-02-25 14:48:41 PST ---
I don't know whether assertEqual and other others will ever get in
(assertThrown/assertNotThrown did), but the implementation can be seriously
simplified.

Here's an example of some assert functions (without a custom user message, but
that could be easily added):

import core.exception;
import std.string;

template assertOp(string op)
{
    void assertOp(T1, T2)(T1 lhs, T2 rhs, 
                          string file = __FILE__, 
                          size_t line = __LINE__)
    {
        string msg = format("(%s %s %s) failed.", lhs, op, rhs);

        mixin(format(q{
            if (!(lhs %s rhs)) throw new AssertError(msg, file, line);
        }, op));
    }
}

alias assertOp!"==" assertEqual;
alias assertOp!"!=" assertNotEqual;
alias assertOp!">" assertGreaterThan;
alias assertOp!">=" assertGreaterThanOrEqual;

unittest
{
    assertEqual(1, 1);
    assertNotEqual(1, 2);
    assertGreaterThan(2, 1);
    assertGreaterThanOrEqual(2, 2);
}

That's a huge save of line numbers. Admittedly both of us have learned a few
tricks since 2010. :)

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