From 4e031416d6017d5b70022d8c9fd8fb82b1014280 Mon Sep 17 00:00:00 2001 From: James Fraser Date: Tue, 4 Dec 2018 20:49:00 +1100 Subject: [PATCH] Update README.md --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 86b75dc..effdc58 100644 --- a/README.md +++ b/README.md @@ -394,6 +394,7 @@ macro. When the last custom fake is reached the fake will keep calling the last fake in the sequence. This macro works much like the SET_RETURN_SEQ macro. ## Return value history + Say you have two functions f1 and f2. f2 must be called to release some resource allocated by f1, but only in the cases where f1 returns zero. f1 could be pthread_mutex_trylock and f2 could be pthread_mutex_unlock. fff will @@ -432,6 +433,7 @@ could call the real fprintf() like this: } ## How do I specify calling conventions for my fake functions? + FFF has a limited capability for enabling specification of Microsoft's Visual C/C++ calling conventions, but this support must be enabled when generating FFF's header file `fff.h`. ```bash @@ -448,6 +450,7 @@ FAKE_VALUE_FUNC(long, __cdecl, longfunc0); ``` ## 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. 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. @@ -487,6 +490,7 @@ TEST_F(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_output ``` ## 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: @@ -570,6 +574,7 @@ TEST_F(FFFTestSuite, test_fake_with_function_pointer) } ``` ## How do I reuse a fake across multiple test-suites? + FFF functions like FAKE_VALUE_FUNC will perform both the declaration AND the definition of the fake function and the corresponding data structs. This cannot be placed in a header, since it will lead to multiple definitions of the fake functions. The solution is to separate declaration and definition of the fakes, and place the declaration into a public header file, and the definition into a private source file. @@ -611,7 +616,7 @@ So whats the point? * To work in both C and C++ test environments -## Under the hood: +## Under the Hood * The fff.h header file is generated by a ruby script * There are tests under src/test * There is an example for testing an embedded UI and a hardware driver under src/examples @@ -622,6 +627,6 @@ So whats the point? |-------|-------------|---------| | 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); | -| FAKE_VOID_FUNC_VARARG(fn [,arg_types*], ...); | Define a fake variadic function returning a value with type return_type taking n arguments and n variadic arguments | FAKE_VOID_FUNC_VARARG(fn, const char*, ...) | +| FAKE_VOID_FUNC_VARARG(fn [,arg_types*], ...); | Define a fake variadic function returning void with type return_type taking n arguments and n variadic arguments | FAKE_VOID_FUNC_VARARG(fn, const char*, ...) | | FAKE_VALUE_FUNC_VARARG(return_type, fn [,arg_types*], ...); | Define a fake variadic function returning a value with type return_type taking n arguments and n variadic arguments | FAKE_VALUE_FUNC_VARARG(int, fprintf, FILE*, const char*, ...) | | RESET_FAKE(fn); | Reset the state of fake function called fn | RESET_FAKE(DISPLAY_init); |