Moved fake variables into struct to avoid polluting global namespace

This commit is contained in:
Mike Long
2012-04-25 18:02:48 +01:00
parent 0a15a67527
commit 1bcf41ff8a
4 changed files with 684 additions and 512 deletions

View File

@@ -32,92 +32,92 @@ public:
// Call counting
TEST_F(FFFTestSuite, when_void_func_never_called_then_callcount_is_zero)
{
ASSERT_EQ(voidfunc1_call_count, 0u);
ASSERT_EQ(voidfunc1_fake.call_count, 0u);
}
TEST_F(FFFTestSuite, when_void_func_called_once_then_callcount_is_one)
{
voidfunc1(66);
ASSERT_EQ(voidfunc1_call_count, 1u);
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_call_count, 0u);
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_arg0_val, 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_arg0_val, 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_arg0_val, 0);
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_arg0_val, 'a');
ASSERT_EQ(voidfunc2_arg1_val, '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_arg0_val, 'c');
ASSERT_EQ(voidfunc2_arg1_val, '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_arg0_val, 0);
ASSERT_EQ(voidfunc2_arg1_val, 0);
ASSERT_EQ(voidfunc2_fake.arg0_val, 0);
ASSERT_EQ(voidfunc2_fake.arg1_val, 0);
}
// Argument history
TEST_F(FFFTestSuite, when_fake_func_created_default_history_is_fifty_calls)
{
ASSERT_EQ(FFF_ARG_HISTORY_LEN, (sizeof voidfunc2_arg0_history) / (sizeof voidfunc2_arg0_history[0]));
ASSERT_EQ(FFF_ARG_HISTORY_LEN, (sizeof voidfunc2_arg1_history) / (sizeof voidfunc2_arg1_history[0]));
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_arg0_history[0]);
ASSERT_EQ('h', voidfunc2_arg1_history[0]);
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_arg0_history[0]);
ASSERT_EQ('h', voidfunc2_arg1_history[0]);
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_arg0_history[0]);
ASSERT_EQ('\0', voidfunc2_arg1_history[0]);
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)
@@ -127,7 +127,7 @@ TEST_F(FFFTestSuite, when_fake_func_called_max_times_then_no_argument_histories_
{
voidfunc2('1' + i, '2' + i);
}
ASSERT_EQ(0u, voidfunc2_arg_histories_dropped);
ASSERT_EQ(0u, voidfunc2_fake.arg_histories_dropped);
}
TEST_F(FFFTestSuite, when_fake_func_called_max_times_plus_one_then_one_argument_history_dropped)
@@ -138,9 +138,9 @@ TEST_F(FFFTestSuite, when_fake_func_called_max_times_plus_one_then_one_argument_
voidfunc2('1' + i, '2' + i);
}
voidfunc2('1', '2');
ASSERT_EQ(1u, voidfunc2_arg_histories_dropped);
ASSERT_EQ(1u, voidfunc2_fake.arg_histories_dropped);
// or in other words..
ASSERT_GT(voidfunc2_call_count, voidfunc2_arg_history_len);
ASSERT_GT(voidfunc2_fake.call_count, voidfunc2_fake.arg_history_len);
}
// Return values
@@ -151,13 +151,13 @@ TEST_F(FFFTestSuite, value_func_will_return_zero_by_default)
TEST_F(FFFTestSuite, value_func_will_return_value_given)
{
longfunc0_return_val = 99l;
longfunc0_fake.return_val = 99l;
ASSERT_EQ(99l, longfunc0());
}
TEST_F(FFFTestSuite, value_func_will_return_zero_after_reset)
{
longfunc0_return_val = 99l;
longfunc0_fake.return_val = 99l;
RESET_FAKE(longfunc0);
ASSERT_EQ(0l, longfunc0());
}
@@ -239,6 +239,6 @@ TEST_F(FFFTestSuite, default_constants_can_be_overridden)
{
unsigned sizeCallHistory = (sizeof call_history) / (sizeof call_history[0]);
ASSERT_EQ(OVERRIDE_CALL_HIST_LEN, sizeCallHistory);
ASSERT_EQ(OVERRIDE_ARG_HIST_LEN, voidfunc2_arg_history_len);
ASSERT_EQ(OVERRIDE_ARG_HIST_LEN, voidfunc2_fake.arg_history_len);
}