TianoCore EDK2 master
Loading...
Searching...
No Matches
FunctionMockLib.h File Reference
#include <Library/GoogleTestLib.h>
#include <Library/SubhookLib.h>
#include <type_traits>

Go to the source code of this file.

Macros

#define MOCK_INTERFACE_DECLARATION(MOCK)
 
#define MOCK_INTERFACE_DEFINITION(MOCK)
 
#define MOCK_FUNCTION_DECLARATION(RET_TYPE, FUNC, ARGS)
 
#define MOCK_FUNCTION_DEFINITION(MOCK, FUNC, NUM_ARGS, CALL_TYPE)    FUNCTION_DEFINITION_TO_CALL_MOCK(MOCK, FUNC, FUNC, NUM_ARGS, CALL_TYPE)
 
#define MOCK_FUNCTION_INTERNAL_DECLARATION(RET_TYPE, FUNC, ARGS)
 
#define MOCK_FUNCTION_INTERNAL_DEFINITION(MOCK, FUNC, NUM_ARGS, CALL_TYPE)
 
#define MOCK_FUNCTION_HOOK_DECLARATIONS(FUNC)
 
#define MOCK_FUNCTION_HOOK_DEFINITIONS(MOCK, FUNC)
 
#define MOCK_FUNCTION_TYPE_DEFINITIONS(RET_TYPE, FUNC, ARGS)
 
#define FUNCTION_DEFINITION_TO_CALL_MOCK(MOCK, FUNC, FUNC_DEF_NAME, NUM_ARGS, CALL_TYPE)
 
#define MOCK_STATIC_INSTANCE_DEFINITION(MOCK)   MOCK * MOCK :: Instance = NULL;
 
#define MOCK_INTERFACE_CONSTRUCTOR(MOCK)
 
#define MOCK_INTERFACE_DECONSTRUCTOR(MOCK)
 

Detailed Description

This header allows the mocking of free (C style) functions using gmock.

Copyright (c) 2023, Intel Corporation. All rights reserved. SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file FunctionMockLib.h.

Macro Definition Documentation

◆ FUNCTION_DEFINITION_TO_CALL_MOCK

#define FUNCTION_DEFINITION_TO_CALL_MOCK (   MOCK,
  FUNC,
  FUNC_DEF_NAME,
  NUM_ARGS,
  CALL_TYPE 
)
Value:
extern "C" { \
typename MOCK :: FUNC##_ret_type CALL_TYPE FUNC_DEF_NAME( \
GMOCK_PP_REPEAT(GMOCK_INTERNAL_PARAMETER, \
(MOCK :: FUNC##_type), \
NUM_ARGS)) \
{ \
EXPECT_TRUE(MOCK :: Instance != NULL) \
<< "Called '" #FUNC "' in '" #MOCK "' function mock object before " \
<< "an instance of '" #MOCK "' was created in test '" \
<< ::testing::UnitTest::GetInstance()->current_test_info()->name() \
<< "'."; \
return MOCK :: Instance->FUNC( \
GMOCK_PP_REPEAT(GMOCK_INTERNAL_FORWARD_ARG, \
(MOCK :: FUNC##_type), \
NUM_ARGS)); \
} \
}
#define NULL
Definition: Base.h:319

Definition at line 95 of file FunctionMockLib.h.

◆ MOCK_FUNCTION_DECLARATION

#define MOCK_FUNCTION_DECLARATION (   RET_TYPE,
  FUNC,
  ARGS 
)
Value:
MOCK_FUNCTION_TYPE_DEFINITIONS(RET_TYPE, FUNC, ARGS) \
MOCK_METHOD (RET_TYPE, FUNC, ARGS);

Definition at line 31 of file FunctionMockLib.h.

◆ MOCK_FUNCTION_DEFINITION

#define MOCK_FUNCTION_DEFINITION (   MOCK,
  FUNC,
  NUM_ARGS,
  CALL_TYPE 
)     FUNCTION_DEFINITION_TO_CALL_MOCK(MOCK, FUNC, FUNC, NUM_ARGS, CALL_TYPE)

Definition at line 37 of file FunctionMockLib.h.

◆ MOCK_FUNCTION_HOOK_DECLARATIONS

#define MOCK_FUNCTION_HOOK_DECLARATIONS (   FUNC)
Value:
static subhook::Hook Hook##FUNC; \
struct MockContainer_##FUNC { \
MockContainer_##FUNC (); \
~MockContainer_##FUNC (); \
}; \
MockContainer_##FUNC MockContainerInst_##FUNC;

