From eee379f61119112ce3f13edde192a38cda66bec2 Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Sat, 28 Nov 2009 01:44:38 +0000 Subject: [PATCH] - added array/pointer "smart" mode support - removed differentiation between array and pointer (since it doesn't exist at this point anyway) git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@147 bf332499-1b4d-0410-844d-d2d48d5cc64c --- lib/cmock_config.rb | 3 +- lib/cmock_generator_plugin_array.rb | 2 +- lib/cmock_generator_plugin_expect.rb | 2 +- lib/cmock_generator_utils.rb | 14 ++- .../array_and_pointer_handling.yml | 18 ++- .../unit/cmock_generator_plugin_array_test.rb | 8 +- .../cmock_generator_plugin_expect_test.rb | 4 +- test/unit/cmock_generator_utils_test.rb | 111 +++++++++++++++++- test/unit/cmock_plugin_manager_test.rb | 2 +- 9 files changed, 141 insertions(+), 23 deletions(-) diff --git a/lib/cmock_config.rb b/lib/cmock_config.rb index 59e617b..ef42930 100644 --- a/lib/cmock_config.rb +++ b/lib/cmock_config.rb @@ -15,8 +15,7 @@ class CMockConfig :treat_as_void => [], :memcmp_if_unknown => true, :when_no_prototypes => :warn, #the options being :ignore, :warn, or :error - :when_ptr_star =>:compare_data, #the options being :compare_ptr, :compare_data - :when_ptr_brackets => :compare_data, #not really supported yet + :when_ptr =>:compare_data, #the options being :compare_ptr, :compare_data, or :smart } def initialize(options=nil) diff --git a/lib/cmock_generator_plugin_array.rb b/lib/cmock_generator_plugin_array.rb index 147601c..38a1223 100644 --- a/lib/cmock_generator_plugin_array.rb +++ b/lib/cmock_generator_plugin_array.rb @@ -5,7 +5,7 @@ class CMockGeneratorPluginArray def initialize(config, utils) @config = config - @ptr_handling = @config.when_ptr_star + @ptr_handling = @config.when_ptr @ordered = @config.enforce_strict_ordering @utils = utils @unity_helper = @utils.helpers[:unity_helper] diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index f57521f..8b4ce99 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -5,7 +5,7 @@ class CMockGeneratorPluginExpect def initialize(config, utils) @config = config - @ptr_handling = @config.when_ptr_star + @ptr_handling = @config.when_ptr @ordered = @config.enforce_strict_ordering @utils = utils @unity_helper = @utils.helpers[:unity_helper] diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index d1dcce3..5701dc2 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -1,11 +1,11 @@ class CMockGeneratorUtils - attr_accessor :config, :helpers, :ordered + attr_accessor :config, :helpers, :ordered, :ptr_handling, :arrays def initialize(config, helpers={}) @config = config - @ptr_handling = @config.when_ptr_star + @ptr_handling = @config.when_ptr @ordered = @config.enforce_strict_ordering @arrays = @config.plugins.include? :array @helpers = helpers @@ -48,7 +48,7 @@ class CMockGeneratorUtils c_type = arg[:type] name = arg[:name] if ((arg[:ptr?]) and (@ptr_handling == :compare_ptr)) - unity_func = "TEST_ASSERT_EQUAL_INT_MESSAGE" + unity_func = "TEST_ASSERT_EQUAL_HEX32_MESSAGE" else unity_func = (@helpers.nil? or @helpers[:unity_helper].nil?) ? "TEST_ASSERT_EQUAL_MESSAGE" : @helpers[:unity_helper].get_helper(c_type) end @@ -62,8 +62,9 @@ class CMockGeneratorUtils [ (INSERT_ARG_DEPTH_START_SNIPPET % [depth_name]), " if (*p_expected == NULL)", " { TEST_ASSERT_NULL(#{name}); }", + ((@ptr_handling == :smart) ? " else if (Depth == 0)\n { TEST_ASSERT_EQUAL_HEX32(*p_expected, #{name}); }" : nil), " else", - " { TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE((void*)(#{expected}), (void*)#{name}, sizeof(#{c_type.sub('*','')}), Depth#{unity_msg}); }"].join("\n") + " { TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE((void*)(#{expected}), (void*)#{name}, sizeof(#{c_type.sub('*','')}), Depth#{unity_msg}); }"].compact.join("\n") else [ " if (*p_expected == NULL)", " { TEST_ASSERT_NULL(#{name}); }", @@ -73,11 +74,12 @@ class CMockGeneratorUtils end when /_ARRAY/ if (@arrays) - [ (INSERT_ARG_DEPTH_START_SNIPPET % ["#{function[:name]}_Expected_#{name}_Depth"]), + [ (INSERT_ARG_DEPTH_START_SNIPPET % [depth_name]), " if (*p_expected == NULL)", " { TEST_ASSERT_NULL(#{name}); }", + ((@ptr_handling == :smart) ? " else if (Depth == 0)\n { TEST_ASSERT_EQUAL_HEX32(*p_expected, #{name}); }" : nil), " else", - " { #{unity_func}(#{expected}, #{name}, Depth); }"].join("\n") + " { #{unity_func}(#{expected}, #{name}, Depth); }"].compact.join("\n") else [ " if (*p_expected == NULL)", " { TEST_ASSERT_NULL(#{name}); }", diff --git a/test/system/test_interactions/array_and_pointer_handling.yml b/test/system/test_interactions/array_and_pointer_handling.yml index 42be1c6..ccad016 100644 --- a/test/system/test_interactions/array_and_pointer_handling.yml +++ b/test/system/test_interactions/array_and_pointer_handling.yml @@ -1,5 +1,6 @@ --- :cmock: + :when_ptr: :smart :plugins: - :array @@ -87,8 +88,8 @@ function_a(); } - - :pass: FALSE - :should: 'handle the situation where it fails because you asked it to compare nothing at all' + - :pass: TRUE + :should: 'handle the situation where it falls back to pointers because you asked it to compare 0 elements' :code: | test() { @@ -99,6 +100,19 @@ function_a(); } + - :pass: FALSE + :should: 'handle the situation where it fails because you asked it to compare zero elements and the pointers do not match' + :code: | + test() + { + POINT_T ex = {1, 2}; + POINT_T pt = {1, 2}; + bar_ExpectAndReturn(&pt); + foo_ExpectWithArray(&ex, 0); + + function_a(); + } + - :pass: TRUE :should: 'handle the situation where we pass single object with expect' :code: | diff --git a/test/unit/cmock_generator_plugin_array_test.rb b/test/unit/cmock_generator_plugin_array_test.rb index 5c190ad..5d29ae6 100644 --- a/test/unit/cmock_generator_plugin_array_test.rb +++ b/test/unit/cmock_generator_plugin_array_test.rb @@ -6,7 +6,7 @@ class CMockGeneratorPluginArrayTest < Test::Unit::TestCase create_mocks :config, :utils #no strict ordering - @config.expect.when_ptr_star.returns(:compare_data) + @config.expect.when_ptr.returns(:compare_data) @config.expect.enforce_strict_ordering.returns(false) @config.stubs!(:respond_to?).returns(true) @utils.expect.helpers.returns({}) @@ -83,19 +83,19 @@ class CMockGeneratorPluginArrayTest < Test::Unit::TestCase assert(!@cmock_generator_plugin_array.respond_to?(:mock_implementation_prefix)) end - should "add not have a mock function implementation for functions of style 'int* func(void)'" do + should "not have a mock function implementation for functions of style 'int* func(void)'" do function = {:name => "Apple", :args => [], :return_type => "int*", :contains_ptr? => false} returned = @cmock_generator_plugin_array.mock_implementation(function) assert_nil(returned) end - should "add not have a mock function implementation for functions containing pointers either (handled in expect)" do + should "not have a mock function implementation for functions containing pointers either (handled in expect)" do function = {:name => "Apple", :args => [{ :type => 'int*', :name => 'sausage', :ptr? => true}], :return_type => "int*", :contains_ptr? => true} returned = @cmock_generator_plugin_array.mock_implementation(function) assert_nil(returned) end - should "add not have a mock interfaces for functions of style 'int* func(void)'" do + should "not have a mock interfaces for functions of style 'int* func(void)'" do function = {:name => "Pear", :args => [], :args_string => "void", :return_type => "int*"} returned = @cmock_generator_plugin_array.mock_interfaces(function) assert_nil(returned) diff --git a/test/unit/cmock_generator_plugin_expect_test.rb b/test/unit/cmock_generator_plugin_expect_test.rb index 71bbdaa..64bf652 100644 --- a/test/unit/cmock_generator_plugin_expect_test.rb +++ b/test/unit/cmock_generator_plugin_expect_test.rb @@ -6,14 +6,14 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase create_mocks :config, :utils #no strict ordering - @config.expect.when_ptr_star.returns(:compare_data) + @config.expect.when_ptr.returns(:compare_data) @config.expect.enforce_strict_ordering.returns(false) @config.stubs!(:respond_to?).returns(true) @utils.expect.helpers.returns({}) @cmock_generator_plugin_expect = CMockGeneratorPluginExpect.new(@config, @utils) #strict ordering - @config.expect.when_ptr_star.returns(:compare_data) + @config.expect.when_ptr.returns(:compare_data) @config.expect.enforce_strict_ordering.returns(true) @config.stubs!(:respond_to?).returns(true) @utils.expect.helpers.returns({}) diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index 2f313e0..dc5eab6 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -4,9 +4,9 @@ require 'cmock_generator_utils' class CMockGeneratorUtilsTest < Test::Unit::TestCase def setup create_mocks :config, :unity_helper - @config.expect.when_ptr_star.returns(:compare_data) + @config.expect.when_ptr.returns(:compare_data) @config.expect.enforce_strict_ordering.returns(false) - @config.expect.plugins.returns([:arrays]) + @config.expect.plugins.returns([]) @cmock_generator_utils = CMockGeneratorUtils.new(@config) end @@ -16,16 +16,18 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase should "have set up internal accessors correctly on init" do assert_equal(@config, @cmock_generator_utils.config) assert_equal({}, @cmock_generator_utils.helpers) + assert_equal(false, @cmock_generator_utils.arrays) end should "have set up internal accessors correctly on init, complete with passed helpers" do create_mocks :config - @config.expect.when_ptr_star.returns(:compare_ptr) + @config.expect.when_ptr.returns(:compare_ptr) @config.expect.enforce_strict_ordering.returns(false) - @config.expect.plugins.returns([]) + @config.expect.plugins.returns([:array]) @cmock_generator_utils = CMockGeneratorUtils.new(@config, {:A=>1, :B=>2}) assert_equal(@config, @cmock_generator_utils.config) assert_equal({:A=>1, :B=>2},@cmock_generator_utils.helpers) + assert_equal(true, @cmock_generator_utils.arrays) end should "make expand array" do @@ -272,4 +274,105 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, {:type => var_type, :name => var_name}) assert_equal(expected, returned) end + + should "make handle default types with array compares using smart mode but only a single item" do + function = { :name => "Blender", :return_type => "uint16*"} + var_type = "FRUIT*" + var_name = "Strawberry" + + @cmock_generator_utils.ptr_handling = :smart + @cmock_generator_utils.helpers = {:unity_helper => @unity_helper} + @unity_helper.expect.get_helper(var_type).returns("TEST_ASSERT_EQUAL_FRUIT_ARRAY") + + expected = ["\n", + " if (Mock.Blender_Expected_Strawberry != Mock.Blender_Expected_Strawberry_Tail)\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(*p_expected, Strawberry, 1); }\n", + " }\n" + ].join + returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, {:type => var_type, :name => var_name}) + assert_equal(expected, returned) + end + + should "make handle default types when working in pointer only mode" do + function = { :name => "Blender", :return_type => "uint16*"} + var_type = "FRUIT*" + var_name = "Strawberry" + + @cmock_generator_utils.ptr_handling = :compare_ptr + @cmock_generator_utils.arrays = true + @cmock_generator_utils.helpers = {:unity_helper => @unity_helper} + + expected = ["\n", + " if (Mock.Blender_Expected_Strawberry != Mock.Blender_Expected_Strawberry_Tail)\n", + " {\n", + " FRUIT** p_expected = Mock.Blender_Expected_Strawberry;\n", + " Mock.Blender_Expected_Strawberry++;\n", + " TEST_ASSERT_EQUAL_HEX32_MESSAGE(*p_expected, Strawberry, \"Function 'Blender' called with unexpected value for argument 'Strawberry'.\");\n\n", + " }\n" + ].join + returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, {:type => var_type, :name => var_name, :ptr? => true}) + assert_equal(expected, returned) + end + + should "make handle default types with array compares using array mode and multiple items" do + function = { :name => "Blender", :return_type => "uint16*"} + var_type = "FRUIT*" + var_name = "Strawberry" + + @cmock_generator_utils.ptr_handling = :compare_data + @cmock_generator_utils.arrays = true + @cmock_generator_utils.helpers = {:unity_helper => @unity_helper} + @unity_helper.expect.get_helper(var_type).returns("TEST_ASSERT_EQUAL_FRUIT_ARRAY") + + expected = ["\n", + " if (Mock.Blender_Expected_Strawberry != Mock.Blender_Expected_Strawberry_Tail)\n", + " {\n", + " FRUIT** p_expected = Mock.Blender_Expected_Strawberry;\n", + " Mock.Blender_Expected_Strawberry++;\n\n", + " int Depth = *Mock.Blender_Expected_Strawberry_Depth;\n", + " Mock.Blender_Expected_Strawberry_Depth++;\n\n", + " if (*p_expected == NULL)\n", + " { TEST_ASSERT_NULL(Strawberry); }\n", + " else\n", + " { TEST_ASSERT_EQUAL_FRUIT_ARRAY(*p_expected, Strawberry, Depth); }\n", + " }\n" + ].join + returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, {:type => var_type, :name => var_name, :ptr? => true}) + assert_equal(expected, returned) + end + + should "make handle default types with array compares using smart mode and multiple items" do + function = { :name => "Blender", :return_type => "uint16*"} + var_type = "FRUIT*" + var_name = "Strawberry" + + @cmock_generator_utils.ptr_handling = :smart + @cmock_generator_utils.arrays = true + @cmock_generator_utils.helpers = {:unity_helper => @unity_helper} + @unity_helper.expect.get_helper(var_type).returns("TEST_ASSERT_EQUAL_FRUIT_ARRAY") + + expected = ["\n", + " if (Mock.Blender_Expected_Strawberry != Mock.Blender_Expected_Strawberry_Tail)\n", + " {\n", + " FRUIT** p_expected = Mock.Blender_Expected_Strawberry;\n", + " Mock.Blender_Expected_Strawberry++;\n\n", + " int Depth = *Mock.Blender_Expected_Strawberry_Depth;\n", + " Mock.Blender_Expected_Strawberry_Depth++;\n\n", + " if (*p_expected == NULL)\n", + " { TEST_ASSERT_NULL(Strawberry); }\n", + " else if (Depth == 0)\n", + " { TEST_ASSERT_EQUAL_HEX32(*p_expected, Strawberry); }\n", + " else\n", + " { TEST_ASSERT_EQUAL_FRUIT_ARRAY(*p_expected, Strawberry, Depth); }\n", + " }\n" + ].join + returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, {:type => var_type, :name => var_name, :ptr? => true}) + assert_equal(expected, returned) + end end diff --git a/test/unit/cmock_plugin_manager_test.rb b/test/unit/cmock_plugin_manager_test.rb index 100a955..dda10ef 100644 --- a/test/unit/cmock_plugin_manager_test.rb +++ b/test/unit/cmock_plugin_manager_test.rb @@ -5,7 +5,7 @@ class CMockPluginManagerTest < Test::Unit::TestCase def setup create_mocks :config, :utils, :pluginA, :pluginB @config.stubs!(:respond_to?).returns(true) - @config.stubs!(:when_ptr_star).returns(:compare_data) + @config.stubs!(:when_ptr).returns(:compare_data) @config.stubs!(:enforce_strict_ordering).returns(false) end