SetArgPointee with string literal

2,633 views
Skip to first unread message

Chris

unread,
Mar 15, 2011, 4:51:06 PM3/15/11
to Google C++ Mocking Framework
I'm trying to use the new functionality of SetArgPointee and string
literals and i'm having some compilation errors.

does anyone have a working example of how to do this?

i have the following expect call:

GMOCK_myClass myClass;

EXPECT_CALL(myClass, func(_)).WillOnce(SetArgPointee<0>("Hello"));

i get some error about an invalid conversion from a const char * to
char

Vlad Losev

unread,
Mar 15, 2011, 7:16:51 PM3/15/11
to Chris, Google C++ Mocking Framework
Chris,
Are you trying to copy the string into the memory pointed by func's parameter? What SetArgPointee does is essentially the assignment *func_arg = action_param. This will copy a char* pointer, provided func is declared as void func(const char**), but it will not copy a string into a location pointed by a const char* pointer. If you want that, you either need to have func be declared void func(string*) or have a custom action:

ACTION_P(StrCpyToArg0, str) { strcpy(arg0, str); }

or

ACTION_TEMPLATE(
    StrCpyToArg,
    HAS_1_TEMPLATE_PARAMS(int, k),
    AND_1_VALUE_PARAMS(str)) {
  strcpy(std::tr1::get<k>(args), str);
}

Then you will be able to write:

EXPECT_CALL(myClass, func(_)).WillOnce(StrCpyToArg<0>("Hello"));

The recent changes in gMock simply enabled one to pass literal strings into SetArgPointee. Prior to those changes, such attempt resulted in a compile error.

HTH,
Vlad
Reply all
Reply to author
Forward
0 new messages