From 868e951c0e31bd55720180ba53105d6a0c14f293 Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Sun, 22 Feb 2009 00:06:43 +0000 Subject: [PATCH] - handles pointers (but not yet arrays) git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@63 bf332499-1b4d-0410-844d-d2d48d5cc64c --- examples/src/AdcConductor.c | 15 ++++++++++ examples/src/AdcConductor.h | 2 ++ examples/src/AdcModel.c | 12 ++++++++ examples/src/AdcModel.h | 2 ++ examples/test/TestAdcConductor.c | 40 +++++++++++++++++++++++++ lib/cmock_generator_utils.rb | 29 ++++++++++++------ test/unit/cmock_generator_utils_test.rb | 29 ++++++++++++++++-- 7 files changed, 118 insertions(+), 11 deletions(-) diff --git a/examples/src/AdcConductor.c b/examples/src/AdcConductor.c index cc2a609..0fbf0cd 100644 --- a/examples/src/AdcConductor.c +++ b/examples/src/AdcConductor.c @@ -25,3 +25,18 @@ bool AdcConductor_JustHereToTest(void) return AdcModel_DoNothingExceptTestASpecialType(ExampleStruct); } + +bool AdcConductor_AlsoHereToTest(void) +{ + EXAMPLE_STRUCT_T example = AdcModel_DoNothingExceptReturnASpecialType(); + + return ((example.x == 99) && (example.y == 1)); +} + +bool AdcConductor_YetAnotherTest(void) +{ + uint32 example = 3; + + return AdModel_DoNothingExceptTestPointers(&example); +} + diff --git a/examples/src/AdcConductor.h b/examples/src/AdcConductor.h index 4b3e05a..99f0717 100644 --- a/examples/src/AdcConductor.h +++ b/examples/src/AdcConductor.h @@ -5,5 +5,7 @@ void AdcConductor_Init(void); void AdcConductor_Run(void); bool AdcConductor_JustHereToTest(void); +bool AdcConductor_AlsoHereToTest(void); +bool AdcConductor_YetAnotherTest(void); #endif // _ADCCONDUCTOR_H diff --git a/examples/src/AdcModel.c b/examples/src/AdcModel.c index 6be56d7..cf13ac2 100644 --- a/examples/src/AdcModel.c +++ b/examples/src/AdcModel.c @@ -18,3 +18,15 @@ bool AdcModel_DoNothingExceptTestASpecialType(EXAMPLE_STRUCT_T ExampleStruct) { //This doesn't really do anything. it's only here to make sure I can compare a struct. } +bool AdModel_DoNothingExceptTestPointers(uint32* pExample) +{ + //This doesn't really do anything. it's only here to make sure I can compare a pointer value. +} + +EXAMPLE_STRUCT_T AdcModel_DoNothingExceptReturnASpecialType(void) +{ + EXAMPLE_STRUCT_T example; //again, this just is here to test that I can return a struct + example.x = 99; + example.y = 1; + return example; +} diff --git a/examples/src/AdcModel.h b/examples/src/AdcModel.h index 9e0e42b..6dfb722 100644 --- a/examples/src/AdcModel.h +++ b/examples/src/AdcModel.h @@ -5,5 +5,7 @@ bool AdcModel_DoGetSample(void); void AdcModel_ProcessInput(uint16 millivolts); bool AdcModel_DoNothingExceptTestASpecialType(EXAMPLE_STRUCT_T ExampleStruct); +bool AdModel_DoNothingExceptTestPointers(uint32* pExample); +EXAMPLE_STRUCT_T AdcModel_DoNothingExceptReturnASpecialType(void); #endif // _ADCMODEL_H diff --git a/examples/test/TestAdcConductor.c b/examples/test/TestAdcConductor.c index 39c455f..f9efb01 100644 --- a/examples/test/TestAdcConductor.c +++ b/examples/test/TestAdcConductor.c @@ -79,3 +79,43 @@ void testJustHereToTest_Should_ProperlyPassAStructAndVerifyIt(void) // TEST_ASSERT_TRUE(AdcConductor_JustHereToTest()); //} +void test_AdcConductor_AlsoHereToTest_Should_ProperlyReturnAStructAsExpected1(void) +{ + EXAMPLE_STRUCT_T TestStruct; + TestStruct.x = 99; + TestStruct.y = 1; + + AdcModel_DoNothingExceptReturnASpecialType_ExpectAndReturn(TestStruct); + + TEST_ASSERT_TRUE(AdcConductor_AlsoHereToTest()); +} + +void test_AdcConductor_AlsoHereToTest_Should_ProperlyReturnAStructAsExpected2(void) +{ + EXAMPLE_STRUCT_T TestStruct; + TestStruct.x = 98; + TestStruct.y = 1; + + AdcModel_DoNothingExceptReturnASpecialType_ExpectAndReturn(TestStruct); + + TEST_ASSERT_FALSE(AdcConductor_AlsoHereToTest()); +} + +void test_AdcConductor_YetAnotherTest_Should_VerifyThatPointersToStructsAreTestable(void) +{ + uint32 TestNum = 3; + + AdModel_DoNothingExceptTestPointers_ExpectAndReturn(&TestNum, TRUE); + + TEST_ASSERT_TRUE(AdcConductor_YetAnotherTest()); +} + +//void test_AdcConductor_YetAnotherTest_Should_FailIfYouUncommentThisTestBecauseTheValuePointedToIsWrong(void) +//{ +// uint32 TestNum = 7; +// +// AdModel_DoNothingExceptTestPointers_ExpectAndReturn(&TestNum, FALSE); +// +// TEST_ASSERT_FALSE(AdcConductor_YetAnotherTest()); +//} + diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index 483bf29..3e91f1f 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -6,6 +6,7 @@ class CMockGeneratorUtils def initialize(config, helpers={}) @config = config @tab = @config.tab + @ptr_handling = @config.when_ptr_star @helpers = helpers end @@ -67,24 +68,34 @@ class CMockGeneratorUtils end def code_verify_an_arg_expectation(function, arg_type, actual) - expect = expect_helper(arg_type, '*p_expected', actual, "\"Function '#{function[:name]}' called with unexpected value for parameter '#{actual}'.\"") lines = ["\n"] lines << "#{@tab}if (Mock.#{function[:name]}_Expected_#{actual} != Mock.#{function[:name]}_Expected_#{actual}_HeadTail)\n" lines << "#{@tab}{\n" lines << "#{@tab}#{@tab}#{arg_type}* p_expected = Mock.#{function[:name]}_Expected_#{actual};\n" lines << "#{@tab}#{@tab}Mock.#{function[:name]}_Expected_#{actual}++;\n" - lines << "#{@tab}#{@tab}#{expect};\n" + lines << expect_helper(arg_type, '*p_expected', actual, "\"Function '#{function[:name]}' called with unexpected value for parameter '#{actual}'.\"","#{@tab}#{@tab}") lines << "#{@tab}}\n" + lines.flatten end - def expect_helper(c_type, expected, actual, msg) - unity_func = (@helpers.nil? or @helpers[:unity_helper].nil?) ? "TEST_ASSERT_EQUAL_MESSAGE" : @helpers[:unity_helper].get_helper(c_type) - unity_msg = (unity_func =~ /_MESSAGE/) ? ", #{msg}" : '' - if (unity_func == "TEST_ASSERT_EQUAL_MEMORY_MESSAGE") - full_expected = (expected.strip[0] == 42) ? expected.slice(1..-1) : "&(#{expected})" - return "#{unity_func}((void*)#{full_expected}, (void*)&(#{actual}), sizeof(#{c_type})#{unity_msg})" + def expect_helper(c_type, expected, actual, msg, indent) + if ((c_type.strip[0] == 42) and (@ptr_handling == :compare_ptr)) + unity_func = "TEST_ASSERT_EQUAL_INT_MESSAGE" else - return "#{unity_func}(#{expected}, #{actual}#{unity_msg})" + unity_func = (@helpers.nil? or @helpers[:unity_helper].nil?) ? "TEST_ASSERT_EQUAL_MESSAGE" : @helpers[:unity_helper].get_helper(c_type) + end + unity_msg = (unity_func =~ /_MESSAGE/) ? ", #{msg}" : '' + case(unity_func) + when "TEST_ASSERT_EQUAL_MEMORY_MESSAGE" + full_expected = (expected.strip[0] == 42) ? expected.slice(1..-1) : "&(#{expected})" + return "#{indent}#{unity_func}((void*)#{full_expected}, (void*)&(#{actual}), sizeof(#{c_type})#{unity_msg});\n" + when /_ARRAY/ + return [ "#{indent}if (*p_expected == NULL)\n", + "#{indent}#{@tab}{ TEST_ASSERT_NULL(#{actual}); }\n", + "#{indent}else\n", + "#{indent}#{@tab}{ #{unity_func}(#{expected}, #{actual}, 1#{unity_msg}); }\n" ] + else + return "#{indent}#{unity_func}(#{expected}, #{actual}#{unity_msg});\n" end end end \ No newline at end of file diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index 986cf37..4b3a138 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -5,6 +5,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase def setup create_mocks :config, :unity_helper @config.expect.tab.returns(" ") + @config.expect.when_ptr_star.returns(:compare_ptr) @cmock_generator_utils = CMockGeneratorUtils.new(@config) end @@ -20,6 +21,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase should "have set up internal accessors correctly on init, complete with passed helpers" do create_mocks :config @config.expect.tab.returns(" ") + @config.expect.when_ptr_star.returns(:compare_ptr) @cmock_generator_utils = CMockGeneratorUtils.new(@config, {:A, :B}) assert_equal(@config, @cmock_generator_utils.config) assert_equal(" ", @cmock_generator_utils.tab) @@ -131,7 +133,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase assert_equal(expected, returned) end - should "make handle expected for non character strings" do + should "make handle expected when no helpers are available" do function = { :name => "CanOpener", :rettype => "uint64"} var_type = "uint16" var_name = "CorkScrew" @@ -168,7 +170,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase assert_equal(expected, returned) end - should "make handle expected for custom types" do + should "make handle expected for custom types from unity helper" do function = { :name => "TeaPot", :rettype => "uint64"} var_type = "MANDELBROT_SET_T" var_name = "TeaSpoon" @@ -207,4 +209,27 @@ 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 array compares, which involves extra work" do + function = { :name => "Blender", :rettype => "uint16*"} + var_type = "FRUIT*" + var_name = "Strawberry" + + @cmock_generator_utils.helpers = {:unity_helper => @unity_helper} + @unity_helper.expect.get_helper(var_type).returns("TEST_ASSERT_EQUAL_FRUIT_ARRAY_MESSAGE") + + expected = ["\n", + " if (Mock.Blender_Expected_Strawberry != Mock.Blender_Expected_Strawberry_HeadTail)\n", + " {\n", + " FRUIT** p_expected = Mock.Blender_Expected_Strawberry;\n", + " Mock.Blender_Expected_Strawberry++;\n", + " if (*p_expected == NULL)\n", + " { TEST_ASSERT_NULL(Strawberry); }\n", + " else\n", + " { TEST_ASSERT_EQUAL_FRUIT_ARRAY_MESSAGE(*p_expected, Strawberry, 1, \"Function 'Blender' called with unexpected value for parameter 'Strawberry'.\"); }\n", + " }\n" + ] + returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name) + assert_equal(expected, returned) + end end