kindrich
kindrich

Reputation: 13

Hippomocks - mocking function with variable count of args

Hippomocks have OnCallFuncOverload macro for mocking overloaded function call. I'm trying to use for mocking function with variable count of args. Can anyone give an example for overloaded function with variable count of args?

My code

void Verbose(LPCTSTR pszFormat, ...); 
void Verbose(int level, LPCTSTR pszFormat, ...);
vlevel Verbose(vlevel level, LPCTSTR pszFormat, ...);

I'm trying this code

TEST_F(VerboseTests, test)
{
    MockRepository mocks;
    mocks.OnCallFuncOverload((void(*)(int,LPCTSTR,...))Verbose);
}

Compiler output:

hippomocks/hippomocks.h:3241:103: error: invalid conversion from ‘void (*)(int, LPCTSTR, ...) {aka void (*)(int, const char*, ...)}’ to ‘void (*)(int, const char*)’ [-fpermissive]
 #define OnCallFuncOverload(func) RegisterExpect_<__LINE__>(func, HM_NS Any, #func,  __FILE__, __LINE__)

Upvotes: 1

Views: 1516

Answers (1)

mrAtari
mrAtari

Reputation: 650

It does work. You have to convince the compiler with a typedef:

int Verbose(int level, ...)
{
    return level;
}

typedef int (*va_func)(int level, const char* p1, int p2);

TEST(check_VA_func)
{
    MockRepository mocks;

    mocks.ExpectCallFuncOverload((va_func)Verbose).Return(22);

    int result = Verbose(3,"Hi",9);

    CHECK(result == 22);

}

The drawback of this solution is, that you have to know at compile time, how many arguments will be called at runtime.

Upvotes: 1

Related Questions