Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EqualProto Matcher #292

Closed
jaslong opened this issue Jul 20, 2018 · 3 comments
Closed

EqualProto Matcher #292

jaslong opened this issue Jul 20, 2018 · 3 comments

Comments

@jaslong
Copy link

jaslong commented Jul 20, 2018

Protobuf equality doesn't work with the default Equal matcher because protobufs have special fields. The best way to test protobuf equality is using proto.Equal. I'm proposing a simple matcher that uses protobuf.Equal.

Something like this:

// EqualProto succeeds if actual proto matches the passed-in proto.
func EqualProto(message proto.Message) types.GomegaMatcher {
	return &equalProtoMatcher{message: message}
}

type equalProtoMatcher struct {
	message proto.Message
}

func (matcher *equalProtoMatcher) Match(actual interface{}) (bool, error) {
	message, ok := actual.(proto.Message)
	if !ok {
		return false, fmt.Errorf("EqualProto matcher expects a proto message.  Got:\n%s", format.Object(actual, 1))
	}

	return proto.Equal(message, matcher.message), nil
}

func (matcher *equalProtoMatcher) FailureMessage(actual interface{}) string {
	return format.Message(actual, "to equal", matcher.message)
}

func (matcher *equalProtoMatcher) NegatedFailureMessage(actual interface{}) string {
	return format.Message(actual, "not to equal %v", matcher.message)
}
@jaslong
Copy link
Author

jaslong commented Oct 20, 2018

Related issue regarding time.Time: #264. Both time.Time and protobufs follow the convention of having an Equal method of the form "(T) Equal(T) bool". Thoughts about special casing this form of Equal method?

My team has gotten around this issue by creating a new matcher that uses https://godoc.org/github.com/google/go-cmp/cmp, which special cases the Equal method. Here's a gist: https://gist.github.com/jaslong/8852fb0ae5367484957dc9b6c33924d3

@MasslessParticle
Copy link

Why not make the Equals matcher look to see if the given value implements proto.Message, and then use proto.Equal under the hood?

@james-lawrence
Copy link

because then protocol buffers becomes a mandatory dependency even if you don't use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants