mirror of
https://github.com/meekrosoft/fff
synced 2026-01-23 00:15:59 +01:00
Merge pull request #15 from alvarez86/custom_fake_seq
Support for custom fake sequences on non-variadic functions
This commit is contained in:
32
fakegen.rb
32
fakegen.rb
@@ -31,6 +31,7 @@ def output_internal_helper_macros
|
||||
putd "/* -- INTERNAL HELPER MACROS -- */"
|
||||
|
||||
define_return_sequence_helper
|
||||
define_custom_fake_sequence_helper
|
||||
define_reset_fake_macro
|
||||
define_declare_arg_helper
|
||||
define_declare_all_func_common_helper
|
||||
@@ -39,6 +40,7 @@ def output_internal_helper_macros
|
||||
define_save_arg_history_helper
|
||||
define_history_dropped_helper
|
||||
define_value_function_variables_helper
|
||||
define_custom_fake_seq_variables_helper
|
||||
define_increment_call_count_helper
|
||||
define_return_fake_result_helper
|
||||
define_extern_c_helper
|
||||
@@ -54,6 +56,12 @@ def define_return_sequence_helper
|
||||
putd " FUNCNAME##_fake.return_val_seq_len = ARRAY_LEN;"
|
||||
end
|
||||
|
||||
def define_custom_fake_sequence_helper
|
||||
putd "#define SET_CUSTOM_FAKE_SEQ(FUNCNAME, ARRAY_POINTER, ARRAY_LEN) \\"
|
||||
putd " FUNCNAME##_fake.custom_fake_seq = ARRAY_POINTER; \\"
|
||||
putd " FUNCNAME##_fake.custom_fake_seq_len = ARRAY_LEN;"
|
||||
end
|
||||
|
||||
def define_reset_fake_macro
|
||||
putd ""
|
||||
putd "/* Defining a function to reset a fake function */"
|
||||
@@ -111,6 +119,13 @@ def define_value_function_variables_helper
|
||||
putd " RETURN_TYPE * return_val_seq; \\"
|
||||
end
|
||||
|
||||
def define_custom_fake_seq_variables_helper
|
||||
putd ""
|
||||
putd "#define DECLARE_CUSTOM_FAKE_SEQ_VARIABLES \\"
|
||||
putd " int custom_fake_seq_len; \\"
|
||||
putd " int custom_fake_seq_idx; \\"
|
||||
end
|
||||
|
||||
def define_increment_call_count_helper
|
||||
putd ""
|
||||
putd "#define INCREMENT_CALL_COUNT(FUNCNAME) \\"
|
||||
@@ -244,7 +259,9 @@ def output_variables(arg_count, has_varargs, is_value_function)
|
||||
}
|
||||
putd "DECLARE_ALL_FUNC_COMMON \\"
|
||||
putd "DECLARE_VALUE_FUNCTION_VARIABLES(RETURN_TYPE) \\" unless not is_value_function
|
||||
putd "DECLARE_CUSTOM_FAKE_SEQ_VARIABLES \\"
|
||||
output_custom_function_signature(arg_count, has_varargs, is_value_function)
|
||||
output_custom_function_array(arg_count, has_varargs, is_value_function)
|
||||
}
|
||||
putd "extern FUNCNAME##_Fake FUNCNAME##_fake;\\"
|
||||
putd "void FUNCNAME##_reset(); \\"
|
||||
@@ -273,6 +290,13 @@ def output_custom_function_signature(arg_count, has_varargs, is_value_function)
|
||||
putd return_type + signature
|
||||
end
|
||||
|
||||
def output_custom_function_array(arg_count, has_varargs, is_value_function)
|
||||
return_type = is_value_function ? "RETURN_TYPE" : "void"
|
||||
ap_list = has_varargs ? ", va_list ap" : ""
|
||||
custom_array = "(**custom_fake_seq)(#{arg_val_list(arg_count)}#{ap_list}); \\"
|
||||
putd return_type + custom_array
|
||||
end
|
||||
|
||||
# example: RETURN_TYPE FUNCNAME(ARG0_TYPE arg0, ARG1_TYPE arg1)
|
||||
def function_signature(arg_count, has_varargs, is_value_function)
|
||||
return_type = is_value_function ? "RETURN_TYPE" : "void"
|
||||
@@ -307,6 +331,14 @@ def output_function_body(arg_count, has_varargs, is_value_function)
|
||||
putd "}\\"
|
||||
else
|
||||
return_type = is_value_function ? "return " : ""
|
||||
putd "if (FUNCNAME##_fake.custom_fake_seq_len){ /* a sequence of custom fakes */ \\"
|
||||
putd " if (FUNCNAME##_fake.custom_fake_seq_idx < FUNCNAME##_fake.custom_fake_seq_len){ \\"
|
||||
putd " #{return_type}FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_idx++](#{arg_list(arg_count)}); \\"
|
||||
putd " } \\"
|
||||
putd " else{ \\"
|
||||
putd " #{return_type}FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_len-1](#{arg_list(arg_count)}); \\"
|
||||
putd " } \\"
|
||||
putd "} \\"
|
||||
putd "if (FUNCNAME##_fake.custom_fake) #{return_type}FUNCNAME##_fake.custom_fake(#{arg_list(arg_count)}); \\"
|
||||
end
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ struct MyStruct {
|
||||
|
||||
FAKE_VOID_FUNC(voidfunc1, int);
|
||||
FAKE_VOID_FUNC(voidfunc2, char, char);
|
||||
FAKE_VOID_FUNC(voidfunc1outparam, char *);
|
||||
FAKE_VALUE_FUNC(long, longfunc0);
|
||||
FAKE_VALUE_FUNC(enum MYBOOL, enumfunc0);
|
||||
FAKE_VALUE_FUNC(struct MyStruct, structfunc0);
|
||||
@@ -36,6 +37,7 @@ void setup()
|
||||
{
|
||||
RESET_FAKE(voidfunc1);
|
||||
RESET_FAKE(voidfunc2);
|
||||
RESET_FAKE(voidfunc1outparam);
|
||||
RESET_FAKE(longfunc0);
|
||||
RESET_FAKE(enumfunc0);
|
||||
RESET_FAKE(structfunc0);
|
||||
|
||||
@@ -19,6 +19,7 @@ DEFINE_FFF_GLOBALS
|
||||
|
||||
FAKE_VOID_FUNC(voidfunc1, int);
|
||||
FAKE_VOID_FUNC(voidfunc2, char, char);
|
||||
FAKE_VOID_FUNC(voidfunc1outparam, char *);
|
||||
FAKE_VALUE_FUNC(long, longfunc0);
|
||||
FAKE_VOID_FUNC(voidfunc20, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
|
||||
|
||||
@@ -30,6 +31,7 @@ public:
|
||||
RESET_FAKE(voidfunc1);
|
||||
RESET_FAKE(voidfunc2);
|
||||
RESET_FAKE(longfunc0);
|
||||
RESET_FAKE(voidfunc1outparam);
|
||||
FFF_RESET_HISTORY();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -10,6 +10,7 @@ void setup()
|
||||
{
|
||||
RESET_FAKE(voidfunc1);
|
||||
RESET_FAKE(voidfunc2);
|
||||
RESET_FAKE(voidfunc1outparam);
|
||||
RESET_FAKE(longfunc0);
|
||||
RESET_FAKE(enumfunc0);
|
||||
RESET_FAKE(structfunc0);
|
||||
|
||||
@@ -14,6 +14,7 @@ public:
|
||||
RESET_FAKE(voidfunc1);
|
||||
RESET_FAKE(voidfunc2);
|
||||
RESET_FAKE(longfunc0);
|
||||
RESET_FAKE(voidfunc1outparam);
|
||||
FFF_RESET_HISTORY();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
DEFINE_FAKE_VOID_FUNC1(voidfunc1, int);
|
||||
DEFINE_FAKE_VOID_FUNC2(voidfunc2, char, char);
|
||||
DEFINE_FAKE_VOID_FUNC1(voidfunc1outparam, char *);
|
||||
DEFINE_FAKE_VALUE_FUNC0(long, longfunc0);
|
||||
DEFINE_FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0);
|
||||
DEFINE_FAKE_VALUE_FUNC0(struct MyStruct, structfunc0);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
//// Imaginary production code header file ///
|
||||
void voidfunc1(int);
|
||||
void voidfunc2(char, char);
|
||||
void voidfunc1outparam(char *);
|
||||
long longfunc0();
|
||||
void voidfunc3var(const char *fmt, int argc, ...);
|
||||
int valuefunc3var(const char *fmt, int argc, ...);
|
||||
@@ -25,6 +26,7 @@ struct MyStruct structfunc();
|
||||
|
||||
DECLARE_FAKE_VOID_FUNC1(voidfunc1, int);
|
||||
DECLARE_FAKE_VOID_FUNC2(voidfunc2, char, char);
|
||||
DECLARE_FAKE_VOID_FUNC1(voidfunc1outparam, char *);
|
||||
DECLARE_FAKE_VALUE_FUNC0(long, longfunc0);
|
||||
DECLARE_FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0);
|
||||
DECLARE_FAKE_VALUE_FUNC0(struct MyStruct, structfunc0);
|
||||
|
||||
@@ -225,6 +225,60 @@ TEST_F(FFFTestSuite, can_register_custom_fake)
|
||||
ASSERT_EQ(1, my_custom_fake_called);
|
||||
}
|
||||
|
||||
void voidfunc1outparam_custom_fake1(char *a)
|
||||
{
|
||||
*a = 'x';
|
||||
}
|
||||
|
||||
void voidfunc1outparam_custom_fake2(char *a)
|
||||
{
|
||||
*a = 'y';
|
||||
}
|
||||
|
||||
void voidfunc1outparam_custom_fake3(char *a)
|
||||
{
|
||||
*a = 'z';
|
||||
}
|
||||
|
||||
TEST_F(FFFTestSuite, custom_fake_sequence_not_exausthed)
|
||||
{
|
||||
void (*custom_fakes[])(char *) = {voidfunc1outparam_custom_fake1,
|
||||
voidfunc1outparam_custom_fake2,
|
||||
voidfunc1outparam_custom_fake3};
|
||||
char a = 'a';
|
||||
|
||||
SET_CUSTOM_FAKE_SEQ(voidfunc1outparam, custom_fakes, 3);
|
||||
|
||||
voidfunc1outparam(&a);
|
||||
ASSERT_EQ('x', a);
|
||||
voidfunc1outparam(&a);
|
||||
ASSERT_EQ('y', a);
|
||||
voidfunc1outparam(&a);
|
||||
ASSERT_EQ('z', a);
|
||||
}
|
||||
|
||||
TEST_F(FFFTestSuite, custom_fake_sequence_exhausted)
|
||||
{
|
||||
void (*custom_fakes[])(char *) = {voidfunc1outparam_custom_fake1,
|
||||
voidfunc1outparam_custom_fake2,
|
||||
voidfunc1outparam_custom_fake3};
|
||||
char a = 'a';
|
||||
|
||||
SET_CUSTOM_FAKE_SEQ(voidfunc1outparam, custom_fakes, 3);
|
||||
|
||||
voidfunc1outparam(&a);
|
||||
ASSERT_EQ('x', a);
|
||||
voidfunc1outparam(&a);
|
||||
ASSERT_EQ('y', a);
|
||||
voidfunc1outparam(&a);
|
||||
ASSERT_EQ('z', a);
|
||||
a = 'a';
|
||||
voidfunc1outparam(&a);
|
||||
ASSERT_EQ('z', a);
|
||||
a = 'b';
|
||||
voidfunc1outparam(&a);
|
||||
ASSERT_EQ('z', a);
|
||||
}
|
||||
//DECLARE_FAKE_VALUE_FUNC0(long, longfunc0);
|
||||
#define MEANING_OF_LIFE 42
|
||||
long my_custom_value_fake(void)
|
||||
|
||||
Reference in New Issue
Block a user