8#ifndef FUNCTION_MOCK_LIB_H_
9#define FUNCTION_MOCK_LIB_H_
19#define MOCK_INTERFACE_DECLARATION(MOCK) \
20 static MOCK * Instance; \
24#define MOCK_INTERFACE_DEFINITION(MOCK) \
25 MOCK_STATIC_INSTANCE_DEFINITION (MOCK) \
26 MOCK_INTERFACE_CONSTRUCTOR (MOCK) \
27 MOCK_INTERFACE_DECONSTRUCTOR (MOCK)
31#define MOCK_FUNCTION_DECLARATION(RET_TYPE, FUNC, ARGS) \
32 MOCK_FUNCTION_TYPE_DEFINITIONS(RET_TYPE, FUNC, ARGS) \
33 MOCK_METHOD (RET_TYPE, FUNC, ARGS);
37#define MOCK_FUNCTION_DEFINITION(MOCK, FUNC, NUM_ARGS, CALL_TYPE) \
38 FUNCTION_DEFINITION_TO_CALL_MOCK(MOCK, FUNC, FUNC, NUM_ARGS, CALL_TYPE)
42#define MOCK_FUNCTION_INTERNAL_DECLARATION(RET_TYPE, FUNC, ARGS) \
43 MOCK_FUNCTION_DECLARATION(RET_TYPE, FUNC, ARGS) \
44 MOCK_FUNCTION_HOOK_DECLARATIONS(FUNC)
49#define MOCK_FUNCTION_INTERNAL_DEFINITION(MOCK, FUNC, NUM_ARGS, CALL_TYPE) \
50 FUNCTION_DEFINITION_TO_CALL_MOCK(MOCK, FUNC, MOCK##_##FUNC, NUM_ARGS, CALL_TYPE) \
51 MOCK_FUNCTION_HOOK_DEFINITIONS(MOCK, FUNC)
56#define MOCK_FUNCTION_HOOK_DECLARATIONS(FUNC) \
57 static subhook::Hook Hook##FUNC; \
58 struct MockContainer_##FUNC { \
59 MockContainer_##FUNC (); \
60 ~MockContainer_##FUNC (); \
62 MockContainer_##FUNC MockContainerInst_##FUNC;
68#define MOCK_FUNCTION_HOOK_DEFINITIONS(MOCK, FUNC) \
69 subhook :: Hook MOCK :: Hook##FUNC; \
70 MOCK :: MockContainer_##FUNC :: MockContainer_##FUNC () { \
71 if (MOCK :: Instance == NULL) \
72 MOCK :: Hook##FUNC .Install( \
73 (FUNC##_ret_type *) ::FUNC, \
74 (FUNC##_ret_type *) MOCK##_##FUNC); \
76 MOCK :: MockContainer_##FUNC :: ~MockContainer_##FUNC () { \
77 MOCK :: Hook##FUNC .Remove(); \
80 std::is_same<decltype(&::FUNC), decltype(&MOCK##_##FUNC)>::value, \
81 "Function signature from 'MOCK_FUNCTION_INTERNAL_DEFINITION' macro " \
82 "invocation for '" #FUNC "' does not match the function signature " \
83 "of '" #FUNC "' function it is mocking. Mismatch could be due to " \
84 "different return type, arguments, or calling convention. See " \
85 "associated 'MOCK_FUNCTION_INTERNAL_DECLARATION' macro invocation " \
88#define MOCK_FUNCTION_TYPE_DEFINITIONS(RET_TYPE, FUNC, ARGS) \
89 using FUNC##_ret_type = RET_TYPE; \
90 using FUNC##_type = FUNC##_ret_type ARGS;
95#define FUNCTION_DEFINITION_TO_CALL_MOCK(MOCK, FUNC, FUNC_DEF_NAME, NUM_ARGS, CALL_TYPE) \
97 typename MOCK :: FUNC##_ret_type CALL_TYPE FUNC_DEF_NAME( \
98 GMOCK_PP_REPEAT(GMOCK_INTERNAL_PARAMETER, \
99 (MOCK :: FUNC##_type), \
102 EXPECT_TRUE(MOCK :: Instance != NULL) \
103 << "Called '" #FUNC "' in '" #MOCK "' function mock object before " \
104 << "an instance of '" #MOCK "' was created in test '" \
105 << ::testing::UnitTest::GetInstance()->current_test_info()->name() \
107 return MOCK :: Instance->FUNC( \
108 GMOCK_PP_REPEAT(GMOCK_INTERNAL_FORWARD_ARG, \
109 (MOCK :: FUNC##_type), \
114#define MOCK_STATIC_INSTANCE_DEFINITION(MOCK) MOCK * MOCK :: Instance = NULL;
116#define MOCK_INTERFACE_CONSTRUCTOR(MOCK) \
118 EXPECT_TRUE(MOCK :: Instance == NULL) \
119 << "Multiple instances of '" #MOCK "' function mock object were " \
120 << "created and only one instance is allowed in test '" \
121 << ::testing::UnitTest::GetInstance()->current_test_info()->name() \
123 MOCK :: Instance = this; \
126#define MOCK_INTERFACE_DECONSTRUCTOR(MOCK) \
127 MOCK :: ~ MOCK () { \
128 MOCK :: Instance = NULL; \