-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
There's a subtle issue when using assertions with protobuf structs, because protobuf structs contain internal fields that should be ignored during assertions.
An
XXX_sizecachefield that is part of the internal implementation of the table-driven serializer. The presence of this field can break tests that usereflect.DeepEqualto compare some received messages with some golden message. It is recommended thatproto.Equalbe used instead for this situation.
I think it'd be nice for testify to support this. Particularly, we can add this line in ObjectsAreEqual to check if both objects are protobuf messages, and if so, proto.Equal as suggested.
expProto, ok := expected.(proto.Message)
if ok {
actProto, ok := actual.(proto.Message)
if ok {
// if both are protobuf messages, use `proto.Equal`
return proto.Equal(expProto, actProto)
}
}The problem would be introducing a dependency on golang/protobuf, which I'm not sure would be acceptable.
If not, one way to address this is using build constraints. We can split the ObjectsAreEqual method into a file equal.go that is compiled by default. And with a build constraint, we can toggle whether to include the protobuf support:
equal.go:
// +build !protobufequal_protos.go:
// +build protobufThis way, we can include support for protobuf without introducing the dependency for everyone.