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

Merge pull request #38 from nabijaczleweli/master

Add syntax highlighting and remove inline HTML from README
This commit is contained in:
Mike Long
2017-10-09 09:20:05 +02:00
committed by GitHub

107
README.md
View File

@@ -13,39 +13,46 @@ 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
you want to create a fake for:
```c
// UI.c
...
void DISPLAY_init();
...
```
Here's how you would define a fake function for this in your test suite:
```c
// test.c(pp)
#include "fff.h"
DEFINE_FFF_GLOBALS;
FAKE_VOID_FUNC(DISPLAY_init);
```
And the unit test might look something like this:
```c
TEST_F(GreeterTests, init_initialises_display)
{
UI_init();
ASSERT_EQ(DISPLAY_init_fake.call_count, 1);
}
```
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.
The magic is in the <tt>FAKE_VOID_FUNC</tt>. This
expands a macro that defines a function returning <tt>void</tt>
The magic is in the `FAKE_VOID_FUNC`. This
expands a macro that defines a function returning `void`
which has zero arguments. It also defines a struct
<tt>"function_name"_fake</tt> which contains all the information about the fake.
For instance, <tt>DISPLAY_init_fake.call_count</tt>is incremented every time the faked
`"function_name"_fake` which contains all the information about the fake.
For instance, `DISPLAY_init_fake.call_count`is incremented every time the faked
function is called.
Under the hood it generates a struct that looks like this:
```c
typedef struct DISPLAY_init_Fake {
unsigned int call_count;
unsigned int arg_history_len;
@@ -53,6 +60,7 @@ Under the hood it generates a struct that looks like this:
void(*custom_fake)();
} DISPLAY_init_Fake;
DISPLAY_init_Fake DISPLAY_init_fake;
```
@@ -62,17 +70,22 @@ Under the hood it generates a struct that looks like this:
Ok, enough with the toy examples. What about faking functions with arguments?
```c
// UI.c
...
void DISPLAY_output(char * message);
...
```
Here's how you would define a fake function for this in your test suite:
```c
FAKE_VOID_FUNC(DISPLAY_output, char *);
```
And the unit test might look something like this:
```c
TEST_F(UITests, write_line_outputs_lines_to_display)
{
char msg[] = "helloworld";
@@ -80,36 +93,42 @@ And the unit test might look something like this:
ASSERT_EQ(DISPLAY_output_fake.call_count, 1);
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,
and the macro arguments following the function name defines the argument
type (a char pointer in this example).
A variable is created for every argument in the form
<tt>"function_name"fake.argN_val</tt>
`"function_name"fake.argN_val`
## Return values
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:
```c
// UI.c
...
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:
```c
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:
```c
TEST_F(UITests, when_empty_lines_write_line_doesnt_clear_screen)
{
// given
@@ -120,15 +139,20 @@ And the unit test might look something like this:
// then
ASSERT_EQ(DISPLAY_clear_fake.call_count, 0);
}
```
Of course you can mix and match these macros to define a value function with
arguments, for instance to fake:
```c
double pow(double base, double exponent);
```
you would use a syntax like this:
```c
FAKE_VALUE_FUNC(double, pow, double, double);
```
@@ -139,6 +163,7 @@ 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
fakes in the setup function of your test suite.
```c
void setup()
{
// Register resets
@@ -148,10 +173,11 @@ fakes in the setup function of your test suite.
RESET_FAKE(DISPLAY_get_line_capacity);
RESET_FAKE(DISPLAY_get_line_insert_index);
}
```
You might want to define a macro to do this:
```
```c
/* List of fakes used by this unit tester */
#define FFF_FAKES_LIST(FAKE) \
FAKE(DISPLAY_init) \
@@ -172,11 +198,12 @@ void setup()
## Call history
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.
Here's how it works:
```c
FAKE_VOID_FUNC(voidfunc2, char, char);
FAKE_VALUE_FUNC(long, longfunc0);
@@ -190,8 +217,9 @@ Here's how it works:
ASSERT_EQ(fff.call_history[1], (void *)voidfunc2);
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
@@ -199,6 +227,7 @@ They are reset by calling <tt>FFF_RESET_HISTORY();</tt>
The framework will by default store the arguments for the last ten calls made
to a fake function.
```c
TEST_F(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history)
{
voidfunc2('g', 'h');
@@ -208,10 +237,12 @@ to a fake function.
ASSERT_EQ('i', voidfunc2_fake.arg0_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
check the dropped histories counter:
```c
TEST_F(FFFTestSuite, when_fake_func_called_max_times_plus_one_then_one_argument_history_dropped)
{
int i;
@@ -222,10 +253,13 @@ check the dropped histories counter:
voidfunc2('1', '2');
ASSERT_EQ(1u, voidfunc2_fake.arg_histories_dropped);
}
```
The other is to check if the call count is greater than the history size:
```c
ASSERT(voidfunc2_fake.arg_history_len < voidfunc2_fake.call_count);
```
The argument histories for a fake function are reset when the RESET_FAKE
function is called
@@ -233,14 +267,16 @@ function is called
## User Defined Argument History
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:
```c
// Want to keep the argument history for 13 calls
#define FFF_ARG_HISTORY_LEN 13
// Want to keep the call sequence history for 17 function calls
#define FFF_CALL_HISTORY_LEN 17
#include "../fff.h"
```
## Function Return Value Sequences
@@ -249,6 +285,7 @@ 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
with for the fake function. It is probably easier to describe with an example:
```c
// faking "long longfunc();"
FAKE_VALUE_FUNC(long, longfunc0);
@@ -262,8 +299,9 @@ with for the fake function. It is probably easier to describe with an example:
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 end of the sequence is reached the fake will continue to return the last
value in the sequence indefinitely.
@@ -273,6 +311,7 @@ value in the sequence indefinitely.
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:
```c
#define MEANING_OF_LIFE 42
long my_custom_value_fake(void)
{
@@ -284,6 +323,7 @@ is done by setting the custom_fake member of the fake. Here's an example:
long retval = longfunc0();
ASSERT_EQ(MEANING_OF_LIFE, retval);
}
```
## Custom Return Value Delegate Sequences
@@ -293,6 +333,7 @@ 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
using the SET_CUSTOM_FAKE_SEQ macro. Here's an example:
```c
void voidfunc1outparam_custom_fake1(char *a)
{
*a = 'x';
@@ -324,6 +365,7 @@ using the SET_CUSTOM_FAKE_SEQ macro. Here's an example:
voidfunc1outparam(&a);
ASSERT_EQ('z', a);
}
```
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
@@ -336,6 +378,7 @@ You need to create a custom function (e.g. getTime_custom_fake) to produce the o
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.
```c
/* The time structure */
typedef struct {
int hour, min;
@@ -365,13 +408,14 @@ In case your project uses a C99 compliant C compiler you can even combine all th
ASSERT_EQ(t.hour, 13);
ASSERT_EQ(t.min, 05);
}
```
## 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.
If you need to stub a function that has a function pointer parameter, e.g. something like:
```
```c
/* timer.h */
typedef int timer_handle;
extern int timer_start(timer_handle handle, long delay, void (*cb_function) (int arg), int arg);
@@ -379,7 +423,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```.
```
```c
/* The fake, attempt one */
FAKE_VALUE_FUNC(int,
timer_start,
@@ -391,7 +435,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.
```
```c
/* Additional type needed to be able to use callback in FFF */
typedef void (*timer_cb) (int argument);
@@ -406,7 +450,7 @@ FAKE_VALUE_FUNC(int,
Here are some ideas how to create a test case with callbacks.
```
```c
/* Unit test */
TEST_F(FFFTestSuite, test_fake_with_function_pointer)
{
@@ -456,7 +500,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:
```
```c
/* Public header file */
#include "fff.h"
@@ -498,25 +542,8 @@ So whats the point?
## Cheat Sheet
<table>
<tr>
<th>Macro</th>
<th>Description</th>
<th>Example</th>
</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>
| Macro | Description | Example |
|-------|-------------|---------|
| 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*); |
| 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); |
| RESET_FAKE(fn); | Reset the state of fake function called fn | RESET_FAKE(DISPLAY_init); |