Gtest std::array matcher for custom type

427 Views Asked by At

I have union as

using my_union = union my_union {
    struct {
        int var1;
        int var2;
    };
    long
    var;
    bool operator == (const my_union & oth) {
        return var == oth.var;
    }
};
using my_union_1d = std::array < my_union, 2 > ;
using my_union_2d = std::array < my_union_1d, 3 > ;

I have some function which accept this array

class test {
    public:
        void Foo(my_union_2d & arg1);
};

Now I need to check if Mocked-Foo is called with my expected arguments

class TestMock: Public Test {
    public: MOCK_METHOD(void, Foo, (my_union_2d & ));
};
my_union_2d expected_args;
EXPECT_CALL( * (TestMock * ) obj, Foo(expected_args)).Times(1):

However code doesn't compile, as the array of array == operator is not found. I tried changing of AllOfArray, but I seem lost in the documentation for GTest. Does anyone know what needs to be done or a Reference point?

2

There are 2 best solutions below

1
On

Proper operator== that is a member function shall be a const method:

bool operator==(const my_union &oth) const { return var == oth.var;}
0
On

After some investigation, I was able to solve the compilation problem.

MATCHER_P(MyMatch, oth, "Arg is not the same")
{
    for (uint8_t outer=0; outer <3; outer++)
    {
        for (uint8_t inner = 0; inner < 2; inner++)
        {
            if ((arg[inner].var1 != oth[inner].var1) ||
                (arg[inner].var2 != oth[inner].var2))
                return 0;
        }
    }
    return 1;
}

And Calling as

EXPECT_CALL( * (TestMock * ) obj, Foo(MyMatch(expected_args))).Times(1);