junit5 icon indicating copy to clipboard operation
junit5 copied to clipboard

assertEquals with a diff

Open delanym opened this issue 1 year ago • 15 comments

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.

delanym avatar Mar 15 '23 12:03 delanym

Am I okay to have a crack at this, assuming no one else has picked it up?

astocker-scottlogic avatar Apr 04 '23 15:04 astocker-scottlogic

Hey, can I also work on this issue?

igmtz avatar Apr 24 '23 21:04 igmtz

@igmtz please have a go at it

delanym avatar Apr 25 '23 10:04 delanym

@igmtz Before this can be started we need to agree an an approach. What did you have in mind? 🙂

marcphilipp avatar Apr 26 '23 14:04 marcphilipp

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.

stale[bot] avatar May 10 '23 16:05 stale[bot]

@marcphilipp How about wrapping the empty strings in quotation marks and then displaying the two results?

astocker-scottlogic avatar May 26 '23 10:05 astocker-scottlogic

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.

github-actions[bot] avatar Jun 10 '23 02:06 github-actions[bot]

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 avatar Jun 28 '23 09:06 astocker-scottlogic

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

delanym avatar Jun 28 '23 09:06 delanym

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.

github-actions[bot] avatar Jul 13 '23 02:07 github-actions[bot]

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?

marcphilipp avatar Jul 14 '23 11:07 marcphilipp

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

delanym avatar Jul 14 '23 12:07 delanym

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.

marcphilipp avatar Jul 17 '23 06:07 marcphilipp

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.

github-actions[bot] avatar Aug 01 '23 01:08 github-actions[bot]

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.

github-actions[bot] avatar Aug 23 '23 01:08 github-actions[bot]