goconvey icon indicating copy to clipboard operation
goconvey copied to clipboard

Make ShouldEqual work for slices

Open ghost opened this issue 4 years ago • 4 comments
trafficstars

There's no point whatsoever in comparing slices by pointers, and it's very strange to see test results like this:

pic

GoConvey can type assert and, if both sides are slices, compare them like ShouldResemble.

ghost avatar May 08 '21 05:05 ghost

ShouldEqual is meant to correspond exactly to the behavior of ==. Sometimes you do actually want to compare the identity of container objects (say you had an algorithm which operated on a slice of slices, and you wanted to verify that it picked exactly the right one).

I do agree, however, that the error message in this case is confusing; It should report the addresses of the two objects in question in addition to their contents.

riannucci avatar May 08 '21 05:05 riannucci

You can't compare slices in Go with ==. Even if you compare pointers, the results aren't so unambiguous:

https://play.golang.org/p/Wq5Hs1z13qM

You can see that slices a and a[:1] seem to be equal, but they're different, despite the fact that they share their underlying array.

ghost avatar May 08 '21 08:05 ghost

We should refer to reflect.DeepEqual, why not?

cavanwang avatar Aug 08 '23 03:08 cavanwang

If we want to compare two object's address equal, we should add a new OP like 'ShouldAddrEqual' ?

cavanwang avatar Aug 08 '23 03:08 cavanwang