ujh / hamcrest-rust

A port of Hamcrest to rust
Apache License 2.0
93 stars 30 forks source link

More expressive error messages #36

Open ujh opened 8 years ago

ujh commented 8 years ago

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.

joshburkart commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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.