Definition at line 56 of file FunctionMockLib.h.

◆ MOCK_FUNCTION_HOOK_DEFINITIONS

#define MOCK_FUNCTION_HOOK_DEFINITIONS (   MOCK,
  FUNC 
)
Value:
subhook :: Hook MOCK :: Hook##FUNC; \
MOCK :: MockContainer_##FUNC :: MockContainer_##FUNC () { \
if (MOCK :: Instance == NULL) \
MOCK :: Hook##FUNC .Install( \
(FUNC##_ret_type *) ::FUNC, \
(FUNC##_ret_type *) MOCK##_##FUNC); \
} \
MOCK :: MockContainer_##FUNC :: ~MockContainer_##FUNC () { \
MOCK :: Hook##FUNC .Remove(); \
} \
static_assert( \
std::is_same<decltype(&::FUNC), decltype(&MOCK##_##FUNC)>::value, \
"Function signature from 'MOCK_FUNCTION_INTERNAL_DEFINITION' macro " \
"invocation for '" #FUNC "' does not match the function signature " \
"of '" #FUNC "' function it is mocking. Mismatch could be due to " \
"different return type, arguments, or calling convention. See " \
"associated 'MOCK_FUNCTION_INTERNAL_DECLARATION' macro invocation " \
"for more details.");

Definition at line 68 of file FunctionMockLib.h.

◆ MOCK_FUNCTION_INTERNAL_DECLARATION

#define MOCK_FUNCTION_INTERNAL_DECLARATION (   RET_TYPE,
  FUNC,
  ARGS 
)
Value:
MOCK_FUNCTION_DECLARATION(RET_TYPE, FUNC, ARGS) \
MOCK_FUNCTION_HOOK_DECLARATIONS(FUNC)

Definition at line 42 of file FunctionMockLib.h.

◆ MOCK_FUNCTION_INTERNAL_DEFINITION

#define MOCK_FUNCTION_INTERNAL_DEFINITION (   MOCK,
  FUNC,
  NUM_ARGS,
  CALL_TYPE 
)
Value:
FUNCTION_DEFINITION_TO_CALL_MOCK(MOCK, FUNC, MOCK##_##FUNC, NUM_ARGS, CALL_TYPE) \
MOCK_FUNCTION_HOOK_DEFINITIONS(MOCK, FUNC)

Definition at line 49 of file FunctionMockLib.h.

◆ MOCK_FUNCTION_TYPE_DEFINITIONS

#define MOCK_FUNCTION_TYPE_DEFINITIONS (   RET_TYPE,
  FUNC,
  ARGS 
)
Value:
using FUNC##_ret_type = RET_TYPE; \
using FUNC##_type = FUNC##_ret_type ARGS;

Definition at line 88 of file FunctionMockLib.h.

◆ MOCK_INTERFACE_CONSTRUCTOR

#define MOCK_INTERFACE_CONSTRUCTOR (   MOCK)
Value:
MOCK :: MOCK () { \
EXPECT_TRUE(MOCK :: Instance == NULL) \
<< "Multiple instances of '" #MOCK "' function mock object were " \
<< "created and only one instance is allowed in test '" \
<< ::testing::UnitTest::GetInstance()->current_test_info()->name() \
<< "'."; \
MOCK :: Instance = this; \
}

Definition at line 116 of file FunctionMockLib.h.

◆ MOCK_INTERFACE_DECLARATION

#define MOCK_INTERFACE_DECLARATION (   MOCK)
Value:
static MOCK * Instance; \
MOCK (); \
~MOCK ();

Definition at line 19 of file FunctionMockLib.h.

◆ MOCK_INTERFACE_DECONSTRUCTOR

#define MOCK_INTERFACE_DECONSTRUCTOR (   MOCK)
Value:
MOCK :: ~ MOCK () { \
MOCK :: Instance = NULL; \
}

Definition at line 126 of file FunctionMockLib.h.

◆ MOCK_INTERFACE_DEFINITION

#define MOCK_INTERFACE_DEFINITION (   MOCK)
Value:
MOCK_STATIC_INSTANCE_DEFINITION (MOCK) \
MOCK_INTERFACE_CONSTRUCTOR (MOCK) \
MOCK_INTERFACE_DECONSTRUCTOR (MOCK)

Definition at line 24 of file FunctionMockLib.h.

◆ MOCK_STATIC_INSTANCE_DEFINITION

#define MOCK_STATIC_INSTANCE_DEFINITION (   MOCK)    MOCK * MOCK :: Instance = NULL;

Definition at line 114 of file FunctionMockLib.h.