junit5
junit5 copied to clipboard
assertEquals with a diff
When I compare two strings
Assertions.assertEquals(expected, fb.toString());
there's no indication from the error what the difference is if its only whitespace.
So I need to compare the byte arrays instead.
Assertions.assertArrayEquals(expected.getBytes(), fb.toString().getBytes());
If there's a failure I'd like to see the string version (to make sure its roughly the correct string) AND the byte values, but I don't want to waste time comparing both of them to satisfy the test condition.
I guess there are many way to solve this. Maybe its possible to compare the values like assertEquals, and if the test fails, output as usual, but append a diff. "assertNoDiff", "assertEqualsDiff" etc.
Am I okay to have a crack at this, assuming no one else has picked it up?
Hey, can I also work on this issue?
@igmtz please have a go at it
@igmtz Before this can be started we need to agree an an approach. What did you have in mind? 🙂
If you would like us to be able to process this issue, please provide the requested information. If the information is not provided within the next 3 weeks, we will be unable to proceed and this issue will be closed.
@marcphilipp How about wrapping the empty strings in quotation marks and then displaying the two results?
If you would like us to be able to process this issue, please provide the requested information. If the information is not provided within the next 3 weeks, we will be unable to proceed and this issue will be closed.
Hi @marcphilipp, do you think the approach I wrote above would be suitable? Wrapping the strings in quotes - I'm worried about starting the work if it's not the best idea
@astocker-scottlogic they are not empty strings: they're strings that differ by whitespace only. The method would also be useful when glyphs have a similar appearance but are in fact different, i.e. different code points.
I've been in a situation where a colleague thought they were looking at the same string. But on my machine, using a different font, the difference was evident.
If you would like us to be able to process this issue, please provide the requested information. If the information is not provided within the next 3 weeks, we will be unable to proceed and this issue will be closed.
Some research:
JUnit 4 highlights the differences between two strings with square brackets (see ComparisonFailure
). For example:
org.junit.ComparisonFailure: message expected:<a[ ]c> but was:<a[ ]c>
Spock does this:
Condition not satisfied:
"a c" == "a\tc"
|
false
4 differences (33% similarity)
a( ~ )c
a(\t---)c
JUnit Jupiter currently does this:
org.opentest4j.AssertionFailedError: expected: <a c> but was: <a c>
We could extend the above message in case we're comparing Strings to show:
org.opentest4j.AssertionFailedError: expected: <a c> but was: <a c>
diff: a[ ]c
a[\t]c
That would replace all control characters with their symbol (\t
, \n
, ...), if known, or their unicode representation (e.g. \u0009
).
@delanym WDYT?
Looks good! Would the diff come in color? In which case it could be a single line (red square brackets green curly)
org.opentest4j.AssertionFailedError: expected: <a c> but was: <a c>
diff: a[ ]{\t}c
Git adds plus/minus
org.opentest4j.AssertionFailedError: expected: <a c> but was: <a c>
diff: a[- -]{+\t+}c
I don't think we can do color in the exception message. We could maybe do this in the ConsoleLauncher. There's a similar issue to output a diff there: #3139.
If you would like us to be able to process this issue, please provide the requested information. If the information is not provided within the next 3 weeks, we will be unable to proceed and this issue will be closed.
Closing due to lack of requested feedback. If you would like to proceed with your contribution, please provide the requested information and we will re-open this issue.