Skip to content

Commit

Permalink
Merge pull request meekrosoft#35 from alvarez86/return_val_history
Browse files Browse the repository at this point in the history
Return val history
  • Loading branch information
Mike Long authored Jan 28, 2018
2 parents ffde053 + 2c28c14 commit 953ff07
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 23 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ TEST_F(FFFTestSuite, calls_in_correct_order)

They are reset by calling `FFF_RESET_HISTORY();`


## Default Argument History

The framework will by default store the arguments for the last ten calls made
Expand Down Expand Up @@ -371,6 +370,27 @@ The fake will call your custom functions in the order specified by the SET_CUSTO
macro. When the last custom fake is reached the fake will keep calling the last custom
fake in the sequence. This macro works much like the SET_RETURN_SEQ macro.
## Return value history
Say you have two functions f1 and f2. f2 must be called to release some resource
allocated by f1, but only in the cases where f1 returns zero. f1 could be
pthread_mutex_trylock and f2 could be pthread_mutex_unlock. <tt>fff</tt> will
save the history of returned values so this can be easily checked, even when
you use a sequence of custom fakes. Here's a simple example:
TEST_F(FFFTestSuite, return_value_sequence_saved_in_history)
{
long myReturnVals[3] = { 3, 7, 9 };
SET_RETURN_SEQ(longfunc0, myReturnVals, 3);
longfunc0();
longfunc0();
longfunc0();
ASSERT_EQ(myReturnVals[0], longfunc0_fake.return_val_history[0]);
ASSERT_EQ(myReturnVals[1], longfunc0_fake.return_val_history[1]);
ASSERT_EQ(myReturnVals[2], longfunc0_fake.return_val_history[2]);
}
You access the returned values in the <tt>return_val_history</tt> field.
## Variadic Functions
You can fake variadic functions using the macros <tt>FAKE_VALUE_FUNC_VARARG</tt>
Expand Down
28 changes: 27 additions & 1 deletion fakegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ def output_internal_helper_macros
define_reset_fake_macro
define_declare_arg_helper
define_declare_all_func_common_helper
define_declare_return_value_history
define_save_arg_helper
define_room_for_more_history
define_save_ret_history_helper
define_save_arg_history_helper
define_history_dropped_helper
define_value_function_variables_helper
Expand Down Expand Up @@ -88,12 +90,25 @@ def define_declare_all_func_common_helper
putd " unsigned int arg_histories_dropped; \\"
end

def define_declare_return_value_history
putd ""
putd "#define DECLARE_RETURN_VALUE_HISTORY(RETURN_TYPE) \\"
putd " RETURN_TYPE return_val_history[FFF_ARG_HISTORY_LEN];"
end

def define_save_arg_helper
putd ""
putd "#define SAVE_ARG(FUNCNAME, n) \\"
putd " memcpy((void*)&FUNCNAME##_fake.arg##n##_val, (void*)&arg##n, sizeof(arg##n));"
end

def define_save_ret_history_helper
putd ""
putd "#define SAVE_RET_HISTORY(FUNCNAME, RETVAL) \\"
putd " if ((FUNCNAME##_fake.call_count - 1) < FFF_ARG_HISTORY_LEN) \\"
putd " memcpy((void *)&FUNCNAME##_fake.return_val_history[FUNCNAME##_fake.call_count - 1], (const void *) &RETVAL, sizeof(RETVAL)); \\"
end

def define_room_for_more_history
putd ""
putd "#define ROOM_FOR_MORE_HISTORY(FUNCNAME) \\"
Expand Down Expand Up @@ -139,10 +154,13 @@ def define_return_fake_result_helper
putd "#define RETURN_FAKE_RESULT(FUNCNAME) \\"
putd " if (FUNCNAME##_fake.return_val_seq_len){ /* then its a sequence */ \\"
putd " if(FUNCNAME##_fake.return_val_seq_idx < FUNCNAME##_fake.return_val_seq_len) { \\"
putd " SAVE_RET_HISTORY(FUNCNAME, FUNCNAME##_fake.return_val_seq[FUNCNAME##_fake.return_val_seq_idx]) \\"
putd " return FUNCNAME##_fake.return_val_seq[FUNCNAME##_fake.return_val_seq_idx++]; \\"
putd " } \\"
putd " SAVE_RET_HISTORY(FUNCNAME, FUNCNAME##_fake.return_val_seq[FUNCNAME##_fake.return_val_seq_len-1]) \\"
putd " return FUNCNAME##_fake.return_val_seq[FUNCNAME##_fake.return_val_seq_len-1]; /* return last element */ \\"
putd " } \\"
putd " SAVE_RET_HISTORY(FUNCNAME, FUNCNAME##_fake.return_val) \\"
putd " return FUNCNAME##_fake.return_val; \\"
end

Expand Down Expand Up @@ -261,6 +279,7 @@ 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_RETURN_VALUE_HISTORY(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)
Expand Down Expand Up @@ -331,15 +350,22 @@ def output_function_body(arg_count, has_varargs, is_value_function)
putd " #{custom_fake_call}\\"
end
putd " va_end(ap);\\"
putd " SAVE_RET_HISTORY(FUNCNAME, ret); \\" unless not is_value_function
putd " return ret;\\" if 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 " RETURN_TYPE ret = FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_idx++](#{arg_list(arg_count)}); \\" unless not is_value_function
putd " SAVE_RET_HISTORY(FUNCNAME, ret); \\" unless not is_value_function
putd " return ret; \\" unless not is_value_function
putd " #{return_type}FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_idx++](#{arg_list(arg_count)}); \\" unless is_value_function
putd " } \\"
putd " else{ \\"
putd " RETURN_TYPE ret = FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_len-1](#{arg_list(arg_count)}); \\" unless not is_value_function
putd " SAVE_RET_HISTORY(FUNCNAME, ret); \\" unless not is_value_function
putd " return ret; \\" unless not is_value_function
putd " #{return_type}FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_len-1](#{arg_list(arg_count)}); \\"
putd " } \\"
putd "} \\"
Expand Down
Loading

0 comments on commit 953ff07

Please sign in to comment.