36 #include "gmock/gmock-generated-function-mockers.h" 43 #endif // GTEST_OS_WINDOWS 47 #include "gmock/gmock.h" 48 #include "gtest/gtest.h" 53 #if !GTEST_OS_WINDOWS || (_MSC_VER >= 1500) 54 # define GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS 55 #endif // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500) 58 namespace gmock_generated_function_mockers_test {
82 virtual bool Unary(
int x) = 0;
83 virtual long Binary(
short x,
int y) = 0;
84 virtual int Decimal(
bool b,
char c,
short d,
int e,
long f,
85 float g,
double h,
unsigned i,
char* j,
const string&
k)
90 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS 92 #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS 104 virtual int TypeWithComma(
const std::map<int, string>& a_map) = 0;
107 STDMETHOD_(
int, CTNullary)() = 0;
108 STDMETHOD_(
bool, CTUnary)(
int x) = 0;
109 STDMETHOD_(
int, CTDecimal)(
bool b,
char c,
short d,
int e,
long f,
110 float g,
double h,
unsigned i,
char* j,
const string&
k) = 0;
111 STDMETHOD_(
char, CTConst)(
int x)
const = 0;
112 #endif // GTEST_OS_WINDOWS 120 # pragma warning(push) 121 # pragma warning(disable : 4373) 136 double,
unsigned,
char*,
const string& str));
141 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS 146 MOCK_METHOD0(ReturnTypeWithComma, std::map<int, string>());
148 std::map<int, string>(
int));
166 short d,
int e,
long f,
float g,
double h,
unsigned i,
char* j,
172 std::map<int, string>());
173 #endif // GTEST_OS_WINDOWS 179 # pragma warning(pop) 193 foo_->VoidReturning(0);
227 Lt(100), 5U, NULL,
"hi"))
230 EXPECT_EQ(5, foo_->Decimal(
true,
'a', 0, 0, 1, 0, 0, 5, NULL,
"hi"));
246 .WillOnce(
Return(
"Hello"));
248 EXPECT_EQ(
"Hello", foo_->TakesConstReference(a));
251 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS 259 #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS 268 EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
269 EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
279 EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
280 EXPECT_EQ(
'b', foo_->OverloadedOnArgumentType(
'a'));
289 EXPECT_EQ(0, foo_->OverloadedOnConstness());
294 const std::map<int, string> a_map;
300 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma());
301 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
328 EXPECT_CALL(mock_foo_, CTDecimal(
true,
'a', 0, 0, 1L, A<float>(),
329 Lt(100), 5U, NULL,
"hi"))
332 EXPECT_EQ(10, foo_->CTDecimal(
true,
'a', 0, 0, 1, 0, 0, 5, NULL,
"hi"));
344 const std::map<int, string> a_map;
348 EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
351 #endif // GTEST_OS_WINDOWS 365 TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
384 template <
typename T>
391 virtual void Pop() = 0;
392 virtual int GetSize()
const = 0;
394 virtual const T& GetTop()
const = 0;
397 template <
typename T>
416 TEST(TemplateMockTest, Works) {
438 TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
441 const std::map<int, int> a_map;
447 EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
448 EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
454 template <
typename T>
455 class StackInterfaceWithCallType {
457 virtual ~StackInterfaceWithCallType() {}
461 STDMETHOD_(
void, Pop)() = 0;
462 STDMETHOD_(
int, GetSize)()
const = 0;
464 STDMETHOD_(
const T&, GetTop)()
const = 0;
467 template <
typename T>
468 class MockStackWithCallType :
public StackInterfaceWithCallType<T> {
470 MockStackWithCallType() {}
482 TEST(TemplateMockTestWithCallType, Works) {
483 MockStackWithCallType<int> mock;
503 #endif // GTEST_OS_WINDOWS 505 #define MY_MOCK_METHODS1_ \ 506 MOCK_METHOD0(Overloaded, void()); \ 507 MOCK_CONST_METHOD1(Overloaded, int(int n)); \ 508 MOCK_METHOD2(Overloaded, bool(bool f, int n)) 520 TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
531 #define MY_MOCK_METHODS2_ \ 532 MOCK_CONST_METHOD1(Overloaded, int(int n)); \ 533 MOCK_METHOD1(Overloaded, int(int n)); 545 TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
555 TEST(MockFunctionTest, WorksForVoidNullary) {
561 TEST(MockFunctionTest, WorksForNonVoidNullary) {
570 TEST(MockFunctionTest, WorksForVoidUnary) {
576 TEST(MockFunctionTest, WorksForNonVoidBinary) {
588 TEST(MockFunctionTest, WorksFor10Arguments) {
589 MockFunction<int(
bool a0,
char a1,
int a2,
int a3,
int a4,
590 int a5,
int a6,
char a7,
int a8,
bool a9)>
foo;
591 EXPECT_CALL(foo, Call(
_,
'a',
_,
_,
_,
_,
_,
_,
_,
_))
594 EXPECT_EQ(1, foo.Call(
false,
'a', 0, 0, 0, 0, 0,
'b', 0,
true));
595 EXPECT_EQ(2, foo.Call(
true,
'a', 0, 0, 0, 0, 0,
'b', 1,
false));
598 #if GTEST_HAS_STD_FUNCTION_ 599 TEST(MockFunctionTest, AsStdFunction) {
601 auto call = [](
const std::function<int(int)> &
f,
int i) {
610 TEST(MockFunctionTest, AsStdFunctionReturnsReference) {
614 int& ref = foo.AsStdFunction()();
619 #endif // GTEST_HAS_STD_FUNCTION_
MockOverloadedOnConstness()
#define MOCK_METHOD1_T(m,...)
#define MOCK_CONST_METHOD0_T_WITH_CALLTYPE(ct, m,...)
#define MOCK_CONST_METHOD1(m,...)
#define MOCK_METHOD10_WITH_CALLTYPE(ct, m,...)
MockOverloadedOnArgNumber()
virtual bool TakesNonConstReference(int &n)=0
#define MOCK_CONST_METHOD0_T(m,...)
internal::EqMatcher< T > Eq(T x)
#define MY_MOCK_METHODS1_
#define EXPECT_EQ(val1, val2)
#define MOCK_METHOD0_T(m,...)
virtual bool Unary(int x)=0
virtual bool TakesConst(const int x)=0
virtual long Binary(short x, int y)=0
internal::ReturnRefAction< R > ReturnRef(R &x)
internal::RefMatcher< T & > Ref(T &x)
#define MOCK_METHOD1_WITH_CALLTYPE(ct, m,...)
internal::LtMatcher< Rhs > Lt(Rhs x)
virtual int TypeWithHole(int(*func)())=0
#define MY_MOCK_METHODS2_
virtual string TakesConstReference(const int &n)=0
#define MOCK_METHOD10(m,...)
#define MOCK_METHOD0_WITH_CALLTYPE(ct, m,...)
#define MOCK_METHOD0_T_WITH_CALLTYPE(ct, m,...)
virtual int OverloadedOnConstness()=0
static const char *const value
virtual int OverloadedOnArgumentNumber()=0
virtual const T & GetTop() const =0
Matcher< Lhs > TypedEq(const Rhs &rhs)
#define EXPECT_TRUE(condition)
virtual int Decimal(bool b, char c, short d, int e, long f, float g, double h, unsigned i, char *j, const string &k)=0
GTEST_API_ Cardinality AnyNumber()
internal::DoDefaultAction DoDefault()
TEST_F(FunctionMockerTest, MocksVoidFunction)
#define MOCK_METHOD1_T_WITH_CALLTYPE(ct, m,...)
virtual int OverloadedOnArgumentType(int n)=0
#define MOCK_METHOD1(m,...)
#define MOCK_CONST_METHOD1_T(m,...)
virtual int GetSize() const =0
virtual void Push(const T &value)=0
#define MOCK_CONST_METHOD0(m,...)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
#define EXPECT_FALSE(condition)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
virtual ~StackInterface()
internal::GeMatcher< Rhs > Ge(Rhs x)
#define MOCK_CONST_METHOD1_WITH_CALLTYPE(ct, m,...)
virtual void VoidReturning(int x)=0
#define MOCK_METHOD2(m,...)
TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes)
TotalRequest HTTPRequestExchange HTTPResponseBodyRead PreConnect PostConnect DNSResolution DNSCache RetryingDNSResolution TCPConnect TLSSetup TotalConnect decompression_filter cert_verification proxy_connect Push
Future< bool > call(int depth, Executor *executor)
const T & Const(const T &x)
virtual int TypeWithComma(const std::map< int, string > &a_map)=0
internal::ReturnAction< R > Return(R value)
#define MOCK_METHOD0(m,...)