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

Better error messages when things are not equal, but the result of toString() is the same. #449

Closed
yogurtearl opened this issue Mar 29, 2023 · 5 comments · Fixed by #499
Closed

Comments

@yogurtearl
Copy link
Contributor

Consider:

import androidx.compose.ui.text.AnnotatedString
...
        assertThat(AnnotatedString("asdf")).isEqualTo("asdf")

This correctly fails, but the error message can be a bit confusing.

as of 0.25, the error message is:

AssertionFailedError: expected:<["asdf"]> but was:<[asdf]>

A couple of thoughts.

  1. add the quotes for any CharSequence not just String. so that AnnotatedString would print as "asdf" (with quotes)
  2. Add type information to the failure message when the toString() result is the same, but they are not equal?
    AssertionFailedError: expected:<["asdf"]> (type `java.lang.String`) but was:<[asdf]> (type `androidx.compose.ui.text.AnnotatedString`)
    
@JakeWharton
Copy link
Contributor

JakeWharton commented Mar 29, 2023

The types can also be the same but have failing equality and matching string representation, so you still need something which highlights this fact.

Also, bonus points for:

Truth has all these and it's a great thing to copy.

@VirtualParticle
Copy link
Contributor

VirtualParticle commented May 24, 2023

Perhaps the best solution would be to just explicitly call out that the two objects are different but have the same toString result in the error message? Maybe something like:

class Example {
    override fun toString() = "hello"
}

assertThat(Example()).isEqualTo(Example())
// AssertionFailedError: expected (type: `Example`) and actual (type: `Example`) are not equal but have the same string representation: "hello"

I'm not sure of the best way to refer to each object there.

@JakeWharton
Copy link
Contributor

Another case where calling out the behavior is important is when equals is broken or identity-based on two otherwise-equivalent types. This will produce two equal outputs and the type of the two is the same. So it perhaps only behooves us to include the type when relevant, otherwise simply noting equals returned false but toString()s match.

@JakeWharton
Copy link
Contributor

JakeWharton commented Nov 29, 2023

Another real world example: JDK 20 (well, the CLDR update in it) changed formatting to use a narrow non-break space from a regular space. Very hard to catch until IntelliJ saved me https://jakewharton.com/@jw/111403629212976596

evant added a commit that referenced this issue Nov 29, 2023
if the types are different, add them to the error message, otherwise
just print out that they don't compare equal

Fixes #449
evant added a commit that referenced this issue Nov 29, 2023
if the types are different, add them to the error message, otherwise
just print out that they don't compare equal

Fixes #449
evant added a commit that referenced this issue Nov 29, 2023
if the types are different, add them to the error message, otherwise
just print out that they don't compare equal

Fixes #449
evant added a commit that referenced this issue Nov 30, 2023
if the types are different, add them to the error message, otherwise
just print out that they don't compare equal

Fixes #449
evant added a commit that referenced this issue Nov 30, 2023
if the types are different, add them to the error message, otherwise
just print out that they don't compare equal

Fixes #449
@evant
Copy link
Collaborator

evant commented Nov 30, 2023

The original issue should be fixed, @JakeWharton feel free to file new issues for any of the other improvements you suggested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants