Skip to content

Commit

Permalink
Merge pull request meekrosoft#15 from alvarez86/custom_fake_seq
Browse files Browse the repository at this point in the history
Support for custom fake sequences on non-variadic functions
  • Loading branch information
Mike Long authored Sep 25, 2016
2 parents 3cd33ed + 218cdf7 commit f88a17c
Show file tree
Hide file tree
Showing 9 changed files with 599 additions and 1 deletion.
34 changes: 33 additions & 1 deletion fakegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 */"
Expand Down Expand Up @@ -108,7 +116,14 @@ def define_value_function_variables_helper
putd " RETURN_TYPE return_val; \\"
putd " int return_val_seq_len; \\"
putd " int return_val_seq_idx; \\"
putd " RETURN_TYPE * return_val_seq; \\"
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
Expand Down Expand Up @@ -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(); \\"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
503 changes: 503 additions & 0 deletions fff.h

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions test/fff_test_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -36,6 +37,7 @@ void setup()
{
RESET_FAKE(voidfunc1);
RESET_FAKE(voidfunc2);
RESET_FAKE(voidfunc1outparam);
RESET_FAKE(longfunc0);
RESET_FAKE(enumfunc0);
RESET_FAKE(structfunc0);
Expand Down
2 changes: 2 additions & 0 deletions test/fff_test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -30,6 +31,7 @@ class FFFTestSuite: public testing::Test
RESET_FAKE(voidfunc1);
RESET_FAKE(voidfunc2);
RESET_FAKE(longfunc0);
RESET_FAKE(voidfunc1outparam);
FFF_RESET_HISTORY();
}
};
Expand Down
1 change: 1 addition & 0 deletions test/fff_test_global_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ void setup()
{
RESET_FAKE(voidfunc1);
RESET_FAKE(voidfunc2);
RESET_FAKE(voidfunc1outparam);
RESET_FAKE(longfunc0);
RESET_FAKE(enumfunc0);
RESET_FAKE(structfunc0);
Expand Down
1 change: 1 addition & 0 deletions test/fff_test_global_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FFFTestSuite: public testing::Test
RESET_FAKE(voidfunc1);
RESET_FAKE(voidfunc2);
RESET_FAKE(longfunc0);
RESET_FAKE(voidfunc1outparam);
FFF_RESET_HISTORY();
}
};
Expand Down
1 change: 1 addition & 0 deletions test/global_fakes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions test/global_fakes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...);
Expand All @@ -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);
Expand Down
54 changes: 54 additions & 0 deletions test/test_cases.include
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f88a17c

Please sign in to comment.