Skip to content

Commit

Permalink
Googletest export
Browse files Browse the repository at this point in the history
Fix mocking method arguments with templated copy constructors.

A previous change removed workarounds for old compilers from googletest and googlemock. Unfortunately, a bit of code that started as a workaround for Symbian's C++ compiler is still needed to avoid copy/move constructor ambiguity when mocking functions with certain argument types.

The test case added by this CL is extracted from Chrome's codebase, and was discovered while attempting to roll googletest.

PiperOrigin-RevId: 229801765
  • Loading branch information
Abseil Team authored and gennadiycivil committed Jan 18, 2019
1 parent 0adeadd commit 9518a57
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
10 changes: 8 additions & 2 deletions googlemock/include/gmock/gmock-spec-builders.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ class OnCallSpec : public UntypedOnCallSpecBase {
const ArgumentMatcherTuple& matchers)
: UntypedOnCallSpecBase(a_file, a_line),
matchers_(matchers),
extra_matcher_(_) {}
// By default, extra_matcher_ should match anything. However,
// we cannot initialize it with _ as that causes ambiguity between
// Matcher's copy and move constructor for some argument types.
extra_matcher_(A<const ArgumentTuple&>()) {}

// Implements the .With() clause.
OnCallSpec& With(const Matcher<const ArgumentTuple&>& m) {
Expand Down Expand Up @@ -890,7 +893,10 @@ class TypedExpectation : public ExpectationBase {
: ExpectationBase(a_file, a_line, a_source_text),
owner_(owner),
matchers_(m),
extra_matcher_(_),
// By default, extra_matcher_ should match anything. However,
// we cannot initialize it with _ as that causes ambiguity between
// Matcher's copy and move constructor for some argument types.
extra_matcher_(A<const ArgumentTuple&>()),
repeated_action_(DoDefault()) {}

~TypedExpectation() override {
Expand Down
17 changes: 17 additions & 0 deletions googlemock/test/gmock-function-mocker_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ using testing::Return;
using testing::ReturnRef;
using testing::TypedEq;

template<typename T>
class TemplatedCopyable {
public:
TemplatedCopyable() {}

template <typename U>
TemplatedCopyable(const U& other) {} // NOLINT
};

class FooInterface {
public:
virtual ~FooInterface() {}
Expand Down Expand Up @@ -90,6 +99,7 @@ class FooInterface {

virtual int TypeWithHole(int (*func)()) = 0;
virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0;
virtual int TypeWithTemplatedCopyCtor(const TemplatedCopyable<int>&) = 0;

#if GTEST_OS_WINDOWS
STDMETHOD_(int, CTNullary)() = 0;
Expand Down Expand Up @@ -146,6 +156,8 @@ class MockFoo : public FooInterface {

MOCK_METHOD(int, TypeWithHole, (int (*)()), ()); // NOLINT
MOCK_METHOD(int, TypeWithComma, ((const std::map<int, std::string>&)));
MOCK_METHOD(int, TypeWithTemplatedCopyCtor,
(const TemplatedCopyable<int>&)); // NOLINT

#if GTEST_OS_WINDOWS
MOCK_METHOD(int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
Expand Down Expand Up @@ -288,6 +300,11 @@ TEST_F(MockMethodFunctionMockerTest, MocksReturnTypeWithComma) {
EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
}

TEST_F(MockMethodFunctionMockerTest, MocksTypeWithTemplatedCopyCtor) {
EXPECT_CALL(mock_foo_, TypeWithTemplatedCopyCtor(_)).WillOnce(Return(true));
EXPECT_TRUE(foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>()));
}

#if GTEST_OS_WINDOWS
// Tests mocking a nullary function with calltype.
TEST_F(MockMethodFunctionMockerTest, MocksNullaryFunctionWithCallType) {
Expand Down
18 changes: 18 additions & 0 deletions googlemock/test/gmock-generated-function-mockers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ using testing::Return;
using testing::ReturnRef;
using testing::TypedEq;

template<typename T>
class TemplatedCopyable {
public:
TemplatedCopyable() {}

template <typename U>
TemplatedCopyable(const U& other) {} // NOLINT
};

class FooInterface {
public:
virtual ~FooInterface() {}
Expand Down Expand Up @@ -91,6 +100,8 @@ class FooInterface {

virtual int TypeWithHole(int (*func)()) = 0;
virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0;
virtual int TypeWithTemplatedCopyCtor(
const TemplatedCopyable<int>& a_vector) = 0;

#if GTEST_OS_WINDOWS
STDMETHOD_(int, CTNullary)() = 0;
Expand Down Expand Up @@ -146,6 +157,8 @@ class MockFoo : public FooInterface {
MOCK_METHOD1(TypeWithHole, int(int (*)())); // NOLINT
MOCK_METHOD1(TypeWithComma,
int(const std::map<int, std::string>&)); // NOLINT
MOCK_METHOD1(TypeWithTemplatedCopyCtor,
int(const TemplatedCopyable<int>&)); // NOLINT

#if GTEST_OS_WINDOWS
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
Expand Down Expand Up @@ -288,6 +301,11 @@ TEST_F(FunctionMockerTest, MocksReturnTypeWithComma) {
EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
}

TEST_F(FunctionMockerTest, MocksTypeWithTemplatedCopyCtor) {
EXPECT_CALL(mock_foo_, TypeWithTemplatedCopyCtor(_)).WillOnce(Return(true));
EXPECT_TRUE(foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>()));
}

#if GTEST_OS_WINDOWS
// Tests mocking a nullary function with calltype.
TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
Expand Down

0 comments on commit 9518a57

Please sign in to comment.