diff --git a/lib/cmock.rb b/lib/cmock.rb index cdc0ea9..234fcd0 100644 --- a/lib/cmock.rb +++ b/lib/cmock.rb @@ -28,7 +28,7 @@ class CMock cm_parser = CMockHeaderParser.new(File.read(src)) cm_writer = CMockFileWriter.new(@cfg) cm_gen_utils = CMockGeneratorUtils.new(@cfg) - cm_gen_plugins = CMockPluginManager.new(@cfg, cm_gen_utils).get_generator_plugins + cm_gen_plugins = CMockPluginManager.new(@cfg, cm_gen_utils) cm_generator = CMockGenerator.new(@cfg, name, cm_writer, cm_gen_utils, cm_gen_plugins) puts "Creating mock for #{name}..." diff --git a/lib/cmock_config.rb b/lib/cmock_config.rb index 74ebd3b..defd105 100644 --- a/lib/cmock_config.rb +++ b/lib/cmock_config.rb @@ -1,9 +1,6 @@ class CMockConfig - attr_accessor :src_path, :mock_path, :tab, :includes, :plugins, :call_count_type, :ignore_bool_type, :cexception_include - attr_accessor :throw_type - CMockDefaultOptions = { 'mock_path' => 'mocks', @@ -17,22 +14,14 @@ class CMockConfig } def initialize(options=nil) - case(options) when NilClass then options = CMockDefaultOptions.clone when String then options = CMockDefaultOptions.clone.merge(load_config_file_from_yaml(options)) when Hash then options = CMockDefaultOptions.clone.merge(options) - else raise "If you specify parameters, it should be a filename or a hash of options" + else raise "If you specify parameters, it should be a filename or a hash of options" end - - @mock_path = options['mock_path'] - @tab = options['tab'] - @includes = options['includes'] - @plugins = options['plugins'] - @call_count_type = options['expect_call_count_type'] - @ignore_bool_type = options['ignore_bool_type'] - @cexception_include = options['cexception_include'] - @throw_type = options['cexception_throw_type'] + @options = options + @options.each_key { |key| eval("def #{key}() return @options['#{key}'] end") } end def load_config_file_from_yaml yaml_filename diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 904d719..fb588a6 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -26,7 +26,7 @@ class CMockGenerator create_mock_header_header(file, filename) create_mock_header_externs(file, parsed_stuff) parsed_stuff[:functions].each do |function| - @plugins.each { |plugin| file << plugin.mock_function_declarations(function[:name], function[:args_string], function[:rettype]) } + file << @plugins.run(:mock_function_declarations, function) end create_mock_header_footer(file) end @@ -42,7 +42,7 @@ class CMockGenerator create_mock_destroy_function(file, parsed_stuff[:functions]) parsed_stuff[:functions].each do |function| create_mock_implementation(file, function) - @plugins.each { |plugin| file << plugin.mock_interfaces( function[:name], function[:args_string], function[:args], function[:rettype]) } + file << @plugins.run(:mock_interfaces, function) end end end @@ -79,7 +79,7 @@ class CMockGenerator file << "#include \n" file << "#include \n" file << "#include \"unity.h\"\n" - @plugins.each { |plugin| file << plugin.include_files } + file << @plugins.run(:include_files) #(@config.includes + include_files).uniq.each {|include| file << "#include \"#{include}\"\n"} #### This is what the comments said in original version, but it wasnt actually doing this @config.includes.each {|include| file << "#include \"#{include}\"\n"} @@ -89,23 +89,16 @@ class CMockGenerator def create_instance_structure(file, functions) file << "static struct #{@mock_name}Instance\n" file << "{\n" - if (functions.size == 0) file << "#{@tab}unsigned char placeHolder;\n" end - file << "#{@tab}unsigned char allocFailure;\n" - - functions.each do |function| - @plugins.each { |plugin| file << plugin.instance_structure( function[:name], function[:args], function[:rettype] ) } - end + file << functions.collect{|function| @plugins.run(:instance_structure, function)}.flatten file << "} Mock;\n\n" end def create_extern_declarations(file, externs) - externs.each do |extern| - file << extern.gsub(/extern\s*/,'') << ";\n" - end + file << externs.collect {|extern| extern.gsub(/extern\s*/,'') << ";\n"}.flatten file << "extern jmp_buf AbortFrame;\n" file << "\n" end @@ -113,9 +106,7 @@ 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" - functions.each do |function| - @plugins.each { |plugin| file << plugin.mock_verify(function[:name]) } - end + file << functions.collect {|function| @plugins.run(:mock_verify, function)}.flatten file << "}\n\n" end @@ -127,9 +118,7 @@ class CMockGenerator def create_mock_destroy_function(file, functions) file << "void #{@mock_name}_Destroy(void)\n{\n" - functions.each do |function| - @plugins.each { |plugin| file << plugin.mock_destroy(function[:name], function[:args], function[:rettype]) } - end + file << functions.collect {|function| @plugins.run(:mock_destroy, function) }.flatten file << "#{@tab}memset(&Mock, 0, sizeof(Mock));\n" file << "}\n\n" end @@ -149,13 +138,12 @@ class CMockGenerator file << "#{function[:attributes]} " if (!function[:attributes].nil? && function[:attributes].length > 0) file << "#{function_mod_and_rettype} #{function[:name]}(#{args_string})\n" file << "{\n" - - @plugins.each { |plugin| file << plugin.mock_implementation_prefix(function[:name], function[:rettype]) } - @plugins.each { |plugin| file << plugin.mock_implementation(function[:name], function[:args]) } + file << @plugins.run(:mock_implementation_prefix, function) + file << @plugins.run(:mock_implementation, function) # Return expected value, if necessary if (function[:rettype] != "void") - file << @utils.make_handle_return(function[:name], function[:rettype], "#{@tab}") + file << @utils.make_handle_return(function, "#{@tab}") end # Close out the function diff --git a/lib/cmock_generator_plugin_cexception.rb b/lib/cmock_generator_plugin_cexception.rb index 6265dbd..525d2b0 100644 --- a/lib/cmock_generator_plugin_cexception.rb +++ b/lib/cmock_generator_plugin_cexception.rb @@ -7,6 +7,10 @@ class CMockGeneratorPluginCException @config = config @tab = @config.tab @utils = utils + + ["cexception_include", "cexception_throw_type"].each do |req| + raise "'#{req}' needs to be defined in config" unless @config.respond_to?(req) + end end def include_files @@ -15,82 +19,74 @@ class CMockGeneratorPluginCException return "#include \"#{include}\"\n" end - def instance_structure(function_name, function_args_as_array, function_return_type) + def instance_structure(function) lines = [] - call_count_type = @config.call_count_type - throw_type = @config.throw_type - lines << "#{@tab}#{call_count_type} *#{function_name}_ThrowOnCallCount;\n" - lines << "#{@tab}#{call_count_type} *#{function_name}_ThrowOnCallCount_Head;\n" - lines << "#{@tab}#{call_count_type} *#{function_name}_ThrowOnCallCount_HeadTail;\n" - lines << "#{@tab}#{throw_type} *#{function_name}_ThrowValue;\n" - lines << "#{@tab}#{throw_type} *#{function_name}_ThrowValue_Head;\n" - lines << "#{@tab}#{throw_type} *#{function_name}_ThrowValue_HeadTail;\n" + call_count_type = @config.cexception_call_count_type + throw_type = @config.cexception_throw_type + lines << "#{@tab}#{call_count_type} *#{function[:name]}_ThrowOnCallCount;\n" + lines << "#{@tab}#{call_count_type} *#{function[:name]}_ThrowOnCallCount_Head;\n" + lines << "#{@tab}#{call_count_type} *#{function[:name]}_ThrowOnCallCount_HeadTail;\n" + lines << "#{@tab}#{throw_type} *#{function[:name]}_ThrowValue;\n" + lines << "#{@tab}#{throw_type} *#{function[:name]}_ThrowValue_Head;\n" + lines << "#{@tab}#{throw_type} *#{function[:name]}_ThrowValue_HeadTail;\n" end - def mock_function_declarations(function_name, function_args, function_return_type) - if (function_args == "void") - return "void #{function_name}_ExpectAndThrow(#{@config.throw_type} toThrow);\n" + def mock_function_declarations(function) + if (function[:args_string] == "void") + return "void #{function[:name]}_ExpectAndThrow(#{@config.cexception_throw_type} toThrow);\n" else - return "void #{function_name}_ExpectAndThrow(#{function_args}, #{@config.throw_type} toThrow);\n" + return "void #{function[:name]}_ExpectAndThrow(#{function[:args_string]}, #{@config.cexception_throw_type} toThrow);\n" end end - def mock_implementation_prefix(function_name, function_return_type) - [] - end - - def mock_implementation(function_name, function_args_as_array) + def mock_implementation(function) lines = ["\n"] - lines << "#{@tab}if((Mock.#{function_name}_ThrowOnCallCount != Mock.#{function_name}_ThrowOnCallCount_HeadTail) &&\n" - lines << "#{@tab}#{@tab}(Mock.#{function_name}_ThrowValue != Mock.#{function_name}_ThrowValue_HeadTail))\n" + lines << "#{@tab}if((Mock.#{function[:name]}_ThrowOnCallCount != Mock.#{function[:name]}_ThrowOnCallCount_HeadTail) &&\n" + lines << "#{@tab}#{@tab}(Mock.#{function[:name]}_ThrowValue != Mock.#{function[:name]}_ThrowValue_HeadTail))\n" lines << "#{@tab}{\n" - lines << "#{@tab}#{@tab}if (*Mock.#{function_name}_ThrowOnCallCount && \n" - lines << "#{@tab}#{@tab}#{@tab}(Mock.#{function_name}_CallCount == *Mock.#{function_name}_ThrowOnCallCount))\n" + lines << "#{@tab}#{@tab}if (*Mock.#{function[:name]}_ThrowOnCallCount && \n" + lines << "#{@tab}#{@tab}#{@tab}(Mock.#{function[:name]}_CallCount == *Mock.#{function[:name]}_ThrowOnCallCount))\n" lines << "#{@tab}#{@tab}{\n" - lines << "#{@tab}#{@tab}#{@tab}#{@config.throw_type} toThrow = *Mock.#{function_name}_ThrowValue;\n" - lines << "#{@tab}#{@tab}#{@tab}Mock.#{function_name}_ThrowOnCallCount++;\n" - lines << "#{@tab}#{@tab}#{@tab}Mock.#{function_name}_ThrowValue++;\n" + lines << "#{@tab}#{@tab}#{@tab}#{@config.cexception_throw_type} toThrow = *Mock.#{function[:name]}_ThrowValue;\n" + lines << "#{@tab}#{@tab}#{@tab}Mock.#{function[:name]}_ThrowOnCallCount++;\n" + lines << "#{@tab}#{@tab}#{@tab}Mock.#{function[:name]}_ThrowValue++;\n" lines << "#{@tab}#{@tab}#{@tab}Throw(toThrow);\n" lines << "#{@tab}#{@tab}}\n" lines << "#{@tab}}\n" end - def mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) - arg_insert = (function_args == "void") ? "" : "#{function_args}, " - call_count_type = @config.call_count_type - throw_type = @config.throw_type + def mock_interfaces(function) + arg_insert = (function[:args_string] == "void") ? "" : "#{function[:args_string]}, " + call_count_type = @config.cexception_call_count_type + throw_type = @config.cexception_throw_type lines = [] - lines << "void #{function_name}_ExpectAndThrow(#{arg_insert}#{throw_type} toThrow)\n" + lines << "void #{function[:name]}_ExpectAndThrow(#{arg_insert}#{throw_type} toThrow)\n" lines << "{\n" - lines << "#{@tab}Mock.#{function_name}_CallsExpected++;\n" - lines << @utils.make_expand_array(call_count_type, "Mock.#{function_name}_ThrowOnCallCount_Head", "Mock.#{function_name}_CallsExpected") - lines << "#{@tab}Mock.#{function_name}_ThrowOnCallCount = Mock.#{function_name}_ThrowOnCallCount_Head;\n" - lines << "#{@tab}Mock.#{function_name}_ThrowOnCallCount += Mock.#{function_name}_CallCount;\n" - lines << @utils.make_expand_array(throw_type, "Mock.#{function_name}_ThrowValue_Head", "toThrow") - lines << "#{@tab}Mock.#{function_name}_ThrowValue = Mock.#{function_name}_ThrowValue_Head;\n" - lines << "#{@tab}Mock.#{function_name}_ThrowValue += Mock.#{function_name}_CallCount;\n" - lines << "#{@tab}ExpectParameters_#{function_name}(#{@utils.create_call_list(function_args_as_array)});\n" if (function_args != "void") + lines << "#{@tab}Mock.#{function[:name]}_CallsExpected++;\n" + lines << @utils.make_expand_array(call_count_type, "Mock.#{function[:name]}_ThrowOnCallCount_Head", "Mock.#{function[:name]}_CallsExpected") + lines << "#{@tab}Mock.#{function[:name]}_ThrowOnCallCount = Mock.#{function[:name]}_ThrowOnCallCount_Head;\n" + lines << "#{@tab}Mock.#{function[:name]}_ThrowOnCallCount += Mock.#{function[:name]}_CallCount;\n" + lines << @utils.make_expand_array(throw_type, "Mock.#{function[:name]}_ThrowValue_Head", "toThrow") + lines << "#{@tab}Mock.#{function[:name]}_ThrowValue = Mock.#{function[:name]}_ThrowValue_Head;\n" + lines << "#{@tab}Mock.#{function[:name]}_ThrowValue += Mock.#{function[:name]}_CallCount;\n" + lines << "#{@tab}ExpectParameters_#{function[:name]}(#{@utils.create_call_list(function)});\n" if (function[:args_string] != "void") lines << "}\n\n" end - def mock_verify(function_name) - [] - end - - def mock_destroy(function_name, function_args_as_array, function_return_type) + def mock_destroy(function) lines = [] - lines << "#{@tab}if(Mock.#{function_name}_ThrowOnCallCount_Head)\n" + lines << "#{@tab}if(Mock.#{function[:name]}_ThrowOnCallCount_Head)\n" lines << "#{@tab}{\n" - lines << "#{@tab}#{@tab}free(Mock.#{function_name}_ThrowOnCallCount_Head);\n" - lines << "#{@tab}#{@tab}Mock.#{function_name}_ThrowOnCallCount_Head=NULL;\n" - lines << "#{@tab}#{@tab}Mock.#{function_name}_ThrowOnCallCount_HeadTail=NULL;\n" + lines << "#{@tab}#{@tab}free(Mock.#{function[:name]}_ThrowOnCallCount_Head);\n" + lines << "#{@tab}#{@tab}Mock.#{function[:name]}_ThrowOnCallCount_Head=NULL;\n" + lines << "#{@tab}#{@tab}Mock.#{function[:name]}_ThrowOnCallCount_HeadTail=NULL;\n" lines << "#{@tab}}\n" - lines << "#{@tab}if(Mock.#{function_name}_ThrowValue_Head)\n" + lines << "#{@tab}if(Mock.#{function[:name]}_ThrowValue_Head)\n" lines << "#{@tab}{\n" - lines << "#{@tab}#{@tab}free(Mock.#{function_name}_ThrowValue_Head);\n" - lines << "#{@tab}#{@tab}Mock.#{function_name}_ThrowValue_Head=NULL;\n" - lines << "#{@tab}#{@tab}Mock.#{function_name}_ThrowValue_HeadTail=NULL;\n" + lines << "#{@tab}#{@tab}free(Mock.#{function[:name]}_ThrowValue_Head);\n" + lines << "#{@tab}#{@tab}Mock.#{function[:name]}_ThrowValue_Head=NULL;\n" + lines << "#{@tab}#{@tab}Mock.#{function[:name]}_ThrowValue_HeadTail=NULL;\n" lines << "#{@tab}}\n" end end diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index 38bec32..1fce09e 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -9,124 +9,116 @@ class CMockGeneratorPluginExpect @utils = utils end - def include_files - [] - end - - def instance_structure(function_name, function_args_as_array, function_return_type) - call_count_type = @config.call_count_type + def instance_structure(function) + call_count_type = @config.expect_call_count_type lines = [] - lines << "#{@tab}#{call_count_type} #{function_name}_CallCount;\n" - lines << "#{@tab}#{call_count_type} #{function_name}_CallsExpected;\n" + lines << "#{@tab}#{call_count_type} #{function[:name]}_CallCount;\n" + lines << "#{@tab}#{call_count_type} #{function[:name]}_CallsExpected;\n" - if (function_return_type != "void") - lines << "#{@tab}#{function_return_type} *#{function_name}_Return;\n" - lines << "#{@tab}#{function_return_type} *#{function_name}_Return_Head;\n" - lines << "#{@tab}#{function_return_type} *#{function_name}_Return_HeadTail;\n" + if (function[:rettype] != "void") + lines << "#{@tab}#{function[:rettype]} *#{function[:name]}_Return;\n" + lines << "#{@tab}#{function[:rettype]} *#{function[:name]}_Return_Head;\n" + lines << "#{@tab}#{function[:rettype]} *#{function[:name]}_Return_HeadTail;\n" end - function_args_as_array.each do |arg| + function[:args].each do |arg| type = arg[:type].sub(/const/, '').strip - lines << "#{@tab}#{type} *#{function_name}_Expected_#{arg[:name]};\n" - lines << "#{@tab}#{type} *#{function_name}_Expected_#{arg[:name]}_Head;\n" - lines << "#{@tab}#{type} *#{function_name}_Expected_#{arg[:name]}_HeadTail;\n" + lines << "#{@tab}#{type} *#{function[:name]}_Expected_#{arg[:name]};\n" + lines << "#{@tab}#{type} *#{function[:name]}_Expected_#{arg[:name]}_Head;\n" + lines << "#{@tab}#{type} *#{function[:name]}_Expected_#{arg[:name]}_HeadTail;\n" end lines end - def mock_function_declarations(function_name, function_args, function_return_type) - if (function_args == "void") - if (function_return_type == 'void') - return "void #{function_name}_Expect(void);\n" + def mock_function_declarations(function) + if (function[:args_string] == "void") + if (function[:rettype] == 'void') + return "void #{function[:name]}_Expect(void);\n" else - return "void #{function_name}_ExpectAndReturn(#{function_return_type} toReturn);\n" + return "void #{function[:name]}_ExpectAndReturn(#{function[:rettype]} toReturn);\n" end else - if (function_return_type == 'void') - return "void #{function_name}_Expect(#{function_args});\n" + if (function[:rettype] == 'void') + return "void #{function[:name]}_Expect(#{function[:args_string]});\n" else - return "void #{function_name}_ExpectAndReturn(#{function_args}, #{function_return_type} toReturn);\n" + return "void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:rettype]} toReturn);\n" end end end - def mock_implementation_prefix(function_name, function_return_type) - [] - end - - def mock_implementation(function_name, function_args_as_array) + def mock_implementation(function) lines = [] - lines << "#{@tab}Mock.#{function_name}_CallCount++;\n" - lines << "#{@tab}if (Mock.#{function_name}_CallCount > Mock.#{function_name}_CallsExpected)\n" + lines << "#{@tab}Mock.#{function[:name]}_CallCount++;\n" + lines << "#{@tab}if (Mock.#{function[:name]}_CallCount > Mock.#{function[:name]}_CallsExpected)\n" lines << "#{@tab}{\n" - lines << "#{@tab}#{@tab}TEST_FAIL(\"#{function_name} Called More Times Than Expected\");\n" + lines << "#{@tab}#{@tab}TEST_FAIL(\"#{function[:name]} Called More Times Than Expected\");\n" lines << "#{@tab}}\n" - function_args_as_array.each do |arg| + function[:args].each do |arg| arg_return_type = arg[:type].sub(/const/, '').strip - lines << @utils.make_handle_expected(function_name, arg_return_type, arg[:name]) + lines << @utils.make_handle_expected(function, arg_return_type, arg[:name]) end lines end - def mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) + def mock_interfaces(function) lines = [] # Parameter Helper Function - if (function_args != "void") - lines << "void ExpectParameters_#{function_name}(#{function_args})\n" + if (function[:args_string] != "void") + lines << "void ExpectParameters_#{function[:name]}(#{function[:args_string]})\n" lines << "{\n" - function_args_as_array.each do |arg| + function[:args].each do |arg| type = arg[:type].sub(/const/, '').strip - lines << @utils.make_add_new_expected(function_name, type, arg[:name]) + lines << @utils.make_add_new_expected(function, type, arg[:name]) end lines << "}\n\n" end #Main Mock Interface - if (function_return_type == "void") - lines << "void #{function_name}_Expect(#{function_args})\n" + if (function[:rettype] == "void") + lines << "void #{function[:name]}_Expect(#{function[:args_string]})\n" else - if (function_args == "void") - lines << "void #{function_name}_ExpectAndReturn(#{function_return_type} toReturn)\n" + if (function[:args_string] == "void") + lines << "void #{function[:name]}_ExpectAndReturn(#{function[:rettype]} toReturn)\n" else - lines << "void #{function_name}_ExpectAndReturn(#{function_args}, #{function_return_type} toReturn)\n" + lines << "void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:rettype]} toReturn)\n" end end lines << "{\n" - lines << "#{@tab}Mock.#{function_name}_CallsExpected++;\n" + lines << "#{@tab}Mock.#{function[:name]}_CallsExpected++;\n" - if (function_args != "void") - lines << "#{@tab}ExpectParameters_#{function_name}(#{@utils.create_call_list(function_args_as_array)});\n" + if (function[:args_string] != "void") + lines << "#{@tab}ExpectParameters_#{function[:name]}(#{@utils.create_call_list(function)});\n" end - if (function_return_type != "void") - lines << @utils.make_expand_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" + if (function[:rettype] != "void") + lines << @utils.make_expand_array(function[:rettype], "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" end lines << "}\n\n" end - def mock_verify(function_name) - return "#{@tab}TEST_ASSERT_EQUAL_MESSAGE(Mock.#{function_name}_CallsExpected, Mock.#{function_name}_CallCount, \"Function '#{function_name}' called unexpected number of times.\");\n" + def mock_verify(function) + return "#{@tab}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_name, function_args_as_array, function_return_type) + def mock_destroy(function) lines = [] - if (function_return_type != "void") - lines << "#{@tab}if (Mock.#{function_name}_Return_Head)\n" + if (function[:rettype] != "void") + lines << "#{@tab}if (Mock.#{function[:name]}_Return_Head)\n" lines << "#{@tab}{\n" - lines << "#{@tab}#{@tab}free(Mock.#{function_name}_Return_Head);\n" - lines << "#{@tab}#{@tab}Mock.#{function_name}_Return_Head=NULL;\n" - lines << "#{@tab}#{@tab}Mock.#{function_name}_Return_HeadTail=NULL;\n" + lines << "#{@tab}#{@tab}free(Mock.#{function[:name]}_Return_Head);\n" + lines << "#{@tab}#{@tab}Mock.#{function[:name]}_Return_Head=NULL;\n" + lines << "#{@tab}#{@tab}Mock.#{function[:name]}_Return_HeadTail=NULL;\n" lines << "#{@tab}}\n" end - function_args_as_array.each do |arg| - lines << "#{@tab}if (Mock.#{function_name}_Expected_#{arg[:name]}_Head)\n" + function[:args].each do |arg| + lines << "#{@tab}if (Mock.#{function[:name]}_Expected_#{arg[:name]}_Head)\n" lines << "#{@tab}{\n" - lines << "#{@tab}#{@tab}free(Mock.#{function_name}_Expected_#{arg[:name]}_Head);\n" - lines << "#{@tab}#{@tab}Mock.#{function_name}_Expected_#{arg[:name]}_Head=NULL;\n" - lines << "#{@tab}#{@tab}Mock.#{function_name}_Expected_#{arg[:name]}_HeadTail=NULL;\n" + lines << "#{@tab}#{@tab}free(Mock.#{function[:name]}_Expected_#{arg[:name]}_Head);\n" + lines << "#{@tab}#{@tab}Mock.#{function[:name]}_Expected_#{arg[:name]}_Head=NULL;\n" + lines << "#{@tab}#{@tab}Mock.#{function[:name]}_Expected_#{arg[:name]}_HeadTail=NULL;\n" lines << "#{@tab}}\n" end lines diff --git a/lib/cmock_generator_plugin_ignore.rb b/lib/cmock_generator_plugin_ignore.rb index bb8b947..6d92026 100644 --- a/lib/cmock_generator_plugin_ignore.rb +++ b/lib/cmock_generator_plugin_ignore.rb @@ -7,64 +7,52 @@ class CMockGeneratorPluginIgnore @config = config @tab = @config.tab @utils = utils + + ["ignore_bool_type"].each do |req| + raise "'#{req}' needs to be defined in config" unless @config.respond_to?(req) + end end - def include_files - [] + def instance_structure(function) + return "#{@tab}#{@config.ignore_bool_type} #{function[:name]}_IgnoreBool;\n" end - def instance_structure(function_name, function_args_as_array, function_return_type) - return "#{@tab}#{@config.ignore_bool_type} #{function_name}_IgnoreBool;\n" - end - - def mock_function_declarations(function_name, function_args, function_return_type) - if (function_return_type == "void") - return "void #{function_name}_Ignore(void);\n" + def mock_function_declarations(function) + if (function[:rettype] == "void") + return "void #{function[:name]}_Ignore(void);\n" else - return "void #{function_name}_IgnoreAndReturn(#{function_return_type} toReturn);\n" + return "void #{function[:name]}_IgnoreAndReturn(#{function[:rettype]} toReturn);\n" end end - def mock_implementation_prefix(function_name, function_return_type) + def mock_implementation_prefix(function) lines = [] - lines << "#{@tab}if (Mock.#{function_name}_IgnoreBool)\n" + lines << "#{@tab}if (Mock.#{function[:name]}_IgnoreBool)\n" lines << "#{@tab}{\n" - if (function_return_type == "void") + if (function[:rettype] == "void") lines << "#{@tab}#{@tab}return;\n" else - lines << @utils.make_handle_return(function_name, function_return_type, "#{@tab}#{@tab}") + lines << @utils.make_handle_return(function, "#{@tab}#{@tab}") end lines << "#{@tab}}\n" end - def mock_implementation(function_name, function_args_as_array) - [] - end - - def mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) + def mock_interfaces(function) lines = [] - if (function_return_type == "void") - lines << "void #{function_name}_Ignore(void)\n" + if (function[:rettype] == "void") + lines << "void #{function[:name]}_Ignore(void)\n" lines << "{\n" - lines << "#{@tab}Mock.#{function_name}_IgnoreBool = (unsigned char)1;\n" + lines << "#{@tab}Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n" lines << "}\n\n" else - lines << "void #{function_name}_IgnoreAndReturn(#{function_return_type} toReturn)\n" + lines << "void #{function[:name]}_IgnoreAndReturn(#{function[:rettype]} toReturn)\n" lines << "{\n" - lines << "#{@tab}Mock.#{function_name}_IgnoreBool = (unsigned char)1;\n" - lines << @utils.make_expand_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 << "#{@tab}Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n" + lines << @utils.make_expand_array(function[:rettype], "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 << "}\n\n" end return lines end - - def mock_verify(function_name) - [] - end - - def mock_destroy(function_name, function_args_as_array, function_return_type) - [] - end end diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index 6f95bfc..63b0303 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -8,9 +8,9 @@ class CMockGeneratorUtils @tab = @config.tab end - def create_call_list(args) + def create_call_list(function) call_list = "" - args.each do |arg| + function[:args].each do |arg| if call_list.empty? call_list = arg[:name] else @@ -45,36 +45,36 @@ class CMockGeneratorUtils lines << "#{@tab}}\n" end - def make_handle_return(function_name, function_return_type, indent) + def make_handle_return(function, indent) lines = ["\n"] - lines << "#{indent}if(Mock.#{function_name}_Return != Mock.#{function_name}_Return_HeadTail)\n" + lines << "#{indent}if(Mock.#{function[:name]}_Return != Mock.#{function[:name]}_Return_HeadTail)\n" lines << "#{indent}{\n" - lines << "#{indent}#{@tab}#{function_return_type} toReturn = *Mock.#{function_name}_Return;\n" - lines << "#{indent}#{@tab}Mock.#{function_name}_Return++;\n" + lines << "#{indent}#{@tab}#{function[:rettype]} toReturn = *Mock.#{function[:name]}_Return;\n" + lines << "#{indent}#{@tab}Mock.#{function[:name]}_Return++;\n" lines << "#{indent}#{@tab}return toReturn;\n" lines << "#{indent}}\n" lines << "#{indent}else\n" lines << "#{indent}{\n" - lines << "#{indent}#{@tab}return *Mock.#{function_name}_Return_Head;\n" + lines << "#{indent}#{@tab}return *Mock.#{function[:name]}_Return_Head;\n" lines << "#{indent}}\n" end - def make_add_new_expected(function_name, arg_type, expected) - lines = make_expand_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" + def make_add_new_expected(function, arg_type, expected) + lines = make_expand_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" end - def make_handle_expected(function_name, arg_type, actual) + def make_handle_expected(function, arg_type, actual) lines = ["\n"] - lines << "#{@tab}if(Mock.#{function_name}_Expected_#{actual} != Mock.#{function_name}_Expected_#{actual}_HeadTail)\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}#{arg_type}* p_expected = Mock.#{function[:name]}_Expected_#{actual};\n" + lines << "#{@tab}#{@tab}Mock.#{function[:name]}_Expected_#{actual}++;\n" if (arg_type == "char*" || arg_type == "const char*") - lines << "#{@tab}#{@tab}TEST_ASSERT_EQUAL_STRING_MESSAGE(*p_expected, #{actual}, \"Function '#{function_name}' called with unexpected string for parameter '#{actual}'.\");\n" + lines << "#{@tab}#{@tab}TEST_ASSERT_EQUAL_STRING_MESSAGE(*p_expected, #{actual}, \"Function '#{function[:name]}' called with unexpected string for parameter '#{actual}'.\");\n" else - lines << "#{@tab}#{@tab}TEST_ASSERT_EQUAL_MESSAGE(*p_expected, #{actual}, \"Function '#{function_name}' called with unexpected value for parameter '#{actual}'.\");\n" + lines << "#{@tab}#{@tab}TEST_ASSERT_EQUAL_MESSAGE(*p_expected, #{actual}, \"Function '#{function[:name]}' called with unexpected value for parameter '#{actual}'.\");\n" end lines << "#{@tab}}\n" end diff --git a/lib/cmock_plugin_manager.rb b/lib/cmock_plugin_manager.rb index e160ac0..7a0c7f3 100644 --- a/lib/cmock_plugin_manager.rb +++ b/lib/cmock_plugin_manager.rb @@ -5,19 +5,21 @@ require "#{$here}/cmock_generator_plugin_cexception.rb" class CMockPluginManager - attr_reader :config, :utils - + attr_accessor :plugins + def initialize(config, utils) - @config = config - @utils = utils + plugins_to_load = config.plugins + @plugins = [] + @plugins << CMockGeneratorPluginExpect.new( config, utils ) + @plugins << CMockGeneratorPluginCException.new( config, utils ) if plugins_to_load.include? 'cexception' + @plugins << CMockGeneratorPluginIgnore.new( config, utils ) if plugins_to_load.include? 'ignore' end - def get_generator_plugins - plugins_to_load = @config.plugins - @plugins = [] - @plugins << CMockGeneratorPluginExpect.new( @config, @utils ) - @plugins << CMockGeneratorPluginCException.new( @config, @utils ) if plugins_to_load.include? 'cexception' - @plugins << CMockGeneratorPluginIgnore.new( @config, @utils ) if plugins_to_load.include? 'ignore' - return @plugins + def run(method, args=nil) + if args.nil? + return @plugins.collect{ |plugin| plugin.send(method) if plugin.respond_to?(method) }.flatten + else + return @plugins.collect{ |plugin| plugin.send(method, args) if plugin.respond_to?(method) }.flatten + end end end diff --git a/test/unit/cmock_config_test.rb b/test/unit/cmock_config_test.rb index 9fe4e74..f30bf14 100644 --- a/test/unit/cmock_config_test.rb +++ b/test/unit/cmock_config_test.rb @@ -14,10 +14,10 @@ class CMockConfigTest < Test::Unit::TestCase assert_equal(CMockConfig::CMockDefaultOptions['includes'], config.includes) assert_equal(CMockConfig::CMockDefaultOptions['plugins'], config.plugins) assert_equal(CMockConfig::CMockDefaultOptions['tab'], config.tab) - assert_equal(CMockConfig::CMockDefaultOptions['expect_call_count_type'],config.call_count_type) + 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) - assert_equal(CMockConfig::CMockDefaultOptions['cexception_throw_type'], config.throw_type) + assert_equal(CMockConfig::CMockDefaultOptions['cexception_throw_type'], config.cexception_throw_type) end should "replace only options specified in a hash" do @@ -28,10 +28,10 @@ class CMockConfigTest < Test::Unit::TestCase assert_equal(test_includes, config.includes) assert_equal(CMockConfig::CMockDefaultOptions['plugins'], config.plugins) assert_equal(CMockConfig::CMockDefaultOptions['tab'], config.tab) - assert_equal(CMockConfig::CMockDefaultOptions['expect_call_count_type'],config.call_count_type) + 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) - assert_equal(CMockConfig::CMockDefaultOptions['cexception_throw_type'], config.throw_type) + assert_equal(CMockConfig::CMockDefaultOptions['cexception_throw_type'], config.cexception_throw_type) end should "replace only options specified in a yaml file" do @@ -42,9 +42,9 @@ class CMockConfigTest < Test::Unit::TestCase 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.call_count_type) + 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) - assert_equal(test_throw_type, config.throw_type) + assert_equal(test_throw_type, config.cexception_throw_type) end end diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index 125527d..6aea1de 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -116,8 +116,7 @@ class CMockGeneratorTest < Test::Unit::TestCase "#include \"ConfigRequiredHeader2.h\"\n", "#include \"MockPoutPoutFish.h\"\n\n" ] - pluginhelper = MockedPluginHelper.new("#include \"PluginRequiredHeader.h\"\n") - @plugins.expect.each.yields(pluginhelper) + @plugins.expect.run(:include_files).returns("#include \"PluginRequiredHeader.h\"\n") @config.expect.includes.returns(["ConfigRequiredHeader1.h","ConfigRequiredHeader2.h"]) @cmock_generator.create_source_header_section(output, "MockPoutPoutFish.c", []) @@ -137,7 +136,7 @@ class CMockGeneratorTest < Test::Unit::TestCase @cmock_generator.create_instance_structure(output, functions) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end should "create the instance structure where it is needed when functions required" do @@ -154,15 +153,12 @@ class CMockGeneratorTest < Test::Unit::TestCase " Dos_Second(bool Smarty, char)", "} Mock;\n\n" ] - - pluginhelper1 = MockedPluginHelper.new("Uno") - pluginhelper2 = MockedPluginHelper.new("Dos") - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) + @plugins.expect.run(:instance_structure, functions[0]).returns([" Uno_First(int Candy, int)"," Dos_First(int Candy, int)"]) + @plugins.expect.run(:instance_structure, functions[1]).returns([" Uno_Second(bool Smarty, char)"," Dos_Second(bool Smarty, char)"]) @cmock_generator.create_instance_structure(output, functions) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end should "create extern declarations for source file if no extra externs requested" do @@ -172,17 +168,17 @@ class CMockGeneratorTest < Test::Unit::TestCase @cmock_generator.create_extern_declarations(output, externs) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end should "create extern declarations for source file if extra externs requested" do externs = ["extern int whatever", "extern short somethingelse"] output = [] - expected = [ "int whatever",";\n", "short somethingelse",";\n", "extern jmp_buf AbortFrame;\n","\n"] + expected = [ "int whatever;\n", "short somethingelse;\n", "extern jmp_buf AbortFrame;\n","\n"] @cmock_generator.create_extern_declarations(output, externs) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end should "create mock verify functions in source file when no functions specified" do @@ -195,7 +191,7 @@ class CMockGeneratorTest < Test::Unit::TestCase @cmock_generator.create_mock_verify_function(output, functions) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end should "create mock verify functions in source file when extra functions specified" do @@ -211,15 +207,12 @@ class CMockGeneratorTest < Test::Unit::TestCase " Dos_Second", "}\n\n" ] - - pluginhelper1 = MockedPluginHelper.new("Uno") - pluginhelper2 = MockedPluginHelper.new("Dos") - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) + @plugins.expect.run(:mock_verify, functions[0]).returns([" Uno_First"," Dos_First"]) + @plugins.expect.run(:mock_verify, functions[1]).returns([" Uno_Second"," Dos_Second"]) @cmock_generator.create_mock_verify_function(output, functions) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end should "create mock init functions in source file" do @@ -244,7 +237,7 @@ class CMockGeneratorTest < Test::Unit::TestCase @cmock_generator.create_mock_destroy_function(output, functions) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end should "create mock destroy functions in source file when extra functions specified" do @@ -260,15 +253,12 @@ class CMockGeneratorTest < Test::Unit::TestCase " memset(&Mock, 0, sizeof(Mock));\n", "}\n\n" ] - - pluginhelper1 = MockedPluginHelper.new("Uno") - pluginhelper2 = MockedPluginHelper.new("Dos") - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) + @plugins.expect.run(:mock_destroy, functions[0]).returns([" Uno_First(int Candy, int)"," Dos_First(int Candy, int)"]) + @plugins.expect.run(:mock_destroy, functions[1]).returns([" Uno_Second(bool Smarty, char)"," Dos_Second(bool Smarty, char)"]) @cmock_generator.create_mock_destroy_function(output, functions) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end should "create mock implementation functions in source file" do @@ -291,16 +281,13 @@ class CMockGeneratorTest < Test::Unit::TestCase " UtilsSupaFunction.bool", "}\n\n" ] - - pluginhelper1 = MockedPluginHelper.new("Uno") - pluginhelper2 = MockedPluginHelper.new("Dos") - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) - @utils.expect.make_handle_return("SupaFunction","bool"," ").returns(" UtilsSupaFunction.bool") + @plugins.expect.run(:mock_implementation_prefix, function).returns([" PreSupaFunctionUno.bool"," PreSupaFunctionDos.bool"]) + @plugins.expect.run(:mock_implementation, function).returns([" MockSupaFunctionUno(uint32 sandwiches, const char* named)"," MockSupaFunctionDos(uint32 sandwiches, const char* named)"]) + @utils.expect.make_handle_return(function," ").returns(" UtilsSupaFunction.bool") @cmock_generator.create_mock_implementation(output, function) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end should "create mock implementation functions in source file with different options" do @@ -322,15 +309,12 @@ class CMockGeneratorTest < Test::Unit::TestCase " UtilsSupaFunction.int", "}\n\n" ] - - pluginhelper1 = MockedPluginHelper.new("Uno") - pluginhelper2 = MockedPluginHelper.new("Dos") - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) - @plugins.expect.each.yields(pluginhelper1, pluginhelper2) - @utils.expect.make_handle_return("SupaFunction","int"," ").returns(" UtilsSupaFunction.int") + @plugins.expect.run(:mock_implementation_prefix, function).returns([" PreSupaFunctionUno.int"," PreSupaFunctionDos.int"]) + @plugins.expect.run(:mock_implementation, function).returns([" MockSupaFunctionUno(uint32 sandwiches)"," MockSupaFunctionDos(uint32 sandwiches)"]) + @utils.expect.make_handle_return(function," ").returns(" UtilsSupaFunction.int") @cmock_generator.create_mock_implementation(output, function) - assert_equal(expected, output) + assert_equal(expected, output.flatten) end end diff --git a/test/unit/cmock_generator_plugin_cexception_test.rb b/test/unit/cmock_generator_plugin_cexception_test.rb index ee45f0e..00cc660 100644 --- a/test/unit/cmock_generator_plugin_cexception_test.rb +++ b/test/unit/cmock_generator_plugin_cexception_test.rb @@ -5,6 +5,7 @@ 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 @@ -32,12 +33,9 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase end should "add to control structure mock needs" do - function_name = "Oak" - function_args_as_array = [] - function_return_type = "void" - - @config.expect.call_count_type.returns("uint32") - @config.expect.throw_type.returns("EXCEPTION_TYPE") + function = { :name => "Oak", :args => [], :rettype => "void" } + @config.expect.cexception_call_count_type.returns("uint32") + @config.expect.cexception_throw_type.returns("EXCEPTION_TYPE") expected = [" uint32 *Oak_ThrowOnCallCount;\n", " uint32 *Oak_ThrowOnCallCount_Head;\n", @@ -46,49 +44,35 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase " EXCEPTION_TYPE *Oak_ThrowValue_Head;\n", " EXCEPTION_TYPE *Oak_ThrowValue_HeadTail;\n" ] - returned = @cmock_generator_plugin_cexception.instance_structure(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_cexception.instance_structure(function) assert_equal(expected, returned) end should "add mock function declarations for functions without arguments" do - function_name = "Spruce" - function_args_as_array = "void" - function_return_type = "void" - - @config.expect.throw_type.returns("EXCEPTION_TYPE") + function = { :name => "Spruce", :args_string => "void", :rettype => "void" } + @config.expect.cexception_throw_type.returns("EXCEPTION_TYPE") expected = "void Spruce_ExpectAndThrow(EXCEPTION_TYPE toThrow);\n" - returned = @cmock_generator_plugin_cexception.mock_function_declarations(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_cexception.mock_function_declarations(function) assert_equal(expected, returned) end should "add mock function declarations for functions with arguments" do - function_name = "Spruce" - function_args_as_array = "const char* Petunia, uint32_t Lily" - function_return_type = "void" - - @config.expect.throw_type.returns("EXCEPTION_TYPE") + function = { :name => "Spruce", :args_string => "const char* Petunia, uint32_t Lily", :rettype => "void" } + @config.expect.cexception_throw_type.returns("EXCEPTION_TYPE") expected = "void Spruce_ExpectAndThrow(const char* Petunia, uint32_t Lily, EXCEPTION_TYPE toThrow);\n" - returned = @cmock_generator_plugin_cexception.mock_function_declarations(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_cexception.mock_function_declarations(function) assert_equal(expected, returned) end should "add nothing during implementation prefix" do - function_name = "Pine" - function_return_type = "void" - - expected = [] - returned = @cmock_generator_plugin_cexception.mock_implementation_prefix(function_name, function_return_type) - assert_equal(expected, returned) + assert(!@cmock_generator_plugin_cexception.respond_to?(:mock_implementation_prefix)) end should "add a mock implementation" do - function_name = "Cherry" - function_args_as_array = [] - function_return_type = "void" - - @config.expect.throw_type.returns("EXCEPTION_TYPE") + function = {:name => "Cherry", :args => [], :rettype => "void"} + @config.expect.cexception_throw_type.returns("EXCEPTION_TYPE") expected = ["\n", " if((Mock.Cherry_ThrowOnCallCount != Mock.Cherry_ThrowOnCallCount_HeadTail) &&\n", @@ -104,18 +88,14 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase " }\n", " }\n" ] - returned = @cmock_generator_plugin_cexception.mock_implementation(function_name, function_args_as_array) + returned = @cmock_generator_plugin_cexception.mock_implementation(function) assert_equal(expected, returned) end should "add a mock interfaces for functions without arguments" do - function_name = "Pear" - function_args = "void" - function_args_as_array = [] - function_return_type = "void" - - @config.expect.call_count_type.returns("uint32") - @config.expect.throw_type.returns("EXCEPTION_TYPE") + function = {:name => "Pear", :args_string => "void", :args => [], :rettype => "void"} + @config.expect.cexception_call_count_type.returns("uint32") + @config.expect.cexception_throw_type.returns("EXCEPTION_TYPE") @utils.expect.make_expand_array("uint32", "Mock.Pear_ThrowOnCallCount_Head", "Mock.Pear_CallsExpected").returns("mock_return_1") @utils.expect.make_expand_array("EXCEPTION_TYPE", "Mock.Pear_ThrowValue_Head", "toThrow").returns("mock_return_2") @@ -130,21 +110,17 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase " Mock.Pear_ThrowValue += Mock.Pear_CallCount;\n", "}\n\n" ] - returned = @cmock_generator_plugin_cexception.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_cexception.mock_interfaces(function) assert_equal(expected, returned) end should "add a mock interfaces for functions with arguments" do - function_name = "Pear" - function_args = "int blah" - function_args_as_array = [{ :type => "int", :name => "blah" }] - function_return_type = "void" - - @config.expect.call_count_type.returns("uint32") - @config.expect.throw_type.returns("EXCEPTION_TYPE") + function = {:name => "Pear", :args_string => "int blah", :args => [{ :type => "int", :name => "blah" }], :rettype => "void"} + @config.expect.cexception_call_count_type.returns("uint32") + @config.expect.cexception_throw_type.returns("EXCEPTION_TYPE") @utils.expect.make_expand_array("uint32", "Mock.Pear_ThrowOnCallCount_Head", "Mock.Pear_CallsExpected").returns("mock_return_1") @utils.expect.make_expand_array("EXCEPTION_TYPE", "Mock.Pear_ThrowValue_Head", "toThrow").returns("mock_return_2") - @utils.expect.create_call_list(function_args_as_array).returns("mock_return_3") + @utils.expect.create_call_list(function).returns("mock_return_3") expected = ["void Pear_ExpectAndThrow(int blah, EXCEPTION_TYPE toThrow)\n", "{\n", @@ -158,19 +134,16 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase " ExpectParameters_Pear(mock_return_3);\n", "}\n\n" ] - returned = @cmock_generator_plugin_cexception.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_cexception.mock_interfaces(function) assert_equal(expected, returned) end should "have nothing to say about verifying" do - assert_equal([], @cmock_generator_plugin_cexception.mock_verify("Maple")) + assert(!@cmock_generator_plugin_cexception.respond_to?(:mock_verify)) end should "add necessary baggage to destroy function" do - function_name = "Banana" - function_args_as_array = [] - function_return_type = "void" - + function = {:name => "Banana", :args_string => "", :args => [], :rettype => "void"} expected = [" if(Mock.Banana_ThrowOnCallCount_Head)\n", " {\n", " free(Mock.Banana_ThrowOnCallCount_Head);\n", @@ -184,7 +157,7 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase " Mock.Banana_ThrowValue_HeadTail=NULL;\n", " }\n" ] - returned = @cmock_generator_plugin_cexception.mock_destroy(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_cexception.mock_destroy(function) assert_equal(expected, returned) end end diff --git a/test/unit/cmock_generator_plugin_expect_test.rb b/test/unit/cmock_generator_plugin_expect_test.rb index bdfa305..a084d50 100644 --- a/test/unit/cmock_generator_plugin_expect_test.rb +++ b/test/unit/cmock_generator_plugin_expect_test.rb @@ -5,6 +5,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase def setup create_mocks :config, :utils @config.expect.tab.returns(" ") + @config.stubs!(:respond_to?).returns(true) @cmock_generator_plugin_expect = CMockGeneratorPluginExpect.new(@config, @utils) end @@ -18,142 +19,116 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase end should "not include any additional include files" do - assert_equal([], @cmock_generator_plugin_expect.include_files) + assert(!@cmock_generator_plugin_expect.respond_to?(:include_files)) end should "add to control structure mock needs of functions of style 'void func(void)'" do - function_name = "Oak" - function_args_as_array = [] - function_return_type = "void" + function = {:name => "Oak", :args => [], :rettype => "void"} count_type = "uint32" + @config.expect.expect_call_count_type.returns(count_type) - @config.expect.call_count_type.returns(count_type) - - expected = [" #{count_type} #{function_name}_CallCount;\n", - " #{count_type} #{function_name}_CallsExpected;\n" + expected = [" #{count_type} #{function[:name]}_CallCount;\n", + " #{count_type} #{function[:name]}_CallsExpected;\n" ] - returned = @cmock_generator_plugin_expect.instance_structure(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.instance_structure(function) assert_equal(expected, returned) end should "add to control structure mock needs of functions of style 'int func(void)'" do - function_name = "Elm" - function_args_as_array = [] - function_return_type = "int" + function = {:name => "Elm", :args => [], :rettype => "int"} count_type = "int16" + @config.expect.expect_call_count_type.returns(count_type) - @config.expect.call_count_type.returns(count_type) - - expected = [" #{count_type} #{function_name}_CallCount;\n", - " #{count_type} #{function_name}_CallsExpected;\n", - " #{function_return_type} *#{function_name}_Return;\n", - " #{function_return_type} *#{function_name}_Return_Head;\n", - " #{function_return_type} *#{function_name}_Return_HeadTail;\n" + expected = [" #{count_type} #{function[:name]}_CallCount;\n", + " #{count_type} #{function[:name]}_CallsExpected;\n", + " #{function[:rettype]} *#{function[:name]}_Return;\n", + " #{function[:rettype]} *#{function[:name]}_Return_Head;\n", + " #{function[:rettype]} *#{function[:name]}_Return_HeadTail;\n" ] - returned = @cmock_generator_plugin_expect.instance_structure(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.instance_structure(function) assert_equal(expected, returned) end should "add to control structure mock needs of functions of style 'void func(int chicken, char* pork)'" do - function_name = "Cedar" - function_args_as_array = [{ :name => "chicken", :type => "int"}, { :name => "pork", :type => "char*"}] - function_return_type = "void" + function = {:name => "Cedar", :args => [{ :name => "chicken", :type => "int"}, { :name => "pork", :type => "char*"}], :rettype => "void"} count_type = "unsigned char" + @config.expect.expect_call_count_type.returns(count_type) - @config.expect.call_count_type.returns(count_type) - - expected = [" #{count_type} #{function_name}_CallCount;\n", - " #{count_type} #{function_name}_CallsExpected;\n", - " int *#{function_name}_Expected_chicken;\n", - " int *#{function_name}_Expected_chicken_Head;\n", - " int *#{function_name}_Expected_chicken_HeadTail;\n", - " char* *#{function_name}_Expected_pork;\n", - " char* *#{function_name}_Expected_pork_Head;\n", - " char* *#{function_name}_Expected_pork_HeadTail;\n" + expected = [" #{count_type} #{function[:name]}_CallCount;\n", + " #{count_type} #{function[:name]}_CallsExpected;\n", + " int *#{function[:name]}_Expected_chicken;\n", + " int *#{function[:name]}_Expected_chicken_Head;\n", + " int *#{function[:name]}_Expected_chicken_HeadTail;\n", + " char* *#{function[:name]}_Expected_pork;\n", + " char* *#{function[:name]}_Expected_pork_Head;\n", + " char* *#{function[:name]}_Expected_pork_HeadTail;\n" ] - returned = @cmock_generator_plugin_expect.instance_structure(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.instance_structure(function) assert_equal(expected, returned) end should "add to control structure mock needs of functions of style 'int func(float beef)'" do - function_name = "Birch" - function_args_as_array = [{ :name => "beef", :type => "float"}] - function_return_type = "int" + function = {:name => "Birch", :args => [{ :name => "beef", :type => "float"}], :rettype => "int"} count_type = "unsigned int" + @config.expect.expect_call_count_type.returns(count_type) - @config.expect.call_count_type.returns(count_type) - - expected = [" #{count_type} #{function_name}_CallCount;\n", - " #{count_type} #{function_name}_CallsExpected;\n", - " #{function_return_type} *#{function_name}_Return;\n", - " #{function_return_type} *#{function_name}_Return_Head;\n", - " #{function_return_type} *#{function_name}_Return_HeadTail;\n", - " float *#{function_name}_Expected_beef;\n", - " float *#{function_name}_Expected_beef_Head;\n", - " float *#{function_name}_Expected_beef_HeadTail;\n", + expected = [" #{count_type} #{function[:name]}_CallCount;\n", + " #{count_type} #{function[:name]}_CallsExpected;\n", + " #{function[:rettype]} *#{function[:name]}_Return;\n", + " #{function[:rettype]} *#{function[:name]}_Return_Head;\n", + " #{function[:rettype]} *#{function[:name]}_Return_HeadTail;\n", + " float *#{function[:name]}_Expected_beef;\n", + " float *#{function[:name]}_Expected_beef_Head;\n", + " float *#{function[:name]}_Expected_beef_HeadTail;\n", ] - returned = @cmock_generator_plugin_expect.instance_structure(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.instance_structure(function) assert_equal(expected, returned) end should "add mock function declaration for functions of style 'void func(void)'" do - function_name = "Maple" - function_args = "void" - function_return_type = "void" - - expected = "void #{function_name}_Expect(#{function_args});\n" - returned = @cmock_generator_plugin_expect.mock_function_declarations(function_name, function_args, function_return_type) + function = {:name => "Maple", :args_string => "void", :rettype => "void"} + expected = "void #{function[:name]}_Expect(#{function[:args_string]});\n" + returned = @cmock_generator_plugin_expect.mock_function_declarations(function) assert_equal(expected, returned) end should "add mock function declaration for functions of style 'int func(void)'" do - function_name = "Spruce" - function_args = "void" - function_return_type = "int" + function = {:name => "Spruce", :args_string => "void", :rettype => "int"} - expected = "void #{function_name}_ExpectAndReturn(#{function_return_type} toReturn);\n" - returned = @cmock_generator_plugin_expect.mock_function_declarations(function_name, function_args, function_return_type) + expected = "void #{function[:name]}_ExpectAndReturn(#{function[:rettype]} toReturn);\n" + returned = @cmock_generator_plugin_expect.mock_function_declarations(function) assert_equal(expected, returned) end should "add mock function declaration for functions of style 'const char* func(int tofu)'" do - function_name = "Pine" - function_args = "int tofu" - function_return_type = "const char*" + function = {:name => "Pine", :args_string => "int tofu", :rettype => "const char*"} - expected = "void #{function_name}_ExpectAndReturn(#{function_args}, #{function_return_type} toReturn);\n" - returned = @cmock_generator_plugin_expect.mock_function_declarations(function_name, function_args, function_return_type) + expected = "void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:rettype]} toReturn);\n" + returned = @cmock_generator_plugin_expect.mock_function_declarations(function) assert_equal(expected, returned) end should "not require anything for implementation prefix" do - function_name = "Ash" - function_return_type = "int" - assert_equal([], @cmock_generator_plugin_expect.mock_implementation_prefix(function_name, function_return_type)) + assert(!@cmock_generator_plugin_expect.respond_to?(:mock_implementation_prefix)) end should "add mock function implementation for functions of style 'void func(void)'" do - function_name = "Apple" - function_args = [] - function_return_type = "void" - + function = {:name => "Apple", :args => [], :rettype => "void"} expected = [" Mock.Apple_CallCount++;\n", " if (Mock.Apple_CallCount > Mock.Apple_CallsExpected)\n", " {\n", " TEST_FAIL(\"Apple Called More Times Than Expected\");\n", " }\n" ] - returned = @cmock_generator_plugin_expect.mock_implementation(function_name, function_args) + returned = @cmock_generator_plugin_expect.mock_implementation(function) assert_equal(expected, returned) end should "add mock function implementation for functions of style 'int func(int veal, unsigned int sushi)'" do - function_name = "Cherry" - function_args = [ { :type => "int", :name => "veal" }, { :type => "unsigned int", :name => "sushi" } ] - function_return_type = "int" + function = {:name => "Cherry", :args => [ { :type => "int", :name => "veal" }, { :type => "unsigned int", :name => "sushi" } ], :rettype => "int"} - @utils.expect.make_handle_expected(function_name, function_args[0][:type], function_args[0][:name]).returns("mocked_retval_1") - @utils.expect.make_handle_expected(function_name, function_args[1][:type], function_args[1][:name]).returns("mocked_retval_2") + @utils.expect.make_handle_expected(function, function[:args][0][:type], function[:args][0][:name]).returns("mocked_retval_1") + @utils.expect.make_handle_expected(function, function[:args][1][:type], function[:args][1][:name]).returns("mocked_retval_2") expected = [" Mock.Cherry_CallCount++;\n", " if (Mock.Cherry_CallCount > Mock.Cherry_CallsExpected)\n", @@ -163,32 +138,24 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase "mocked_retval_1", "mocked_retval_2" ] - returned = @cmock_generator_plugin_expect.mock_implementation(function_name, function_args) + returned = @cmock_generator_plugin_expect.mock_implementation(function) assert_equal(expected, returned) end should "add mock interfaces for functions of style 'void func(void)'" do - function_name = "Pear" - function_args = "void" - function_args_as_array = [] - function_return_type = "void" - + function = {:name => "Pear", :args => [], :args_string => "void", :rettype => "void"} expected = ["void Pear_Expect(void)\n", "{\n", " Mock.Pear_CallsExpected++;\n", "}\n\n" ] - returned = @cmock_generator_plugin_expect.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.mock_interfaces(function) assert_equal(expected, returned) end should "add mock interfaces for functions of style 'unsigned short func(void)'" do - function_name = "Orange" - function_args = "void" - function_args_as_array = [] - function_return_type = "unsigned short" - - @utils.expect.make_expand_array(function_return_type, "Mock.Orange_Return_Head","toReturn").returns("mock_retval_1") + function = {:name => "Orange", :args => [], :args_string => "void", :rettype => "unsigned short"} + @utils.expect.make_expand_array(function[:rettype], "Mock.Orange_Return_Head","toReturn").returns("mock_retval_1") expected = ["void Orange_ExpectAndReturn(unsigned short toReturn)\n", "{\n", @@ -198,19 +165,15 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase " Mock.Orange_Return += Mock.Orange_CallCount;\n", "}\n\n" ] - returned = @cmock_generator_plugin_expect.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.mock_interfaces(function) assert_equal(expected, returned) end should "add mock interfaces for functions of style 'int func(char* pescado)'" do - function_name = "Lemon" - function_args = "char* pescado" - function_args_as_array = [{ :type => "char*", :name => "pescado"}] - function_return_type = "int" - - @utils.expect.make_add_new_expected(function_name, "char*", "pescado").returns("mock_retval_2") - @utils.expect.create_call_list(function_args_as_array).returns("mock_retval_3") - @utils.expect.make_expand_array(function_return_type, "Mock.Lemon_Return_Head","toReturn").returns("mock_retval_1") + function = {:name => "Lemon", :args => [{ :type => "char*", :name => "pescado"}], :args_string => "char* pescado", :rettype => "int"} + @utils.expect.make_add_new_expected(function, "char*", "pescado").returns("mock_retval_2") + @utils.expect.create_call_list(function).returns("mock_retval_3") + @utils.expect.make_expand_array(function[:rettype], "Mock.Lemon_Return_Head","toReturn").returns("mock_retval_1") expected = ["void ExpectParameters_Lemon(char* pescado)\n", "{\n", @@ -225,33 +188,27 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase " Mock.Lemon_Return += Mock.Lemon_CallCount;\n", "}\n\n" ] - returned = @cmock_generator_plugin_expect.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.mock_interfaces(function) assert_equal(expected, returned) end should "add mock verify lines" do - function_name = "Banana" + function = {:name => "Banana" } expected = " TEST_ASSERT_EQUAL_MESSAGE(Mock.Banana_CallsExpected, Mock.Banana_CallCount, \"Function 'Banana' called unexpected number of times.\");\n" - returned = @cmock_generator_plugin_expect.mock_verify(function_name) + returned = @cmock_generator_plugin_expect.mock_verify(function) assert_equal(expected, returned) end should "add mock destroy for functions of style 'void func(void)'" do - function_name = "Peach" - function_args_as_array = [] - function_return_type = "void" - + function = {:name => "Peach", :args => [], :rettype => "void" } expected = [] - returned = @cmock_generator_plugin_expect.mock_destroy(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.mock_destroy(function) assert_equal(expected, returned) end should "add mock destroy for functions of style 'char func(void)'" do - function_name = "Palm" - function_args_as_array = [] - function_return_type = "char" - + function = {:name => "Palm", :args => [], :rettype => "char" } expected = [" if (Mock.Palm_Return_Head)\n", " {\n", " free(Mock.Palm_Return_Head);\n", @@ -259,15 +216,12 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase " Mock.Palm_Return_HeadTail=NULL;\n", " }\n" ] - returned = @cmock_generator_plugin_expect.mock_destroy(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.mock_destroy(function) assert_equal(expected, returned) end should "add mock destroy for functions of style 'int func(uint32 grease)'" do - function_name = "Coconut" - function_args_as_array = [{ :type => "uint32", :name => "grease"}] - function_return_type = "int" - + function = {:name => "Coconut", :args => [{ :type => "uint32", :name => "grease"}], :rettype => "int" } expected = [" if (Mock.Coconut_Return_Head)\n", " {\n", " free(Mock.Coconut_Return_Head);\n", @@ -281,7 +235,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase " Mock.Coconut_Expected_grease_HeadTail=NULL;\n", " }\n" ] - returned = @cmock_generator_plugin_expect.mock_destroy(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_expect.mock_destroy(function) assert_equal(expected, returned) end end diff --git a/test/unit/cmock_generator_plugin_ignore_test.rb b/test/unit/cmock_generator_plugin_ignore_test.rb index fa43f7d..ba4d24b 100644 --- a/test/unit/cmock_generator_plugin_ignore_test.rb +++ b/test/unit/cmock_generator_plugin_ignore_test.rb @@ -5,6 +5,7 @@ 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 @@ -17,97 +18,74 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase assert_equal(" ", @cmock_generator_plugin_ignore.tab) end - should "not have no additional include file requirements" do - assert_equal([], @cmock_generator_plugin_ignore.include_files) + should "not have any additional include file requirements" do + assert(!@cmock_generator_plugin_ignore.respond_to?(:include_files)) end should "add a required variable to the instance structure" do - function_name = "Grass" - function_args_as_array = [] - function_return_type = "void" - + function = {:name => "Grass", :args => [], :rettype => "void"} @config.expect.ignore_bool_type.returns("BOOL") expected = " BOOL Grass_IgnoreBool;\n" - returned = @cmock_generator_plugin_ignore.instance_structure(function_name, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_ignore.instance_structure(function) assert_equal(expected, returned) end should "handle function declarations for functions without return values" do - function_name = "Mold" - function_args = "void" - function_return_type = "void" - + function = {:name => "Mold", :args_string => "void", :rettype => "void"} expected = "void Mold_Ignore(void);\n" - returned = @cmock_generator_plugin_ignore.mock_function_declarations(function_name, function_args, function_return_type) + returned = @cmock_generator_plugin_ignore.mock_function_declarations(function) assert_equal(expected, returned) end should "handle function declarations for functions that returns something" do - function_name = "Fungus" - function_args = "void" - function_return_type = "const char*" - + function = {:name => "Fungus", :args_string => "void", :rettype => "const char*"} expected = "void Fungus_IgnoreAndReturn(const char* toReturn);\n" - returned = @cmock_generator_plugin_ignore.mock_function_declarations(function_name, function_args, function_return_type) + returned = @cmock_generator_plugin_ignore.mock_function_declarations(function) assert_equal(expected, returned) end should "add required code to implementation prefix with void function" do - function_name = "Mold" - function_args = "void" - function_return_type = "void" - + function = {:name => "Mold", :args_string => "void", :rettype => "void"} expected = [" if (Mock.Mold_IgnoreBool)\n", " {\n", " return;\n", " }\n" ] - returned = @cmock_generator_plugin_ignore.mock_implementation_prefix(function_name, function_return_type) + returned = @cmock_generator_plugin_ignore.mock_implementation_prefix(function) assert_equal(expected, returned) end should "add required code to implementation prefix with return functions" do - function_name = "Mold" - function_args = "void" - function_return_type = "int" - - @utils.expect.make_handle_return(function_name, function_return_type, " ").returns(" mock_return_1") + function = {:name => "Fungus", :args_string => "void", :rettype => "int"} + @utils.expect.make_handle_return(function, " ").returns(" mock_return_1") - expected = [" if (Mock.Mold_IgnoreBool)\n", + expected = [" if (Mock.Fungus_IgnoreBool)\n", " {\n", " mock_return_1", " }\n" ] - returned = @cmock_generator_plugin_ignore.mock_implementation_prefix(function_name, function_return_type) + returned = @cmock_generator_plugin_ignore.mock_implementation_prefix(function) assert_equal(expected, returned) end should "have nothing new for mock implementation" do - assert_equal([], @cmock_generator_plugin_ignore.mock_implementation("Ooze", [])) + assert(!@cmock_generator_plugin_ignore.respond_to?(:mock_implementation)) end should "add a new mock interface for ignoring when function had no return value" do - function_name = "Slime" - function_args = "void" - function_args_as_array = [] - function_return_type = "void" - + function = {:name => "Slime", :args => [], :args_string => "void", :rettype => "void"} expected = ["void Slime_Ignore(void)\n", "{\n", " Mock.Slime_IgnoreBool = (unsigned char)1;\n", "}\n\n" ] - returned = @cmock_generator_plugin_ignore.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_ignore.mock_interfaces(function) assert_equal(expected, returned) end should "add a new mock interface for ignoring when function has return value" do - function_name = "Slime" - function_args = "void" - function_args_as_array = [] - function_return_type = "uint32" - + function = {:name => "Slime", :args => [], :args_string => "void", :rettype => "uint32"} @utils.expect.make_expand_array("uint32", "Mock.Slime_Return_Head", "toReturn").returns("mock_return_1") expected = ["void Slime_IgnoreAndReturn(uint32 toReturn)\n", @@ -118,16 +96,16 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase " Mock.Slime_Return += Mock.Slime_CallCount;\n", "}\n\n" ] - returned = @cmock_generator_plugin_ignore.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type) + returned = @cmock_generator_plugin_ignore.mock_interfaces(function) assert_equal(expected, returned) end should "have nothing new for mock verify" do - assert_equal([], @cmock_generator_plugin_ignore.mock_verify("Ooze")) + assert(!@cmock_generator_plugin_ignore.respond_to?(:mock_verify)) end should "have nothing new for mock destroy" do - assert_equal([], @cmock_generator_plugin_ignore.mock_destroy("Ooze", [], "void")) + assert(!@cmock_generator_plugin_ignore.respond_to?(:mock_destroy)) end end diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index 4aebcba..3645aff 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -17,26 +17,23 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase end should "set up an empty call list if no arguments passed" do - args = [] - + function = {:args => []} expected = "" - returned = @cmock_generator_utils.create_call_list(args) + returned = @cmock_generator_utils.create_call_list(function) assert_equal(expected, returned) end should "set up a single call list if one arguments passed" do - args = [{ :type => "const char*", :name => "spoon"}] - + function = {:args => [{ :type => "const char*", :name => "spoon"}]} expected = "spoon" - returned = @cmock_generator_utils.create_call_list(args) + returned = @cmock_generator_utils.create_call_list(function) assert_equal(expected, returned) end should "set up a call list if multiple arguments passed" do - args = [{ :type => "const char*", :name => "spoon"}, { :type => "int", :name => "fork"}, { :type => "unsigned int", :name => "knife"}] - + function = {:args => [{ :type => "const char*", :name => "spoon"}, { :type => "int", :name => "fork"}, { :type => "unsigned int", :name => "knife"}] } expected = "spoon, fork, knife" - returned = @cmock_generator_utils.create_call_list(args) + returned = @cmock_generator_utils.create_call_list(function) assert_equal(expected, returned) end @@ -72,10 +69,8 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase end should "make handle return" do - func_name = "Spatula" - func_rettype = "uint64" + function = { :name => "Spatula", :rettype => "uint64"} indent = "[tab]" - expected = ["\n", "[tab]if(Mock.Spatula_Return != Mock.Spatula_Return_HeadTail)\n", "[tab]{\n", @@ -88,12 +83,12 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase "[tab] return *Mock.Spatula_Return_Head;\n", "[tab]}\n" ] - returned = @cmock_generator_utils.make_handle_return(func_name, func_rettype, indent) + returned = @cmock_generator_utils.make_handle_return(function, indent) assert_equal(expected, returned) end should "add new expected handler" do - func_name = "PizzaCutter" + function = { :name => "PizzaCutter", :rettype => "uint64"} var_type = "uint16" var_name = "Spork" @@ -122,12 +117,12 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase " Mock.PizzaCutter_Expected_Spork = Mock.PizzaCutter_Expected_Spork_Head;\n", " Mock.PizzaCutter_Expected_Spork += Mock.PizzaCutter_CallCount;\n" ] - returned = @cmock_generator_utils.make_add_new_expected(func_name, var_type, var_name) + returned = @cmock_generator_utils.make_add_new_expected(function, var_type, var_name) assert_equal(expected, returned) end should "make handle expected for non character strings" do - func_name = "CanOpener" + function = { :name => "CanOpener", :rettype => "uint64"} var_type = "uint16" var_name = "CorkScrew" @@ -139,12 +134,12 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase " TEST_ASSERT_EQUAL_MESSAGE(*p_expected, CorkScrew, \"Function 'CanOpener' called with unexpected value for parameter 'CorkScrew'.\");\n", " }\n" ] - returned = @cmock_generator_utils.make_handle_expected(func_name, var_type, var_name) + returned = @cmock_generator_utils.make_handle_expected(function, var_type, var_name) assert_equal(expected, returned) end should "make handle expected for character strings" do - func_name = "MeasureCup" + function = { :name => "MeasureCup", :rettype => "uint64"} var_type = "const char*" var_name = "TeaSpoon" @@ -156,7 +151,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase " TEST_ASSERT_EQUAL_STRING_MESSAGE(*p_expected, TeaSpoon, \"Function 'MeasureCup' called with unexpected string for parameter 'TeaSpoon'.\");\n", " }\n" ] - returned = @cmock_generator_utils.make_handle_expected(func_name, var_type, var_name) + returned = @cmock_generator_utils.make_handle_expected(function, var_type, var_name) 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 81f04e0..769671a 100644 --- a/test/unit/cmock_plugin_manager_test.rb +++ b/test/unit/cmock_plugin_manager_test.rb @@ -3,22 +3,20 @@ require File.expand_path(File.dirname(__FILE__)) + "/../../lib/cmock_plugin_mana class CMockPluginManagerTest < Test::Unit::TestCase def setup - create_mocks :config, :utils - @cmock_plugins = CMockPluginManager.new(@config, @utils) + create_mocks :config, :utils, :pluginA, :pluginB + @config.stubs!(:respond_to?).returns(true) end def teardown end - should "have set up internal accessors correctly on init" do - assert_equal(@config, @cmock_plugins.config) - assert_equal(@utils, @cmock_plugins.utils) - end - should "return all plugins by default" do @config.stubs!(:tab).returns(" ") @config.expect.plugins.returns(['cexception','ignore']) - test_plugins = @cmock_plugins.get_generator_plugins + + @cmock_plugins = CMockPluginManager.new(@config, @utils) + + test_plugins = @cmock_plugins.plugins contained = { :expect => false, :ignore => false, :cexception => false } test_plugins.each do |plugin| contained[:expect] = true if plugin.instance_of?(CMockGeneratorPluginExpect) @@ -33,7 +31,10 @@ class CMockPluginManagerTest < Test::Unit::TestCase should "return restricted plugins based on config" do @config.stubs!(:tab).returns(" ") @config.expect.plugins.returns([]) - test_plugins = @cmock_plugins.get_generator_plugins + + @cmock_plugins = CMockPluginManager.new(@config, @utils) + + test_plugins = @cmock_plugins.plugins contained = { :expect => false, :ignore => false, :cexception => false } test_plugins.each do |plugin| contained[:expect] = true if plugin.instance_of?(CMockGeneratorPluginExpect) @@ -45,4 +46,31 @@ class CMockPluginManagerTest < Test::Unit::TestCase assert_equal(false,contained[:cexception]) end + should "run a desired method over each plugin requested and return the results" do + @config.stubs!(:tab).returns(" ") + @config.expect.plugins.returns([]) + @cmock_plugins = CMockPluginManager.new(@config, @utils) + + @cmock_plugins.plugins = [@pluginA, @pluginB] + @pluginA.stubs!(:test_method).returns("This Is An Awesome Test") + @pluginB.stubs!(:test_method).returns(["And This is Part 2","Of An Awesome Test"]) + + expected = ["This Is An Awesome Test","And This is Part 2","Of An Awesome Test"] + output = @cmock_plugins.run(:test_method) + assert_equal(expected, output.flatten) + 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([]) + @cmock_plugins = CMockPluginManager.new(@config, @utils) + + @cmock_plugins.plugins = [@pluginA, @pluginB] + @pluginA.stubs!(:test_method).returns("This Is An Awesome Test") + @pluginB.stubs!(:test_method).returns(["And This is Part 2","Of An Awesome Test"]) + + expected = ["This Is An Awesome Test","And This is Part 2","Of An Awesome Test"] + output = @cmock_plugins.run(:test_method, "chickenpotpie") + assert_equal(expected, output.flatten) + end end