Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking a function with default arguments values doesn't work #152

Open
ereisinger opened this issue Sep 14, 2021 · 0 comments
Open

Mocking a function with default arguments values doesn't work #152

ereisinger opened this issue Sep 14, 2021 · 0 comments

Comments

@ereisinger
Copy link

Hi,

does anybody maintain this project actively?
There is very little activity on this project.

I have to mock a function with default arguments. But it doesn't work with define CXXTEST_MOCK. It produces redefinition errors (default values in definitions) in test source file. I've tried the compilers xlC and g++. So it's not only an issue with our used xlC compiler.

For the real source file the default value has to be defined in Prototype/ARGS argument of CXXTEST_MOCK.
But in the test source file the CXXTEST_MOCK expands to

#define CXXTEST_MOCK( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
    __CXXTEST_MOCK__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
    __CXXTEST_MOCK__CLASS_DECLARATION( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
    __CXXTEST_MOCK__CLASS_IMPLEMENTATION( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
    using namespace dummy_mock_ns

where the ARGS (with the default value defined) is used in PROTOTYPE and IMPLEMENTATION. But in the IMPLEMENTATION the default values have not to be used (redefinition error).

I suggest a new define CXXTEST_MOCK_WITH_DEFAULT_ARGUMENT (for namespaces) with an additional argument ARGS_DEFINITION to separate the args for PROTOTYPE/CLASS_DECLARATION and CLASS_IMPLEMENTATION:

#define CXXTEST_MOCK_WITH_DEFAULT_ARGUMENT ( MOCK, TYPE, NAME, ARGS, ARGS_DEFINITION, REAL, CALL ) \
    __CXXTEST_MOCK__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
    __CXXTEST_MOCK__CLASS_DECLARATION( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
    __CXXTEST_MOCK__CLASS_IMPLEMENTATION( MOCK, TYPE, NAME, ARGS_DEFINITION, REAL, CALL ) \
    using namespace dummy_mock_ns

As a workaround I'm using the CXXTEST_MOCK implementation in the *Mock.H file:

// Function with default argument value.
#ifdef CXXTEST_MOCK_TEST_SOURCE_FILE
// Can't use CXXTEST_MOCK because of default argument redefinitions in __CXXTEST_MOCK__CLASS_IMPLEMENTATION
__CXXTEST_MOCK__PROTOTYPE(extver_CalcAccountingPeriodDelta, int, CalcAccountingPeriodDelta, (e_AccPeriod& period, char* sStart, char* sEnd, bool reinit = false), extver::CalcAccountingPeriodDelta, (period, sStart, sEnd, reinit) )
__CXXTEST_MOCK__CLASS_DECLARATION(extver_CalcAccountingPeriodDelta, int, CalcAccountingPeriodDelta, (e_AccPeriod& period, char* sStart, char* sEnd, bool reinit = false), extver::CalcAccountingPeriodDelta, (period, sStart, sEnd, reinit) )
__CXXTEST_MOCK__CLASS_IMPLEMENTATION(extver_CalcAccountingPeriodDelta, int, CalcAccountingPeriodDelta, (e_AccPeriod& period, char* sStart, char* sEnd, bool reinit), extver::CalcAccountingPeriodDelta, (period, sStart, sEnd, reinit) )
using namespace dummy_mock_ns
    ;
#else
CXXTEST_MOCK(extver_CalcAccountingPeriodDelta, int, CalcAccountingPeriodDelta, (e_AccPeriod& period, char* sStart, char* sEnd, bool reinit = false), extver::CalcAccountingPeriodDelta, (period, sStart, sEnd, reinit) );
#endif

// Function without default argument value.
CXXTEST_MOCK(extver_CalcAccountingPeriodRL3Delta, int, CalcAccountingPeriodRL3Delta, (e_AccPeriod& period, char* sStart, char* sEnd), extver::CalcAccountingPeriodRL3Delta, (period, sStart, sEnd) );

Cheers
Emanuel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant