diff --git a/lib/cmock_generator_plugin_ignore_arg.rb b/lib/cmock_generator_plugin_ignore_arg.rb index e71b707..7086422 100644 --- a/lib/cmock_generator_plugin_ignore_arg.rb +++ b/lib/cmock_generator_plugin_ignore_arg.rb @@ -1,12 +1,9 @@ class CMockGeneratorPluginIgnoreArg attr_reader :priority - attr_accessor :config, :utils, :unity_helper, :ordered + attr_accessor :utils def initialize(config, utils) - @config = config - @ordered = @config.enforce_strict_ordering @utils = utils - @unity_helper = @utils.helpers[:unity_helper] @priority = 10 end diff --git a/lib/cmock_generator_plugin_return_thru_ptr.rb b/lib/cmock_generator_plugin_return_thru_ptr.rb index 8ba4fdb..4a72265 100644 --- a/lib/cmock_generator_plugin_return_thru_ptr.rb +++ b/lib/cmock_generator_plugin_return_thru_ptr.rb @@ -1,12 +1,9 @@ class CMockGeneratorPluginReturnThruPtr attr_reader :priority - attr_accessor :config, :utils, :unity_helper, :ordered + attr_accessor :utils def initialize(config, utils) - @config = config - @ordered = @config.enforce_strict_ordering @utils = utils - @unity_helper = @utils.helpers[:unity_helper] @priority = 9 end diff --git a/test/unit/cmock_generator_plugin_ignore_arg_test.rb b/test/unit/cmock_generator_plugin_ignore_arg_test.rb new file mode 100644 index 0000000..9fcab0c --- /dev/null +++ b/test/unit/cmock_generator_plugin_ignore_arg_test.rb @@ -0,0 +1,116 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require 'cmock_generator_plugin_ignore_arg' + +class CMockGeneratorPluginIgnoreArgTest < Test::Unit::TestCase + def setup + create_mocks :config, :utils + + # int *Oak(void)" + @void_func = {:name => "Oak", :args => [], :return => test_return[:int_ptr]} + + # void Pine(int chicken, const int beef, int *tofu) + @complex_func = {:name => "Pine", + :args => [{ :type => "int", + :name => "chicken", + :ptr? => false, + }, + { :type => "int*", + :name => "beef", + :ptr? => true, + :const? => true, + }, + { :type => "int*", + :name => "tofu", + :ptr? => true, + }], + :return => test_return[:void], + :contains_ptr? => true } + + #no strict ordering + @cmock_generator_plugin_ignore_arg = CMockGeneratorPluginIgnoreArg.new(@config, @utils) + end + + def teardown + end + + should "have set up internal accessors correctly on init" do + assert_equal(@utils, @cmock_generator_plugin_ignore_arg.utils) + assert_equal(10, @cmock_generator_plugin_ignore_arg.priority) + end + + should "not include any additional include files" do + assert(!@cmock_generator_plugin_ignore_arg.respond_to?(:include_files)) + end + + should "not add to typedef structure for functions with no args" do + returned = @cmock_generator_plugin_ignore_arg.instance_typedefs(@void_func) + assert_equal("", returned) + end + + should "add to tyepdef structure mock needs of functions of style 'void func(int chicken, int* pork)'" do + expected = " int IgnoreArg_chicken;\n" + + " int IgnoreArg_beef;\n" + + " int IgnoreArg_tofu;\n" + returned = @cmock_generator_plugin_ignore_arg.instance_typedefs(@complex_func) + assert_equal(expected, returned) + end + + should "add mock function declarations for all arguments" do + expected = + "#define Pine_IgnoreArg_chicken()" + + " Pine_CMockIgnoreArg_chicken(__LINE__)\n" + + "void Pine_CMockIgnoreArg_chicken(UNITY_LINE_TYPE cmock_line);\n" + + + "#define Pine_IgnoreArg_beef()" + + " Pine_CMockIgnoreArg_beef(__LINE__)\n" + + "void Pine_CMockIgnoreArg_beef(UNITY_LINE_TYPE cmock_line);\n" + + + "#define Pine_IgnoreArg_tofu()" + + " Pine_CMockIgnoreArg_tofu(__LINE__)\n" + + "void Pine_CMockIgnoreArg_tofu(UNITY_LINE_TYPE cmock_line);\n" + + returned = @cmock_generator_plugin_ignore_arg.mock_function_declarations(@complex_func) + assert_equal(expected, returned) + end + + should "add mock interfaces for all arguments" do + expected = + "void Pine_CMockIgnoreArg_chicken(UNITY_LINE_TYPE cmock_line)\n" + + "{\n" + + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + + "cmock_call_instance = (CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"chicken IgnoreArg called before Expect on 'Pine'.\");\n" + + " cmock_call_instance->IgnoreArg_chicken = 1;\n" + + "}\n\n" + + + "void Pine_CMockIgnoreArg_beef(UNITY_LINE_TYPE cmock_line)\n" + + "{\n" + + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + + "cmock_call_instance = (CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"beef IgnoreArg called before Expect on 'Pine'.\");\n" + + " cmock_call_instance->IgnoreArg_beef = 1;\n" + + "}\n\n" + + + "void Pine_CMockIgnoreArg_tofu(UNITY_LINE_TYPE cmock_line)\n" + + "{\n" + + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + + "cmock_call_instance = (CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"tofu IgnoreArg called before Expect on 'Pine'.\");\n" + + " cmock_call_instance->IgnoreArg_tofu = 1;\n" + + "}\n\n" + + returned = @cmock_generator_plugin_ignore_arg.mock_interfaces(@complex_func).join("") + assert_equal(expected, returned) + end + + should "not add a mock implementation" do + assert(!@cmock_generator_plugin_ignore_arg.respond_to?(:mock_implementation)) + end + +end diff --git a/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb b/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb new file mode 100644 index 0000000..db97701 --- /dev/null +++ b/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb @@ -0,0 +1,134 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require 'cmock_generator_plugin_return_thru_ptr' + +class CMockGeneratorPluginReturnThruPtrTest < Test::Unit::TestCase + def setup + create_mocks :config, :utils + + # int *Oak(void)" + @void_func = {:name => "Oak", :args => [], :return => test_return[:int_ptr]} + + # char *Maple(int blah) + @simple_func = {:name => "Maple", + :args => [{:name => "blah", :type => "int", :ptr? => false}], + :return => test_return[:string], + :contains_ptr? => false} + + # void Pine(int chicken, const int beef, int *tofu) + @complex_func = {:name => "Pine", + :args => [{ :type => "int", + :name => "chicken", + :ptr? => false, + }, + { :type => "int*", + :name => "beef", + :ptr? => true, + :const? => true, + }, + { :type => "int*", + :name => "tofu", + :ptr? => true, + }], + :return => test_return[:void], + :contains_ptr? => true } + + #no strict ordering + @cmock_generator_plugin_return_thru_ptr = CMockGeneratorPluginReturnThruPtr.new(@config, @utils) + end + + def teardown + end + + def simple_func_expect + @utils.expect.ptr_or_str?('int').returns(false) + end + + def complex_func_expect + @utils.expect.ptr_or_str?('int').returns(false) + @utils.expect.ptr_or_str?('int*').returns(true) + @utils.expect.ptr_or_str?('int*').returns(true) + end + + should "have set up internal accessors correctly on init" do + assert_equal(@utils, @cmock_generator_plugin_return_thru_ptr.utils) + assert_equal(9, @cmock_generator_plugin_return_thru_ptr.priority) + end + + should "not include any additional include files" do + assert(!@cmock_generator_plugin_return_thru_ptr.respond_to?(:include_files)) + end + + should "not add to typedef structure for functions of style 'int* func(void)'" do + returned = @cmock_generator_plugin_return_thru_ptr.instance_typedefs(@void_func) + assert_equal("", returned) + end + + should "add to tyepdef structure mock needs of functions of style 'void func(int chicken, int* pork)'" do + complex_func_expect() + expected = " int ReturnThruPtr_tofu_Used;\n" + + " int* ReturnThruPtr_tofu_Val;\n" + + " int ReturnThruPtr_tofu_Size;\n" + returned = @cmock_generator_plugin_return_thru_ptr.instance_typedefs(@complex_func) + assert_equal(expected, returned) + end + + should "not add an additional mock interface for functions not containing pointers" do + simple_func_expect() + returned = @cmock_generator_plugin_return_thru_ptr.mock_function_declarations(@simple_func) + assert_equal("", returned) + end + + should "add a mock function declaration only for non-const pointer arguments" do + complex_func_expect(); + + expected = + "#define Pine_ReturnThruPtr_tofu(tofu)" + + " Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, sizeof(*tofu))\n" + + "#define Pine_ReturnArrayThruPtr_tofu(tofu, cmock_len)" + + " Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, cmock_len * sizeof(*tofu))\n" + + "#define Pine_ReturnMemThruPtr_tofu(tofu, cmock_size)" + + " Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, cmock_size)\n" + + "void Pine_CMockReturnMemThruPtr_tofu(UNITY_LINE_TYPE cmock_line, int* tofu, int cmock_size);\n" + + returned = @cmock_generator_plugin_return_thru_ptr.mock_function_declarations(@complex_func) + assert_equal(expected, returned) + end + + should "add mock interfaces only for non-const pointer arguments" do + complex_func_expect(); + + expected = + "void Pine_CMockReturnMemThruPtr_tofu(UNITY_LINE_TYPE cmock_line, int* tofu, int cmock_size)\n" + + "{\n" + + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + + "cmock_call_instance = (CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"tofu ReturnThruPtr called before Expect on 'Pine'.\");\n" + + " cmock_call_instance->ReturnThruPtr_tofu_Used = 1;\n" + + " cmock_call_instance->ReturnThruPtr_tofu_Val = tofu;\n" + + " cmock_call_instance->ReturnThruPtr_tofu_Size = cmock_size;\n" + + "}\n\n" + + returned = @cmock_generator_plugin_return_thru_ptr.mock_interfaces(@complex_func).join("") + assert_equal(expected, returned) + end + + should "add mock implementations only for non-const pointer arguments" do + complex_func_expect() + + expected = + " if (cmock_call_instance->ReturnThruPtr_tofu_Used)\n" + + " {\n" + + " memcpy(tofu, cmock_call_instance->ReturnThruPtr_tofu_Val,\n" + + " cmock_call_instance->ReturnThruPtr_tofu_Size);\n" + + " }\n" + + + returned = @cmock_generator_plugin_return_thru_ptr.mock_implementation(@complex_func).join("") + end + +end