Search code examples
c++structgoogletestmatchergooglemock

Gmock - matching structures with more than two vairables


Unlike the question in Gmock - matching structures, I'm wondring how I could create a matcher for a struct with >2 members. Let's say I've got a structure of 8 members, and that MyFun() takes a pointer to a SomeStruct_t as an argument.

typedef struct
{
  int data_1;
  int data_2;
  int data_3;
  int data_4;
  int data_5;
  int data_6;
  int data_7;
  int data_8;
} SomeStruct_t;

SomeStruct_t my_struct;

EXPECT_CALL(*myMock, MyFun(MyMatcher(my_struct)));

Do you have any suggestions/examples on how MyMatcher should be implemented? Or, could I solve this without using a matcher? I'd like to check each element of my_struct.


Solution

  • MATCHER_P(MyMatcher, MyStruct, "arg struct does not match expected struct")
    {
      return (arg.data_1 == MyStruct.data_1) && (arg.data_2 == MyStruct.data_2)  &&
        (arg.data_3 == MyStruct.data_3)  && (arg.data_4 == MyStruct.data_4)  &&
        (arg.data_5 == MyStruct.data_5)  && (arg.data_6 == MyStruct.data_6)  &&
        (arg.data_7 == MyStruct.data_7) && (arg.data_8 == MyStruct.data_8);
    }
    

    This solved my problem. Though it would be nice with more examples in the gmock documentaion.