Skip to content

Commit

Permalink
Merge remote-tracking branch 'original/master' into refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
usr42 committed Jan 30, 2018
2 parents 15408ba + 953ff07 commit d9082e8
Show file tree
Hide file tree
Showing 5 changed files with 415 additions and 33 deletions.
39 changes: 38 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,44 @@ 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>
and <tt>FAKE_VOID_FUNC_VARARG</tt>. For instance:
FAKE_VALUE_FUNC_VARARG(int, fprintf, FILE *, const char*, ...);
In order to access the variadic parameters from a custom fake function, declare a
<tt>va_list</tt> parameter. For instance, a custom fake for <tt>fprintf()</tt>
could call the real <tt>fprintf()</tt> like this:
int fprintf_custom(FILE *stream, const char *format, va_list ap) {
if (fprintf0_fake.return_val < 0) // should we fail?
return fprintf0_fake.return_val;
return vfprintf(stream, format, ap);
}
## How do I fake a function that returns a value by reference?
The basic mechanism that FFF provides you in this case is the custom_fake field described in the *Custom Return Value Delegate* example above.
Expand Down
35 changes: 34 additions & 1 deletion fakegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,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 @@ -102,6 +104,14 @@ def define_declare_all_func_common_helper
}
end

def define_declare_return_value_history
putd ""
putd_backslash "#define DECLARE_RETURN_VALUE_HISTORY(RETURN_TYPE)"
indent {
putd "RETURN_TYPE return_val_history[FFF_ARG_HISTORY_LEN];"
}
end

def define_save_arg_helper
puts
putd_backslash "#define SAVE_ARG(FUNCNAME, n)"
Expand All @@ -110,6 +120,17 @@ def define_save_arg_helper
}
end

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

def define_room_for_more_history
puts
putd_backslash "#define ROOM_FOR_MORE_HISTORY(FUNCNAME)"
Expand Down Expand Up @@ -170,12 +191,15 @@ def define_return_fake_result_helper
indent {
putd_backslash "if(FUNCNAME##_fake.return_val_seq_idx < FUNCNAME##_fake.return_val_seq_len) {"
indent {
putd_backslash "SAVE_RET_HISTORY(FUNCNAME, FUNCNAME##_fake.return_val_seq[FUNCNAME##_fake.return_val_seq_idx])"
putd_backslash "return FUNCNAME##_fake.return_val_seq[FUNCNAME##_fake.return_val_seq_idx++];"
}
putd_backslash "}"
putd_backslash "SAVE_RET_HISTORY(FUNCNAME, FUNCNAME##_fake.return_val_seq[FUNCNAME##_fake.return_val_seq_len-1])"
putd_backslash "return FUNCNAME##_fake.return_val_seq[FUNCNAME##_fake.return_val_seq_len-1]; /* return last element */"
}
putd_backslash "}"
putd_backslash "SAVE_RET_HISTORY(FUNCNAME, FUNCNAME##_fake.return_val)"
putd_backslash "return FUNCNAME##_fake.return_val;"
}
end
Expand Down Expand Up @@ -310,12 +334,14 @@ def output_variables(arg_count, has_varargs, is_value_function)
}
putd_backslash "DECLARE_ALL_FUNC_COMMON"
putd_backslash "DECLARE_VALUE_FUNCTION_VARIABLES(RETURN_TYPE)" unless not is_value_function
putd_backslash "DECLARE_RETURN_VALUE_HISTORY(RETURN_TYPE)" unless not is_value_function
putd_backslash "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_backslash "extern FUNCNAME##_Fake FUNCNAME##_fake;"
putd_backslash "void FUNCNAME##_reset(void);"
putd_backslash function_signature(arg_count, has_varargs, is_value_function) + ";"
end

#example: ARG0_TYPE arg0, ARG1_TYPE arg1
Expand Down Expand Up @@ -386,6 +412,7 @@ def output_function_body(arg_count, has_varargs, is_value_function)
putd_backslash "#{custom_fake_call}"
end
putd_backslash "va_end(ap);"
putd_backslash "SAVE_RET_HISTORY(FUNCNAME, ret);" unless not is_value_function
putd_backslash "return ret;" if is_value_function
}
putd_backslash "}"
Expand All @@ -395,11 +422,17 @@ def output_function_body(arg_count, has_varargs, is_value_function)
indent {
putd_backslash "if (FUNCNAME##_fake.custom_fake_seq_idx < FUNCNAME##_fake.custom_fake_seq_len){"
indent {
putd_backslash "#{return_type}FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_idx++](#{arg_list(arg_count)});"
putd_backslash "RETURN_TYPE ret = FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_idx++](#{arg_list(arg_count)});" unless not is_value_function
putd_backslash "SAVE_RET_HISTORY(FUNCNAME, ret);" unless not is_value_function
putd_backslash "return ret;" unless not is_value_function
putd_backslash "#{return_type}FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_idx++](#{arg_list(arg_count)});" unless is_value_function
}
putd_backslash "}"
putd_backslash "else{"
indent {
putd_backslash "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_backslash "SAVE_RET_HISTORY(FUNCNAME, ret);" unless not is_value_function
putd_backslash "return ret;" unless not is_value_function
putd_backslash "#{return_type}FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_len-1](#{arg_list(arg_count)});"
}
putd_backslash "}"
Expand Down
Loading

0 comments on commit d9082e8

Please sign in to comment.