Red Alert
Red Alert

Reputation: 3816

Matching a struct in gmock

I'm trying to get a simple gmock test going where I match against a struct. However, I am getting compiler errors in the internals of gmock.

To preface, I want to point out that I have defined the operator ==, and I can compare these structs myself using ==. Defined as :

inline bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{
    return true;
}

Here is the error given by g++:

In file included from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock-spec-builders.h:75:0,
                 from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock-generated-function-mockers.h:43,
                 from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock.h:61,
                 from /unit_test/tests.cpp:3:
/unit_test/gmock/gmock-1.7.0/include/gmock/gmock-matchers.h: In instantiation of ‘bool testing::internal::EqMatcher<Rhs>::Impl<Lhs>::MatchAndExplain(Lhs, testing::MatchResultListener*) const [with Lhs = MyStruct&; Rhs = MyStruct]’:
/unit_test/tests.cpp:115:1:   required from here
/unit_test/gmock/gmock-1.7.0/include/gmock/gmock-matchers.h:912:1: error: no match for ‘operator==’ in ‘lhs == ((const testing::internal::EqMatcher<MyStruct>::Impl<MyStruct&>*)this)->testing::internal::EqMatcher<MyStruct>::Impl<MyStruct&>::rhs_’
/unit_test/gmock/gmock-1.7.0/include/gmock/gmock-matchers.h:912:1: note: candidates are:
In file included from /unit_test/gmock/gmock-1.7.0/gtest/include/gtest/internal/gtest-param-util.h:45:0,
                 from /unit_test/gmock/gmock-1.7.0/gtest/include/gtest/gtest-param-test.h:192,
                 from /unit_test/gmock/gmock-1.7.0/gtest/include/gtest/gtest.h:62,
                 from /unit_test/tests.cpp:2:
/unit_test/gmock/gmock-1.7.0/gtest/include/gtest/internal/gtest-linked_ptr.h:213:6: note: template<class T> bool testing::internal::operator==(T*, const testing::internal::linked_ptr<T>&)
/unit_test/gmock/gmock-1.7.0/gtest/include/gtest/internal/gtest-linked_ptr.h:213:6: note:   template argument deduction/substitution failed:
In file included from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock-spec-builders.h:75:0,
                 from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock-generated-function-mockers.h:43,
                 from /unit_test/gmock/gmock-1.7.0/include/gmock/gmock.h:61,
                 from /unit_test/tests.cpp:3:
/unit_test/gmock/gmock-1.7.0/include/gmock/gmock-matchers.h:912:1: note:   mismatched types ‘T*’ and ‘MyStruct’
In file included from /ifs/MyStruct.h:14:0,
                 from /unit_test/mocks.h:1,
                 from /unit_test/tests.cpp:1:

What follows is a huge list of other operator ==s that I have defined, that aren't related to the struct I made. Things like:

 /ifs/types.h:2123:5: note: bool operator==(EnumType, const string&)
 /ifs/types.h:2123:5: note:   no known conversion for argument 1 from ‘MyStruct’ to ‘EnumType’
...etc

My mock method is defined as:

MOCK_METHOD1(send, int(MyStruct& data));

And I am trying to match using this:

MyStruct data;
if(data == data); // this compiles fine
EXPECT_CALL(mockObj, send(data)); // this does not compile, why?

Upvotes: 0

Views: 4973

Answers (2)

Redict
Redict

Reputation: 1

You could make the method available in the testing namespace as follows:

namespace testing::internal {
bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{
    return lhs.foo == rhs.foo;
}
}

Upvotes: 0

Michael Karcher
Michael Karcher

Reputation: 4041

It seems like the operator== is not available in the namespace(s) the Google Mock matcher is looking for. You need to declare it in the global namespace or the namespace MyStruct is defined in (so argument-dependent lookup works). You can't have it in a different non-global namespace than the one MyStruct is defined in, or as a member of the fixture.

Upvotes: 2

Related Questions