From a875949914bd6bddacefea1b796de22728c24496 Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Thu, 27 Aug 2009 23:07:07 +0000 Subject: [PATCH] - fixed an error in pointer handling. beefed up tests git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@142 bf332499-1b4d-0410-844d-d2d48d5cc64c --- lib/cmock_generator_utils.rb | 4 +- lib/cmock_unityhelper_parser.rb | 8 +- .../fancy_pointer_handling.yml | 166 ++++++++++++++++++ test/unit/cmock_generator_utils_test.rb | 23 +++ test/unit/cmock_unityhelper_parser_test.rb | 7 +- 5 files changed, 204 insertions(+), 4 deletions(-) create mode 100644 test/system/test_interactions/fancy_pointer_handling.yml diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index 50137b3..b277b88 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -59,7 +59,9 @@ class CMockGeneratorUtils case(unity_func) when "TEST_ASSERT_EQUAL_MEMORY_MESSAGE" full_expected = (expected =~ /^\*/) ? expected.slice(1..-1) : "&(#{expected})" - return " #{unity_func}((void*)#{full_expected}, (void*)&(#{arg}), sizeof(#{c_type})#{unity_msg});\n" + return " TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)#{full_expected}, (void*)&(#{arg}), sizeof(#{c_type})#{unity_msg});\n" + when "TEST_ASSERT_EQUAL_MEMORY_MESSAGE_ARRAY" + return " if (*p_expected == NULL)\n { TEST_ASSERT_NULL(#{arg}); }\n else\n { TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)(#{expected}), (void*)#{arg}, sizeof(#{c_type.sub('*','')})#{unity_msg}); }\n" when /_ARRAY/ return " if (*p_expected == NULL)\n { TEST_ASSERT_NULL(#{arg}); }\n else\n { #{unity_func}(#{expected}, #{arg}, 1#{unity_msg}); }\n" else diff --git a/lib/cmock_unityhelper_parser.rb b/lib/cmock_unityhelper_parser.rb index 3dc3458..6d9d719 100644 --- a/lib/cmock_unityhelper_parser.rb +++ b/lib/cmock_unityhelper_parser.rb @@ -8,10 +8,14 @@ class CMockUnityHelperParser end def get_helper(ctype) - lookup = ctype.gsub(/const\s+/,'').strip.gsub(/\s+/,'_') + lookup = ctype.gsub(/(?:^|(\S?)(\s*)|(\W))const(?:$|(\s*)(\S)|(\W))/,'\1\3\5\6').strip.gsub(/\s+/,'_') return @c_types[lookup] if (@c_types[lookup]) raise("Don't know how to test #{ctype} and memory tests are disabled!") unless @config.memcmp_if_unknown - return 'TEST_ASSERT_EQUAL_MEMORY_MESSAGE' + if (ctype =~ /\*/) + return 'TEST_ASSERT_EQUAL_MEMORY_MESSAGE_ARRAY' + else + return 'TEST_ASSERT_EQUAL_MEMORY_MESSAGE' + end end private ########################### diff --git a/test/system/test_interactions/fancy_pointer_handling.yml b/test/system/test_interactions/fancy_pointer_handling.yml new file mode 100644 index 0000000..11cd5f3 --- /dev/null +++ b/test/system/test_interactions/fancy_pointer_handling.yml @@ -0,0 +1,166 @@ +--- +:cmock: + :plugins: + - # none + +:systest: + :types: | + typedef struct _POINT_T { + int x; + int y; + } POINT_T; + + :mockable: | + void foo(POINT_T* a); + POINT_T* bar(void); + void fooa(POINT_T a[]); + void foos(const char const * a); + const char const * bars(void); + + :source: + :header: | + void function_a(void); + void function_b(void); + + :code: | + void function_a(void) + { + foo(bar()); + } + + void function_b(void) { + fooa(bar()); + } + + void function_c(void) { + foos(bars()); + } + + :tests: + :common: | + void setUp(void) {} + void tearDown(void) {} + + :units: + - :pass: TRUE + :should: 'handle the situation where we pass nulls to pointers' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + + function_a(); + } + + - :pass: TRUE + :should: 'handle the situation where we pass single object with expect' + :code: | + test() + { + POINT_T pt = {1, 2}; + POINT_T ex = {1, 2}; + bar_ExpectAndReturn(&pt); + foo_Expect(&ex); + + function_a(); + } + + - :pass: FALSE + :should: 'handle the situation where we pass single object with expect and it is wrong' + :code: | + test() + { + POINT_T pt = {1, 2}; + POINT_T ex = {1, 3}; + bar_ExpectAndReturn(&pt); + foo_Expect(&ex); + + function_a(); + } + + - :pass: FALSE + :should: 'handle the situation where we pass nulls to pointers and fail' + :code: | + test() + { + POINT_T pt = {1, 2}; + bar_ExpectAndReturn(&pt); + foo_Expect(NULL); + + function_a(); + } + + - :pass: TRUE + :should: 'handle the situation where we pass nulls to arrays' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + fooa_Expect(NULL); + + function_b(); + } + + - :pass: TRUE + :should: 'handle the situation where we pass single array element with expect' + :code: | + test() + { + POINT_T pt = {1, 2}; + POINT_T ex = {1, 2}; + bar_ExpectAndReturn(&pt); + fooa_Expect(&ex); + + function_b(); + } + + - :pass: FALSE + :should: 'handle the situation where we pass single array element with expect and it is wrong' + :code: | + test() + { + POINT_T pt = {1, 2}; + POINT_T ex = {1, 3}; + bar_ExpectAndReturn(&pt); + fooa_Expect(&ex); + + function_b(); + } + + - :pass: FALSE + :should: 'handle the situation where we pass nulls to arrays and fail' + :code: | + test() + { + POINT_T pt = {1, 2}; + bar_ExpectAndReturn(&pt); + fooa_Expect(NULL); + + function_b(); + } + + - :pass: TRUE + :should: 'handle standard c string as null terminated on not do crappy memory compares of a byte, passing' + :code: | + test() + { + bars_ExpectAndReturn("This is a\0 silly string"); + foos_Expect("This is a\0 wacky string"); + + function_c(); + } + + - :pass: FALSE + :should: 'handle standard c string as null terminated on not do crappy memory compares of a byte, finding failures' + :code: | + test() + { + bars_ExpectAndReturn("This is a silly string"); + foos_Expect("This is a wacky string"); + + function_c(); + } + + +... diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index 3f8dcb0..15b356b 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -245,6 +245,29 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name) assert_equal(expected, returned) end + + should "make handle default types with memory compares and arrays, which involves extra work" do + function = { :name => "Toaster", :return_type => "uint64"} + var_type = "SOME_STRUCT*" + var_name = "Bread" + + @cmock_generator_utils.helpers = {:unity_helper => @unity_helper} + @unity_helper.expect.get_helper(var_type).returns("TEST_ASSERT_EQUAL_MEMORY_MESSAGE_ARRAY") + + expected = ["\n", + " if (Mock.Toaster_Expected_Bread != Mock.Toaster_Expected_Bread_Tail)\n", + " {\n", + " SOME_STRUCT** p_expected = Mock.Toaster_Expected_Bread;\n", + " Mock.Toaster_Expected_Bread++;\n", + " if (*p_expected == NULL)\n", + " { TEST_ASSERT_NULL(Bread); }\n", + " else\n", + " { TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)(*p_expected), (void*)Bread, sizeof(SOME_STRUCT), \"Function 'Toaster' called with unexpected value for argument 'Bread'.\"); }\n", + " }\n" + ].join + returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name) + assert_equal(expected, returned) + end should "make handle default types with array compares, which involves extra work" do function = { :name => "Blender", :return_type => "uint16*"} diff --git a/test/unit/cmock_unityhelper_parser_test.rb b/test/unit/cmock_unityhelper_parser_test.rb index 7e2bb8b..12be59f 100644 --- a/test/unit/cmock_unityhelper_parser_test.rb +++ b/test/unit/cmock_unityhelper_parser_test.rb @@ -170,10 +170,15 @@ class CMockUnityHelperParserTest < Test::Unit::TestCase 'SPINACH' => "TEST_ASSERT_EQUAL_SPINACH", } - ["UINT16","UINT8*","SPINACH_T","SALAD","PINEAPPLE"].each do |ctype| + ["UINT16","SPINACH_T","SALAD","PINEAPPLE"].each do |ctype| @config.expect.memcmp_if_unknown.returns(true) assert_equal("TEST_ASSERT_EQUAL_MEMORY_MESSAGE", @parser.get_helper(ctype)) end + + ["UINT8*","SPINACH_T*"].each do |ctype| + @config.expect.memcmp_if_unknown.returns(true) + assert_equal("TEST_ASSERT_EQUAL_MEMORY_MESSAGE_ARRAY", @parser.get_helper(ctype)) + end end should "raise error when asked to fetch helper of type not on my list and not allowed to mem check" do