googletest/googlemock/g3doc/includes/g3_matching_proto_buffers_cookbook_recipe.md
2021-01-13 20:58:40 -05:00

1.5 KiB

Matching Protocol Buffers

Many Google APIs pass protocol buffers around. gMock provides some matchers for protocol buffers. You can use them to specify that an argument must be equal (or equivalent) to a given protocol buffer.

EqualsProto(proto_buffer) matches an argument iff it's equal to proto_buffer, as determined by the Equals() method of the argument. The argument must be a protocol buffer; pointers must be dereferenced.

Sometimes we want to test for equivalence instead of equality, i.e. we want to use the Equivalent() method to compare two protocol buffers. For this we can use EquivToProto(proto_buffer).

It's worth noting that all of the matchers we mention here make a copy of proto_buffer. This means that you can use a matcher even if the original protocol buffer used for creating the matcher has been destroyed. Just one less thing for you to worry about!

Note that EqualsProto and EquivToProto work for both proto1 and proto2. They are declared in gmock.h, so you do not have to include other files. See go/protomatchers for more proto buffer matching goodies.

In addition: One application of Property() is testing protocol buffers:

`Property(&MyProto::has_size, true)` Matches `proto` where `proto.has_size()` returns `true`.
`Property(&MyProto::size, Gt(5))` Matches `proto` where `proto.size()` is greater than 5.