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

Test Comparable #129

Closed
xenoterracide opened this issue Sep 23, 2015 · 6 comments
Closed

Test Comparable #129

xenoterracide opened this issue Sep 23, 2015 · 6 comments

Comments

@xenoterracide
Copy link

It'd be a nice feature if EqualsVerifier could check that compareTo matches equals if an object is Comparable.

I'm not sure if this would, or should be anything more than (pseudocode)

this.equals( that ) && this instanceof Comparable && this.compareTo( that ) == 0
@mkordas
Copy link
Contributor

mkordas commented Sep 23, 2015

That would be really useful. Even in JDK many classes are violating this: http://stackoverflow.com/questions/30152202/are-there-any-standard-java-classes-with-inconsistent-compareto-and-equals

@jqno
Copy link
Owner

jqno commented Sep 24, 2015

I agree. I'll add it for the next version.

It might take a while before it's released though. I already started working on it, but I have a number of other complicated things I want to fix for this one too.

@xenoterracide
Copy link
Author

thought I'd check in on this

@jqno
Copy link
Owner

jqno commented Jul 30, 2016

Sorry for the long radio silence.

I've tried implementing this, but I ran into some problems that I didn't know how to solve. Then life intervened, you know how that goes :).

Maybe you have an idea about how to fix the problem, though. Let's take BigDecimal as an example. As you know, new BigDecimal("1") and new BigDecimal("1.0") are not equal, but they do compareTo 0. EqualsVerifier has a list of values for well-known types (the so-called 'prefab values') that it uses to see if objects are equal or not. In case of BigDecimal, it uses BigDecimal.ZERO and BigDecimal.ONE. With these values, the check you propose will always match. I would have to add an additional value for 1 that isn't equal to BigDecimal.ONE.

This is fine, of course, and I could do the same thing for other well-known classes like the ones mentioned in @mkordas 's StackOverflow link. But for Comparable classes that you make yourself, or that you pull in from a library, or that I simply forgot to add, the check will (probably) never match, because EqualsVerifier doesn't know which values to test (and I don't, either). That will give you, as a user, a false sense of security. You'll have to remember to add very specific prefab values every time you use such a class, and if you don't, EqualsVerifier can't tell you about it.

In other words, you will get so many false positives, that it's not really worth testing for it anymore, IMO.

Maybe you see a way out of this? If you do, I'd love to hear it :)!

@jqno
Copy link
Owner

jqno commented Sep 26, 2018

I want to do this right, or not at all. Since I haven't found a good way to do this right in a very long time, I'll go ahead and close this issue. The best way to test compareTo, IMO, is by using some form of property based testing.

Sorry about this.

@jqno jqno closed this as completed Sep 26, 2018
@jqno jqno added won't fix and removed accepted labels Sep 26, 2018
@TonioGela
Copy link

So sad about it :(

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