Skip to content

Commit df8f5eb

Browse files
author
Cormac Cannon
committed
Variadic macro extensions to the FFF
See header comment in fff.h for details -- summary extracted below: Return value macros: SET_RETURN_VAL_SEQ(function_name, ...) Accepts a raw list of appropriately typed values SET_RETURN_VAL(function_name, value) Provided for API consistency Assertion macros: TEST_ASSERT_CALLS(function_name, expected_call_count) TEST_ASSERT_CALLED(function_name) Only one call made to the function. TEST_ASSERT_NOT_CALLED(function_name) No calls made to the function. TEST_ASSERT_CALL(function_name, ...parameters) Only one call made to the function with the specified parameter values. TEST_ASSERT_LAST_CALL(function_name, ...parameters) The final call to this function was made with the specified parameter values. TEST_ASSERT_ANY_CALL(function_name, ...parameters) At least one call was made with the specified parameter values. TEST_ASSERT_NO_CALL(function_name, ...parameters) No calls were made with the specified parameter values. TEST_ASSERT_NTH_CALL(function_name, call_index, ...parameters) A specified invocation of the function used the specified parameter values. <<Amendment 1>> Added unit tests. Added TEST_ASSERT_CALL(...), as couldn't get optional parameter verification for TEST_ASSERT_CALLED to work in c99 mode. <<Amendment 2>> Updated generator. Function name changes for consistency. Initial documentation updates. <<Amendment 3>> FFF_RETURN: Made array declaration static to allow use in a fixture setup method
1 parent ef24c19 commit df8f5eb

File tree

6 files changed

+1128
-186
lines changed

6 files changed

+1128
-186
lines changed

README.md

