Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,20 @@ void setup()
}
```

You might want to define a macro to do this:
If you simply want to reset all called fakes, you can do so like this:

```c
void setup()
{
/* call resets of all previously called fakes */
FFF_RESET_CALLED_FAKES();

/* reset common FFF internal structures */
FFF_RESET_HISTORY();
}
```

Alternatively, you might want to define a macro to do this:

```c
/* List of fakes used by this unit tester */
Expand Down Expand Up @@ -663,3 +676,4 @@ So whats the point?
| 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); |
| FFF_RESET_CALLED_FAKES(); | Reset the state of all called fake functions globally | FFF_RESET_CALLED_FAKES(); |
50 changes: 50 additions & 0 deletions fakegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
$MAX_ARGS = 20
$DEFAULT_ARG_HISTORY = 50
$MAX_CALL_HISTORY = 50
$MAX_RESET_LIST = 50

def license
File.foreach("#{__dir__}/LICENSE") { |line| putd line }
Expand All @@ -30,6 +31,11 @@ def output_constants
putd "#define FFF_CALL_HISTORY_LEN (#{$MAX_CALL_HISTORY}u)"
}
putd "#endif"
putd "#ifndef FFF_RESET_LIST_LEN"
indent {
putd "#define FFF_RESET_LIST_LEN (#{$MAX_RESET_LIST}u)"
}
putd "#endif"
putd "#ifndef FFF_GCC_FUNCTION_ATTRIBUTES"
indent {
putd "#define FFF_GCC_FUNCTION_ATTRIBUTES"
Expand Down Expand Up @@ -431,6 +437,7 @@ def output_function_body(arg_count, has_varargs, is_value_function)
putd_backslash "}"
putd_backslash "INCREMENT_CALL_COUNT(FUNCNAME);"
putd_backslash "REGISTER_CALL(FUNCNAME);"
putd_backslash "REGISTER_RESET_CALL(FUNCNAME##_reset);"

if has_varargs
return_type = is_value_function ? "return " : ""
Expand Down Expand Up @@ -517,10 +524,14 @@ def output_function_body(arg_count, has_varargs, is_value_function)

def define_fff_globals
putd "typedef void (*fff_function_t)(void);"
putd "typedef void (*fff_reset_function_t)(void);"
putd "typedef struct { "
indent {
putd "fff_function_t call_history[FFF_CALL_HISTORY_LEN];"
putd "unsigned int call_history_idx;"
putd "fff_reset_function_t reset_list[FFF_RESET_LIST_LEN];"
putd "unsigned int reset_list_idx;"
putd "unsigned int dropped_resets;"
}
putd "} fff_globals_t;"
puts
Expand All @@ -543,13 +554,52 @@ def define_fff_globals
putd "memset(fff.call_history, 0, sizeof(fff.call_history));"
}
puts
putd_backslash "#define FFF_RESET_CALLED_FAKES()"
indent {
putd_backslash "for(unsigned int i = 0; i < fff.reset_list_idx; ++i) {"
indent {
putd_backslash "fff.reset_list[i]();"
}
putd_backslash "}"
putd_backslash "fff.reset_list_idx = 0;"
putd_backslash "fff.dropped_resets = 0;"
putd "memset(fff.reset_list, 0, sizeof(fff.reset_list));"
}
puts
putd_backslash "#define REGISTER_CALL(function)"
indent {
putd_backslash "if(fff.call_history_idx < FFF_CALL_HISTORY_LEN)"
indent {
putd "fff.call_history[fff.call_history_idx++] = (fff_function_t)function;"
}
}
puts
putd_backslash "#define REGISTER_RESET_CALL(reset_function)"
indent {
putd_backslash "if(fff.reset_list_idx < FFF_RESET_LIST_LEN) {"
indent {
putd_backslash "int already_in_list = 0;"
putd_backslash "for(unsigned int i = 0; i < fff.reset_list_idx; ++i) {"
indent {
putd_backslash "if(fff.reset_list[i] == (fff_reset_function_t)reset_function) {"
indent {
putd_backslash "already_in_list = 1;"
putd_backslash "break;"
}
putd_backslash "}"
}
putd_backslash "}"
putd_backslash "if(!already_in_list)"
indent {
putd_backslash "fff.reset_list[fff.reset_list_idx++] = (fff_reset_function_t)reset_function;"
}
}
putd_backslash "} else {"
indent {
putd_backslash "fff.dropped_resets++;"
}
putd "}"
}
end

def in_struct
Expand Down
Loading