mirror of
https://github.com/meekrosoft/fff
synced 2026-01-23 08:25:59 +01:00
Merge pull request #11 from rubiot/variadic_custom_fakes
Support for variadic custom fakes
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
set -e
|
||||
|
||||
cat LICENSE > fff.h
|
||||
echo >> fff.h
|
||||
ruby fakegen.rb >> fff.h
|
||||
make clean
|
||||
make all
|
||||
|
||||
53
fakegen.rb
53
fakegen.rb
@@ -8,6 +8,10 @@ $MAX_ARGS = 20
|
||||
$DEFAULT_ARG_HISTORY = 50
|
||||
$MAX_CALL_HISTORY = 50
|
||||
|
||||
def include_dependencies
|
||||
putd "#include <stdarg.h>"
|
||||
end
|
||||
|
||||
def output_constants
|
||||
putd "#define FFF_MAX_ARGS (#{$MAX_ARGS}u)"
|
||||
putd "#ifndef FFF_ARG_HISTORY_LEN"
|
||||
@@ -264,7 +268,8 @@ end
|
||||
# void (*custom_fake)(ARG0_TYPE arg0, ARG1_TYPE arg1, ARG2_TYPE arg2);\
|
||||
def output_custom_function_signature(arg_count, has_varargs, is_value_function)
|
||||
return_type = is_value_function ? "RETURN_TYPE" : "void"
|
||||
signature = "(*custom_fake)(#{arg_val_list(arg_count)}); \\"
|
||||
ap_list = has_varargs ? ", va_list ap" : ""
|
||||
signature = "(*custom_fake)(#{arg_val_list(arg_count)}#{ap_list}); \\"
|
||||
putd return_type + signature
|
||||
end
|
||||
|
||||
@@ -285,10 +290,26 @@ def output_function_body(arg_count, has_varargs, is_value_function)
|
||||
putd "}\\"
|
||||
putd "INCREMENT_CALL_COUNT(FUNCNAME); \\"
|
||||
putd "REGISTER_CALL(FUNCNAME); \\"
|
||||
|
||||
return_type = is_value_function ? "return" : ""
|
||||
putd "if (FUNCNAME##_fake.custom_fake) #{return_type} FUNCNAME##_fake.custom_fake(#{arg_list(arg_count)}); \\"
|
||||
|
||||
|
||||
if has_varargs
|
||||
putd "if(FUNCNAME##_fake.custom_fake){\\"
|
||||
putd " RETURN_TYPE ret;\\" if is_value_function
|
||||
putd " va_list ap;\\"
|
||||
putd " va_start(ap, arg#{arg_count-1});\\"
|
||||
custom_fake_call = "FUNCNAME##_fake.custom_fake(#{arg_list(arg_count)}, ap);"
|
||||
if is_value_function
|
||||
putd " ret = #{custom_fake_call}\\"
|
||||
else
|
||||
putd " #{custom_fake_call}\\"
|
||||
end
|
||||
putd " va_end(ap);\\"
|
||||
putd " return ret;\\" if is_value_function
|
||||
putd "}\\"
|
||||
else
|
||||
return_type = is_value_function ? "return " : ""
|
||||
putd "if (FUNCNAME##_fake.custom_fake) #{return_type}FUNCNAME##_fake.custom_fake(#{arg_list(arg_count)}); \\"
|
||||
end
|
||||
|
||||
putd "RETURN_FAKE_RESULT(FUNCNAME) \\" if is_value_function
|
||||
end
|
||||
|
||||
@@ -385,6 +406,7 @@ def output_macro_counting_shortcuts
|
||||
#define PP_RSEQ_N_MINUS1() \
|
||||
20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
|
||||
|
||||
|
||||
#define FAKE_VOID_FUNC(...) \
|
||||
FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__)
|
||||
|
||||
@@ -394,12 +416,33 @@ def output_macro_counting_shortcuts
|
||||
#define FUNC_VOID_N(N,...) \
|
||||
FAKE_VOID_FUNC ## N(__VA_ARGS__)
|
||||
|
||||
|
||||
#define FAKE_VALUE_FUNC_VARARG(...) \
|
||||
FUNC_VALUE_VARARG_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__)
|
||||
|
||||
#define FUNC_VALUE_VARARG_(N,...) \
|
||||
FUNC_VALUE_VARARG_N(N,__VA_ARGS__)
|
||||
|
||||
#define FUNC_VALUE_VARARG_N(N,...) \
|
||||
FAKE_VALUE_FUNC ## N ## _VARARG(__VA_ARGS__)
|
||||
|
||||
|
||||
#define FAKE_VOID_FUNC_VARARG(...) \
|
||||
FUNC_VOID_VARARG_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__)
|
||||
|
||||
#define FUNC_VOID_VARARG_(N,...) \
|
||||
FUNC_VOID_VARARG_N(N,__VA_ARGS__)
|
||||
|
||||
#define FUNC_VOID_VARARG_N(N,...) \
|
||||
FAKE_VOID_FUNC ## N ## _VARARG(__VA_ARGS__)
|
||||
|
||||
MACRO_COUNTING
|
||||
end
|
||||
|
||||
def output_c_and_cpp
|
||||
|
||||
include_guard {
|
||||
include_dependencies
|
||||
output_constants
|
||||
output_internal_helper_macros
|
||||
yield
|
||||
|
||||
@@ -27,7 +27,8 @@ FAKE_VOID_FUNC(voidfunc2, char, char);
|
||||
FAKE_VALUE_FUNC(long, longfunc0);
|
||||
FAKE_VALUE_FUNC(enum MYBOOL, enumfunc0);
|
||||
FAKE_VALUE_FUNC(struct MyStruct, structfunc0);
|
||||
FAKE_VOID_FUNC3_VARARG(voidfunc3var, char *, int, ...);
|
||||
FAKE_VOID_FUNC_VARARG(voidfunc3var, char *, int, ...);
|
||||
FAKE_VALUE_FUNC_VARARG(int, valuefunc3var, char *, int, ...);
|
||||
FAKE_VALUE_FUNC(int, strlcpy3, char* const, const char* const, const size_t);
|
||||
FAKE_VOID_FUNC(voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
|
||||
|
||||
@@ -39,6 +40,7 @@ void setup()
|
||||
RESET_FAKE(enumfunc0);
|
||||
RESET_FAKE(structfunc0);
|
||||
RESET_FAKE(voidfunc3var);
|
||||
RESET_FAKE(valuefunc3var);
|
||||
RESET_FAKE(strlcpy3);
|
||||
FFF_RESET_HISTORY();
|
||||
}
|
||||
@@ -96,7 +98,8 @@ int main()
|
||||
RUN_TEST(FFFTestSuite, can_register_custom_fake);
|
||||
RUN_TEST(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_return_value);
|
||||
|
||||
RUN_TEST(FFFTestSuite, use_vararg_fake_with_different_number_of_arguments);
|
||||
RUN_TEST(FFFTestSuite, use_void_vararg_fake_with_different_number_of_arguments);
|
||||
RUN_TEST(FFFTestSuite, use_value_vararg_fake_with_different_number_of_arguments);
|
||||
|
||||
RUN_TEST(FFFTestSuite, can_capture_upto_20_arguments_correctly);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ void setup()
|
||||
RESET_FAKE(enumfunc0);
|
||||
RESET_FAKE(structfunc0);
|
||||
RESET_FAKE(voidfunc3var);
|
||||
RESET_FAKE(valuefunc3var);
|
||||
RESET_FAKE(strlcpy3);
|
||||
|
||||
FFF_RESET_HISTORY();
|
||||
@@ -64,7 +65,8 @@ int main()
|
||||
RUN_TEST(FFFTestSuite, can_register_custom_fake);
|
||||
RUN_TEST(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_return_value);
|
||||
|
||||
RUN_TEST(FFFTestSuite, use_vararg_fake_with_different_number_of_arguments);
|
||||
RUN_TEST(FFFTestSuite, use_void_vararg_fake_with_different_number_of_arguments);
|
||||
RUN_TEST(FFFTestSuite, use_value_vararg_fake_with_different_number_of_arguments);
|
||||
|
||||
RUN_TEST(FFFTestSuite, can_capture_upto_20_arguments_correctly);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ DEFINE_FAKE_VALUE_FUNC0(long, longfunc0);
|
||||
DEFINE_FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0);
|
||||
DEFINE_FAKE_VALUE_FUNC0(struct MyStruct, structfunc0);
|
||||
DEFINE_FAKE_VOID_FUNC3_VARARG(voidfunc3var, const char *, int, ...);
|
||||
DEFINE_FAKE_VALUE_FUNC3_VARARG(int, valuefunc3var, const char *, int, ...);
|
||||
#ifndef __cplusplus
|
||||
DEFINE_FAKE_VALUE_FUNC3(int, strlcpy3, char* const, const char* const, const size_t);
|
||||
#endif /* __cplusplus */
|
||||
|
||||
@@ -11,6 +11,7 @@ void voidfunc1(int);
|
||||
void voidfunc2(char, char);
|
||||
long longfunc0();
|
||||
void voidfunc3var(const char *fmt, int argc, ...);
|
||||
int valuefunc3var(const char *fmt, int argc, ...);
|
||||
void voidfunc20(int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
|
||||
|
||||
enum MYBOOL { FALSE = 899, TRUE };
|
||||
@@ -28,6 +29,7 @@ DECLARE_FAKE_VALUE_FUNC0(long, longfunc0);
|
||||
DECLARE_FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0);
|
||||
DECLARE_FAKE_VALUE_FUNC0(struct MyStruct, structfunc0);
|
||||
DECLARE_FAKE_VOID_FUNC3_VARARG(voidfunc3var, const char *, int, ...);
|
||||
DECLARE_FAKE_VALUE_FUNC3_VARARG(int, valuefunc3var, const char *, int, ...);
|
||||
DECLARE_FAKE_VOID_FUNC20(voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
|
||||
|
||||
#ifndef __cplusplus
|
||||
|
||||
@@ -239,13 +239,21 @@ TEST_F(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_return
|
||||
}
|
||||
|
||||
#ifndef __cplusplus
|
||||
TEST_F(FFFTestSuite, use_vararg_fake_with_different_number_of_arguments)
|
||||
TEST_F(FFFTestSuite, use_void_vararg_fake_with_different_number_of_arguments)
|
||||
{
|
||||
voidfunc3var("0 parameters", 0);
|
||||
voidfunc3var("1 parameter", 1, 10);
|
||||
voidfunc3var("2 parameters", 2, 10, 20);
|
||||
voidfunc3var("3 parameters", 3, 10, 20, 30);
|
||||
}
|
||||
|
||||
TEST_F(FFFTestSuite, use_value_vararg_fake_with_different_number_of_arguments)
|
||||
{
|
||||
valuefunc3var("0 parameters", 0);
|
||||
valuefunc3var("1 parameter", 1, 10);
|
||||
valuefunc3var("2 parameters", 2, 10, 20);
|
||||
valuefunc3var("3 parameters", 3, 10, 20, 30);
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
TEST_F(FFFTestSuite, can_capture_upto_20_arguments_correctly)
|
||||
|
||||
Reference in New Issue
Block a user