Check if a repeated field in a protobuf message contains a value

Viewed 661

I'm doing some unit testing using gTest and gMock, trying to test if a function is passed a protobuf message which contains a specific value in a repeated field. I'm currently building an exact copy of the expected message, and then using google::protobuf::util::MessageDifferencer to expect that the exact message is received.

The issue is that I don't really want to compare the entire message; I only want to verify that a specific value exists in a repeated field. There could be other values in there I don't care about.

I was thinking about writing a custom matcher, something like

MATCHER_P(FieldFinder, expected, "")
{
  for (element : arg.repeated_field())
  {
    if (element == expected)
    {
      return true;
    }
  }
  return false;
}

But this matcher would be tied into this specific message structure and wouldn't let me reuse it if the repeated field is at a different location. Is there a better way to do this?

0 Answers
Related