Skip to content

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

@ereisinger

Description

@ereisinger

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions