Skip to content

Commit

Permalink
Merge pull request meekrosoft#113 from yperess/peress/custom_fake_sig
Browse files Browse the repository at this point in the history
Support custom function signatures
  • Loading branch information
meekrosoft authored Sep 8, 2022
2 parents 7e0126b + 3ede051 commit 11ab05b
Show file tree
Hide file tree
Showing 15 changed files with 368 additions and 259 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,17 @@ The basic mechanism that fff provides you in this case is the custom_fake field
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 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.
In case your project uses a C compiler that supports nested functions (e.g. GCC), or when using C++ lambdas, you can even combine all this in a single unit test function so you can easily oversee all details of the test.
```c
#include <functional>
/* Configure FFF to use std::function, which enables capturing lambdas */
#define CUSTOM_FFF_FUNCTION_TEMPLATE(RETURN, FUNCNAME, ...) \
std::function<RETURN (__VA_ARGS__)> FUNCNAME
#include "fff.h"
/* The time structure */
typedef struct {
int hour, min;
Expand All @@ -474,15 +482,13 @@ FAKE_VOID_FUNC(getTime, Time*);
TEST_F(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_output)
{
Time t;
Time getTime_custom_now;
void getTime_custom_fake(Time *now) {
*now = getTime_custom_now;
}
getTime_fake.custom_fake = getTime_custom_fake;
/* given a specific time */
getTime_custom_now.hour = 13;
getTime_custom_now.min = 05;
Time getTime_custom_now = {
.hour = 13,
.min = 05,
};
getTime_fake.custom_fake = [getTime_custom_now](Time *now) {
*now = getTime_custom_now;
};
/* when getTime is called */
getTime(&t);
Expand Down
40 changes: 30 additions & 10 deletions fakegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,21 @@ def output_constants
putd "#define FFF_GCC_FUNCTION_ATTRIBUTES"
}
putd "#endif"

end


def output_default_function_pointer_macro(has_calling_conventions)
name = has_calling_conventions ? "(CALLING_CONVENTION *FUNCNAME)" : "(*FUNCNAME)"
calling_conv = has_calling_conventions ? ", CALLING_CONVENTION" : ""
putd "#ifndef CUSTOM_FFF_FUNCTION_TEMPLATE"
putd_backslash "#define CUSTOM_FFF_FUNCTION_TEMPLATE(RETURN#{calling_conv}, FUNCNAME, ...)"
indent {
putd "RETURN#{name}(__VA_ARGS__)"
}
putd "#endif /* CUSTOM_FFF_FUNCTION_TEMPLATE */"
end




Expand Down Expand Up @@ -235,7 +246,9 @@ def define_reset_fake_helper
indent {
putd_backslash "void FUNCNAME##_reset(void){"
indent {
putd_backslash "memset(&FUNCNAME##_fake, 0, sizeof(FUNCNAME##_fake));"
putd_backslash "memset((void*)&FUNCNAME##_fake, 0, sizeof(FUNCNAME##_fake) - sizeof(FUNCNAME##_fake.custom_fake) - sizeof(FUNCNAME##_fake.custom_fake_seq));"
putd_backslash "FUNCNAME##_fake.custom_fake = NULL;"
putd_backslash "FUNCNAME##_fake.custom_fake_seq = NULL;"
putd_backslash "FUNCNAME##_fake.arg_history_len = FFF_ARG_HISTORY_LEN;"
}
putd "}"
Expand Down Expand Up @@ -359,6 +372,14 @@ def arg_val_list(args_count)
arguments.join(", ")
end

#example: ARG0_TYPE, ARG1_TYPE
def arg_type_list(args_count)
return "void" if (args_count == 0)
arguments = []
args_count.times { |i| arguments << "ARG#{i}_TYPE" }
arguments.join(", ")
end

#example: arg0, arg1
def arg_list(args_count)
arguments = []
Expand All @@ -374,17 +395,15 @@ def arg_list(args_count)
def output_custom_function_signature(arg_count, has_varargs, has_calling_conventions, is_value_function)
return_type = is_value_function ? "RETURN_TYPE" : "void"
ap_list = has_varargs ? ", va_list ap" : ""
signature = has_calling_conventions ? "(CALLING_CONVENTION *custom_fake)" : "(*custom_fake)"
signature += "(#{arg_val_list(arg_count)}#{ap_list});"
putd_backslash return_type + signature
calling_conv = has_calling_conventions ? ", CALLING_CONVENTION" : ""
putd_backslash "CUSTOM_FFF_FUNCTION_TEMPLATE(#{return_type}#{calling_conv}, custom_fake, #{arg_type_list(arg_count)}#{ap_list});"
end

def output_custom_function_array(arg_count, has_varargs, has_calling_conventions, is_value_function)
return_type = is_value_function ? "RETURN_TYPE" : "void"
ap_list = has_varargs ? ", va_list ap" : ""
custom_array = has_calling_conventions ? "(CALLING_CONVENTION **custom_fake_seq)" : "(**custom_fake_seq)"
custom_array += "(#{arg_val_list(arg_count)}#{ap_list});"
putd_backslash return_type + custom_array
calling_conv = has_calling_conventions ? ", CALLING_CONVENTION" : ""
putd_backslash "CUSTOM_FFF_FUNCTION_TEMPLATE(#{return_type}#{calling_conv}, *custom_fake_seq, #{arg_type_list(arg_count)}#{ap_list});"
end

# example: RETURN_TYPE FUNCNAME(ARG0_TYPE arg0, ARG1_TYPE arg1)
Expand Down Expand Up @@ -483,7 +502,7 @@ def output_function_body(arg_count, has_varargs, is_value_function)
putd_backslash "}"
}
putd_backslash "}"
putd_backslash "if (FUNCNAME##_fake.custom_fake){ "
putd_backslash "if (FUNCNAME##_fake.custom_fake != NULL){ "
indent {
putd_backslash "RETURN_TYPE ret = FUNCNAME##_fake.custom_fake(#{arg_list(arg_count)});" unless not is_value_function
putd_backslash "SAVE_RET_HISTORY(FUNCNAME, ret);" unless not is_value_function
Expand Down Expand Up @@ -644,6 +663,7 @@ def output_c_and_cpp(has_calling_conventions)
include_guard {
include_dependencies
output_constants
output_default_function_pointer_macro(has_calling_conventions)
output_internal_helper_macros
yield
output_macro_counting_shortcuts(has_calling_conventions)
Expand Down Expand Up @@ -675,4 +695,4 @@ def help
(2..$MAX_ARGS).each {|arg_count| output_macro(arg_count, true, has_calling_conventions, false)}
(2..$MAX_ARGS).each {|arg_count| output_macro(arg_count, true, has_calling_conventions, true)}
}
}
}
Loading

0 comments on commit 11ab05b

Please sign in to comment.