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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Aug 16 01:10:03 PDT 2010


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

           Summary: More unit test functions should be added - like
                    assertEqual() and assertNotEqual()
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: jmdavisProg at gmail.com


--- Comment #0 from Jonathan M Davis <jmdavisProg at gmail.com> 2010-08-16 01:10:00 PDT ---
Most unit test frameworks seem to have more functions for test verification
than just assert. assert is nice and all, but it doesn't do a very good job of
telling you what went wrong. A good example would be if you're checking for
equality between two objects. Ideally, the test failure would indicate what the
expected result and actual result were, so that you know _what_ was wrong and
can start tracking it down. Otherwise, you have to go edit the test to tell
you, recompile, and run it again. Having a function like assertEqual() would
make the tests clearer and be much more useful when a test fails. There are a
whole host of such functions which could be added - assertNull(),
assertNotNull(), assertLessThan(), assertGreaterThan(), etc. I don't know how
many we really want to add to Phobos, but it seems to me that adding a few such
functions would help make clearer tests. Here are possible implementations for
assertEqual() and assertNotEqual():

void assertEqual(T, U)(in T actual, U expected, string msg = null, string file
= __FILE__, size_t line = __LINE__)
    if(__traits(compiles, actual != expected) && __traits(compiles,
format("%s", actual)))
{
    if(actual != expected)
    {
        if(msg.empty)
            throw new AssertError(format("assertEquals() failed: actual <%s>,
expected <%s>", actual, expected), file, line);
        else
            throw new AssertError(format("assertEquals() failed: actual <%s>,
expected <%s> : %s", actual, expected, msg), file, line);
    }
}

void assertNotEqual(T, U)(in T actual, U expected, string msg = null, string
file = __FILE__, size_t line = __LINE__)
    if(__traits(compiles, actual == expected) && __traits(compiles,
format("%s", actual)))
{
    if(actual == expected)
    {
        if(msg.empty)
            throw new AssertError(format("assertNotEquals() failed: value
<%s>", actual), file, line);
        else
            throw new AssertError(format("assertNotEquals() failed: value <%s>
: %s", actual, msg), file, line);
    }
}

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