"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "googlemock/test/gmock-function-mocker_test.cc" between
googletest-release-1.11.0.tar.gz and googletest-release-1.12.0.tar.gz

About: GoogleTest is Google's (unit) testing and mocking framework for C++ tests.

gmock-function-mocker_test.cc  (googletest-release-1.11.0):gmock-function-mocker_test.cc  (googletest-release-1.12.0)
skipping to change at line 30 skipping to change at line 30
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Silence C4503 (decorated name length exceeded) for MSVC.
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4503)
#endif
// Google Mock - a framework for writing C++ mock classes. // Google Mock - a framework for writing C++ mock classes.
// //
// This file tests the function mocker classes. // This file tests the function mocker classes.
#include "gmock/gmock-function-mocker.h" #include "gmock/gmock-function-mocker.h"
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
// MSDN says the header file to be included for STDMETHOD is BaseTyps.h but // MSDN says the header file to be included for STDMETHOD is BaseTyps.h but
// we are getting compiler errors if we use basetyps.h, hence including // we are getting compiler errors if we use basetyps.h, hence including
// objbase.h for definition of STDMETHOD. // objbase.h for definition of STDMETHOD.
# include <objbase.h> #include <objbase.h>
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
#include <functional> #include <functional>
#include <map> #include <map>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
skipping to change at line 67 skipping to change at line 73
using testing::Const; using testing::Const;
using testing::DoDefault; using testing::DoDefault;
using testing::Eq; using testing::Eq;
using testing::Lt; using testing::Lt;
using testing::MockFunction; using testing::MockFunction;
using testing::Ref; using testing::Ref;
using testing::Return; using testing::Return;
using testing::ReturnRef; using testing::ReturnRef;
using testing::TypedEq; using testing::TypedEq;
template<typename T> template <typename T>
class TemplatedCopyable { class TemplatedCopyable {
public: public:
TemplatedCopyable() {} TemplatedCopyable() {}
template <typename U> template <typename U>
TemplatedCopyable(const U& other) {} // NOLINT TemplatedCopyable(const U& other) {} // NOLINT
}; };
class FooInterface { class FooInterface {
public: public:
virtual ~FooInterface() {} virtual ~FooInterface() {}
virtual void VoidReturning(int x) = 0; virtual void VoidReturning(int x) = 0;
virtual int Nullary() = 0; virtual int Nullary() = 0;
virtual bool Unary(int x) = 0; virtual bool Unary(int x) = 0;
virtual long Binary(short x, int y) = 0; // NOLINT virtual long Binary(short x, int y) = 0; // NOLINT
virtual int Decimal(bool b, char c, short d, int e, long f, // NOLINT virtual int Decimal(bool b, char c, short d, int e, long f, // NOLINT
float g, double h, unsigned i, char* j, float g, double h, unsigned i, char* j,
const std::string& k) = 0; const std::string& k) = 0;
virtual bool TakesNonConstReference(int& n) = 0; // NOLINT virtual bool TakesNonConstReference(int& n) = 0; // NOLINT
virtual std::string TakesConstReference(const int& n) = 0; virtual std::string TakesConstReference(const int& n) = 0;
virtual bool TakesConst(const int x) = 0; virtual bool TakesConst(const int x) = 0;
virtual int OverloadedOnArgumentNumber() = 0; virtual int OverloadedOnArgumentNumber() = 0;
virtual int OverloadedOnArgumentNumber(int n) = 0; virtual int OverloadedOnArgumentNumber(int n) = 0;
skipping to change at line 135 skipping to change at line 141
float g, double h, unsigned i, char* j, const std::string& k) = 0; float g, double h, unsigned i, char* j, const std::string& k) = 0;
STDMETHOD_(char, CTConst)(int x) const = 0; STDMETHOD_(char, CTConst)(int x) const = 0;
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
}; };
// Const qualifiers on arguments were once (incorrectly) considered // Const qualifiers on arguments were once (incorrectly) considered
// significant in determining whether two virtual functions had the same // significant in determining whether two virtual functions had the same
// signature. This was fixed in Visual Studio 2008. However, the compiler // signature. This was fixed in Visual Studio 2008. However, the compiler
// still emits a warning that alerts about this change in behavior. // still emits a warning that alerts about this change in behavior.
#ifdef _MSC_VER #ifdef _MSC_VER
# pragma warning(push) #pragma warning(push)
# pragma warning(disable : 4373) #pragma warning(disable : 4373)
#endif #endif
class MockFoo : public FooInterface { class MockFoo : public FooInterface {
public: public:
MockFoo() {} MockFoo() {}
// Makes sure that a mock function parameter can be named. // Makes sure that a mock function parameter can be named.
MOCK_METHOD(void, VoidReturning, (int n)); // NOLINT MOCK_METHOD(void, VoidReturning, (int n)); // NOLINT
MOCK_METHOD(int, Nullary, ()); // NOLINT MOCK_METHOD(int, Nullary, ()); // NOLINT
skipping to change at line 205 skipping to change at line 211
MOCK_METHOD(int, RefQualifiedConstRefRef, (), (const, ref(&&), override)); MOCK_METHOD(int, RefQualifiedConstRefRef, (), (const, ref(&&), override));
MOCK_METHOD(int, RefQualifiedRef, (), (ref(&), override)); MOCK_METHOD(int, RefQualifiedRef, (), (ref(&), override));
MOCK_METHOD(int, RefQualifiedRefRef, (), (ref(&&), override)); MOCK_METHOD(int, RefQualifiedRefRef, (), (ref(&&), override));
MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&), override)); MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&), override));
MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&&), override)); MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&&), override));
MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&), override)); MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&), override));
MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&&), override)); MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&&), override));
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); MockFoo(const MockFoo&) = delete;
MockFoo& operator=(const MockFoo&) = delete;
}; };
class LegacyMockFoo : public FooInterface { class LegacyMockFoo : public FooInterface {
public: public:
LegacyMockFoo() {} LegacyMockFoo() {}
// Makes sure that a mock function parameter can be named. // Makes sure that a mock function parameter can be named.
MOCK_METHOD1(VoidReturning, void(int n)); // NOLINT MOCK_METHOD1(VoidReturning, void(int n)); // NOLINT
MOCK_METHOD0(Nullary, int()); // NOLINT MOCK_METHOD0(Nullary, int()); // NOLINT
skipping to change at line 277 skipping to change at line 284
int RefQualifiedConstRef() const& override { return 0; } int RefQualifiedConstRef() const& override { return 0; }
int RefQualifiedConstRefRef() const&& override { return 0; } int RefQualifiedConstRefRef() const&& override { return 0; }
int RefQualifiedRef() & override { return 0; } int RefQualifiedRef() & override { return 0; }
int RefQualifiedRefRef() && override { return 0; } int RefQualifiedRefRef() && override { return 0; }
int RefQualifiedOverloaded() const& override { return 0; } int RefQualifiedOverloaded() const& override { return 0; }
int RefQualifiedOverloaded() const&& override { return 0; } int RefQualifiedOverloaded() const&& override { return 0; }
int RefQualifiedOverloaded() & override { return 0; } int RefQualifiedOverloaded() & override { return 0; }
int RefQualifiedOverloaded() && override { return 0; } int RefQualifiedOverloaded() && override { return 0; }
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockFoo); LegacyMockFoo(const LegacyMockFoo&) = delete;
LegacyMockFoo& operator=(const LegacyMockFoo&) = delete;
}; };
#ifdef _MSC_VER #ifdef _MSC_VER
# pragma warning(pop) #pragma warning(pop)
#endif #endif
template <class T> template <class T>
class FunctionMockerTest : public testing::Test { class FunctionMockerTest : public testing::Test {
protected: protected:
FunctionMockerTest() : foo_(&mock_foo_) {} FunctionMockerTest() : foo_(&mock_foo_) {}
FooInterface* const foo_; FooInterface* const foo_;
T mock_foo_; T mock_foo_;
}; };
skipping to change at line 495 skipping to change at line 503
EXPECT_EQ(std::move(mock_foo).RefQualifiedOverloaded(), 8); // NOLINT EXPECT_EQ(std::move(mock_foo).RefQualifiedOverloaded(), 8); // NOLINT
} }
class MockB { class MockB {
public: public:
MockB() {} MockB() {}
MOCK_METHOD(void, DoB, ()); MOCK_METHOD(void, DoB, ());
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB); MockB(const MockB&) = delete;
MockB& operator=(const MockB&) = delete;
}; };
class LegacyMockB { class LegacyMockB {
public: public:
LegacyMockB() {} LegacyMockB() {}
MOCK_METHOD0(DoB, void()); MOCK_METHOD0(DoB, void());
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockB); LegacyMockB(const LegacyMockB&) = delete;
LegacyMockB& operator=(const LegacyMockB&) = delete;
}; };
template <typename T> template <typename T>
class ExpectCallTest : public ::testing::Test {}; class ExpectCallTest : public ::testing::Test {};
using ExpectCallTestTypes = ::testing::Types<MockB, LegacyMockB>; using ExpectCallTestTypes = ::testing::Types<MockB, LegacyMockB>;
TYPED_TEST_SUITE(ExpectCallTest, ExpectCallTestTypes); TYPED_TEST_SUITE(ExpectCallTest, ExpectCallTestTypes);
// Tests that functions with no EXPECT_CALL() rules can be called any // Tests that functions with no EXPECT_CALL() rules can be called any
// number of times. // number of times.
TYPED_TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) { TYPED_TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
skipping to change at line 560 skipping to change at line 570
MOCK_METHOD(void, Push, (const T& elem), ()); MOCK_METHOD(void, Push, (const T& elem), ());
MOCK_METHOD(void, Pop, (), (final)); MOCK_METHOD(void, Pop, (), (final));
MOCK_METHOD(int, GetSize, (), (const, override)); MOCK_METHOD(int, GetSize, (), (const, override));
MOCK_METHOD(const T&, GetTop, (), (const)); MOCK_METHOD(const T&, GetTop, (), (const));
// Tests that the function return type can contain unprotected comma. // Tests that the function return type can contain unprotected comma.
MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (), ()); MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (), ());
MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (int), (const)); MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (int), (const));
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack); MockStack(const MockStack&) = delete;
MockStack& operator=(const MockStack&) = delete;
}; };
template <typename T> template <typename T>
class LegacyMockStack : public StackInterface<T> { class LegacyMockStack : public StackInterface<T> {
public: public:
LegacyMockStack() {} LegacyMockStack() {}
MOCK_METHOD1_T(Push, void(const T& elem)); MOCK_METHOD1_T(Push, void(const T& elem));
MOCK_METHOD0_T(Pop, void()); MOCK_METHOD0_T(Pop, void());
MOCK_CONST_METHOD0_T(GetSize, int()); // NOLINT MOCK_CONST_METHOD0_T(GetSize, int()); // NOLINT
MOCK_CONST_METHOD0_T(GetTop, const T&()); MOCK_CONST_METHOD0_T(GetTop, const T&());
// Tests that the function return type can contain unprotected comma. // Tests that the function return type can contain unprotected comma.
MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>()); MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>());
MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int)); // NOLINT MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int)); // NOLINT
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockStack); LegacyMockStack(const LegacyMockStack&) = delete;
LegacyMockStack& operator=(const LegacyMockStack&) = delete;
}; };
template <typename T> template <typename T>
class TemplateMockTest : public ::testing::Test {}; class TemplateMockTest : public ::testing::Test {};
using TemplateMockTestTypes = using TemplateMockTestTypes =
::testing::Types<MockStack<int>, LegacyMockStack<int>>; ::testing::Types<MockStack<int>, LegacyMockStack<int>>;
TYPED_TEST_SUITE(TemplateMockTest, TemplateMockTestTypes); TYPED_TEST_SUITE(TemplateMockTest, TemplateMockTestTypes);
// Tests that template mock works. // Tests that template mock works.
TYPED_TEST(TemplateMockTest, Works) { TYPED_TEST(TemplateMockTest, Works) {
TypeParam mock; TypeParam mock;
EXPECT_CALL(mock, GetSize()) EXPECT_CALL(mock, GetSize())
.WillOnce(Return(0)) .WillOnce(Return(0))
.WillOnce(Return(1)) .WillOnce(Return(1))
.WillOnce(Return(0)); .WillOnce(Return(0));
EXPECT_CALL(mock, Push(_)); EXPECT_CALL(mock, Push(_));
int n = 5; int n = 5;
EXPECT_CALL(mock, GetTop()) EXPECT_CALL(mock, GetTop()).WillOnce(ReturnRef(n));
.WillOnce(ReturnRef(n)); EXPECT_CALL(mock, Pop()).Times(AnyNumber());
EXPECT_CALL(mock, Pop())
.Times(AnyNumber());
EXPECT_EQ(0, mock.GetSize()); EXPECT_EQ(0, mock.GetSize());
mock.Push(5); mock.Push(5);
EXPECT_EQ(1, mock.GetSize()); EXPECT_EQ(1, mock.GetSize());
EXPECT_EQ(5, mock.GetTop()); EXPECT_EQ(5, mock.GetTop());
mock.Pop(); mock.Pop();
EXPECT_EQ(0, mock.GetSize()); EXPECT_EQ(0, mock.GetSize());
} }
TYPED_TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) { TYPED_TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
TypeParam mock; TypeParam mock;
const std::map<int, int> a_map; const std::map<int, int> a_map;
EXPECT_CALL(mock, ReturnTypeWithComma()) EXPECT_CALL(mock, ReturnTypeWithComma()).WillOnce(Return(a_map));
.WillOnce(Return(a_map)); EXPECT_CALL(mock, ReturnTypeWithComma(1)).WillOnce(Return(a_map));
EXPECT_CALL(mock, ReturnTypeWithComma(1))
.WillOnce(Return(a_map));
EXPECT_EQ(a_map, mock.ReturnTypeWithComma()); EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1)); EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
} }
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
// Tests mocking template interfaces with calltype. // Tests mocking template interfaces with calltype.
template <typename T> template <typename T>
class StackInterfaceWithCallType { class StackInterfaceWithCallType {
skipping to change at line 652 skipping to change at line 660
MockStackWithCallType() {} MockStackWithCallType() {}
MOCK_METHOD(void, Push, (const T& elem), MOCK_METHOD(void, Push, (const T& elem),
(Calltype(STDMETHODCALLTYPE), override)); (Calltype(STDMETHODCALLTYPE), override));
MOCK_METHOD(void, Pop, (), (Calltype(STDMETHODCALLTYPE), override)); MOCK_METHOD(void, Pop, (), (Calltype(STDMETHODCALLTYPE), override));
MOCK_METHOD(int, GetSize, (), (Calltype(STDMETHODCALLTYPE), override, const)); MOCK_METHOD(int, GetSize, (), (Calltype(STDMETHODCALLTYPE), override, const));
MOCK_METHOD(const T&, GetTop, (), MOCK_METHOD(const T&, GetTop, (),
(Calltype(STDMETHODCALLTYPE), override, const)); (Calltype(STDMETHODCALLTYPE), override, const));
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType); MockStackWithCallType(const MockStackWithCallType&) = delete;
MockStackWithCallType& operator=(const MockStackWithCallType&) = delete;
}; };
template <typename T> template <typename T>
class LegacyMockStackWithCallType : public StackInterfaceWithCallType<T> { class LegacyMockStackWithCallType : public StackInterfaceWithCallType<T> {
public: public:
LegacyMockStackWithCallType() {} LegacyMockStackWithCallType() {}
MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem)); MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem));
MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void()); MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void());
MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int()); MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int());
MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&()); MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&());
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockStackWithCallType); LegacyMockStackWithCallType(const LegacyMockStackWithCallType&) = delete;
LegacyMockStackWithCallType& operator=(const LegacyMockStackWithCallType&) =
delete;
}; };
template <typename T> template <typename T>
class TemplateMockTestWithCallType : public ::testing::Test {}; class TemplateMockTestWithCallType : public ::testing::Test {};
using TemplateMockTestWithCallTypeTypes = using TemplateMockTestWithCallTypeTypes =
::testing::Types<MockStackWithCallType<int>, ::testing::Types<MockStackWithCallType<int>,
LegacyMockStackWithCallType<int>>; LegacyMockStackWithCallType<int>>;
TYPED_TEST_SUITE(TemplateMockTestWithCallType, TYPED_TEST_SUITE(TemplateMockTestWithCallType,
TemplateMockTestWithCallTypeTypes); TemplateMockTestWithCallTypeTypes);
// Tests that template mock with calltype works. // Tests that template mock with calltype works.
TYPED_TEST(TemplateMockTestWithCallType, Works) { TYPED_TEST(TemplateMockTestWithCallType, Works) {
TypeParam mock; TypeParam mock;
EXPECT_CALL(mock, GetSize()) EXPECT_CALL(mock, GetSize())
.WillOnce(Return(0)) .WillOnce(Return(0))
.WillOnce(Return(1)) .WillOnce(Return(1))
.WillOnce(Return(0)); .WillOnce(Return(0));
EXPECT_CALL(mock, Push(_)); EXPECT_CALL(mock, Push(_));
int n = 5; int n = 5;
EXPECT_CALL(mock, GetTop()) EXPECT_CALL(mock, GetTop()).WillOnce(ReturnRef(n));
.WillOnce(ReturnRef(n)); EXPECT_CALL(mock, Pop()).Times(AnyNumber());
EXPECT_CALL(mock, Pop())
.Times(AnyNumber());
EXPECT_EQ(0, mock.GetSize()); EXPECT_EQ(0, mock.GetSize());
mock.Push(5); mock.Push(5);
EXPECT_EQ(1, mock.GetSize()); EXPECT_EQ(1, mock.GetSize());
EXPECT_EQ(5, mock.GetTop()); EXPECT_EQ(5, mock.GetTop());
mock.Pop(); mock.Pop();
EXPECT_EQ(0, mock.GetSize()); EXPECT_EQ(0, mock.GetSize());
} }
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
skipping to change at line 718 skipping to change at line 727
MOCK_CONST_METHOD1(Overloaded, int(int n)); \ MOCK_CONST_METHOD1(Overloaded, int(int n)); \
MOCK_METHOD2(Overloaded, bool(bool f, int n)) MOCK_METHOD2(Overloaded, bool(bool f, int n))
class MockOverloadedOnArgNumber { class MockOverloadedOnArgNumber {
public: public:
MockOverloadedOnArgNumber() {} MockOverloadedOnArgNumber() {}
MY_MOCK_METHODS1_; MY_MOCK_METHODS1_;
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber); MockOverloadedOnArgNumber(const MockOverloadedOnArgNumber&) = delete;
MockOverloadedOnArgNumber& operator=(const MockOverloadedOnArgNumber&) =
delete;
}; };
class LegacyMockOverloadedOnArgNumber { class LegacyMockOverloadedOnArgNumber {
public: public:
LegacyMockOverloadedOnArgNumber() {} LegacyMockOverloadedOnArgNumber() {}
LEGACY_MY_MOCK_METHODS1_; LEGACY_MY_MOCK_METHODS1_;
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockOverloadedOnArgNumber); LegacyMockOverloadedOnArgNumber(const LegacyMockOverloadedOnArgNumber&) =
delete;
LegacyMockOverloadedOnArgNumber& operator=(
const LegacyMockOverloadedOnArgNumber&) = delete;
}; };
template <typename T> template <typename T>
class OverloadedMockMethodTest : public ::testing::Test {}; class OverloadedMockMethodTest : public ::testing::Test {};
using OverloadedMockMethodTestTypes = using OverloadedMockMethodTestTypes =
::testing::Types<MockOverloadedOnArgNumber, ::testing::Types<MockOverloadedOnArgNumber,
LegacyMockOverloadedOnArgNumber>; LegacyMockOverloadedOnArgNumber>;
TYPED_TEST_SUITE(OverloadedMockMethodTest, OverloadedMockMethodTestTypes); TYPED_TEST_SUITE(OverloadedMockMethodTest, OverloadedMockMethodTestTypes);
TYPED_TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) { TYPED_TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
TypeParam mock; TypeParam mock;
EXPECT_CALL(mock, Overloaded()); EXPECT_CALL(mock, Overloaded());
EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2)); EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true)); EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
mock.Overloaded(); mock.Overloaded();
EXPECT_EQ(2, mock.Overloaded(1)); EXPECT_EQ(2, mock.Overloaded(1));
EXPECT_TRUE(mock.Overloaded(true, 1)); EXPECT_TRUE(mock.Overloaded(true, 1));
} }
#define MY_MOCK_METHODS2_ \ #define MY_MOCK_METHODS2_ \
MOCK_CONST_METHOD1(Overloaded, int(int n)); \ MOCK_CONST_METHOD1(Overloaded, int(int n)); \
MOCK_METHOD1(Overloaded, int(int n)) MOCK_METHOD1(Overloaded, int(int n))
class MockOverloadedOnConstness { class MockOverloadedOnConstness {
public: public:
MockOverloadedOnConstness() {} MockOverloadedOnConstness() {}
MY_MOCK_METHODS2_; MY_MOCK_METHODS2_;
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness); MockOverloadedOnConstness(const MockOverloadedOnConstness&) = delete;
MockOverloadedOnConstness& operator=(const MockOverloadedOnConstness&) =
delete;
}; };
TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) { TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
MockOverloadedOnConstness mock; MockOverloadedOnConstness mock;
const MockOverloadedOnConstness* const_mock = &mock; const MockOverloadedOnConstness* const_mock = &mock;
EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2)); EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3)); EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
EXPECT_EQ(2, mock.Overloaded(1)); EXPECT_EQ(2, mock.Overloaded(1));
EXPECT_EQ(3, const_mock->Overloaded(1)); EXPECT_EQ(3, const_mock->Overloaded(1));
} }
TEST(MockMethodMockFunctionTest, WorksForVoidNullary) { TEST(MockMethodMockFunctionTest, WorksForVoidNullary) {
MockFunction<void()> foo; MockFunction<void()> foo;
EXPECT_CALL(foo, Call()); EXPECT_CALL(foo, Call());
foo.Call(); foo.Call();
} }
TEST(MockMethodMockFunctionTest, WorksForNonVoidNullary) { TEST(MockMethodMockFunctionTest, WorksForNonVoidNullary) {
MockFunction<int()> foo; MockFunction<int()> foo;
EXPECT_CALL(foo, Call()) EXPECT_CALL(foo, Call()).WillOnce(Return(1)).WillOnce(Return(2));
.WillOnce(Return(1))
.WillOnce(Return(2));
EXPECT_EQ(1, foo.Call()); EXPECT_EQ(1, foo.Call());
EXPECT_EQ(2, foo.Call()); EXPECT_EQ(2, foo.Call());
} }
TEST(MockMethodMockFunctionTest, WorksForVoidUnary) { TEST(MockMethodMockFunctionTest, WorksForVoidUnary) {
MockFunction<void(int)> foo; MockFunction<void(int)> foo;
EXPECT_CALL(foo, Call(1)); EXPECT_CALL(foo, Call(1));
foo.Call(1); foo.Call(1);
} }
TEST(MockMethodMockFunctionTest, WorksForNonVoidBinary) { TEST(MockMethodMockFunctionTest, WorksForNonVoidBinary) {
MockFunction<int(bool, int)> foo; MockFunction<int(bool, int)> foo;
EXPECT_CALL(foo, Call(false, 42)) EXPECT_CALL(foo, Call(false, 42)).WillOnce(Return(1)).WillOnce(Return(2));
.WillOnce(Return(1)) EXPECT_CALL(foo, Call(true, Ge(100))).WillOnce(Return(3));
.WillOnce(Return(2));
EXPECT_CALL(foo, Call(true, Ge(100)))
.WillOnce(Return(3));
EXPECT_EQ(1, foo.Call(false, 42)); EXPECT_EQ(1, foo.Call(false, 42));
EXPECT_EQ(2, foo.Call(false, 42)); EXPECT_EQ(2, foo.Call(false, 42));
EXPECT_EQ(3, foo.Call(true, 120)); EXPECT_EQ(3, foo.Call(true, 120));
} }
TEST(MockMethodMockFunctionTest, WorksFor10Arguments) { TEST(MockMethodMockFunctionTest, WorksFor10Arguments) {
MockFunction<int(bool a0, char a1, int a2, int a3, int a4, MockFunction<int(bool a0, char a1, int a2, int a3, int a4, int a5, int a6,
int a5, int a6, char a7, int a8, bool a9)> foo; char a7, int a8, bool a9)>
foo;
EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _)) EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
.WillOnce(Return(1)) .WillOnce(Return(1))
.WillOnce(Return(2)); .WillOnce(Return(2));
EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true)); EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false)); EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
} }
TEST(MockMethodMockFunctionTest, AsStdFunction) { TEST(MockMethodMockFunctionTest, AsStdFunction) {
MockFunction<int(int)> foo; MockFunction<int(int)> foo;
auto call = [](const std::function<int(int)> &f, int i) { auto call = [](const std::function<int(int)>& f, int i) { return f(i); };
return f(i);
};
EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1)); EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1));
EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2)); EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2));
EXPECT_EQ(-1, call(foo.AsStdFunction(), 1)); EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
EXPECT_EQ(-2, call(foo.AsStdFunction(), 2)); EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
} }
TEST(MockMethodMockFunctionTest, AsStdFunctionReturnsReference) { TEST(MockMethodMockFunctionTest, AsStdFunctionReturnsReference) {
MockFunction<int&()> foo; MockFunction<int&()> foo;
int value = 1; int value = 1;
EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value)); EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value));
int& ref = foo.AsStdFunction()(); int& ref = foo.AsStdFunction()();
EXPECT_EQ(1, ref); EXPECT_EQ(1, ref);
value = 2; value = 2;
EXPECT_EQ(2, ref); EXPECT_EQ(2, ref);
} }
TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) { TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) {
MockFunction<int(int &)> foo; MockFunction<int(int&)> foo;
auto call = [](const std::function<int(int& )> &f, int &i) { auto call = [](const std::function<int(int&)>& f, int& i) { return f(i); };
return f(i);
};
int i = 42; int i = 42;
EXPECT_CALL(foo, Call(i)).WillOnce(Return(-1)); EXPECT_CALL(foo, Call(i)).WillOnce(Return(-1));
EXPECT_EQ(-1, call(foo.AsStdFunction(), i)); EXPECT_EQ(-1, call(foo.AsStdFunction(), i));
} }
namespace { namespace {
template <typename Expected, typename F> template <typename Expected, typename F>
static constexpr bool IsMockFunctionTemplateArgumentDeducedTo( static constexpr bool IsMockFunctionTemplateArgumentDeducedTo(
const internal::MockFunction<F>&) { const internal::MockFunction<F>&) {
skipping to change at line 890 skipping to change at line 898
TYPED_TEST( TYPED_TEST(
MockMethodMockFunctionSignatureTest, MockMethodMockFunctionSignatureTest,
IsMockFunctionCallMethodSignatureTheSameForRawSignatureAndStdFunction) { IsMockFunctionCallMethodSignatureTheSameForRawSignatureAndStdFunction) {
using ForRawSignature = decltype(&MockFunction<TypeParam>::Call); using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
using ForStdFunction = using ForStdFunction =
decltype(&MockFunction<std::function<TypeParam>>::Call); decltype(&MockFunction<std::function<TypeParam>>::Call);
EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value)); EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
} }
template <typename F> template <typename F>
struct AlternateCallable { struct AlternateCallable {};
};
TYPED_TEST(MockMethodMockFunctionSignatureTest, TYPED_TEST(MockMethodMockFunctionSignatureTest,
IsMockFunctionTemplateArgumentDeducedForAlternateCallable) { IsMockFunctionTemplateArgumentDeducedForAlternateCallable) {
using Argument = AlternateCallable<TypeParam>; using Argument = AlternateCallable<TypeParam>;
MockFunction<Argument> foo; MockFunction<Argument> foo;
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo)); EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
} }
TYPED_TEST( TYPED_TEST(MockMethodMockFunctionSignatureTest,
MockMethodMockFunctionSignatureTest, IsMockFunctionCallMethodSignatureTheSameForAlternateCallable) {
IsMockFunctionCallMethodSignatureTheSameForAlternateCallable) {
using ForRawSignature = decltype(&MockFunction<TypeParam>::Call); using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
using ForStdFunction = using ForStdFunction =
decltype(&MockFunction<std::function<TypeParam>>::Call); decltype(&MockFunction<std::function<TypeParam>>::Call);
EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value)); EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
} }
struct MockMethodSizes0 { struct MockMethodSizes0 {
MOCK_METHOD(void, func, ()); MOCK_METHOD(void, func, ());
}; };
struct MockMethodSizes1 { struct MockMethodSizes1 {
skipping to change at line 926 skipping to change at line 932
MOCK_METHOD(void, func, (int, int)); MOCK_METHOD(void, func, (int, int));
}; };
struct MockMethodSizes3 { struct MockMethodSizes3 {
MOCK_METHOD(void, func, (int, int, int)); MOCK_METHOD(void, func, (int, int, int));
}; };
struct MockMethodSizes4 { struct MockMethodSizes4 {
MOCK_METHOD(void, func, (int, int, int, int)); MOCK_METHOD(void, func, (int, int, int, int));
}; };
struct LegacyMockMethodSizes0 { struct LegacyMockMethodSizes0 {
MOCK_METHOD0(func, void()); MOCK_METHOD0(func, void());
}; };
struct LegacyMockMethodSizes1 { struct LegacyMockMethodSizes1 {
MOCK_METHOD1(func, void(int)); MOCK_METHOD1(func, void(int));
}; };
struct LegacyMockMethodSizes2 { struct LegacyMockMethodSizes2 {
MOCK_METHOD2(func, void(int, int)); MOCK_METHOD2(func, void(int, int));
}; };
struct LegacyMockMethodSizes3 { struct LegacyMockMethodSizes3 {
MOCK_METHOD3(func, void(int, int, int)); MOCK_METHOD3(func, void(int, int, int));
}; };
struct LegacyMockMethodSizes4 { struct LegacyMockMethodSizes4 {
MOCK_METHOD4(func, void(int, int, int, int)); MOCK_METHOD4(func, void(int, int, int, int));
}; };
TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) { TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) {
EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes1)); EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes1));
EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes2)); EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes2));
EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes3)); EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes3));
EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4)); EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4));
EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(LegacyMockMethodSizes1)); EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(LegacyMockMethodSizes1));
EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(LegacyMockMethodSizes2)); EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(LegacyMockMethodSizes2));
 End of changes. 33 change blocks. 
59 lines changed or deleted 65 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)