Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertEquals with a diff #3192

Open
delanym opened this issue Mar 15, 2023 · 15 comments
Open

assertEquals with a diff #3192

delanym opened this issue Mar 15, 2023 · 15 comments

Comments

@delanym
Copy link

delanym commented Mar 15, 2023

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.

@astocker-scottlogic
Copy link

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

@igmtz
Copy link

igmtz commented Apr 24, 2023

Hey, can I also work on this issue?

@delanym
Copy link
Author

delanym commented Apr 25, 2023

@igmtz please have a go at it

@marcphilipp
Copy link
Member

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

@stale
Copy link

stale bot commented May 10, 2023

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 stale bot added the status: stale label May 10, 2023
@astocker-scottlogic
Copy link

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

@github-actions
Copy link

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.

@astocker-scottlogic
Copy link

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

@delanym
Copy link
Author

delanym commented Jun 28, 2023

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

@github-actions
Copy link

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
Copy link
Member

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?

@delanym
Copy link
Author

delanym commented Jul 14, 2023

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

@marcphilipp
Copy link
Member

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.

@github-actions
Copy link

github-actions bot commented Aug 1, 2023

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

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 github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants