meekrosoft / fff

A testing micro framework for creating function test doubles
Other
761 stars 167 forks source link

How to obtain variadic function arguments past the fixed number of args? #57

Open ykoehler opened 5 years ago

ykoehler commented 5 years ago

Given a variadic function ABC(a,b,c,...) I can retrieve arg0_val, arg1_val and arg2_val, but past that I get no argument present.

Is there a way to capture the last arguments using something like args_val as a va_list or something like that?

rubiot commented 5 years ago

You can by using a custom fake fuction: For instance:

FAKE_VALUE_FUNC_VARARG(int, fprintf, FILE *, const char*, ...);

In order to access the variadic parameters from your custom fake function, declare a va_list parameter. For instance, a custom fake for fprintf() could call the real fprintf() like this:

int fprintf_custom(FILE *stream, const char *format, va_list ap) {
  if (fprintf0_fake.return_val < 0) // should we fail?
    return fprintf0_fake.return_val;
  return vfprintf(stream, format, ap);
}
ykoehler commented 5 years ago

Yes, I realized that afterwards, but I would prefer the other way since I could validate “after” running the function.

rubiot commented 5 years ago

You're talking about the call history, right? The problem is the variadic arg types are not known, so there's no way to declare an array to automatically store them. But if you can predict the types you can create you're own history using the custom fake.