1
0
mirror of https://github.com/meekrosoft/fff synced 2026-01-23 08:25:59 +01:00

Merge branch 'master' into return_val_history

This commit is contained in:
Mike Long
2018-01-28 17:24:54 +00:00
committed by GitHub
4 changed files with 421 additions and 359 deletions

300
README.md
View File

@@ -13,46 +13,54 @@ is too short to spend time hand-writing fake functions for testing.
Say you are testing an embedded user interface and you have a function that Say you are testing an embedded user interface and you have a function that
you want to create a fake for: you want to create a fake for:
// UI.c ```c
... // UI.c
void DISPLAY_init(); ...
... void DISPLAY_init();
...
```
Here's how you would define a fake function for this in your test suite: Here's how you would define a fake function for this in your test suite:
// test.c(pp) ```c
#include "fff.h" // test.c(pp)
DEFINE_FFF_GLOBALS; #include "fff.h"
FAKE_VOID_FUNC(DISPLAY_init); DEFINE_FFF_GLOBALS;
FAKE_VOID_FUNC(DISPLAY_init);
```
And the unit test might look something like this: And the unit test might look something like this:
TEST_F(GreeterTests, init_initialises_display) ```c
{ TEST_F(GreeterTests, init_initialises_display)
{
UI_init(); UI_init();
ASSERT_EQ(DISPLAY_init_fake.call_count, 1); ASSERT_EQ(DISPLAY_init_fake.call_count, 1);
} }
```
So what has happened here? The first thing to note is that the framework is So what has happened here? The first thing to note is that the framework is
header only, all you need to do to use it is download <tt>fff.h</tt> and include header only, all you need to do to use it is download `fff.h` and include
it in your test suite. it in your test suite.
The magic is in the <tt>FAKE_VOID_FUNC</tt>. This The magic is in the `FAKE_VOID_FUNC`. This
expands a macro that defines a function returning <tt>void</tt> expands a macro that defines a function returning `void`
which has zero arguments. It also defines a struct which has zero arguments. It also defines a struct
<tt>"function_name"_fake</tt> which contains all the information about the fake. `"function_name"_fake` which contains all the information about the fake.
For instance, <tt>DISPLAY_init_fake.call_count</tt>is incremented every time the faked For instance, `DISPLAY_init_fake.call_count`is incremented every time the faked
function is called. function is called.
Under the hood it generates a struct that looks like this: Under the hood it generates a struct that looks like this:
typedef struct DISPLAY_init_Fake { ```c
typedef struct DISPLAY_init_Fake {
unsigned int call_count; unsigned int call_count;
unsigned int arg_history_len; unsigned int arg_history_len;
unsigned int arg_histories_dropped; unsigned int arg_histories_dropped;
void(*custom_fake)(); void(*custom_fake)();
} DISPLAY_init_Fake; } DISPLAY_init_Fake;
DISPLAY_init_Fake DISPLAY_init_fake; DISPLAY_init_Fake DISPLAY_init_fake;
```
@@ -62,56 +70,67 @@ Under the hood it generates a struct that looks like this:
Ok, enough with the toy examples. What about faking functions with arguments? Ok, enough with the toy examples. What about faking functions with arguments?
// UI.c ```c
... // UI.c
void DISPLAY_output(char * message); ...
... void DISPLAY_output(char * message);
...
```
Here's how you would define a fake function for this in your test suite: Here's how you would define a fake function for this in your test suite:
FAKE_VOID_FUNC(DISPLAY_output, char *); ```c
FAKE_VOID_FUNC(DISPLAY_output, char *);
```
And the unit test might look something like this: And the unit test might look something like this:
TEST_F(UITests, write_line_outputs_lines_to_display) ```c
{ TEST_F(UITests, write_line_outputs_lines_to_display)
{
char msg[] = "helloworld"; char msg[] = "helloworld";
UI_write_line(msg); UI_write_line(msg);
ASSERT_EQ(DISPLAY_output_fake.call_count, 1); ASSERT_EQ(DISPLAY_output_fake.call_count, 1);
ASSERT_EQ(strncmp(DISPLAY_output_fake.arg0_val, msg, 26), 0); ASSERT_EQ(strncmp(DISPLAY_output_fake.arg0_val, msg, 26), 0);
} }
```
There is no more magic here, the <tt>FAKE_VOID_FUNC</tt> works as in the There is no more magic here, the `FAKE_VOID_FUNC` works as in the
previous example. The number of arguments that the function takes is calculated, previous example. The number of arguments that the function takes is calculated,
and the macro arguments following the function name defines the argument and the macro arguments following the function name defines the argument
type (a char pointer in this example). type (a char pointer in this example).
A variable is created for every argument in the form A variable is created for every argument in the form
<tt>"function_name"fake.argN_val</tt> `"function_name"fake.argN_val`
## Return values ## Return values
When you want to define a fake function that returns a value, you should use the When you want to define a fake function that returns a value, you should use the
<tt>FAKE_VALUE_FUNC</tt> macro. For instance: `FAKE_VALUE_FUNC` macro. For instance:
// UI.c ```c
... // UI.c
unsigned int DISPLAY_get_line_capacity(); ...
unsigned int DISPLAY_get_line_insert_index(); unsigned int DISPLAY_get_line_capacity();
... unsigned int DISPLAY_get_line_insert_index();
...
```
Here's how you would define fake functions for these in your test suite: Here's how you would define fake functions for these in your test suite:
FAKE_VALUE_FUNC(unsigned int, DISPLAY_get_line_capacity); ```c
FAKE_VALUE_FUNC(unsigned int, DISPLAY_get_line_insert_index); FAKE_VALUE_FUNC(unsigned int, DISPLAY_get_line_capacity);
FAKE_VALUE_FUNC(unsigned int, DISPLAY_get_line_insert_index);
```
And the unit test might look something like this: And the unit test might look something like this:
TEST_F(UITests, when_empty_lines_write_line_doesnt_clear_screen) ```c
{ TEST_F(UITests, when_empty_lines_write_line_doesnt_clear_screen)
{
// given // given
DISPLAY_get_line_insert_index_fake.return_val = 1; DISPLAY_get_line_insert_index_fake.return_val = 1;
char msg[] = "helloworld"; char msg[] = "helloworld";
@@ -119,16 +138,21 @@ And the unit test might look something like this:
UI_write_line(msg); UI_write_line(msg);
// then // then
ASSERT_EQ(DISPLAY_clear_fake.call_count, 0); ASSERT_EQ(DISPLAY_clear_fake.call_count, 0);
} }
```
Of course you can mix and match these macros to define a value function with Of course you can mix and match these macros to define a value function with
arguments, for instance to fake: arguments, for instance to fake:
double pow(double base, double exponent); ```c
double pow(double base, double exponent);
```
you would use a syntax like this: you would use a syntax like this:
FAKE_VALUE_FUNC(double, pow, double, double); ```c
FAKE_VALUE_FUNC(double, pow, double, double);
```
@@ -139,19 +163,21 @@ unit test. All the fakes have a reset function to reset their arguments and
call counts. It is good practice is to call the reset function for all the call counts. It is good practice is to call the reset function for all the
fakes in the setup function of your test suite. fakes in the setup function of your test suite.
void setup() ```c
{ void setup()
{
// Register resets // Register resets
RESET_FAKE(DISPLAY_init); RESET_FAKE(DISPLAY_init);
RESET_FAKE(DISPLAY_clear); RESET_FAKE(DISPLAY_clear);
RESET_FAKE(DISPLAY_output_message); RESET_FAKE(DISPLAY_output_message);
RESET_FAKE(DISPLAY_get_line_capacity); RESET_FAKE(DISPLAY_get_line_capacity);
RESET_FAKE(DISPLAY_get_line_insert_index); RESET_FAKE(DISPLAY_get_line_insert_index);
} }
```
You might want to define a macro to do this: You might want to define a macro to do this:
``` ```c
/* List of fakes used by this unit tester */ /* List of fakes used by this unit tester */
#define FFF_FAKES_LIST(FAKE) \ #define FFF_FAKES_LIST(FAKE) \
FAKE(DISPLAY_init) \ FAKE(DISPLAY_init) \
@@ -172,16 +198,17 @@ void setup()
## Call history ## Call history
Say you want to test that a function calls functionA, then functionB, then Say you want to test that a function calls functionA, then functionB, then
functionA again, how would you do that? Well <tt>fff</tt> maintains a call functionA again, how would you do that? Well `fff` maintains a call
history so that it is easy to assert these expectations. history so that it is easy to assert these expectations.
Here's how it works: Here's how it works:
FAKE_VOID_FUNC(voidfunc2, char, char); ```c
FAKE_VALUE_FUNC(long, longfunc0); FAKE_VOID_FUNC(voidfunc2, char, char);
FAKE_VALUE_FUNC(long, longfunc0);
TEST_F(FFFTestSuite, calls_in_correct_order) TEST_F(FFFTestSuite, calls_in_correct_order)
{ {
longfunc0(); longfunc0();
voidfunc2(); voidfunc2();
longfunc0(); longfunc0();
@@ -189,30 +216,34 @@ Here's how it works:
ASSERT_EQ(fff.call_history[0], (void *)longfunc0); ASSERT_EQ(fff.call_history[0], (void *)longfunc0);
ASSERT_EQ(fff.call_history[1], (void *)voidfunc2); ASSERT_EQ(fff.call_history[1], (void *)voidfunc2);
ASSERT_EQ(fff.call_history[2], (void *)longfunc0); ASSERT_EQ(fff.call_history[2], (void *)longfunc0);
} }
```
They are reset by calling <tt>FFF_RESET_HISTORY();</tt> They are reset by calling `FFF_RESET_HISTORY();`
## Default Argument History ## Default Argument History
The framework will by default store the arguments for the last ten calls made The framework will by default store the arguments for the last ten calls made
to a fake function. to a fake function.
TEST_F(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history) ```c
{ TEST_F(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history)
{
voidfunc2('g', 'h'); voidfunc2('g', 'h');
voidfunc2('i', 'j'); voidfunc2('i', 'j');
ASSERT_EQ('g', voidfunc2_fake.arg0_history[0]); ASSERT_EQ('g', voidfunc2_fake.arg0_history[0]);
ASSERT_EQ('h', voidfunc2_fake.arg1_history[0]); ASSERT_EQ('h', voidfunc2_fake.arg1_history[0]);
ASSERT_EQ('i', voidfunc2_fake.arg0_history[1]); ASSERT_EQ('i', voidfunc2_fake.arg0_history[1]);
ASSERT_EQ('j', voidfunc2_fake.arg1_history[1]); ASSERT_EQ('j', voidfunc2_fake.arg1_history[1]);
} }
```
There are two ways to find out if calls have been dropped. The first is to There are two ways to find out if calls have been dropped. The first is to
check the dropped histories counter: check the dropped histories counter:
TEST_F(FFFTestSuite, when_fake_func_called_max_times_plus_one_then_one_argument_history_dropped) ```c
{ TEST_F(FFFTestSuite, when_fake_func_called_max_times_plus_one_then_one_argument_history_dropped)
{
int i; int i;
for(i = 0; i < 10; i++) for(i = 0; i < 10; i++)
{ {
@@ -220,11 +251,14 @@ check the dropped histories counter:
} }
voidfunc2('1', '2'); voidfunc2('1', '2');
ASSERT_EQ(1u, voidfunc2_fake.arg_histories_dropped); ASSERT_EQ(1u, voidfunc2_fake.arg_histories_dropped);
} }
```
The other is to check if the call count is greater than the history size: The other is to check if the call count is greater than the history size:
ASSERT(voidfunc2_fake.arg_history_len < voidfunc2_fake.call_count); ```c
ASSERT(voidfunc2_fake.arg_history_len < voidfunc2_fake.call_count);
```
The argument histories for a fake function are reset when the RESET_FAKE The argument histories for a fake function are reset when the RESET_FAKE
function is called function is called
@@ -232,14 +266,16 @@ function is called
## User Defined Argument History ## User Defined Argument History
If you wish to control how many calls to capture for argument history you can If you wish to control how many calls to capture for argument history you can
override the default by defining it before include the <tt>fff.h</tt> like this: override the default by defining it before include the `fff.h` like this:
// Want to keep the argument history for 13 calls ```c
#define FFF_ARG_HISTORY_LEN 13 // Want to keep the argument history for 13 calls
// Want to keep the call sequence history for 17 function calls #define FFF_ARG_HISTORY_LEN 13
#define FFF_CALL_HISTORY_LEN 17 // Want to keep the call sequence history for 17 function calls
#define FFF_CALL_HISTORY_LEN 17
#include "../fff.h" #include "../fff.h"
```
## Function Return Value Sequences ## Function Return Value Sequences
@@ -248,11 +284,12 @@ Often in testing we would like to test the behaviour of sequence of function cal
events. One way to do this with fff is to specify a sequence of return values events. One way to do this with fff is to specify a sequence of return values
with for the fake function. It is probably easier to describe with an example: with for the fake function. It is probably easier to describe with an example:
// faking "long longfunc();" ```c
FAKE_VALUE_FUNC(long, longfunc0); // faking "long longfunc();"
FAKE_VALUE_FUNC(long, longfunc0);
TEST_F(FFFTestSuite, return_value_sequences_exhausted) TEST_F(FFFTestSuite, return_value_sequences_exhausted)
{ {
long myReturnVals[3] = { 3, 7, 9 }; long myReturnVals[3] = { 3, 7, 9 };
SET_RETURN_SEQ(longfunc0, myReturnVals, 3); SET_RETURN_SEQ(longfunc0, myReturnVals, 3);
ASSERT_EQ(myReturnVals[0], longfunc0()); ASSERT_EQ(myReturnVals[0], longfunc0());
@@ -260,9 +297,10 @@ with for the fake function. It is probably easier to describe with an example:
ASSERT_EQ(myReturnVals[2], longfunc0()); ASSERT_EQ(myReturnVals[2], longfunc0());
ASSERT_EQ(myReturnVals[2], longfunc0()); ASSERT_EQ(myReturnVals[2], longfunc0());
ASSERT_EQ(myReturnVals[2], longfunc0()); ASSERT_EQ(myReturnVals[2], longfunc0());
} }
```
By specifying a return value sequence using the <tt>SET_RETURN_SEQ</tt> macro, By specifying a return value sequence using the `SET_RETURN_SEQ` macro,
the fake will return the values given in the parameter array in sequence. When the fake will return the values given in the parameter array in sequence. When
the end of the sequence is reached the fake will continue to return the last the end of the sequence is reached the fake will continue to return the last
value in the sequence indefinitely. value in the sequence indefinitely.
@@ -272,17 +310,19 @@ value in the sequence indefinitely.
You can specify your own function to provide the return value for the fake. This You can specify your own function to provide the return value for the fake. This
is done by setting the custom_fake member of the fake. Here's an example: is done by setting the custom_fake member of the fake. Here's an example:
#define MEANING_OF_LIFE 42 ```c
long my_custom_value_fake(void) #define MEANING_OF_LIFE 42
{ long my_custom_value_fake(void)
{
return MEANING_OF_LIFE; return MEANING_OF_LIFE;
} }
TEST_F(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_return_value) TEST_F(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_return_value)
{ {
longfunc0_fake.custom_fake = my_custom_value_fake; longfunc0_fake.custom_fake = my_custom_value_fake;
long retval = longfunc0(); long retval = longfunc0();
ASSERT_EQ(MEANING_OF_LIFE, retval); ASSERT_EQ(MEANING_OF_LIFE, retval);
} }
```
## Custom Return Value Delegate Sequences ## Custom Return Value Delegate Sequences
@@ -292,23 +332,24 @@ the value 'y' to the out parameter on the second call, and the value 'z' to the
on the third call. You can specify a sequence of custom functions to a non-variadic function on the third call. You can specify a sequence of custom functions to a non-variadic function
using the SET_CUSTOM_FAKE_SEQ macro. Here's an example: using the SET_CUSTOM_FAKE_SEQ macro. Here's an example:
void voidfunc1outparam_custom_fake1(char *a) ```c
{ void voidfunc1outparam_custom_fake1(char *a)
{
*a = 'x'; *a = 'x';
} }
void voidfunc1outparam_custom_fake2(char *a) void voidfunc1outparam_custom_fake2(char *a)
{ {
*a = 'y'; *a = 'y';
} }
void voidfunc1outparam_custom_fake3(char *a) void voidfunc1outparam_custom_fake3(char *a)
{ {
*a = 'z'; *a = 'z';
} }
TEST_F(FFFTestSuite, custom_fake_sequence_not_exausthed) TEST_F(FFFTestSuite, custom_fake_sequence_not_exausthed)
{ {
void (*custom_fakes[])(char *) = {voidfunc1outparam_custom_fake1, void (*custom_fakes[])(char *) = {voidfunc1outparam_custom_fake1,
voidfunc1outparam_custom_fake2, voidfunc1outparam_custom_fake2,
voidfunc1outparam_custom_fake3}; voidfunc1outparam_custom_fake3};
@@ -322,7 +363,8 @@ using the SET_CUSTOM_FAKE_SEQ macro. Here's an example:
ASSERT_EQ('y', a); ASSERT_EQ('y', a);
voidfunc1outparam(&a); voidfunc1outparam(&a);
ASSERT_EQ('z', a); ASSERT_EQ('z', a);
} }
```
The fake will call your custom functions in the order specified by the SET_CUSTOM_FAKE_SEQ The fake will call your custom functions in the order specified by the SET_CUSTOM_FAKE_SEQ
macro. When the last custom fake is reached the fake will keep calling the last custom macro. When the last custom fake is reached the fake will keep calling the last custom
@@ -349,24 +391,42 @@ you use a sequence of custom fakes. Here's a simple example:
You access the returned values in the <tt>return_val_history</tt> field. You access the returned values in the <tt>return_val_history</tt> field.
## Variadic Functions
You can fake variadic functions using the macros <tt>FAKE_VALUE_FUNC_VARARG</tt>
and <tt>FAKE_VOID_FUNC_VARARG</tt>. For instance:
FAKE_VALUE_FUNC_VARARG(int, fprintf, FILE *, const char*, ...);
In order to access the variadic parameters from a custom fake function, declare a
<tt>va_list</tt> parameter. For instance, a custom fake for <tt>fprintf()</tt>
could call the real <tt>fprintf()</tt> 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);
}
## How do I fake a function that returns a value by reference? ## How do I fake a function that returns a value by reference?
The basic mechanism that FFF provides you in this case is the custom_fake field described in the *Custom Return Value Delegate* example above. The basic mechanism that FFF provides you in this case is the custom_fake field described in the *Custom Return Value Delegate* example above.
You need to create a custom function (e.g. getTime_custom_fake) to produce the output optionally by use of a helper variable (e.g. getTime_custom_now) to retrieve that output from. Then some creativity to tie it all together. The most important part (IMHO) is to keep your test case readable and maintainable. You need to create a custom function (e.g. getTime_custom_fake) to produce the output optionally by use of a helper variable (e.g. getTime_custom_now) to retrieve that output from. Then some creativity to tie it all together. The most important part (IMHO) is to keep your test case readable and maintainable.
In case your project uses a C99 compliant C compiler you can even combine all this in a single unit test function so you can easily oversee all details of the test. See the example below. In case your project uses a C compiler that supports nested functions (e.g. GCC) you can even combine all this in a single unit test function so you can easily oversee all details of the test.
/* The time structure */ ```c
typedef struct { /* The time structure */
typedef struct {
int hour, min; int hour, min;
} Time; } Time;
/* Our fake function */ /* Our fake function */
FAKE_VOID_FUNC(getTime, Time*); FAKE_VOID_FUNC(getTime, Time*);
/* A test using the getTime fake function */ /* A test using the getTime fake function */
TEST_F(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_output) TEST_F(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_output)
{ {
Time t; Time t;
Time getTime_custom_now; Time getTime_custom_now;
void getTime_custom_fake(Time *now) { void getTime_custom_fake(Time *now) {
@@ -384,14 +444,15 @@ In case your project uses a C99 compliant C compiler you can even combine all th
/* then the specific time must be produced */ /* then the specific time must be produced */
ASSERT_EQ(t.hour, 13); ASSERT_EQ(t.hour, 13);
ASSERT_EQ(t.min, 05); ASSERT_EQ(t.min, 05);
} }
```
## How do I fake a function with a function pointer parameter? ## How do I fake a function with a function pointer parameter?
Using FFF to stub functions that have function pointer parameter can cause problems when trying to stub them. Presented here is an example how to deal with this situation. Using FFF to stub functions that have function pointer parameter can cause problems when trying to stub them. Presented here is an example how to deal with this situation.
If you need to stub a function that has a function pointer parameter, e.g. something like: If you need to stub a function that has a function pointer parameter, e.g. something like:
``` ```c
/* timer.h */ /* timer.h */
typedef int timer_handle; typedef int timer_handle;
extern int timer_start(timer_handle handle, long delay, void (*cb_function) (int arg), int arg); extern int timer_start(timer_handle handle, long delay, void (*cb_function) (int arg), int arg);
@@ -399,7 +460,7 @@ extern int timer_start(timer_handle handle, long delay, void (*cb_function) (int
Then creating a fake like below will horribly fail when trying to compile because the FFF macro will internally expand into an illegal variable ```int (*)(int) arg2_val```. Then creating a fake like below will horribly fail when trying to compile because the FFF macro will internally expand into an illegal variable ```int (*)(int) arg2_val```.
``` ```c
/* The fake, attempt one */ /* The fake, attempt one */
FAKE_VALUE_FUNC(int, FAKE_VALUE_FUNC(int,
timer_start, timer_start,
@@ -411,7 +472,7 @@ FAKE_VALUE_FUNC(int,
The solution to this problem is to create a bridging type that needs only to be visible in the unit tester. The fake will use that intermediate type. This way the compiler will not complain because the types match. The solution to this problem is to create a bridging type that needs only to be visible in the unit tester. The fake will use that intermediate type. This way the compiler will not complain because the types match.
``` ```c
/* Additional type needed to be able to use callback in FFF */ /* Additional type needed to be able to use callback in FFF */
typedef void (*timer_cb) (int argument); typedef void (*timer_cb) (int argument);
@@ -426,7 +487,7 @@ FAKE_VALUE_FUNC(int,
Here are some ideas how to create a test case with callbacks. Here are some ideas how to create a test case with callbacks.
``` ```c
/* Unit test */ /* Unit test */
TEST_F(FFFTestSuite, test_fake_with_function_pointer) TEST_F(FFFTestSuite, test_fake_with_function_pointer)
{ {
@@ -476,7 +537,7 @@ The solution is to separate declaration and definition of the fakes, and place t
Here is an example of how it could be done: Here is an example of how it could be done:
``` ```c
/* Public header file */ /* Public header file */
#include "fff.h" #include "fff.h"
@@ -518,25 +579,8 @@ So whats the point?
## Cheat Sheet ## Cheat Sheet
<table> | Macro | Description | Example |
<tr> |-------|-------------|---------|
<th>Macro</th> | FAKE_VOID_FUNC(fn [,arg_types*]); | Define a fake function named fn returning void with n arguments | FAKE_VOID_FUNC(DISPLAY_output_message, const char*); |
<th>Description</th> | FAKE_VALUE_FUNC(return_type, fn [,arg_types*]); | Define a fake function returning a value with type return_type taking n arguments | FAKE_VALUE_FUNC(int, DISPLAY_get_line_insert_index); |
<th>Example</th> | RESET_FAKE(fn); | Reset the state of fake function called fn | RESET_FAKE(DISPLAY_init); |
</tr>
<tr>
<td>FAKE_VOID_FUNC(fn [,arg_types*]);</td>
<td>Define a fake function named fn returning void with n arguments</td>
<td>FAKE_VOID_FUNC(DISPLAY_output_message, const char*);</td>
</tr>
<tr>
<td>FAKE_VALUE_FUNC(return_type, fn [,arg_types*]);</td>
<td>Define a fake function returning a value with type return_type taking n arguments</td>
<td>FAKE_VALUE_FUNC(int, DISPLAY_get_line_insert_index);</td>
</tr>
<tr>
<td>RESET_FAKE(fn);</td>
<td>Reset the state of fake function called fn</td>
<td>RESET_FAKE(DISPLAY_init);</td>
</tr>
</table>

View File

@@ -431,6 +431,11 @@ def include_guard
putd "#endif /* FAKE_FUNCTIONS */" putd "#endif /* FAKE_FUNCTIONS */"
end end
def msvc_expand_macro_fix
putd "/* MSVC expand macro fix */"
putd "#define EXPAND(x) x"
end
def generate_arg_sequence(args, prefix, do_reverse, joinstr) def generate_arg_sequence(args, prefix, do_reverse, joinstr)
fmap = (0..args).flat_map {|i| [prefix + i.to_s]} fmap = (0..args).flat_map {|i| [prefix + i.to_s]}
if do_reverse then fmap.reverse.join(joinstr) else fmap.join(", ") end if do_reverse then fmap.reverse.join(joinstr) else fmap.join(", ") end
@@ -442,25 +447,26 @@ def counting_macro_instance(type, vararg = :non_vararg, prefix = "")
<<-MACRO_COUNTING_INSTANCE <<-MACRO_COUNTING_INSTANCE
#define #{prefix}FAKE_#{type.to_s}_FUNC#{appendix}(...) \ #define #{prefix}FAKE_#{type.to_s}_FUNC#{appendix}(...) \
#{prefix}FUNC_#{type.to_s}#{appendix}_(PP_NARG_MINUS#{minus_count}(__VA_ARGS__), __VA_ARGS__) EXPAND(#{prefix}FUNC_#{type.to_s}#{appendix}_(PP_NARG_MINUS#{minus_count}(__VA_ARGS__), __VA_ARGS__))
#define #{prefix}FUNC_#{type.to_s}#{appendix}_(N,...) \ #define #{prefix}FUNC_#{type.to_s}#{appendix}_(N,...) \
#{prefix}FUNC_#{type.to_s}#{appendix}_N(N,__VA_ARGS__) EXPAND(#{prefix}FUNC_#{type.to_s}#{appendix}_N(N,__VA_ARGS__))
#define #{prefix}FUNC_#{type.to_s}#{appendix}_N(N,...) \ #define #{prefix}FUNC_#{type.to_s}#{appendix}_N(N,...) \
#{prefix}FAKE_#{type.to_s}_FUNC ## N#{" ## _VARARG" if vararg == :vararg}(__VA_ARGS__) EXPAND(#{prefix}FAKE_#{type.to_s}_FUNC ## N#{" ## _VARARG" if vararg == :vararg}(__VA_ARGS__))
MACRO_COUNTING_INSTANCE MACRO_COUNTING_INSTANCE
end end
def output_macro_counting_shortcuts def output_macro_counting_shortcuts
msvc_expand_macro_fix
putd <<-MACRO_COUNTING putd <<-MACRO_COUNTING
#define PP_NARG_MINUS2(...) \ #define PP_NARG_MINUS2(...) \
PP_NARG_MINUS2_(__VA_ARGS__, PP_RSEQ_N_MINUS2()) EXPAND(PP_NARG_MINUS2_(__VA_ARGS__, PP_RSEQ_N_MINUS2()))
#define PP_NARG_MINUS2_(...) \ #define PP_NARG_MINUS2_(...) \
PP_ARG_MINUS2_N(__VA_ARGS__) EXPAND(PP_ARG_MINUS2_N(__VA_ARGS__))
#define PP_ARG_MINUS2_N(returnVal, #{generate_arg_sequence($MAX_ARGS, '_', false, ", ")}, N, ...) N #define PP_ARG_MINUS2_N(returnVal, #{generate_arg_sequence($MAX_ARGS, '_', false, ", ")}, N, ...) N
@@ -469,10 +475,10 @@ def output_macro_counting_shortcuts
#define PP_NARG_MINUS1(...) \ #define PP_NARG_MINUS1(...) \
PP_NARG_MINUS1_(__VA_ARGS__, PP_RSEQ_N_MINUS1()) EXPAND(PP_NARG_MINUS1_(__VA_ARGS__, PP_RSEQ_N_MINUS1()))
#define PP_NARG_MINUS1_(...) \ #define PP_NARG_MINUS1_(...) \
PP_ARG_MINUS1_N(__VA_ARGS__) EXPAND(PP_ARG_MINUS1_N(__VA_ARGS__))
#define PP_ARG_MINUS1_N(#{generate_arg_sequence($MAX_ARGS, '_', false, ", ")}, N, ...) N #define PP_ARG_MINUS1_N(#{generate_arg_sequence($MAX_ARGS, '_', false, ", ")}, N, ...) N

82
fff.h
View File

@@ -5986,19 +5986,21 @@ FFF_END_EXTERN_C \
DECLARE_FAKE_VALUE_FUNC20_VARARG(RETURN_TYPE, FUNCNAME, ARG0_TYPE, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE, ARG7_TYPE, ARG8_TYPE, ARG9_TYPE, ARG10_TYPE, ARG11_TYPE, ARG12_TYPE, ARG13_TYPE, ARG14_TYPE, ARG15_TYPE, ARG16_TYPE, ARG17_TYPE, ARG18_TYPE, ...) \ DECLARE_FAKE_VALUE_FUNC20_VARARG(RETURN_TYPE, FUNCNAME, ARG0_TYPE, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE, ARG7_TYPE, ARG8_TYPE, ARG9_TYPE, ARG10_TYPE, ARG11_TYPE, ARG12_TYPE, ARG13_TYPE, ARG14_TYPE, ARG15_TYPE, ARG16_TYPE, ARG17_TYPE, ARG18_TYPE, ...) \
DEFINE_FAKE_VALUE_FUNC20_VARARG(RETURN_TYPE, FUNCNAME, ARG0_TYPE, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE, ARG7_TYPE, ARG8_TYPE, ARG9_TYPE, ARG10_TYPE, ARG11_TYPE, ARG12_TYPE, ARG13_TYPE, ARG14_TYPE, ARG15_TYPE, ARG16_TYPE, ARG17_TYPE, ARG18_TYPE, ...) \ DEFINE_FAKE_VALUE_FUNC20_VARARG(RETURN_TYPE, FUNCNAME, ARG0_TYPE, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE, ARG7_TYPE, ARG8_TYPE, ARG9_TYPE, ARG10_TYPE, ARG11_TYPE, ARG12_TYPE, ARG13_TYPE, ARG14_TYPE, ARG15_TYPE, ARG16_TYPE, ARG17_TYPE, ARG18_TYPE, ...) \
/* MSVC expand macro fix */
#define EXPAND(x) x
#define PP_NARG_MINUS2(...) PP_NARG_MINUS2_(__VA_ARGS__, PP_RSEQ_N_MINUS2()) #define PP_NARG_MINUS2(...) EXPAND(PP_NARG_MINUS2_(__VA_ARGS__, PP_RSEQ_N_MINUS2()))
#define PP_NARG_MINUS2_(...) PP_ARG_MINUS2_N(__VA_ARGS__) #define PP_NARG_MINUS2_(...) EXPAND(PP_ARG_MINUS2_N(__VA_ARGS__))
#define PP_ARG_MINUS2_N(returnVal, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, N, ...) N #define PP_ARG_MINUS2_N(returnVal, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, N, ...) N
#define PP_RSEQ_N_MINUS2() 20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 #define PP_RSEQ_N_MINUS2() 20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
#define PP_NARG_MINUS1(...) PP_NARG_MINUS1_(__VA_ARGS__, PP_RSEQ_N_MINUS1()) #define PP_NARG_MINUS1(...) EXPAND(PP_NARG_MINUS1_(__VA_ARGS__, PP_RSEQ_N_MINUS1()))
#define PP_NARG_MINUS1_(...) PP_ARG_MINUS1_N(__VA_ARGS__) #define PP_NARG_MINUS1_(...) EXPAND(PP_ARG_MINUS1_N(__VA_ARGS__))
#define PP_ARG_MINUS1_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, N, ...) N #define PP_ARG_MINUS1_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, N, ...) N
@@ -6008,94 +6010,94 @@ FFF_END_EXTERN_C \
/* DECLARE AND DEFINE FAKE FUNCTIONS - PLACE IN TEST FILES */ /* DECLARE AND DEFINE FAKE FUNCTIONS - PLACE IN TEST FILES */
#define FAKE_VALUE_FUNC(...) FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__) #define FAKE_VALUE_FUNC(...) EXPAND(FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__))
#define FUNC_VALUE_(N,...) FUNC_VALUE_N(N,__VA_ARGS__) #define FUNC_VALUE_(N,...) EXPAND(FUNC_VALUE_N(N,__VA_ARGS__))
#define FUNC_VALUE_N(N,...) FAKE_VALUE_FUNC ## N(__VA_ARGS__) #define FUNC_VALUE_N(N,...) EXPAND(FAKE_VALUE_FUNC ## N(__VA_ARGS__))
#define FAKE_VOID_FUNC(...) FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__) #define FAKE_VOID_FUNC(...) EXPAND(FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__))
#define FUNC_VOID_(N,...) FUNC_VOID_N(N,__VA_ARGS__) #define FUNC_VOID_(N,...) EXPAND(FUNC_VOID_N(N,__VA_ARGS__))
#define FUNC_VOID_N(N,...) FAKE_VOID_FUNC ## N(__VA_ARGS__) #define FUNC_VOID_N(N,...) EXPAND(FAKE_VOID_FUNC ## N(__VA_ARGS__))
#define FAKE_VALUE_FUNC_VARARG(...) FUNC_VALUE_VARARG_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__) #define FAKE_VALUE_FUNC_VARARG(...) EXPAND(FUNC_VALUE_VARARG_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__))
#define FUNC_VALUE_VARARG_(N,...) FUNC_VALUE_VARARG_N(N,__VA_ARGS__) #define FUNC_VALUE_VARARG_(N,...) EXPAND(FUNC_VALUE_VARARG_N(N,__VA_ARGS__))
#define FUNC_VALUE_VARARG_N(N,...) FAKE_VALUE_FUNC ## N ## _VARARG(__VA_ARGS__) #define FUNC_VALUE_VARARG_N(N,...) EXPAND(FAKE_VALUE_FUNC ## N ## _VARARG(__VA_ARGS__))
#define FAKE_VOID_FUNC_VARARG(...) FUNC_VOID_VARARG_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__) #define FAKE_VOID_FUNC_VARARG(...) EXPAND(FUNC_VOID_VARARG_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__))
#define FUNC_VOID_VARARG_(N,...) FUNC_VOID_VARARG_N(N,__VA_ARGS__) #define FUNC_VOID_VARARG_(N,...) EXPAND(FUNC_VOID_VARARG_N(N,__VA_ARGS__))
#define FUNC_VOID_VARARG_N(N,...) FAKE_VOID_FUNC ## N ## _VARARG(__VA_ARGS__) #define FUNC_VOID_VARARG_N(N,...) EXPAND(FAKE_VOID_FUNC ## N ## _VARARG(__VA_ARGS__))
/* DECLARE FAKE FUNCTIONS - PLACE IN HEADER FILES */ /* DECLARE FAKE FUNCTIONS - PLACE IN HEADER FILES */
#define DECLARE_FAKE_VALUE_FUNC(...) DECLARE_FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__) #define DECLARE_FAKE_VALUE_FUNC(...) EXPAND(DECLARE_FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__))
#define DECLARE_FUNC_VALUE_(N,...) DECLARE_FUNC_VALUE_N(N,__VA_ARGS__) #define DECLARE_FUNC_VALUE_(N,...) EXPAND(DECLARE_FUNC_VALUE_N(N,__VA_ARGS__))
#define DECLARE_FUNC_VALUE_N(N,...) DECLARE_FAKE_VALUE_FUNC ## N(__VA_ARGS__) #define DECLARE_FUNC_VALUE_N(N,...) EXPAND(DECLARE_FAKE_VALUE_FUNC ## N(__VA_ARGS__))
#define DECLARE_FAKE_VOID_FUNC(...) DECLARE_FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__) #define DECLARE_FAKE_VOID_FUNC(...) EXPAND(DECLARE_FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__))
#define DECLARE_FUNC_VOID_(N,...) DECLARE_FUNC_VOID_N(N,__VA_ARGS__) #define DECLARE_FUNC_VOID_(N,...) EXPAND(DECLARE_FUNC_VOID_N(N,__VA_ARGS__))
#define DECLARE_FUNC_VOID_N(N,...) DECLARE_FAKE_VOID_FUNC ## N(__VA_ARGS__) #define DECLARE_FUNC_VOID_N(N,...) EXPAND(DECLARE_FAKE_VOID_FUNC ## N(__VA_ARGS__))
#define DECLARE_FAKE_VALUE_FUNC_VARARG(...) DECLARE_FUNC_VALUE_VARARG_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__) #define DECLARE_FAKE_VALUE_FUNC_VARARG(...) EXPAND(DECLARE_FUNC_VALUE_VARARG_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__))
#define DECLARE_FUNC_VALUE_VARARG_(N,...) DECLARE_FUNC_VALUE_VARARG_N(N,__VA_ARGS__) #define DECLARE_FUNC_VALUE_VARARG_(N,...) EXPAND(DECLARE_FUNC_VALUE_VARARG_N(N,__VA_ARGS__))
#define DECLARE_FUNC_VALUE_VARARG_N(N,...) DECLARE_FAKE_VALUE_FUNC ## N ## _VARARG(__VA_ARGS__) #define DECLARE_FUNC_VALUE_VARARG_N(N,...) EXPAND(DECLARE_FAKE_VALUE_FUNC ## N ## _VARARG(__VA_ARGS__))
#define DECLARE_FAKE_VOID_FUNC_VARARG(...) DECLARE_FUNC_VOID_VARARG_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__) #define DECLARE_FAKE_VOID_FUNC_VARARG(...) EXPAND(DECLARE_FUNC_VOID_VARARG_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__))
#define DECLARE_FUNC_VOID_VARARG_(N,...) DECLARE_FUNC_VOID_VARARG_N(N,__VA_ARGS__) #define DECLARE_FUNC_VOID_VARARG_(N,...) EXPAND(DECLARE_FUNC_VOID_VARARG_N(N,__VA_ARGS__))
#define DECLARE_FUNC_VOID_VARARG_N(N,...) DECLARE_FAKE_VOID_FUNC ## N ## _VARARG(__VA_ARGS__) #define DECLARE_FUNC_VOID_VARARG_N(N,...) EXPAND(DECLARE_FAKE_VOID_FUNC ## N ## _VARARG(__VA_ARGS__))
/* DEFINE FAKE FUNCTIONS - PLACE IN SOURCE FILES */ /* DEFINE FAKE FUNCTIONS - PLACE IN SOURCE FILES */
#define DEFINE_FAKE_VALUE_FUNC(...) DEFINE_FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__) #define DEFINE_FAKE_VALUE_FUNC(...) EXPAND(DEFINE_FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__))
#define DEFINE_FUNC_VALUE_(N,...) DEFINE_FUNC_VALUE_N(N,__VA_ARGS__) #define DEFINE_FUNC_VALUE_(N,...) EXPAND(DEFINE_FUNC_VALUE_N(N,__VA_ARGS__))
#define DEFINE_FUNC_VALUE_N(N,...) DEFINE_FAKE_VALUE_FUNC ## N(__VA_ARGS__) #define DEFINE_FUNC_VALUE_N(N,...) EXPAND(DEFINE_FAKE_VALUE_FUNC ## N(__VA_ARGS__))
#define DEFINE_FAKE_VOID_FUNC(...) DEFINE_FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__) #define DEFINE_FAKE_VOID_FUNC(...) EXPAND(DEFINE_FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__))
#define DEFINE_FUNC_VOID_(N,...) DEFINE_FUNC_VOID_N(N,__VA_ARGS__) #define DEFINE_FUNC_VOID_(N,...) EXPAND(DEFINE_FUNC_VOID_N(N,__VA_ARGS__))
#define DEFINE_FUNC_VOID_N(N,...) DEFINE_FAKE_VOID_FUNC ## N(__VA_ARGS__) #define DEFINE_FUNC_VOID_N(N,...) EXPAND(DEFINE_FAKE_VOID_FUNC ## N(__VA_ARGS__))
#define DEFINE_FAKE_VALUE_FUNC_VARARG(...) DEFINE_FUNC_VALUE_VARARG_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__) #define DEFINE_FAKE_VALUE_FUNC_VARARG(...) EXPAND(DEFINE_FUNC_VALUE_VARARG_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__))
#define DEFINE_FUNC_VALUE_VARARG_(N,...) DEFINE_FUNC_VALUE_VARARG_N(N,__VA_ARGS__) #define DEFINE_FUNC_VALUE_VARARG_(N,...) EXPAND(DEFINE_FUNC_VALUE_VARARG_N(N,__VA_ARGS__))
#define DEFINE_FUNC_VALUE_VARARG_N(N,...) DEFINE_FAKE_VALUE_FUNC ## N ## _VARARG(__VA_ARGS__) #define DEFINE_FUNC_VALUE_VARARG_N(N,...) EXPAND(DEFINE_FAKE_VALUE_FUNC ## N ## _VARARG(__VA_ARGS__))
#define DEFINE_FAKE_VOID_FUNC_VARARG(...) DEFINE_FUNC_VOID_VARARG_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__) #define DEFINE_FAKE_VOID_FUNC_VARARG(...) EXPAND(DEFINE_FUNC_VOID_VARARG_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__))
#define DEFINE_FUNC_VOID_VARARG_(N,...) DEFINE_FUNC_VOID_VARARG_N(N,__VA_ARGS__) #define DEFINE_FUNC_VOID_VARARG_(N,...) EXPAND(DEFINE_FUNC_VOID_VARARG_N(N,__VA_ARGS__))
#define DEFINE_FUNC_VOID_VARARG_N(N,...) DEFINE_FAKE_VOID_FUNC ## N ## _VARARG(__VA_ARGS__) #define DEFINE_FUNC_VOID_VARARG_N(N,...) EXPAND(DEFINE_FAKE_VOID_FUNC ## N ## _VARARG(__VA_ARGS__))

View File

@@ -357,6 +357,11 @@ TEST_F(FFFTestSuite, use_void_vararg_fake_with_different_number_of_arguments)
voidfunc3var("1 parameter", 1, 10); voidfunc3var("1 parameter", 1, 10);
voidfunc3var("2 parameters", 2, 10, 20); voidfunc3var("2 parameters", 2, 10, 20);
voidfunc3var("3 parameters", 3, 10, 20, 30); voidfunc3var("3 parameters", 3, 10, 20, 30);
ASSERT_EQ(voidfunc3var_fake.call_count, 4);
char msg[] = "3 parameters";
ASSERT_EQ(strcmp(voidfunc3var_fake.arg0_val, msg), 0);
ASSERT_EQ(3, voidfunc3var_fake.arg1_val);
} }
TEST_F(FFFTestSuite, use_value_vararg_fake_with_different_number_of_arguments) TEST_F(FFFTestSuite, use_value_vararg_fake_with_different_number_of_arguments)
@@ -365,6 +370,11 @@ TEST_F(FFFTestSuite, use_value_vararg_fake_with_different_number_of_arguments)
valuefunc3var("1 parameter", 1, 10); valuefunc3var("1 parameter", 1, 10);
valuefunc3var("2 parameters", 2, 10, 20); valuefunc3var("2 parameters", 2, 10, 20);
valuefunc3var("3 parameters", 3, 10, 20, 30); valuefunc3var("3 parameters", 3, 10, 20, 30);
ASSERT_EQ(valuefunc3var_fake.call_count, 4);
char msg[] = "3 parameters";
ASSERT_EQ(strcmp(valuefunc3var_fake.arg0_val, msg), 0);
ASSERT_EQ(3, valuefunc3var_fake.arg1_val);
} }
#endif /* __cplusplus */ #endif /* __cplusplus */