Search code examples
c++c++11googletest

gtest expect unordered elements are with custom comparator/matcher


I have a list of objects and a list of expected values that the objects can evaluate to after running them through a function.

std::vector<Objects> actual;
std::vector<Enum> expected;
bool equals(Object one, Enum two);

I want to be able to do something like UnorderedElementsAre with a matcher or use Pointwise without expecting an order.

I have a couple thoughts for solutions, but I'm looking for other suggestions (especially one's more native to gtest or that make the test easier to read)

  1. Sort actual then use Pointwise
  2. Convert all values in actual to the corresponding Enum value then do UnorderedElementsAre on that list and expected
  3. Write a matcher that hides 1 or 2

Solution

  • I know this is relatively an old question, anyway I think it's worth to mention that UnorderedPointwise container matcher is likely what you were looking for.