testify icon indicating copy to clipboard operation
testify copied to clipboard

impl EqualValues 2 objects have `(T) Equal(T) bool`

Open rubensayshi opened this issue 4 years ago • 3 comments

Summary

when using EqualValues for 2 objects which are of the same type and implement an (T) Equal(T) bool method, use the method to compare.

Tbh I'd almost put this in Equal, but EqualValues is more correct...

I chose not to implement supporting EqualValues(Foo{1}, &Foo{1}), though I do have a branch with it implemented ...

Motivation

This seems very much inline with many many other languages and makes it much easier to compare custom types, ranging from application level types to decimal.Decimal.

rubensayshi avatar Jun 28 '21 13:06 rubensayshi

We had to switch to https://github.com/google/go-cmp for the same reason.

arvenil avatar Jun 02 '23 12:06 arvenil

We had to switch to https://github.com/google/go-cmp for the same reason.

Do you mean that you integrated google's go-cmp with stretchr, or that you used go-cmp instead. I don't think that go-cmp is a replacement for stretchr.

rowland66 avatar Jul 14 '23 18:07 rowland66

rebased this old MR, I still think this is a good change! we've been using a fork for years now to have this change...

we use decimal.Decimal extensively and DeepEqual doesn't work well for those because the same decimal can be represented in different ways...

rubensayshi avatar Feb 18 '25 16:02 rubensayshi

I am firmly against the use of this Equal interface in testify's comparisons because there are examples of existing Equal methods which do not test that the instances are "equal", including in the standard library (see time.Time).

The Equal interface has no relation to the purpose of EqualValues which was to allow people to continue to try to compare types which are not comparable, accepting the false positive and negative matches which occur when you try to do that.

brackendawson avatar Mar 21 '25 12:03 brackendawson

@brackendawson so you might be open for a variant of his with EqualValues interface instead?

rubensayshi avatar Mar 21 '25 19:03 rubensayshi

No, we will not be changing the logic behind Equal functions any more.

brackendawson avatar Mar 22 '25 10:03 brackendawson