From c9fdbe29fde6ba2ec50eb26cc5e67b10c7d1d026 Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Thu, 25 Jun 2009 19:24:40 +0000 Subject: [PATCH] - removed configurable tabs because they aren't valuable compared to their hit on performance * WARNING: This change will very likely break your calling of generate_version_runner in unity * git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@138 bf332499-1b4d-0410-844d-d2d48d5cc64c --- lib/cmock_config.rb | 1 - lib/cmock_generator.rb | 37 +++--- lib/cmock_generator_plugin_cexception.rb | 83 +++++++------ lib/cmock_generator_plugin_expect.rb | 111 +++++++++--------- lib/cmock_generator_plugin_ignore.rb | 45 ++++--- lib/cmock_generator_utils.rb | 81 +++++++------ rakefile_helper.rb | 2 +- test/unit/cmock_config_test.rb | 3 - test/unit/cmock_generator_main_test.rb | 3 - .../cmock_generator_plugin_cexception_test.rb | 2 - .../cmock_generator_plugin_expect_test.rb | 3 - .../cmock_generator_plugin_ignore_test.rb | 2 - test/unit/cmock_generator_utils_test.rb | 27 ++--- test/unit/cmock_plugin_manager_test.rb | 4 - 14 files changed, 188 insertions(+), 216 deletions(-) diff --git a/lib/cmock_config.rb b/lib/cmock_config.rb index e19cbec..e90b6ca 100644 --- a/lib/cmock_config.rb +++ b/lib/cmock_config.rb @@ -8,7 +8,6 @@ class CMockConfig :plugins => ['cexception', 'ignore'], :includes => [], :attributes => ['__ramfunc', '__irq', '__fiq'], - :tab => ' ', :expect_call_count_type => 'unsigned short', :enforce_strict_ordering => false, :ignore_bool_type => 'unsigned char', diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 7110920..2a1e370 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -2,7 +2,7 @@ $here = File.dirname __FILE__ class CMockGenerator - attr_reader :config, :file_writer, :tab, :module_name, :mock_name, :utils, :plugins + attr_reader :config, :file_writer, :module_name, :mock_name, :utils, :plugins def initialize(config, module_name, file_writer, utils, plugins=[]) @file_writer = file_writer @@ -10,7 +10,6 @@ class CMockGenerator @utils = utils @plugins = plugins @config = config - @tab = @config.tab @mock_name = @config.mock_prefix + @module_name @ordered = @config.enforce_strict_ordering end @@ -93,9 +92,9 @@ class CMockGenerator file << "static struct #{@mock_name}Instance\n" file << "{\n" if (functions.size == 0) - file << "#{@tab}unsigned char placeHolder;\n" + file << " unsigned char placeHolder;\n" end - file << "#{@tab}unsigned char allocFailure;\n" + file << " unsigned char allocFailure;\n" file << functions.collect{|function| @plugins.run(:instance_structure, function)}.join file << "} Mock;\n\n" end @@ -112,35 +111,35 @@ class CMockGenerator def create_mock_verify_function(file, functions) file << "void #{@mock_name}_Verify(void)\n{\n" - file << "#{@tab}TEST_ASSERT_EQUAL(0, Mock.allocFailure);\n" + file << " TEST_ASSERT_EQUAL(0, Mock.allocFailure);\n" file << functions.collect {|function| @plugins.run(:mock_verify, function)}.join if (@ordered) - file << "#{@tab}if (GlobalOrderError)\n" - file << "#{@tab}{\n" - file << "#{@tab}#{@tab}TEST_FAIL(GlobalOrderError);\n" - file << "#{@tab}}\n" + file << " if (GlobalOrderError)\n" + file << " {\n" + file << " TEST_FAIL(GlobalOrderError);\n" + file << " }\n" end file << "}\n\n" end def create_mock_init_function(file) file << "void #{@mock_name}_Init(void)\n{\n" - file << "#{@tab}#{@mock_name}_Destroy();\n" + file << " #{@mock_name}_Destroy();\n" file << "}\n\n" end def create_mock_destroy_function(file, functions) file << "void #{@mock_name}_Destroy(void)\n{\n" file << functions.collect {|function| @plugins.run(:mock_destroy, function) }.join - file << "#{@tab}memset(&Mock, 0, sizeof(Mock));\n" + file << " memset(&Mock, 0, sizeof(Mock));\n" if (@ordered) - file << "#{@tab}GlobalExpectCount = 0;\n" - file << "#{@tab}GlobalVerifyOrder = 0;\n" - file << "#{@tab}if (GlobalOrderError)\n" - file << "#{@tab}{\n" - file << "#{@tab}#{@tab}free(GlobalOrderError);\n" - file << "#{@tab}#{@tab}GlobalOrderError = NULL;\n" - file << "#{@tab}}\n" + file << " GlobalExpectCount = 0;\n" + file << " GlobalVerifyOrder = 0;\n" + file << " if (GlobalOrderError)\n" + file << " {\n" + file << " free(GlobalOrderError);\n" + file << " GlobalOrderError = NULL;\n" + file << " }\n" end file << "}\n\n" end @@ -165,7 +164,7 @@ class CMockGenerator # Return expected value, if necessary if (function[:return_type] != "void") - file << @utils.code_handle_return_value(function, "#{@tab}").join + file << @utils.code_handle_return_value(function, " ").join end # Close out the function diff --git a/lib/cmock_generator_plugin_cexception.rb b/lib/cmock_generator_plugin_cexception.rb index 5bf35ad..9ca7901 100644 --- a/lib/cmock_generator_plugin_cexception.rb +++ b/lib/cmock_generator_plugin_cexception.rb @@ -1,11 +1,10 @@ class CMockGeneratorPluginCException - attr_reader :config, :utils, :tab + attr_reader :config, :utils def initialize(config, utils) @config = config - @tab = @config.tab @utils = utils ["cexception_include", "cexception_throw_type"].each do |req| @@ -22,12 +21,12 @@ class CMockGeneratorPluginCException def instance_structure(function) call_count_type = @config.expect_call_count_type throw_type = @config.cexception_throw_type - [ "#{@tab}#{call_count_type} *#{function[:name]}_ThrowOnCallCount;\n", - "#{@tab}#{call_count_type} *#{function[:name]}_ThrowOnCallCount_Head;\n", - "#{@tab}#{call_count_type} *#{function[:name]}_ThrowOnCallCount_Tail;\n", - "#{@tab}#{throw_type} *#{function[:name]}_ThrowValue;\n", - "#{@tab}#{throw_type} *#{function[:name]}_ThrowValue_Head;\n", - "#{@tab}#{throw_type} *#{function[:name]}_ThrowValue_Tail;\n" ] + [ " #{call_count_type} *#{function[:name]}_ThrowOnCallCount;\n", + " #{call_count_type} *#{function[:name]}_ThrowOnCallCount_Head;\n", + " #{call_count_type} *#{function[:name]}_ThrowOnCallCount_Tail;\n", + " #{throw_type} *#{function[:name]}_ThrowValue;\n", + " #{throw_type} *#{function[:name]}_ThrowValue_Head;\n", + " #{throw_type} *#{function[:name]}_ThrowValue_Tail;\n" ] end def mock_function_declarations(function) @@ -40,18 +39,18 @@ class CMockGeneratorPluginCException def mock_implementation(function) [ "\n", - "#{@tab}if((Mock.#{function[:name]}_ThrowOnCallCount != Mock.#{function[:name]}_ThrowOnCallCount_Tail) &&\n", - "#{@tab}#{@tab}(Mock.#{function[:name]}_ThrowValue != Mock.#{function[:name]}_ThrowValue_Tail))\n", - "#{@tab}{\n", - "#{@tab}#{@tab}if (*Mock.#{function[:name]}_ThrowOnCallCount && \n", - "#{@tab}#{@tab}#{@tab}(Mock.#{function[:name]}_CallCount == *Mock.#{function[:name]}_ThrowOnCallCount))\n", - "#{@tab}#{@tab}{\n", - "#{@tab}#{@tab}#{@tab}#{@config.cexception_throw_type} toThrow = *Mock.#{function[:name]}_ThrowValue;\n", - "#{@tab}#{@tab}#{@tab}Mock.#{function[:name]}_ThrowOnCallCount++;\n", - "#{@tab}#{@tab}#{@tab}Mock.#{function[:name]}_ThrowValue++;\n", - "#{@tab}#{@tab}#{@tab}Throw(toThrow);\n", - "#{@tab}#{@tab}}\n", - "#{@tab}}\n" ] + " if((Mock.#{function[:name]}_ThrowOnCallCount != Mock.#{function[:name]}_ThrowOnCallCount_Tail) &&\n", + " (Mock.#{function[:name]}_ThrowValue != Mock.#{function[:name]}_ThrowValue_Tail))\n", + " {\n", + " if (*Mock.#{function[:name]}_ThrowOnCallCount && \n", + " (Mock.#{function[:name]}_CallCount == *Mock.#{function[:name]}_ThrowOnCallCount))\n", + " {\n", + " #{@config.cexception_throw_type} toThrow = *Mock.#{function[:name]}_ThrowValue;\n", + " Mock.#{function[:name]}_ThrowOnCallCount++;\n", + " Mock.#{function[:name]}_ThrowValue++;\n", + " Throw(toThrow);\n", + " }\n", + " }\n" ] end def mock_interfaces(function) @@ -63,32 +62,32 @@ class CMockGeneratorPluginCException @utils.code_add_base_expectation(function[:name]), @utils.code_insert_item_into_expect_array(call_count_type, "Mock.#{function[:name]}_ThrowOnCallCount_Head", "Mock.#{function[:name]}_CallsExpected"), @utils.code_insert_item_into_expect_array(throw_type, "Mock.#{function[:name]}_ThrowValue_Head", "toThrow"), - "#{@tab}Mock.#{function[:name]}_ThrowValue = Mock.#{function[:name]}_ThrowValue_Head;\n", - "#{@tab}Mock.#{function[:name]}_ThrowOnCallCount = Mock.#{function[:name]}_ThrowOnCallCount_Head;\n", - "#{@tab}while ((*Mock.#{function[:name]}_ThrowOnCallCount <= Mock.#{function[:name]}_CallCount) && (Mock.#{function[:name]}_ThrowOnCallCount < Mock.#{function[:name]}_ThrowOnCallCount_Tail))\n", - "#{@tab}{\n", - "#{@tab}#{@tab}Mock.#{function[:name]}_ThrowValue++;\n", - "#{@tab}#{@tab}Mock.#{function[:name]}_ThrowOnCallCount++;\n", - "#{@tab}}\n", - (function[:args_string] != "void") ? "#{@tab}ExpectParameters_#{function[:name]}(#{@utils.create_call_list(function)});\n" : nil, + " Mock.#{function[:name]}_ThrowValue = Mock.#{function[:name]}_ThrowValue_Head;\n", + " Mock.#{function[:name]}_ThrowOnCallCount = Mock.#{function[:name]}_ThrowOnCallCount_Head;\n", + " while ((*Mock.#{function[:name]}_ThrowOnCallCount <= Mock.#{function[:name]}_CallCount) && (Mock.#{function[:name]}_ThrowOnCallCount < Mock.#{function[:name]}_ThrowOnCallCount_Tail))\n", + " {\n", + " Mock.#{function[:name]}_ThrowValue++;\n", + " Mock.#{function[:name]}_ThrowOnCallCount++;\n", + " }\n", + (function[:args_string] != "void") ? " ExpectParameters_#{function[:name]}(#{@utils.create_call_list(function)});\n" : nil, "}\n\n" ].compact end def mock_destroy(function) - [ "#{@tab}if(Mock.#{function[:name]}_ThrowOnCallCount_Head)\n", - "#{@tab}{\n", - "#{@tab}#{@tab}free(Mock.#{function[:name]}_ThrowOnCallCount_Head);\n", - "#{@tab}}\n", - "#{@tab}Mock.#{function[:name]}_ThrowOnCallCount=NULL;\n", - "#{@tab}Mock.#{function[:name]}_ThrowOnCallCount_Head=NULL;\n", - "#{@tab}Mock.#{function[:name]}_ThrowOnCallCount_Tail=NULL;\n", - "#{@tab}if(Mock.#{function[:name]}_ThrowValue_Head)\n", - "#{@tab}{\n", - "#{@tab}#{@tab}free(Mock.#{function[:name]}_ThrowValue_Head);\n", - "#{@tab}}\n", - "#{@tab}Mock.#{function[:name]}_ThrowValue=NULL;\n", - "#{@tab}Mock.#{function[:name]}_ThrowValue_Head=NULL;\n", - "#{@tab}Mock.#{function[:name]}_ThrowValue_Tail=NULL;\n" + [ " if(Mock.#{function[:name]}_ThrowOnCallCount_Head)\n", + " {\n", + " free(Mock.#{function[:name]}_ThrowOnCallCount_Head);\n", + " }\n", + " Mock.#{function[:name]}_ThrowOnCallCount=NULL;\n", + " Mock.#{function[:name]}_ThrowOnCallCount_Head=NULL;\n", + " Mock.#{function[:name]}_ThrowOnCallCount_Tail=NULL;\n", + " if(Mock.#{function[:name]}_ThrowValue_Head)\n", + " {\n", + " free(Mock.#{function[:name]}_ThrowValue_Head);\n", + " }\n", + " Mock.#{function[:name]}_ThrowValue=NULL;\n", + " Mock.#{function[:name]}_ThrowValue_Head=NULL;\n", + " Mock.#{function[:name]}_ThrowValue_Tail=NULL;\n" ] end end diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index b3bbb04..0972251 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -1,11 +1,10 @@ class CMockGeneratorPluginExpect - attr_accessor :config, :utils, :tab, :unity_helper, :ordered + attr_accessor :config, :utils, :unity_helper, :ordered def initialize(config, utils) @config = config - @tab = @config.tab @ptr_handling = @config.when_ptr_star @ordered = @config.enforce_strict_ordering @utils = utils @@ -14,25 +13,25 @@ class CMockGeneratorPluginExpect def instance_structure(function) call_count_type = @config.expect_call_count_type - lines = [ "#{@tab}#{call_count_type} #{function[:name]}_CallCount;\n", - "#{@tab}#{call_count_type} #{function[:name]}_CallsExpected;\n" ] + lines = [ " #{call_count_type} #{function[:name]}_CallCount;\n", + " #{call_count_type} #{function[:name]}_CallsExpected;\n" ] if (function[:return_type] != "void") - lines << [ "#{@tab}#{function[:return_type]} *#{function[:name]}_Return;\n", - "#{@tab}#{function[:return_type]} *#{function[:name]}_Return_Head;\n", - "#{@tab}#{function[:return_type]} *#{function[:name]}_Return_Tail;\n" ] + lines << [ " #{function[:return_type]} *#{function[:name]}_Return;\n", + " #{function[:return_type]} *#{function[:name]}_Return_Head;\n", + " #{function[:return_type]} *#{function[:name]}_Return_Tail;\n" ] end if (@ordered) - lines << [ "#{@tab}int *#{function[:name]}_CallOrder;\n", - "#{@tab}int *#{function[:name]}_CallOrder_Head;\n", - "#{@tab}int *#{function[:name]}_CallOrder_Tail;\n" ] + lines << [ " int *#{function[:name]}_CallOrder;\n", + " int *#{function[:name]}_CallOrder_Head;\n", + " int *#{function[:name]}_CallOrder_Tail;\n" ] end function[:args].each do |arg| - lines << [ "#{@tab}#{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]};\n", - "#{@tab}#{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]}_Head;\n", - "#{@tab}#{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]}_Tail;\n" ] + lines << [ " #{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]};\n", + " #{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]}_Head;\n", + " #{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]}_Tail;\n" ] end lines.flatten end @@ -54,27 +53,27 @@ class CMockGeneratorPluginExpect end def mock_implementation(function) - lines = [ "#{@tab}Mock.#{function[:name]}_CallCount++;\n", - "#{@tab}if (Mock.#{function[:name]}_CallCount > Mock.#{function[:name]}_CallsExpected)\n", - "#{@tab}{\n", - "#{@tab}#{@tab}TEST_FAIL(\"Function '#{function[:name]}' called more times than expected\");\n", - "#{@tab}}\n" ] + lines = [ " Mock.#{function[:name]}_CallCount++;\n", + " if (Mock.#{function[:name]}_CallCount > Mock.#{function[:name]}_CallsExpected)\n", + " {\n", + " TEST_FAIL(\"Function '#{function[:name]}' called more times than expected\");\n", + " }\n" ] if (@ordered) err_msg = "Out of order function calls. Function '#{function[:name]}'" #" expected to be call %i but was call %i" - lines << [ "#{@tab}{\n", - "#{@tab}#{@tab}int* p_expected = Mock.#{function[:name]}_CallOrder;\n", - "#{@tab}#{@tab}++GlobalVerifyOrder;\n", - "#{@tab}#{@tab}if (Mock.#{function[:name]}_CallOrder != Mock.#{function[:name]}_CallOrder_Tail)\n", - "#{@tab}#{@tab}#{@tab}Mock.#{function[:name]}_CallOrder++;\n", - "#{@tab}#{@tab}if ((*p_expected != GlobalVerifyOrder) && (GlobalOrderError == NULL))\n", - "#{@tab}#{@tab}{\n", - "#{@tab}#{@tab}#{@tab}const char* ErrStr = \"#{err_msg}\";\n", - "#{@tab}#{@tab}#{@tab}GlobalOrderError = malloc(#{err_msg.size + 1});\n", - "#{@tab}#{@tab}#{@tab}if (GlobalOrderError)\n", - "#{@tab}#{@tab}#{@tab}#{@tab}strcpy(GlobalOrderError, ErrStr);\n", - "#{@tab}#{@tab}}\n", - "#{@tab}}\n" ] + lines << [ " {\n", + " int* p_expected = Mock.#{function[:name]}_CallOrder;\n", + " ++GlobalVerifyOrder;\n", + " if (Mock.#{function[:name]}_CallOrder != Mock.#{function[:name]}_CallOrder_Tail)\n", + " Mock.#{function[:name]}_CallOrder++;\n", + " if ((*p_expected != GlobalVerifyOrder) && (GlobalOrderError == NULL))\n", + " {\n", + " const char* ErrStr = \"#{err_msg}\";\n", + " GlobalOrderError = malloc(#{err_msg.size + 1});\n", + " if (GlobalOrderError)\n", + " strcpy(GlobalOrderError, ErrStr);\n", + " }\n", + " }\n" ] end function[:args].each do |arg| @@ -110,51 +109,51 @@ class CMockGeneratorPluginExpect lines << @utils.code_add_base_expectation(function[:name]) if (function[:args_string] != "void") - lines << "#{@tab}ExpectParameters_#{function[:name]}(#{@utils.create_call_list(function)});\n" + lines << " ExpectParameters_#{function[:name]}(#{@utils.create_call_list(function)});\n" end if (function[:return_type] != "void") lines << @utils.code_insert_item_into_expect_array(function[:return_type], "Mock.#{function[:name]}_Return_Head", 'toReturn') - lines << "#{@tab}Mock.#{function[:name]}_Return = Mock.#{function[:name]}_Return_Head;\n" - lines << "#{@tab}Mock.#{function[:name]}_Return += Mock.#{function[:name]}_CallCount;\n" + lines << " Mock.#{function[:name]}_Return = Mock.#{function[:name]}_Return_Head;\n" + lines << " Mock.#{function[:name]}_Return += Mock.#{function[:name]}_CallCount;\n" end lines << "}\n\n" end def mock_verify(function) - ["#{@tab}TEST_ASSERT_EQUAL_MESSAGE(Mock.#{function[:name]}_CallsExpected, Mock.#{function[:name]}_CallCount, \"Function '#{function[:name]}' called unexpected number of times.\");\n"] + [" TEST_ASSERT_EQUAL_MESSAGE(Mock.#{function[:name]}_CallsExpected, Mock.#{function[:name]}_CallCount, \"Function '#{function[:name]}' called unexpected number of times.\");\n"] end def mock_destroy(function) lines = [] if (function[:return_type] != "void") - lines << [ "#{@tab}if (Mock.#{function[:name]}_Return_Head)\n", - "#{@tab}{\n", - "#{@tab}#{@tab}free(Mock.#{function[:name]}_Return_Head);\n", - "#{@tab}}\n", - "#{@tab}Mock.#{function[:name]}_Return=NULL;\n", - "#{@tab}Mock.#{function[:name]}_Return_Head=NULL;\n", - "#{@tab}Mock.#{function[:name]}_Return_Tail=NULL;\n" + lines << [ " if (Mock.#{function[:name]}_Return_Head)\n", + " {\n", + " free(Mock.#{function[:name]}_Return_Head);\n", + " }\n", + " Mock.#{function[:name]}_Return=NULL;\n", + " Mock.#{function[:name]}_Return_Head=NULL;\n", + " Mock.#{function[:name]}_Return_Tail=NULL;\n" ] end if (@ordered) - lines << [ "#{@tab}if (Mock.#{function[:name]}_CallOrder_Head)\n", - "#{@tab}{\n", - "#{@tab}#{@tab}free(Mock.#{function[:name]}_CallOrder_Head);\n", - "#{@tab}}\n", - "#{@tab}Mock.#{function[:name]}_CallOrder=NULL;\n", - "#{@tab}Mock.#{function[:name]}_CallOrder_Head=NULL;\n", - "#{@tab}Mock.#{function[:name]}_CallOrder_Tail=NULL;\n" + lines << [ " if (Mock.#{function[:name]}_CallOrder_Head)\n", + " {\n", + " free(Mock.#{function[:name]}_CallOrder_Head);\n", + " }\n", + " Mock.#{function[:name]}_CallOrder=NULL;\n", + " Mock.#{function[:name]}_CallOrder_Head=NULL;\n", + " Mock.#{function[:name]}_CallOrder_Tail=NULL;\n" ] end function[:args].each do |arg| - lines << [ "#{@tab}if (Mock.#{function[:name]}_Expected_#{arg[:name]}_Head)\n", - "#{@tab}{\n", - "#{@tab}#{@tab}free(Mock.#{function[:name]}_Expected_#{arg[:name]}_Head);\n", - "#{@tab}}\n", - "#{@tab}Mock.#{function[:name]}_Expected_#{arg[:name]}=NULL;\n", - "#{@tab}Mock.#{function[:name]}_Expected_#{arg[:name]}_Head=NULL;\n", - "#{@tab}Mock.#{function[:name]}_Expected_#{arg[:name]}_Tail=NULL;\n" + lines << [ " if (Mock.#{function[:name]}_Expected_#{arg[:name]}_Head)\n", + " {\n", + " free(Mock.#{function[:name]}_Expected_#{arg[:name]}_Head);\n", + " }\n", + " Mock.#{function[:name]}_Expected_#{arg[:name]}=NULL;\n", + " Mock.#{function[:name]}_Expected_#{arg[:name]}_Head=NULL;\n", + " Mock.#{function[:name]}_Expected_#{arg[:name]}_Tail=NULL;\n" ] end lines.flatten diff --git a/lib/cmock_generator_plugin_ignore.rb b/lib/cmock_generator_plugin_ignore.rb index de7940d..fed5dff 100644 --- a/lib/cmock_generator_plugin_ignore.rb +++ b/lib/cmock_generator_plugin_ignore.rb @@ -1,11 +1,10 @@ class CMockGeneratorPluginIgnore - attr_reader :config, :utils, :tab + attr_reader :config, :utils def initialize(config, utils) @config = config - @tab = @config.tab @utils = utils ["ignore_bool_type"].each do |req| @@ -14,7 +13,7 @@ class CMockGeneratorPluginIgnore end def instance_structure(function) - return ["#{@tab}#{@config.ignore_bool_type} #{function[:name]}_IgnoreBool;\n"] + return [" #{@config.ignore_bool_type} #{function[:name]}_IgnoreBool;\n"] end def mock_function_declarations(function) @@ -26,27 +25,27 @@ class CMockGeneratorPluginIgnore end def mock_implementation_prefix(function) - lines = [ "#{@tab}if (Mock.#{function[:name]}_IgnoreBool)\n", - "#{@tab}{\n" + lines = [ " if (Mock.#{function[:name]}_IgnoreBool)\n", + " {\n" ] if (function[:return_type] == "void") - lines << ["#{@tab*2}return;\n"] + lines << [" return;\n"] else - lines << [ "#{@tab*2}if (Mock.#{function[:name]}_Return != Mock.#{function[:name]}_Return_Tail)\n", - "#{@tab*2}{\n", - "#{@tab*3}#{function[:return_type]} toReturn = *Mock.#{function[:name]}_Return;\n", - "#{@tab*3}Mock.#{function[:name]}_Return++;\n", - "#{@tab*3}Mock.#{function[:name]}_CallCount++;\n", - "#{@tab*3}Mock.#{function[:name]}_CallsExpected++;\n", - "#{@tab*3}return toReturn;\n", - "#{@tab*2}}\n", - "#{@tab*2}else\n", - "#{@tab*2}{\n", - "#{@tab*3}return *(Mock.#{function[:name]}_Return_Tail - 1);\n", - "#{@tab*2}}\n" + lines << [ " if (Mock.#{function[:name]}_Return != Mock.#{function[:name]}_Return_Tail)\n", + " {\n", + " #{function[:return_type]} toReturn = *Mock.#{function[:name]}_Return;\n", + " Mock.#{function[:name]}_Return++;\n", + " Mock.#{function[:name]}_CallCount++;\n", + " Mock.#{function[:name]}_CallsExpected++;\n", + " return toReturn;\n", + " }\n", + " else\n", + " {\n", + " return *(Mock.#{function[:name]}_Return_Tail - 1);\n", + " }\n" ] end - lines << [ "#{@tab}}\n" ] + lines << [ " }\n" ] lines.flatten end @@ -54,15 +53,15 @@ class CMockGeneratorPluginIgnore if (function[:return_type] == "void") [ "void #{function[:name]}_Ignore(void)\n", "{\n", - "#{@tab}Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n", + " Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n", "}\n\n" ] else [ "void #{function[:name]}_IgnoreAndReturn(#{function[:return_string]})\n", "{\n", - "#{@tab}Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n", + " Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n", @utils.code_insert_item_into_expect_array(function[:return_type], "Mock.#{function[:name]}_Return_Head", 'toReturn'), - "#{@tab}Mock.#{function[:name]}_Return = Mock.#{function[:name]}_Return_Head;\n", - "#{@tab}Mock.#{function[:name]}_Return += Mock.#{function[:name]}_CallCount;\n", + " Mock.#{function[:name]}_Return = Mock.#{function[:name]}_Return_Head;\n", + " Mock.#{function[:name]}_Return += Mock.#{function[:name]}_CallCount;\n", "}\n\n" ] end end diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index 890b107..40482ca 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -1,11 +1,10 @@ class CMockGeneratorUtils - attr_accessor :config, :tab, :helpers, :ordered + attr_accessor :config, :helpers, :ordered def initialize(config, helpers={}) @config = config - @tab = @config.tab @ptr_handling = @config.when_ptr_star @ordered = @config.enforce_strict_ordering @helpers = helpers @@ -26,54 +25,54 @@ class CMockGeneratorUtils def code_insert_item_into_expect_array(type, array, newValue) tail = array.gsub(/Head$/,'Tail') lines = ["\n"] - lines << "#{@tab}{\n" - lines << "#{@tab}#{@tab}int sz = 0;\n" - lines << "#{@tab}#{@tab}#{type} *pointer = #{array};\n" - lines << "#{@tab}#{@tab}while (pointer && pointer != #{tail}) { sz++; pointer++; }\n" - lines << "#{@tab}#{@tab}if (sz == 0)\n" - lines << "#{@tab}#{@tab}{\n" - lines << "#{@tab}#{@tab}#{@tab}#{array} = (#{type}*)malloc(2*sizeof(#{type}));\n" - lines << "#{@tab}#{@tab}#{@tab}if (!#{array})\n" - lines << "#{@tab}#{@tab}#{@tab}#{@tab}Mock.allocFailure++;\n" - lines << "#{@tab}#{@tab}}\n" - lines << "#{@tab}#{@tab}else\n" - lines << "#{@tab}#{@tab}{\n" - lines << "#{@tab}#{@tab}#{@tab}#{type} *ptmp = (#{type}*)realloc(#{array}, sizeof(#{type}) * (sz+1));\n" - lines << "#{@tab}#{@tab}#{@tab}if (!ptmp)\n" - lines << "#{@tab}#{@tab}#{@tab}#{@tab}Mock.allocFailure++;\n" - lines << "#{@tab}#{@tab}#{@tab}else\n" - lines << "#{@tab}#{@tab}#{@tab}#{@tab}#{array} = ptmp;\n" - lines << "#{@tab}#{@tab}}\n" - lines << "#{@tab}#{@tab}memcpy(&#{array}[sz], &#{newValue}, sizeof(#{type}));\n" - lines << "#{@tab}#{@tab}#{tail} = &#{array}[sz+1];\n" - lines << "#{@tab}}\n" + lines << " {\n" + lines << " int sz = 0;\n" + lines << " #{type} *pointer = #{array};\n" + lines << " while (pointer && pointer != #{tail}) { sz++; pointer++; }\n" + lines << " if (sz == 0)\n" + lines << " {\n" + lines << " #{array} = (#{type}*)malloc(2*sizeof(#{type}));\n" + lines << " if (!#{array})\n" + lines << " Mock.allocFailure++;\n" + lines << " }\n" + lines << " else\n" + lines << " {\n" + lines << " #{type} *ptmp = (#{type}*)realloc(#{array}, sizeof(#{type}) * (sz+1));\n" + lines << " if (!ptmp)\n" + lines << " Mock.allocFailure++;\n" + lines << " else\n" + lines << " #{array} = ptmp;\n" + lines << " }\n" + lines << " memcpy(&#{array}[sz], &#{newValue}, sizeof(#{type}));\n" + lines << " #{tail} = &#{array}[sz+1];\n" + lines << " }\n" end def code_add_an_arg_expectation(function, arg_type, expected) lines = code_insert_item_into_expect_array(arg_type, "Mock.#{function[:name]}_Expected_#{expected}_Head", expected) - lines << "#{@tab}Mock.#{function[:name]}_Expected_#{expected} = Mock.#{function[:name]}_Expected_#{expected}_Head;\n" - lines << "#{@tab}Mock.#{function[:name]}_Expected_#{expected} += Mock.#{function[:name]}_CallCount;\n" + lines << " Mock.#{function[:name]}_Expected_#{expected} = Mock.#{function[:name]}_Expected_#{expected}_Head;\n" + lines << " Mock.#{function[:name]}_Expected_#{expected} += Mock.#{function[:name]}_CallCount;\n" end def code_add_base_expectation(func_name) - lines = ["#{@tab}Mock.#{func_name}_CallsExpected++;\n"] + lines = [" Mock.#{func_name}_CallsExpected++;\n"] if (@ordered) - lines << [ "#{@tab}++GlobalExpectCount;\n", + lines << [ " ++GlobalExpectCount;\n", code_insert_item_into_expect_array("int", "Mock.#{func_name}_CallOrder_Head", "GlobalExpectCount"), - "#{@tab}Mock.#{func_name}_CallOrder = Mock.#{func_name}_CallOrder_Head;\n", - "#{@tab}Mock.#{func_name}_CallOrder += Mock.#{func_name}_CallCount;\n" ] + " Mock.#{func_name}_CallOrder = Mock.#{func_name}_CallOrder_Head;\n", + " Mock.#{func_name}_CallOrder += Mock.#{func_name}_CallCount;\n" ] end lines.flatten end def code_verify_an_arg_expectation(function, arg_type, actual) [ "\n", - "#{@tab}if (Mock.#{function[:name]}_Expected_#{actual} != Mock.#{function[:name]}_Expected_#{actual}_Tail)\n", - "#{@tab}{\n", - "#{@tab}#{@tab}#{arg_type}* p_expected = Mock.#{function[:name]}_Expected_#{actual};\n", - "#{@tab}#{@tab}Mock.#{function[:name]}_Expected_#{actual}++;\n", - expect_helper(arg_type, '*p_expected', actual, "\"Function '#{function[:name]}' called with unexpected value for argument '#{actual}'.\"","#{@tab}#{@tab}"), - "#{@tab}}\n" ].flatten + " if (Mock.#{function[:name]}_Expected_#{actual} != Mock.#{function[:name]}_Expected_#{actual}_Tail)\n", + " {\n", + " #{arg_type}* p_expected = Mock.#{function[:name]}_Expected_#{actual};\n", + " Mock.#{function[:name]}_Expected_#{actual}++;\n", + expect_helper(arg_type, '*p_expected', actual, "\"Function '#{function[:name]}' called with unexpected value for argument '#{actual}'.\""," "), + " }\n" ].flatten end def expect_helper(c_type, expected, actual, msg, indent) @@ -89,9 +88,9 @@ class CMockGeneratorUtils 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} { TEST_ASSERT_NULL(#{actual}); }\n", "#{indent}else\n", - "#{indent}#{@tab}{ #{unity_func}(#{expected}, #{actual}, 1#{unity_msg}); }\n" ] + "#{indent} { #{unity_func}(#{expected}, #{actual}, 1#{unity_msg}); }\n" ] else return "#{indent}#{unity_func}(#{expected}, #{actual}#{unity_msg});\n" end @@ -101,13 +100,13 @@ class CMockGeneratorUtils [ "\n", "#{indent}if (Mock.#{function[:name]}_Return != Mock.#{function[:name]}_Return_Tail)\n", "#{indent}{\n", - "#{indent}#{@tab}#{function[:return_type]} toReturn = *Mock.#{function[:name]}_Return;\n", - "#{indent}#{@tab}Mock.#{function[:name]}_Return++;\n", - "#{indent}#{@tab}return toReturn;\n", + "#{indent} #{function[:return_type]} toReturn = *Mock.#{function[:name]}_Return;\n", + "#{indent} Mock.#{function[:name]}_Return++;\n", + "#{indent} return toReturn;\n", "#{indent}}\n", "#{indent}else\n", "#{indent}{\n", - "#{indent}#{@tab}return *(Mock.#{function[:name]}_Return_Tail - 1);\n", + "#{indent} return *(Mock.#{function[:name]}_Return_Tail - 1);\n", "#{indent}}\n" ] end end \ No newline at end of file diff --git a/rakefile_helper.rb b/rakefile_helper.rb index 4e9b90f..5f55a03 100644 --- a/rakefile_helper.rb +++ b/rakefile_helper.rb @@ -206,7 +206,7 @@ module RakefileHelpers runner_path = $cfg['compiler']['source_path'] + runner_name test_gen = UnityTestRunnerGenerator.new gen_inc, gen_opt = UnityTestRunnerGenerator::grab_config(SYSTEST_GENERATED_FILES_PATH + cmock_config) - test_gen.run(test, runner_path, gen_inc, ' ', gen_opt) + test_gen.run(test, runner_path, gen_inc, gen_opt) compile(runner_path) obj_list << runner_name.ext($cfg['compiler']['object_files']['extension']) diff --git a/test/unit/cmock_config_test.rb b/test/unit/cmock_config_test.rb index c98cb35..c763b0b 100644 --- a/test/unit/cmock_config_test.rb +++ b/test/unit/cmock_config_test.rb @@ -14,7 +14,6 @@ class CMockConfigTest < Test::Unit::TestCase assert_equal(CMockConfig::CMockDefaultOptions[:includes], config.includes) assert_equal(CMockConfig::CMockDefaultOptions[:attributes], config.attributes) assert_equal(CMockConfig::CMockDefaultOptions[:plugins], config.plugins) - assert_equal(CMockConfig::CMockDefaultOptions[:tab], config.tab) assert_equal(CMockConfig::CMockDefaultOptions[:expect_call_count_type],config.expect_call_count_type) assert_equal(CMockConfig::CMockDefaultOptions[:ignore_bool_type], config.ignore_bool_type) assert_equal(CMockConfig::CMockDefaultOptions[:cexception_include], config.cexception_include) @@ -30,7 +29,6 @@ class CMockConfigTest < Test::Unit::TestCase assert_equal(test_includes, config.includes) assert_equal(test_attributes, config.attributes) assert_equal(CMockConfig::CMockDefaultOptions[:plugins], config.plugins) - assert_equal(CMockConfig::CMockDefaultOptions[:tab], config.tab) assert_equal(CMockConfig::CMockDefaultOptions[:expect_call_count_type], config.expect_call_count_type) assert_equal(test_bool_type, config.ignore_bool_type) assert_equal(CMockConfig::CMockDefaultOptions[:cexception_include], config.cexception_include) @@ -44,7 +42,6 @@ class CMockConfigTest < Test::Unit::TestCase assert_equal(CMockConfig::CMockDefaultOptions[:mock_path], config.mock_path) assert_equal(CMockConfig::CMockDefaultOptions[:includes], config.includes) assert_equal(test_plugins, config.plugins) - assert_equal(CMockConfig::CMockDefaultOptions[:tab], config.tab) assert_equal(CMockConfig::CMockDefaultOptions[:expect_call_count_type], config.expect_call_count_type) assert_equal(CMockConfig::CMockDefaultOptions[:ignore_bool_type], config.ignore_bool_type) assert_equal(CMockConfig::CMockDefaultOptions[:cexception_include], config.cexception_include) diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index e15673a..1de2637 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -40,13 +40,11 @@ class CMockGeneratorTest < Test::Unit::TestCase @module_name = "PoutPoutFish" #no strict handling - @config.expect.tab.returns(" ") @config.expect.mock_prefix.returns("Mock") @config.expect.enforce_strict_ordering.returns(false) @cmock_generator = CMockGenerator.new(@config, @module_name, @file_writer, @utils, @plugins) #strict handling - @config.expect.tab.returns(" ") @config.expect.mock_prefix.returns("Mock") @config.expect.enforce_strict_ordering.returns(true) @cmock_generator_strict = CMockGenerator.new(@config, @module_name, @file_writer, @utils, @plugins) @@ -62,7 +60,6 @@ class CMockGeneratorTest < Test::Unit::TestCase assert_equal(@utils, @cmock_generator.utils) assert_equal(@plugins, @cmock_generator.plugins) assert_equal("Mock#{@module_name}", @cmock_generator.mock_name) - assert_equal(" ", @cmock_generator.tab) end should "create the top of a header file" do diff --git a/test/unit/cmock_generator_plugin_cexception_test.rb b/test/unit/cmock_generator_plugin_cexception_test.rb index 8b7f784..207c7fc 100644 --- a/test/unit/cmock_generator_plugin_cexception_test.rb +++ b/test/unit/cmock_generator_plugin_cexception_test.rb @@ -4,7 +4,6 @@ require 'cmock_generator_plugin_cexception' class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase def setup create_mocks :config, :utils - @config.expect.tab.returns(" ") @config.stubs!(:respond_to?).returns(true) @cmock_generator_plugin_cexception = CMockGeneratorPluginCException.new(@config, @utils) end @@ -15,7 +14,6 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase should "have set up internal accessors correctly on init" do assert_equal(@config, @cmock_generator_plugin_cexception.config) assert_equal(@utils, @cmock_generator_plugin_cexception.utils) - assert_equal(" ", @cmock_generator_plugin_cexception.tab) end should "include the cexception library" do diff --git a/test/unit/cmock_generator_plugin_expect_test.rb b/test/unit/cmock_generator_plugin_expect_test.rb index 7de01cf..64b9456 100644 --- a/test/unit/cmock_generator_plugin_expect_test.rb +++ b/test/unit/cmock_generator_plugin_expect_test.rb @@ -6,7 +6,6 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase create_mocks :config, :utils #no strict ordering - @config.expect.tab.returns(" ") @config.expect.when_ptr_star.returns(:compare_data) @config.expect.enforce_strict_ordering.returns(false) @config.stubs!(:respond_to?).returns(true) @@ -14,7 +13,6 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase @cmock_generator_plugin_expect = CMockGeneratorPluginExpect.new(@config, @utils) #strict ordering - @config.expect.tab.returns(" ") @config.expect.when_ptr_star.returns(:compare_data) @config.expect.enforce_strict_ordering.returns(true) @config.stubs!(:respond_to?).returns(true) @@ -28,7 +26,6 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase should "have set up internal accessors correctly on init" do assert_equal(@config, @cmock_generator_plugin_expect.config) assert_equal(@utils, @cmock_generator_plugin_expect.utils) - assert_equal(" ", @cmock_generator_plugin_expect.tab) assert_equal(nil, @cmock_generator_plugin_expect.unity_helper) end diff --git a/test/unit/cmock_generator_plugin_ignore_test.rb b/test/unit/cmock_generator_plugin_ignore_test.rb index 57bd560..e246f50 100644 --- a/test/unit/cmock_generator_plugin_ignore_test.rb +++ b/test/unit/cmock_generator_plugin_ignore_test.rb @@ -4,7 +4,6 @@ require 'cmock_generator_plugin_ignore' class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase def setup create_mocks :config, :utils - @config.expect.tab.returns(" ") @config.stubs!(:respond_to?).returns(true) @cmock_generator_plugin_ignore = CMockGeneratorPluginIgnore.new(@config, @utils) end @@ -15,7 +14,6 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase should "have set up internal accessors correctly on init" do assert_equal(@config, @cmock_generator_plugin_ignore.config) assert_equal(@utils, @cmock_generator_plugin_ignore.utils) - assert_equal(" ", @cmock_generator_plugin_ignore.tab) end should "not have any additional include file requirements" do diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index fb4fc4a..678f567 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -4,7 +4,6 @@ require 'cmock_generator_utils' class CMockGeneratorUtilsTest < Test::Unit::TestCase def setup create_mocks :config, :unity_helper - @config.expect.tab.returns(" ") @config.expect.when_ptr_star.returns(:compare_data) @config.expect.enforce_strict_ordering.returns(false) @cmock_generator_utils = CMockGeneratorUtils.new(@config) @@ -15,18 +14,15 @@ 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.tab) assert_equal({}, @cmock_generator_utils.helpers) end 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) @config.expect.enforce_strict_ordering.returns(false) @cmock_generator_utils = CMockGeneratorUtils.new(@config, {:A=>1, :B=>2}) assert_equal(@config, @cmock_generator_utils.config) - assert_equal(" ", @cmock_generator_utils.tab) assert_equal({:A=>1, :B=>2},@cmock_generator_utils.helpers) end @@ -84,20 +80,19 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase should "make handle return" do function = { :name => "Spatula", :return_type => "uint64"} - indent = "[tab]" expected = ["\n", - "[tab]if (Mock.Spatula_Return != Mock.Spatula_Return_Tail)\n", - "[tab]{\n", - "[tab] uint64 toReturn = *Mock.Spatula_Return;\n", - "[tab] Mock.Spatula_Return++;\n", - "[tab] return toReturn;\n", - "[tab]}\n", - "[tab]else\n", - "[tab]{\n", - "[tab] return *(Mock.Spatula_Return_Tail - 1);\n", - "[tab]}\n" + " if (Mock.Spatula_Return != Mock.Spatula_Return_Tail)\n", + " {\n", + " uint64 toReturn = *Mock.Spatula_Return;\n", + " Mock.Spatula_Return++;\n", + " return toReturn;\n", + " }\n", + " else\n", + " {\n", + " return *(Mock.Spatula_Return_Tail - 1);\n", + " }\n" ] - returned = @cmock_generator_utils.code_handle_return_value(function, indent) + returned = @cmock_generator_utils.code_handle_return_value(function, ' ') assert_equal(expected, returned) end diff --git a/test/unit/cmock_plugin_manager_test.rb b/test/unit/cmock_plugin_manager_test.rb index 4ac6219..450b148 100644 --- a/test/unit/cmock_plugin_manager_test.rb +++ b/test/unit/cmock_plugin_manager_test.rb @@ -13,7 +13,6 @@ class CMockPluginManagerTest < Test::Unit::TestCase end should "return all plugins by default" do - @config.stubs!(:tab).returns(" ") @config.expect.plugins.returns(['cexception','ignore']) @utils.expect.helpers.returns({}) @@ -32,7 +31,6 @@ class CMockPluginManagerTest < Test::Unit::TestCase end should "return restricted plugins based on config" do - @config.stubs!(:tab).returns(" ") @config.expect.plugins.returns([]) @utils.expect.helpers.returns({}) @@ -51,7 +49,6 @@ class CMockPluginManagerTest < Test::Unit::TestCase end should "run a desired method over each plugin requested and return the results" do - @config.stubs!(:tab).returns(" ") @config.expect.plugins.returns([]) @utils.expect.helpers.returns({}) @cmock_plugins = CMockPluginManager.new(@config, @utils) @@ -66,7 +63,6 @@ class CMockPluginManagerTest < Test::Unit::TestCase end should "run a desired method and arg list over each plugin requested and return the results" do - @config.stubs!(:tab).returns(" ") @config.expect.plugins.returns([]) @utils.expect.helpers.returns({}) @cmock_plugins = CMockPluginManager.new(@config, @utils)