From e35ae98cac70f4fbc3f10110bdfadee241ea7d03 Mon Sep 17 00:00:00 2001 From: Micha Hoiting Date: Mon, 5 Nov 2012 16:55:37 +0100 Subject: [PATCH 1/2] Add test with const parameters --- test/fff_test_c.c | 3 +++ test/fff_test_cpp.cpp | 2 ++ test/fff_test_global_c.c | 2 ++ test/global_fakes.c | 1 + test/global_fakes.h | 1 + test/test_cases.include | 6 ++++++ 6 files changed, 15 insertions(+) diff --git a/test/fff_test_c.c b/test/fff_test_c.c index fc3bd50..31c4261 100644 --- a/test/fff_test_c.c +++ b/test/fff_test_c.c @@ -27,6 +27,7 @@ FAKE_VOID_FUNC(voidfunc2, char, char); FAKE_VALUE_FUNC(long, longfunc0); FAKE_VALUE_FUNC(enum MYBOOL, enumfunc0); FAKE_VALUE_FUNC(struct MyStruct, structfunc0); +FAKE_VALUE_FUNC(int, strlcpy3, char* const, const char* const, const size_t); void setup() @@ -36,6 +37,7 @@ void setup() RESET_FAKE(longfunc0); RESET_FAKE(enumfunc0); RESET_FAKE(structfunc0); + RESET_FAKE(strlcpy3); FFF_RESET_HISTORY(); } @@ -68,6 +70,7 @@ int main() RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_then_last_args_captured); RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_twice_then_last_args_captured); RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_and_reset_then_captured_arg_is_zero); + RUN_TEST(FFFTestSuite, when_fake_func_called_then_const_arguments_captured); RUN_TEST(FFFTestSuite, when_fake_func_created_default_history_is_fifty_calls); RUN_TEST(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history); diff --git a/test/fff_test_cpp.cpp b/test/fff_test_cpp.cpp index 77103e9..9599e31 100644 --- a/test/fff_test_cpp.cpp +++ b/test/fff_test_cpp.cpp @@ -20,6 +20,7 @@ DEFINE_FFF_GLOBALS FAKE_VOID_FUNC(voidfunc1, int); FAKE_VOID_FUNC(voidfunc2, char, char); FAKE_VALUE_FUNC(long, longfunc0); +FAKE_VALUE_FUNC(int, strlcpy3, char* const, const char* const, const size_t); class FFFTestSuite: public testing::Test { @@ -29,6 +30,7 @@ public: RESET_FAKE(voidfunc1); RESET_FAKE(voidfunc2); RESET_FAKE(longfunc0); + RESET_FAKE(strlcpy3); FFF_RESET_HISTORY(); } }; diff --git a/test/fff_test_global_c.c b/test/fff_test_global_c.c index 6f6d640..a2a7684 100644 --- a/test/fff_test_global_c.c +++ b/test/fff_test_global_c.c @@ -13,6 +13,7 @@ void setup() RESET_FAKE(longfunc0); RESET_FAKE(enumfunc0); RESET_FAKE(structfunc0); + RESET_FAKE(strlcpy3); FFF_RESET_HISTORY(); } @@ -39,6 +40,7 @@ int main() RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_then_last_args_captured); RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_twice_then_last_args_captured); RUN_TEST(FFFTestSuite, when_void_func_with_2_char_args_called_and_reset_then_captured_arg_is_zero); + RUN_TEST(FFFTestSuite, when_fake_func_called_then_const_arguments_captured); RUN_TEST(FFFTestSuite, when_fake_func_created_default_history_is_fifty_calls); RUN_TEST(FFFTestSuite, when_fake_func_called_then_arguments_captured_in_history); diff --git a/test/global_fakes.c b/test/global_fakes.c index b64aaf9..008aa10 100644 --- a/test/global_fakes.c +++ b/test/global_fakes.c @@ -6,3 +6,4 @@ DEFINE_FAKE_VOID_FUNC2(voidfunc2, char, char); DEFINE_FAKE_VALUE_FUNC0(long, longfunc0); DEFINE_FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0); DEFINE_FAKE_VALUE_FUNC0(struct MyStruct, structfunc0); +DECLARE_FAKE_VALUE_FUNC3(int, strlcpy3, char* const, const char* const, const size_t); diff --git a/test/global_fakes.h b/test/global_fakes.h index c899649..3bb6d20 100644 --- a/test/global_fakes.h +++ b/test/global_fakes.h @@ -24,5 +24,6 @@ DECLARE_FAKE_VOID_FUNC2(voidfunc2, char, char); DECLARE_FAKE_VALUE_FUNC0(long, longfunc0); DECLARE_FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0); DECLARE_FAKE_VALUE_FUNC0(struct MyStruct, structfunc0); +DECLARE_FAKE_VALUE_FUNC3(int, strlcpy3, char* const, const char* const, const size_t); #endif /* GLOBAL_FAKES_H_ */ diff --git a/test/test_cases.include b/test/test_cases.include index 1cb4c29..9689731 100644 --- a/test/test_cases.include +++ b/test/test_cases.include @@ -63,6 +63,12 @@ TEST_F(FFFTestSuite, when_void_func_with_2_char_args_called_and_reset_then_captu ASSERT_EQ(voidfunc2_fake.arg1_val, 0); } +TEST_F(FFFTestSuite, when_fake_func_called_then_const_arguments_captured) +{ + char dst[80]; + strlcpy3(dst, __FUNCTION__, sizeof(__FUNCTION__)); +} + // Argument history TEST_F(FFFTestSuite, when_fake_func_created_default_history_is_fifty_calls) { From 3cf610ea9797ffa59bb30428272d69b1171a5e72 Mon Sep 17 00:00:00 2001 From: Micha Hoiting Date: Thu, 8 Nov 2012 23:14:02 +0100 Subject: [PATCH 2/2] Add support for const parameters in fakes (C only) --- fakegen.rb | 4 ++-- fff.h | 4 ++-- test/fff_test_cpp.cpp | 2 -- test/global_fakes.c | 4 +++- test/global_fakes.h | 3 +++ test/test_cases.include | 2 ++ 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/fakegen.rb b/fakegen.rb index 4a04bc9..339742c 100644 --- a/fakegen.rb +++ b/fakegen.rb @@ -77,7 +77,7 @@ end def define_save_arg_helper putd "" putd "#define SAVE_ARG(FUNCNAME, n) \\" - putd " FUNCNAME##_fake.arg##n##_val = arg##n" + putd " memcpy((void*)&FUNCNAME##_fake.arg##n##_val, (void*)&arg##n, sizeof(arg##n));" end def define_room_for_more_history @@ -89,7 +89,7 @@ end def define_save_arg_history_helper putd "" putd "#define SAVE_ARG_HISTORY(FUNCNAME, ARGN) \\" - putd " FUNCNAME##_fake.arg##ARGN##_history[FUNCNAME##_fake.call_count] = arg##ARGN" + putd " memcpy((void*)&FUNCNAME##_fake.arg##ARGN##_history[FUNCNAME##_fake.call_count], (void*)&arg##ARGN, sizeof(arg##ARGN));" end def define_history_dropped_helper diff --git a/fff.h b/fff.h index e62e403..a116073 100644 --- a/fff.h +++ b/fff.h @@ -29,13 +29,13 @@ unsigned int arg_histories_dropped; \ #define SAVE_ARG(FUNCNAME, n) \ - FUNCNAME##_fake.arg##n##_val = arg##n + memcpy((void*)&FUNCNAME##_fake.arg##n##_val, (void*)&arg##n, sizeof(arg##n)); #define ROOM_FOR_MORE_HISTORY(FUNCNAME) \ FUNCNAME##_fake.call_count < FFF_ARG_HISTORY_LEN #define SAVE_ARG_HISTORY(FUNCNAME, ARGN) \ - FUNCNAME##_fake.arg##ARGN##_history[FUNCNAME##_fake.call_count] = arg##ARGN + memcpy((void*)&FUNCNAME##_fake.arg##ARGN##_history[FUNCNAME##_fake.call_count], (void*)&arg##ARGN, sizeof(arg##ARGN)); #define HISTORY_DROPPED(FUNCNAME) \ FUNCNAME##_fake.arg_histories_dropped++ diff --git a/test/fff_test_cpp.cpp b/test/fff_test_cpp.cpp index 9599e31..77103e9 100644 --- a/test/fff_test_cpp.cpp +++ b/test/fff_test_cpp.cpp @@ -20,7 +20,6 @@ DEFINE_FFF_GLOBALS FAKE_VOID_FUNC(voidfunc1, int); FAKE_VOID_FUNC(voidfunc2, char, char); FAKE_VALUE_FUNC(long, longfunc0); -FAKE_VALUE_FUNC(int, strlcpy3, char* const, const char* const, const size_t); class FFFTestSuite: public testing::Test { @@ -30,7 +29,6 @@ public: RESET_FAKE(voidfunc1); RESET_FAKE(voidfunc2); RESET_FAKE(longfunc0); - RESET_FAKE(strlcpy3); FFF_RESET_HISTORY(); } }; diff --git a/test/global_fakes.c b/test/global_fakes.c index 008aa10..eb0d59c 100644 --- a/test/global_fakes.c +++ b/test/global_fakes.c @@ -6,4 +6,6 @@ DEFINE_FAKE_VOID_FUNC2(voidfunc2, char, char); DEFINE_FAKE_VALUE_FUNC0(long, longfunc0); DEFINE_FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0); DEFINE_FAKE_VALUE_FUNC0(struct MyStruct, structfunc0); -DECLARE_FAKE_VALUE_FUNC3(int, strlcpy3, char* const, const char* const, const size_t); +#ifndef __cplusplus +DEFINE_FAKE_VALUE_FUNC3(int, strlcpy3, char* const, const char* const, const size_t); +#endif /* __cplusplus */ diff --git a/test/global_fakes.h b/test/global_fakes.h index 3bb6d20..37b933e 100644 --- a/test/global_fakes.h +++ b/test/global_fakes.h @@ -3,6 +3,7 @@ #define GLOBAL_FAKES_H_ #include "../fff.h" +#include "string.h" //// Imaginary production code header file /// @@ -24,6 +25,8 @@ DECLARE_FAKE_VOID_FUNC2(voidfunc2, char, char); DECLARE_FAKE_VALUE_FUNC0(long, longfunc0); DECLARE_FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0); DECLARE_FAKE_VALUE_FUNC0(struct MyStruct, structfunc0); +#ifndef __cplusplus DECLARE_FAKE_VALUE_FUNC3(int, strlcpy3, char* const, const char* const, const size_t); +#endif /* __cplusplus */ #endif /* GLOBAL_FAKES_H_ */ diff --git a/test/test_cases.include b/test/test_cases.include index 9689731..52ae902 100644 --- a/test/test_cases.include +++ b/test/test_cases.include @@ -63,11 +63,13 @@ TEST_F(FFFTestSuite, when_void_func_with_2_char_args_called_and_reset_then_captu 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)