| |
| |
| TEST_F(FFFTestSuite, when_void_func_never_called_then_callcount_is_zero) |
| { |
| ASSERT_EQ(voidfunc1_fake.call_count, 0u); |
| } |
| |
| TEST_F(FFFTestSuite, when_void_func_called_once_then_callcount_is_one) |
| { |
| voidfunc1(66); |
| ASSERT_EQ(voidfunc1_fake.call_count, 1u); |
| } |
| |
| TEST_F(FFFTestSuite, when_void_func_called_once_and_reset_then_callcount_is_zero) |
| { |
| voidfunc1(66); |
| RESET_FAKE(voidfunc1); |
| ASSERT_EQ(voidfunc1_fake.call_count, 0u); |
| } |
| |
| // Single Argument |
| TEST_F(FFFTestSuite, when_void_func_with_1_integer_arg_called_then_last_arg_captured) |
| { |
| voidfunc1(77); |
| ASSERT_EQ(voidfunc1_fake.arg0_val, 77); |
| } |
| |
| TEST_F(FFFTestSuite, when_void_func_with_1_integer_arg_called_twice_then_last_arg_captured) |
| { |
| voidfunc1(77); |
| voidfunc1(12); |
| ASSERT_EQ(voidfunc1_fake.arg0_val, 12); |
| } |
| |
| TEST_F(FFFTestSuite, when_void_func_with_1_integer_arg_called_and_reset_then_captured_arg_is_zero) |
| { |
| voidfunc1(11); |
| RESET_FAKE(voidfunc1); |
| ASSERT_EQ(voidfunc1_fake.arg0_val, 0); |
| } |
| |
| // Two Arguments |
| TEST_F(FFFTestSuite, when_void_func_with_2_char_args_called_then_last_args_captured) |
| { |
| voidfunc2('a', 'b'); |
| ASSERT_EQ(voidfunc2_fake.arg0_val, 'a'); |
| ASSERT_EQ(voidfunc2_fake.arg1_val, 'b'); |
| } |
| |
| TEST_F(FFFTestSuite, when_void_func_with_2_char_args_called_twice_then_last_args_captured) |
| { |
| voidfunc2('a', 'b'); |
| voidfunc2('c', 'd'); |
| ASSERT_EQ(voidfunc2_fake.arg0_val, 'c'); |
| ASSERT_EQ(voidfunc2_fake.arg1_val, 'd'); |
| } |
| |
| TEST_F(FFFTestSuite, when_void_func_with_2_char_args_called_and_reset_then_captured_arg_is_zero) |
| { |
| voidfunc2('e', 'f'); |
| RESET_FAKE(voidfunc2); |
| ASSERT_EQ(voidfunc2_fake.arg0_val, 0); |
| ASSERT_EQ(voidfunc2_fake.arg1_val, 0); |
| } |
| |
| #ifndef __cplusplus |
| TEST_F(FFFTestSuite, when_fake_func_called_then_const_arguments_captured) |
| { |
| char dst[80]; |
| strlcpy3(dst, __FUNCTION__, sizeof(__FUNCTION__)); |
| } |
| #endif /* __cplusplus */ |
| |
| // Argument history |
| TEST_F(FFFTestSuite, when_fake_func_created_default_history_is_fifty_calls) |
| { |
| ASSERT_EQ(FFF_ARG_HISTORY_LEN, (sizeof voidfunc2_fake.arg0_history) / (sizeof voidfunc2_fake.arg0_history[0])); |
| ASSERT_EQ(FFF_ARG_HISTORY_LEN, (sizeof voidfunc2_fake.arg1_history) / (sizeof voidfunc2_fake.arg1_history[0])); |
| } |
| |
| TEST_F(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history) |
| { |
| voidfunc2('g', 'h'); |
| ASSERT_EQ('g', voidfunc2_fake.arg0_history[0]); |
| ASSERT_EQ('h', voidfunc2_fake.arg1_history[0]); |
| } |
| |
| TEST_F(FFFTestSuite, argument_history_is_reset_when_RESET_FAKE_called) |
| { |
| //given |
| voidfunc2('g', 'h'); |
| ASSERT_EQ('g', voidfunc2_fake.arg0_history[0]); |
| ASSERT_EQ('h', voidfunc2_fake.arg1_history[0]); |
| //when |
| RESET_FAKE(voidfunc2); |
| //then |
| ASSERT_EQ('\0', voidfunc2_fake.arg0_history[0]); |
| ASSERT_EQ('\0', voidfunc2_fake.arg1_history[0]); |
| } |
| |
| TEST_F(FFFTestSuite, when_fake_func_called_max_times_then_no_argument_histories_dropped) |
| { |
| unsigned int i; |
| for (i = 0; i < FFF_ARG_HISTORY_LEN; i++) |
| { |
| voidfunc2('1' + i, '2' + i); |
| } |
| ASSERT_EQ(0u, voidfunc2_fake.arg_histories_dropped); |
| } |
| |
| TEST_F(FFFTestSuite, when_fake_func_called_max_times_plus_one_then_one_argument_history_dropped) |
| { |
| unsigned int i; |
| for (i = 0; i < FFF_ARG_HISTORY_LEN; i++) |
| { |
| voidfunc2('1' + i, '2' + i); |
| } |
| voidfunc2('1', '2'); |
| ASSERT_EQ(1u, voidfunc2_fake.arg_histories_dropped); |
| // or in other words.. |
| ASSERT_TRUE(voidfunc2_fake.call_count > voidfunc2_fake.arg_history_len); |
| } |
| |
| // Return values |
| TEST_F(FFFTestSuite, value_func_will_return_zero_by_default) |
| { |
| ASSERT_EQ(0l, longfunc0()); |
| } |
| |
| TEST_F(FFFTestSuite, value_func_will_return_value_given) |
| { |
| longfunc0_fake.return_val = 99l; |
| ASSERT_EQ(99l, longfunc0()); |
| } |
| |
| TEST_F(FFFTestSuite, value_func_will_return_zero_after_reset) |
| { |
| longfunc0_fake.return_val = 99l; |
| RESET_FAKE(longfunc0); |
| ASSERT_EQ(0l, longfunc0()); |
| } |
| |
| TEST_F(FFFTestSuite, register_call_macro_registers_one_call) |
| { |
| REGISTER_CALL(longfunc0); |
| ASSERT_EQ(fff.call_history[0], (void *)longfunc0); |
| } |
| |
| TEST_F(FFFTestSuite, register_call_macro_registers_two_calls) |
| { |
| REGISTER_CALL(longfunc0); |
| REGISTER_CALL(voidfunc2); |
| |
| ASSERT_EQ(fff.call_history[0], (void *)longfunc0); |
| ASSERT_EQ(fff.call_history[1], (void *)voidfunc2); |
| } |
| |
| TEST_F(FFFTestSuite, reset_call_history_resets_call_history) |
| { |
| REGISTER_CALL(longfunc0); |
| FFF_RESET_HISTORY(); |
| REGISTER_CALL(voidfunc2); |
| |
| ASSERT_EQ(1u, fff.call_history_idx); |
| ASSERT_EQ(fff.call_history[0], (void *)voidfunc2); |
| } |
| |
| TEST_F(FFFTestSuite, call_history_will_not_write_past_array_bounds) |
| { |
| for (unsigned int i = 0; i < FFF_CALL_HISTORY_LEN + 1; i++) |
| { |
| REGISTER_CALL(longfunc0); |
| } |
| ASSERT_EQ(FFF_CALL_HISTORY_LEN, fff.call_history_idx); |
| } |
| |
| TEST_F(FFFTestSuite, calling_fake_registers_one_call) |
| { |
| longfunc0(); |
| ASSERT_EQ(fff.call_history_idx, 1u); |
| ASSERT_EQ(fff.call_history[0], (void *)longfunc0); |
| } |
| |
| TEST_F(FFFTestSuite, return_value_sequences_not_exhausted) |
| { |
| long myReturnVals[3] = { 3, 7, 9 }; |
| SET_RETURN_SEQ(longfunc0, myReturnVals, 3); |
| ASSERT_EQ(myReturnVals[0], longfunc0()); |
| ASSERT_EQ(myReturnVals[1], longfunc0()); |
| ASSERT_EQ(myReturnVals[2], longfunc0()); |
| } |
| |
| |
| TEST_F(FFFTestSuite, return_value_sequences_exhausted) |
| { |
| long myReturnVals[3] = { 3, 7, 9 }; |
| SET_RETURN_SEQ(longfunc0, myReturnVals, 3); |
| ASSERT_EQ(myReturnVals[0], longfunc0()); |
| ASSERT_EQ(myReturnVals[1], longfunc0()); |
| ASSERT_EQ(myReturnVals[2], longfunc0()); |
| ASSERT_EQ(myReturnVals[2], longfunc0()); |
| ASSERT_EQ(myReturnVals[2], longfunc0()); |
| } |
| |
| TEST_F(FFFTestSuite, return_value_sequences_reset) |
| { |
| long myReturnVals[3] = { 3, 7, 9 }; |
| SET_RETURN_SEQ(longfunc0, myReturnVals, 3); |
| ASSERT_EQ(myReturnVals[0], longfunc0()); |
| ASSERT_EQ(myReturnVals[1], longfunc0()); |
| RESET_FAKE(longfunc0); |
| ASSERT_EQ(0, longfunc0()); |
| } |
| |
| static int my_custom_fake_called = 0; |
| void my_custom_fake(char a, char b) |
| { |
| my_custom_fake_called++; |
| } |
| |
| TEST_F(FFFTestSuite, can_register_custom_fake) |
| { |
| voidfunc2_fake.custom_fake = my_custom_fake; |
| voidfunc2('a', 'b'); |
| ASSERT_EQ(1, my_custom_fake_called); |
| } |
| |
| //DECLARE_FAKE_VALUE_FUNC0(long, longfunc0); |
| #define MEANING_OF_LIFE 42 |
| long my_custom_value_fake(void) |
| { |
| return MEANING_OF_LIFE; |
| } |
| TEST_F(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_return_value) |
| { |
| longfunc0_fake.custom_fake = my_custom_value_fake; |
| long retval = longfunc0(); |
| ASSERT_EQ(MEANING_OF_LIFE, retval); |
| } |
| |
| #ifndef __cplusplus |
| 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) |
| { |
| voidfunc20(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19); |
| ASSERT_EQ(0, voidfunc20_fake.arg0_val); |
| ASSERT_EQ(1, voidfunc20_fake.arg1_val); |
| ASSERT_EQ(2, voidfunc20_fake.arg2_val); |
| ASSERT_EQ(3, voidfunc20_fake.arg3_val); |
| ASSERT_EQ(4, voidfunc20_fake.arg4_val); |
| ASSERT_EQ(5, voidfunc20_fake.arg5_val); |
| ASSERT_EQ(6, voidfunc20_fake.arg6_val); |
| ASSERT_EQ(7, voidfunc20_fake.arg7_val); |
| ASSERT_EQ(8, voidfunc20_fake.arg8_val); |
| ASSERT_EQ(9, voidfunc20_fake.arg9_val); |
| ASSERT_EQ(10, voidfunc20_fake.arg10_val); |
| ASSERT_EQ(11, voidfunc20_fake.arg11_val); |
| ASSERT_EQ(12, voidfunc20_fake.arg12_val); |
| ASSERT_EQ(13, voidfunc20_fake.arg13_val); |
| ASSERT_EQ(14, voidfunc20_fake.arg14_val); |
| ASSERT_EQ(15, voidfunc20_fake.arg15_val); |
| ASSERT_EQ(16, voidfunc20_fake.arg16_val); |
| ASSERT_EQ(17, voidfunc20_fake.arg17_val); |
| ASSERT_EQ(18, voidfunc20_fake.arg18_val); |
| ASSERT_EQ(19, voidfunc20_fake.arg19_val); |
| } |
| |
| |