I want to pass a string "Device Name" to a void *
pointer argument of a method and retrieve it to a character array later.
For this I've done as shown below.
Here I have created an action to achieve this.
ACTION_P(SetArg2ToChar, value) {*static_cast<char*>(arg2) = *value; }
Actual method to be called/mocked
bool getDictItem(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo)
My mock method
MOCK_METHOD5(getDictItem,
bool(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo));
in code it is called as
if( !can.getDictItem(wIndex, bSubIndex, pObjData, dwLength, tSdo) )
I want to pass a string to this pObjData
(3rd argument in the list).
In my google tests, I'm doing like this.
char szDeviceName[30]= {0};
snprintf(szDeviceName, sizeof(szDeviceName), "%s", "Device Name" );
EXPECT_CALL( mockCan, getDictItem(_,_,_,_,_) )
.WillOnce(DoAll(SetArg2ToChar(szDeviceName),
Return(true)))
.RetiresOnSaturation();
/* Call a real method within which this mock method is called */
If I try to set this argument(pObjData) using "SetArgPointee<2>" directly, I get the below error.
error: 'void' is not a pointer-to-object type*
Hence I'm trying with ACTION_P
Now with this implementation, I only get the first letter of the szDeviceName
variable (into this pObjData
) i.e., "D" followed by 29 0's in the real code flow after this mock object is called.
I want to get the full string name set into this void *
arguement.
I refered to this below question and was able progress this far. But I'm not able to pass the full string. How to set, in google mock, a void* argument to a set of values?
Any information regarding this will be helpful.