1

I am unable to compile the following code with gcc 6.4. gmock version is 1.8.1.

Maybe operator== here looks strange, but I cannot attach the full code due to its size.

#include <iostream>
#include <gmock/gmock.h>

template<class T>
constexpr bool operator==(T&&, T&&) noexcept
{
    return true;
}

namespace NNNN
{

struct Param2
{
    int i;
};

} // namespace NN

using ::testing::_;
using ::testing::Return;
using ::testing::AtMost;


struct Param
{
    int i;
};

struct A
{
    MOCK_METHOD1(f, int(const Param&));
};

struct B
{
    MOCK_METHOD1(f, int(const NNNN::Param2&));
};


TEST(Test, test)
{
    A a;
    EXPECT_CALL(a, f(Param{ 1 }));

    B b;
    std::cout << (NNNN::Param2{ 1 } == NNNN::Param2{ 2 }) << std::endl;
    EXPECT_CALL(b, f(NNNN::Param2{ 1 }));
}

int main(int argc, char** argv)
{
    ::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

The error I get:

In file included from /usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-spec-builders.h:71:0,
                 from /usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-generated-function-mockers.h:44,
                 from /usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock.h:62,
                 from /home/jzldfb/projects/uc_workspace/ultracruise/rte/utils/configuration/daemon/sub_module/test/TestApi2.cpp:7:
/usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-matchers.h: In instantiation of 'bool testing::internal::AnyEq::operator()(const A&, const B&) const [with A = NNNN::Param2; B = NNNN::Param2]':
/usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-matchers.h:1083:18:   required from 'bool testing::internal::ComparisonBase<D, Rhs, Op>::Impl<Lhs>::MatchAndExplain(Lhs, testing::MatchResultListener*) const [with Lhs = const NNNN::Param2&; D = testing::internal::EqMatcher<NNNN::Param2>; Rhs = NNNN::Param2; Op = testing::internal::AnyEq]'
/home/jzldfb/projects/uc_workspace/ultracruise/rte/utils/configuration/daemon/sub_module/test/TestApi2.cpp:60:1:   required from here
/usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-matchers.h:238:60: error: no match for 'operator==' (operand types are 'const NNNN::Param2' and 'const NNNN::Param2')
   bool operator()(const A& a, const B& b) const { return a == b; }

If I remove NNNN namespace, the compilation succeeds. If I add template wrapper for Param2 that calls to template operator== in its operator==, the compilation succeeds.

Quarra
  • 2,527
  • 1
  • 19
  • 27
Sasha Itin
  • 161
  • 1
  • 6

1 Answers1

1

Thanks for sending me to ADL page.

The solution is to add:

namespace testing {
namespace internal {
    using ::operator==;
} }

testing::internal it is a namespace in gmock where operator== is called. The code above instructs this namespace to look for operator== in global namespace.

Sasha Itin
  • 161
  • 1
  • 6
  • 1
    Usage of `internal` or `detail` namespace outside the code that declares it is always fishy to me. I'd rather declare the `operator==(const Param2&, const Param2&)` in `NNNN` namespace so that ADL works properly. Also it is a good coding practice to have `operator==` in the same namespace as the class it is defined for. if you don't need `operator==` in your production code, then I'd declare `MATCHER_P` for testing purpose only. – Quarra Oct 21 '20 at 08:01
  • Of course it is fishy. The problem is that my ```operator==``` is template and handles all the classes with specific ```SFINAE``` property, so it can't be defined in the concrete namespace. The real implementation is different from an example I provided. In this case, it is a bug in gmock. For example gtest contains ```using ::operator<<;``` – Sasha Itin Oct 21 '20 at 12:55