google / go-cmp

Package for comparing Go values in tests
BSD 3-Clause "New" or "Revised" License
4.08k stars 209 forks source link

Add cmpopts.EquateComparable #340

Closed dsnet closed 10 months ago

dsnet commented 10 months ago

This helper function makes it easier to specify that comparable types are safe to directly compare with the == operator in Go.

The API does not use generics as it follows existing options like cmp.AllowUnexported, cmpopts.IgnoreUnexported, or cmpopts.IgnoreTypes.

While generics provides type safety, the user experience is not as nice. Our current API allows multiple types to be specified:

cmpopts.EquateComparable(netip.Addr{}, netip.Prefix{})

While generics would not allow variadic arguments:

cmpopts.EquateComparable[netip.Addr]()
cmpopts.EquateComparable[netip.Prefix]()

Bump mininimum supported Go to 1.18 for net/netip type. Start testing on Go 1.21.

Fixes #339

dsnet commented 10 months ago

Upgrading to Go 1.18 is not strictly necessary, if we're not ready to do that yet, I can adjust the tests accordingly. It's still worthwhile to upgrade as that would unblock some other cleanups.

dsnet commented 10 months ago

Hmmm... I can't submit this due to CI thinking it still needs to test on Go1.13 to Go 1.15. I feel like we had this issue before, but I don't remember what the resolution was.