Skip to content

Support protobuf equality #758

@evie404

Description

@evie404

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_sizecache field that is part of the internal implementation of the table-driven serializer. The presence of this field can break tests that use reflect.DeepEqual to compare some received messages with some golden message. It is recommended that proto.Equal be 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 !protobuf

equal_protos.go:

// +build protobuf

This way, we can include support for protobuf without introducing the dependency for everyone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions