0

I have a function that expects a buffer length input in a pointer argument, and then puts the number of the read bytes into that same argument as output. Now in my unit test I try to mock it with Hippomocks, and would like to check if the function was called with the correct input value, and at the same time, provide a different output value. How could I achieve this? Thanks!

YaniMan
  • 293
  • 2
  • 13

2 Answers2

0

Picking in my memory since I haven't used Hippomocks in a long time, I can't give the exact syntax, but that's what Hippomocks' Do() method is for.

In Hippomocks' test suite you can find a usage example with a lambda expression: https://github.com/dascandy/hippomocks/blob/master/HippoMocksTest/test_do.cpp#L20.

Roland Sarrazin
  • 1,203
  • 11
  • 29
  • Thank you. I was expecting something like .With(InOut(inParam, outParam)), because it seems like something that is needed by more people than me, and would be easy to implement (for the maintainer of Hippomocks), but in my heart I knew that no such option exists. I managed to do it with your method & help, so thanks. – YaniMan Sep 25 '20 at 10:05
  • @YaniMan you can combine `.With()` and `.Do()`. I randomly found this example, have a look at the code's last line: https://stackoverflow.com/a/11773161/1945549. You can then expect the buffer pointer passing in `.With()` while using it to actually fill the buffer in `.Do()`. – Roland Sarrazin Sep 28 '20 at 05:52
0

You can do it for the function

void addingFunc(const int* inputArg_p, int* returnArg_p)
{
    *returnArg_p = *inputArg_p +1;
}

with

const int expected = 42;
int readbackActual = 0;
int readbackActual_p = &readbackActual;
const int writeValue = 1;
const int* writeValue_p = writeValue;

mocks.ExpectFuncCall(someFunc).With(Out(writeValue_p), In(readbackActual_p ); //Writes writeValue into *inputArg_p, then writes *returnArg_p into *readbackActual_p.

funcUnderTest(); //Function which uses addingFunc somehow.

ASSERT("FooBarError", expected == readbackActual); //Will fail: 42 is unequal to 1+1  !

(These unpretty pointers are used because Hippomocks complains about types when using &readbackActual directly.)

Dan
  • 61
  • 12