[Issue 5547] New: Improve assert to give information on values given to it when it fails

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Feb 8 07:50:11 PST 2011


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

           Summary: Improve assert to give information on values given to
                    it when it fails
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: jmdavisProg at gmx.com


--- Comment #0 from Jonathan M Davis <jmdavisProg at gmx.com> 2011-02-08 07:47:45 PST ---
Right now, assert gives no useful information beyond the file and line number
where the failure occurred. It really should give information on what the
failure was. The recent attempt at getting assertPred in Phobos was to fix
this, but it was decided that it would be better to fix assert to do it. So,
essentially, when asserts like

assert(a == b);
assert(a.canFind(b));

fails, it should print out what the arguments to the expression were. For
instance, take this from the assertPred docs:

assert(collectExceptionMsg!AssertError(
            assertPred!"=="("hello", "goodbye")) ==
       `assertPred!"==" failed:` ~ "\n" ~
       `[hello] (lhs)` ~ "\n" ~
       `[goodbye] (rhs).`);

On failure assertPred would print out that == failed and that the values given
to it were hello and goodbye. Ideally, assert("hello" == "goodbye"); would
print something similar.

Another example would be

assert(collectExceptionMsg!AssertError(
            assertPred!((int a, int b, int c, float d){return a * b < c * d;})
                        (22, 4, 5, 1.7)) ==
       `assertPred failed: arguments: [22], [4], [5], [1.7].`);

Something similar to this - such as

assert((int a, int b, int c, float d){return a * b < c * d;}(22, 4, 5, 1.7));
or
assert(22 * 4 < 5 * 1.7);

- should print something similar to what assertPred printed.

Exactly what the best format should be and exactly what values will be best for
assert to print for more complicated expressions, I don't know - that will
depend on what is reasonable to implement - but assert should at least make a
valiant attempt at printing what the values in the expression were that failed
so that the programmer has sufficient debugging information not to have to go
and add a debug statement to print out the arguments, because the assert didn't
give enough information to properly debug the failure.

Also, it was pointed out that assertPred should print values such that they
line up on separate lines so that they're easier to compare (which it does in
the == case, though not in the case of the more general predicate - perhas it
should have). So, it would probably be a good idea for assert to do something
similar.

These improvements need to be made for assert to be properly usable for unit
tests. Otherwise, we're really going to need to add something like assertPred
to Phobos.

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