+92-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ FAKE_VOID_FUNC(DISPLAY_init);
3131
3232
And the unit test might look something like this:
3333
34+
```c
35+
TEST_F(GreeterTests, init_initialises_display)
36+
{
37+
UI_init();
38+
FFF_ASSERT_CALLS(DISPLAY_init, 1);
39+
}
40+
```
41+
42+
... or ...
43+
3444
```c
3545
TEST_F(GreeterTests, init_initialises_display)
3646
{
@@ -85,6 +95,17 @@ FAKE_VOID_FUNC(DISPLAY_output, char *);
8595

8696
And the unit test might look something like this:
8797

98+
```c
99+
TEST_F(UITests, write_line_outputs_lines_to_display)
100+
{
101+
char msg[] = "helloworld";
102+
UI_write_line(msg);
103+
FFF_ASSERT(DISPLAY_output, msg);
104+
}
105+
```
106+
107+
... or ...
108+
88109
```c
89110
TEST_F(UITests, write_line_outputs_lines_to_display)
90111
{
@@ -95,7 +116,6 @@ TEST_F(UITests, write_line_outputs_lines_to_display)
95116
}
96117
```
97118

98-
99119
There is no more magic here, the `FAKE_VOID_FUNC` works as in the
100120
previous example. The number of arguments that the function takes is calculated,
101121
and the macro arguments following the function name defines the argument
@@ -104,6 +124,20 @@ type (a char pointer in this example).
104124
A variable is created for every argument in the form
105125
`"function_name"fake.argN_val`
106126

127+
### Variadic argument assertions
128+
129+
These are a recent addition to the framework. The range of available assertions is
130+
listed in the cheat sheet at the foot of this page. The assertions use a simple
131+
assertion macro defined internally. To override with a macro from some test framework,
132+
do the following in some header you include in all your tests:
133+
134+
```c
135+
// my_test_framework_header.h
136+
...
137+
#define _FFF_ASSERT_EQ_MSG(expected, actual, message) MY_CUSTOM_ASSERTION(expected, actual, message)
138+
#include "fff.h"
139+
...
140+
```
107141
108142
109143
## Return values
@@ -128,6 +162,21 @@ FAKE_VALUE_FUNC(unsigned int, DISPLAY_get_line_insert_index);
128162
129163
And the unit test might look something like this:
130164
165+
```c
166+
TEST_F(UITests, when_empty_lines_write_line_doesnt_clear_screen)
167+
{
168+
// given
169+
FFF_RETURN(DISPLAY_get_line_insert_index,1);
170+
char msg[] = "helloworld";
171+
// when
172+
UI_write_line(msg);
173+
// then
174+
FFF_ASSERT_NOT_CALLED(DISPLAY_clear);
175+
}
176+
```
177+
178+
... or ...
179+
131180
```c
132181
TEST_F(UITests, when_empty_lines_write_line_doesnt_clear_screen)
133182
{
@@ -226,6 +275,18 @@ They are reset by calling `FFF_RESET_HISTORY();`
226275
The framework will by default store the arguments for the last ten calls made
227276
to a fake function.
228277
278+
```c
279+
TEST_F(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history)
280+
{
281+
voidfunc2('g', 'h');
282+
voidfunc2('i', 'j');
283+
FFF_ASSERT_NTH(voidfunc2, 1, 'g', 'h');
284+
FFF_ASSERT_NTH(voidfunc2, 2, 'i', 'j');
285+
}
286+
```
287+
288+
... or ...
289+
229290
```c
230291
TEST_F(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history)
231292
{
@@ -288,6 +349,21 @@ with for the fake function. It is probably easier to describe with an example:
288349
// faking "long longfunc();"
289350
FAKE_VALUE_FUNC(long, longfunc0);
290351

352+
TEST_F(FFFTestSuite, return_value_sequences_exhausted)
353+
{
354+
FFF_RETURN(longfunc0, myReturnVals, 3, 7, 9);
355+
ASSERT_EQ(3, longfunc0());
356+
ASSERT_EQ(7, longfunc0());
357+
ASSERT_EQ(9, longfunc0());
358+
ASSERT_EQ(9, longfunc0());
359+
ASSERT_EQ(9, longfunc0());
360+
}
361+
```
362+
... or ...
363+
```c
364+
// faking "long longfunc();"
365+
FAKE_VALUE_FUNC(long, longfunc0);
366+
291367
TEST_F(FFFTestSuite, return_value_sequences_exhausted)
292368
{
293369
long myReturnVals[3] = { 3, 7, 9 };
@@ -579,8 +655,18 @@ So whats the point?
579655
580656
581657
## Cheat Sheet
582-
| Macro | Description | Example |
583-
|-------|-------------|---------|
584-
| 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*); |
585-
| 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); |
586-
| RESET_FAKE(fn); | Reset the state of fake function called fn | RESET_FAKE(DISPLAY_init); |
658+
| Macro | Description | Example |
659+
|-------------------------------------------------|-----------------------------------------------------------------------------------|----------------------------------------------------------|
660+
| 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*); |
661+
| 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); |
662+
| RESET_FAKE(fn); | Reset the state of fake function called fn | RESET_FAKE(DISPLAY_init); |
663+
| FFF_RETURN(fn, ...return_value(s)*); | Set one or more return values (final value repeated if calls > values) | FFF_RETURN(DISPLAY_init, 1, 2, 3); |
664+
| FFF_ASSERT_CALLED(fn); | Assert that a function was called once and only once. | FFF_ASSERT_CALLED(DISPLAY_init); |
665+
| FFF_ASSERT_NOT_CALLED(fn); | Assert that a function was not called. | FFF_ASSERT_NOT_CALLED(DISPLAY_init); |
666+
| FFF_ASSERT_CALLS(fn, call_count); | Assert that a function was called the specified number of times. | FFF_ASSERT_CALLS(DISPLAY_init, 3); |
667+
| FFF_ASSERT(fn, ...arg_value(s)) | Assert that a function was called only once with the specified argument values. | FFF_ASSERT(DISPLAY_output_message, my_message_ptr); |
668+
| FFF_ASSERT_LAST(fn, ...arg_value(s)) | Assert that the last call to a function had the specified argument values. | FFF_ASSERT_LAST(DISPLAY_output_message, my_message_ptr); |
669+
| FFF_ASSERT_ANY(fn, ...arg_value(s)) | Assert that any call to a function had the specified argument values. | FFF_ASSERT_ANY(DISPLAY_output_message, my_message_ptr); |
670+
| FFF_ASSERT_NONE(fn, ...arg_value(s)) | Assert that no calls to a function had the specified argument values. | FFF_ASSERT_NONE(DISPLAY_output_message, my_message_ptr); |
671+
672+
*N.B.* All of the function argument assertions support partial specification of arguments. I.e. Given a function taking 3 args, you may just specify the first argument for verification. Any arguments that are specified must be specified from left to right however. i.e. it is possibly to specify arguments 0 and 1, but not arguments 0 and 2 (ignoring the value of 1).

0 commit comments

Comments
 (0)