hamcrest-rust icon indicating copy to clipboard operation
hamcrest-rust copied to clipboard

More expressive error messages

Open ujh opened this issue 9 years ago • 3 comments

Enhance the error message format so that we can have a nicer error for the regular expression case. Right now it says:

Expected: \d
    but: was "abc"', src/core.rs:31

Something like Expected \d to match "abc" would be much nicer.

ujh avatar Sep 19 '16 09:09 ujh

Great crate! I agree the messages could be improved a bit... I'm not sure that nice sentences can always be produced, though, without a large amount of effort... What if the example you mentioned produced the following instead?

Expected: MatchesRegex("\d")
     Got: "abc"

joshburkart avatar Feb 17 '18 19:02 joshburkart

Also, in addition to perhaps tweaking how the matchers display themselves, one thought I had was to add a third (possibly optional) "Explanation" or "Details" section, in addition to "Expected" and "Got"... This might be nice in cases where it's not totally obvious why "expected" and "got" didn't match? For example, say we had a matcher for numerical vectors. It would be nice if the output of this:

let vector = vec![1., 1.000001, 0.99];
assert_that!(vector, is(almost_equal_to(1.).with_tol(1e-4)));

was perhaps

  Expected:   AlmostEqualTo(1., tol=1e-4)
       Got:   [1., 1.000001, 0.99]
   Details:   33% of values didn't match

Just thought I'd throw that out there...

joshburkart avatar Feb 17 '18 20:02 joshburkart

The Swift Hamcrest implementation prints error messages like this:

assertThat(CGPoint(x: 5, y: 10), hasProperty("x", closeTo(5.0, 0.00001))) // ✓
assertThat(CGPoint(x: 5, y: 10), hasProperty("y", closeTo(0.0, 0.00001)))
// GOT: (5.0,10.0) (property value 10.0 (difference of 10.0)),
// EXPECTED: has property "y" with value within 1e-05 of 0.0

So the "explanation" is put in parentheses in the "Got" section.

joshburkart avatar Feb 17 '18 20:02 joshburkart