Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock function with arguments as pointer

I have a function:

void setData(int *ptr) {
   *ptr = 3
};

Can I use Hippomock to mock this function and set the value of ptr? Something like: mock.OnCallFunc(setData).With(int *ptr).Do({ *ptr = 5;});

So I can do something like this later

int p;
setData(&p);
printf("value of p is suppose to be 5: %d\n", p);
like image 777
Samantha Avatar asked Sep 12 '25 12:09

Samantha


1 Answers

You can write with a normal function or with a lambda

void setDataMock(int *ptr) {
  *ptr = 5;
}
MockRepository mocks;
mocks.OnCallFunc(setData).Do(setDataMock);
// Or
mocks.OnCallFunc(setData).Do([](int *ptr) {
  *ptr = 5;
});

int p;
setData(&p);
printf("Value of p: %d\n", p);
like image 199
hidayat Avatar answered Sep 15 '25 03:09

hidayat