From 9bb250ea4a6a0bbe9561679eafe59a84b2a138e6 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Fri, 5 Feb 2021 15:15:14 -0500 Subject: [PATCH] Update so only config is shared. Everything else about the current job is passed. --- lib/cmock_file_writer.rb | 5 +- lib/cmock_generator.rb | 153 ++++++++++++++----------- lib/cmock_header_parser.rb | 57 ++++----- test/unit/cmock_generator_main_test.rb | 87 +++++++------- test/unit/cmock_header_parser_test.rb | 86 +++++++------- 5 files changed, 210 insertions(+), 178 deletions(-) diff --git a/lib/cmock_file_writer.rb b/lib/cmock_file_writer.rb index 787d688..f30c44b 100644 --- a/lib/cmock_file_writer.rb +++ b/lib/cmock_file_writer.rb @@ -41,8 +41,7 @@ class CMockFileWriter def update_file(dest, src) require 'fileutils' - FileUtils.rm(dest) if File.exist?(dest) - FileUtils.cp(src, dest) - FileUtils.rm(src) + FileUtils.rm(dest, :force => true) + FileUtils.mv(src, dest) end end diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index fdc29b1..34542d6 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -20,9 +20,7 @@ class CMockGenerator @framework = @config.framework.to_s @fail_on_unexpected_calls = @config.fail_on_unexpected_calls @exclude_setjmp_h = @config.exclude_setjmp_h - @subdir = @config.subdir - @folder = nil @includes_h_pre_orig_header = (@config.includes || @config.includes_h_pre_orig_header || []).map { |h| h =~ / module_name, + :module_ext => (module_ext || '.h'), + :mock_name => mock_name, + :clean_name => TypeSanitizer.sanitize_c_identifier(mock_name), + :folder => mock_folder, + :parsed_stuff => parsed_stuff, + :skeleton => false + } - create_mock_header_file(parsed_stuff) - create_mock_source_file(parsed_stuff) + create_mock_subdir(mock_project) + create_mock_header_file(mock_project) + create_mock_source_file(mock_project) end def create_skeleton(module_name, parsed_stuff) - @module_name = module_name - create_skeleton_source_file(parsed_stuff) + + mock_project = { + :module_name => module_name, + :module_ext => '.h', + :parsed_stuff => parsed_stuff, + :skeleton => true + } + + create_skeleton_source_file(mock_project) end private if $ThisIsOnlyATest.nil? ############################## - def create_mock_subdir - @file_writer.create_subdir(@folder) + def create_mock_subdir(mock_project) + @file_writer.create_subdir( mock_project[:folder] ) end def create_using_statement(file, function) file << "using namespace #{function[:namespace].join('::')};\n" unless function[:namespace].empty? end - def create_mock_header_file(parsed_stuff) + def create_mock_header_file(mock_project) if @include_inline == :include - @file_writer.create_file(@module_name + (@module_ext || '.h'), @folder) do |file, _filename| - file << parsed_stuff[:normalized_source] + @file_writer.create_file(mock_project[:module_name] + (mock_project[:module_ext]), mock_project[:folder]) do |file, _filename| + file << mock_project[:parsed_stuff][:normalized_source] end end - @file_writer.create_file(@mock_name + (@module_ext || '.h'), @folder) do |file, filename| - create_mock_header_header(file, filename) - create_mock_header_service_call_declarations(file) - create_typedefs(file, parsed_stuff[:typedefs]) - parsed_stuff[:functions].each do |function| + @file_writer.create_file(mock_project[:mock_name] + mock_project[:module_ext], mock_project[:folder]) do |file, filename| + create_mock_header_header(file, filename, mock_project) + create_mock_header_service_call_declarations(file, mock_project) + create_typedefs(file, mock_project) + mock_project[:parsed_stuff][:functions].each do |function| create_using_statement(file, function) file << @plugins.run(:mock_function_declarations, function) end @@ -102,35 +118,37 @@ class CMockGenerator end end - def create_mock_source_file(parsed_stuff) - @file_writer.create_file(@mock_name + '.c', @folder) do |file, filename| - create_source_header_section(file, filename, parsed_stuff[:functions]) - create_instance_structure(file, parsed_stuff[:functions]) + def create_mock_source_file(mock_project) + @file_writer.create_file(mock_project[:mock_name] + '.c', mock_project[:folder]) do |file, filename| + create_source_header_section(file, filename, mock_project) + create_instance_structure(file, mock_project) create_extern_declarations(file) - create_mock_verify_function(file, parsed_stuff[:functions]) - create_mock_init_function(file) - create_mock_destroy_function(file, parsed_stuff[:functions]) - parsed_stuff[:functions].each do |function| + create_mock_verify_function(file, mock_project) + create_mock_init_function(file, mock_project) + create_mock_destroy_function(file, mock_project) + mock_project[:parsed_stuff][:functions].each do |function| create_mock_implementation(file, function) create_mock_interfaces(file, function) end end end - def create_skeleton_source_file(parsed_stuff) - filename = "#{@config.mock_path}/#{@subdir + '/' if @subdir}#{module_name}.c" + def create_skeleton_source_file(mock_project) + filename = "#{@config.mock_path}/#{@subdir + '/' if @subdir}#{mock_project[:module_name]}.c" existing = File.exist?(filename) ? File.read(filename) : '' - @file_writer.append_file(@module_name + '.c', @subdir) do |file, fullname| - create_source_header_section(file, fullname, []) if existing.empty? - parsed_stuff[:functions].each do |function| + @file_writer.append_file(mock_project[:module_name] + '.c', @subdir) do |file, fullname| + blank_project = mock_project.clone + blank_project[:parsed_stuff] = { :functions => [] } + create_source_header_section(file, fullname, blank_project) if existing.empty? + mock_project[:parsed_stuff][:functions].each do |function| create_function_skeleton(file, function, existing) end end end - def create_mock_header_header(file, _filename) - define_name = @clean_mock_name.upcase - orig_filename = (@folder || '') + @module_name + (@module_ext || '.h') + def create_mock_header_header(file, _filename, mock_project) + define_name = mock_project[:clean_name].upcase + orig_filename = (mock_project[:folder] || '') + mock_project[:module_name] + mock_project[:module_ext] file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" file << "#ifndef _#{define_name}_H\n" file << "#define _#{define_name}_H\n\n" @@ -155,16 +173,16 @@ class CMockGenerator file << "\n" end - def create_typedefs(file, typedefs) + def create_typedefs(file, mock_project) file << "\n" - typedefs.each { |typedef| file << "#{typedef}\n" } + mock_project[:parsed_stuff][:typedefs].each { |typedef| file << "#{typedef}\n" } file << "\n\n" end - def create_mock_header_service_call_declarations(file) - file << "void #{@clean_mock_name}_Init(void);\n" - file << "void #{@clean_mock_name}_Destroy(void);\n" - file << "void #{@clean_mock_name}_Verify(void);\n\n" + def create_mock_header_service_call_declarations(file, mock_project) + file << "void #{mock_project[:clean_name]}_Init(void);\n" + file << "void #{mock_project[:clean_name]}_Destroy(void);\n" + file << "void #{mock_project[:clean_name]}_Verify(void);\n\n" end def create_mock_header_footer(header) @@ -178,9 +196,9 @@ class CMockGenerator header << "#endif\n" end - def create_source_header_section(file, filename, functions) - header_file = (@folder || '') + filename.gsub('.c', (@module_ext || '.h')) - file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" unless functions.empty? + def create_source_header_section(file, filename, mock_project) + header_file = (mock_project[:folder] || '') + filename.gsub('.c', mock_project[:module_ext]) + file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" unless mock_project[:parsed_stuff][:functions].empty? file << "#include \n" file << "#include \n" unless @exclude_setjmp_h @@ -192,7 +210,7 @@ class CMockGenerator @includes_c_post_header.each { |inc| file << "#include #{inc}\n" } file << "\n" strs = [] - functions.each do |func| + mock_project[:parsed_stuff][:functions].each do |func| strs << func[:name] func[:args].each { |arg| strs << arg[:name] } end @@ -202,14 +220,15 @@ class CMockGenerator file << "\n" end - def create_instance_structure(file, functions) + def create_instance_structure(file, mock_project) + functions = mock_project[:parsed_stuff][:functions] functions.each do |function| file << "typedef struct _CMOCK_#{function[:name]}_CALL_INSTANCE\n{\n" file << " UNITY_LINE_TYPE LineNumber;\n" file << @plugins.run(:instance_typedefs, function) file << "\n} CMOCK_#{function[:name]}_CALL_INSTANCE;\n\n" end - file << "static struct #{@clean_mock_name}Instance\n{\n" + file << "static struct #{mock_project[:clean_name]}Instance\n{\n" if functions.empty? file << " unsigned char placeHolder;\n" end @@ -231,9 +250,9 @@ class CMockGenerator file << "\n" end - def create_mock_verify_function(file, functions) - file << "void #{@clean_mock_name}_Verify(void)\n{\n" - verifications = functions.collect do |function| + def create_mock_verify_function(file, mock_project) + file << "void #{mock_project[:clean_name]}_Verify(void)\n{\n" + verifications = mock_project[:parsed_stuff][:functions].collect do |function| v = @plugins.run(:mock_verify, function) v.empty? ? v : [" call_instance = Mock.#{function[:name]}_CallInstance;\n", v] end.join @@ -245,20 +264,20 @@ class CMockGenerator file << "}\n\n" end - def create_mock_init_function(file) - file << "void #{@clean_mock_name}_Init(void)\n{\n" - file << " #{@clean_mock_name}_Destroy();\n" + def create_mock_init_function(file, mock_project) + file << "void #{mock_project[:clean_name]}_Init(void)\n{\n" + file << " #{mock_project[:clean_name]}_Destroy();\n" file << "}\n\n" end - def create_mock_destroy_function(file, functions) - file << "void #{@clean_mock_name}_Destroy(void)\n{\n" + def create_mock_destroy_function(file, mock_project) + file << "void #{mock_project[:clean_name]}_Destroy(void)\n{\n" file << " CMock_Guts_MemFreeAll();\n" file << " memset(&Mock, 0, sizeof(Mock));\n" - file << functions.collect { |function| @plugins.run(:mock_destroy, function) }.join + file << mock_project[:parsed_stuff][:functions].collect { |function| @plugins.run(:mock_destroy, function) }.join unless @fail_on_unexpected_calls - file << functions.collect { |function| @plugins.run(:mock_ignore, function) }.join + file << mock_project[:parsed_stuff][:functions].collect { |function| @plugins.run(:mock_ignore, function) }.join end if @ordered diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index ec08eef..7ea3b26 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -8,7 +8,6 @@ class CMockHeaderParser attr_accessor :funcs, :c_attr_noconst, :c_attributes, :treat_as_void, :treat_externs, :treat_inlines, :inline_function_patterns def initialize(cfg) - @funcs = [] @c_strippables = cfg.strippables @c_attr_noconst = cfg.attributes.uniq - ['const'] @c_attributes = ['const'] + c_attr_noconst @@ -31,32 +30,36 @@ class CMockHeaderParser end def parse(name, source) - @module_name = name.gsub(/\W/, '') - @typedefs = [] - @funcs = [] - @normalized_source = nil + + parse_project = { + :module_name => name.gsub(/\W/, ''), + :typedefs => [], + :functions => [], + :normalized_source => nil + } + function_names = [] - all_funcs = parse_functions(import_source(source)).map { |item| [item] } - all_funcs += parse_cpp_functions(import_source(source, true)) + all_funcs = parse_functions(import_source(source, parse_project)).map { |item| [item] } + all_funcs += parse_cpp_functions(import_source(source, parse_project, true)) all_funcs.map do |decl| - func = parse_declaration(*decl) + func = parse_declaration(parse_project, *decl) unless function_names.include? func[:name] - @funcs << func + parse_project[:functions] << func function_names << func[:name] end end - @normalized_source = if @treat_inlines == :include - transform_inline_functions(source) - else - '' - end + parse_project[:normalized_source] = if @treat_inlines == :include + transform_inline_functions(source) + else + '' + end { :includes => nil, - :functions => @funcs, - :typedefs => @typedefs, - :normalized_source => @normalized_source } + :functions => parse_project[:functions], + :typedefs => parse_project[:typedefs], + :normalized_source => parse_project[:normalized_source] } end private if $ThisIsOnlyATest.nil? ################ @@ -211,7 +214,7 @@ class CMockHeaderParser source end - def import_source(source, cpp = false) + def import_source(source, parse_project, cpp = false) # let's clean up the encoding in case they've done anything weird with the characters we might find source = source.force_encoding('ISO-8859-1').encode('utf-8', :replace => nil) @@ -264,9 +267,9 @@ class CMockHeaderParser # scan for functions which return function pointers, because they are a pain source.gsub!(/([\w\s\*]+)\(*\(\s*\*([\w\s\*]+)\s*\(([\w\s\*,]*)\)\)\s*\(([\w\s\*,]*)\)\)*/) do |_m| - functype = "cmock_#{@module_name}_func_ptr#{@typedefs.size + 1}" + functype = "cmock_#{parse_project[:module_name]}_func_ptr#{parse_project[:typedefs].size + 1}" unless cpp # only collect once - @typedefs << "typedef #{Regexp.last_match(1).strip}(*#{functype})(#{Regexp.last_match(4)});" + parse_project[:typedefs] << "typedef #{Regexp.last_match(1).strip}(*#{functype})(#{Regexp.last_match(4)});" "#{functype} #{Regexp.last_match(2).strip}(#{Regexp.last_match(3)});" end end @@ -469,7 +472,7 @@ class CMockHeaderParser divination end - def clean_args(arg_list) + def clean_args(arg_list, parse_project) if @local_as_void.include?(arg_list.strip) || arg_list.empty? 'void' else @@ -483,7 +486,7 @@ class CMockHeaderParser # scan argument list for function pointers and replace them with custom types arg_list.gsub!(/([\w\s\*]+)\(+\s*\*[\*\s]*([\w\s]*)\s*\)+\s*\(((?:[\w\s\*]*,?)*)\s*\)*/) do |_m| - functype = "cmock_#{@module_name}_func_ptr#{@typedefs.size + 1}" + functype = "cmock_#{parse_project[:module_name]}_func_ptr#{parse_project[:typedefs].size + 1}" funcret = Regexp.last_match(1).strip funcname = Regexp.last_match(2).strip funcargs = Regexp.last_match(3).strip @@ -492,14 +495,14 @@ class CMockHeaderParser funcname.gsub!('const', '').strip! funconst = 'const ' end - @typedefs << "typedef #{funcret}(*#{functype})(#{funcargs});" + parse_project[:typedefs] << "typedef #{funcret}(*#{functype})(#{funcargs});" funcname = "cmock_arg#{c += 1}" if funcname.empty? "#{functype} #{funconst}#{funcname}" end # scan argument list for function pointers with shorthand notation and replace them with custom types arg_list.gsub!(/([\w\s\*]+)+\s+(\w+)\s*\(((?:[\w\s\*]*,?)*)\s*\)*/) do |_m| - functype = "cmock_#{@module_name}_func_ptr#{@typedefs.size + 1}" + functype = "cmock_#{parse_project[:module_name]}_func_ptr#{parse_project[:typedefs].size + 1}" funcret = Regexp.last_match(1).strip funcname = Regexp.last_match(2).strip funcargs = Regexp.last_match(3).strip @@ -508,7 +511,7 @@ class CMockHeaderParser funcname.gsub!('const', '').strip! funconst = 'const ' end - @typedefs << "typedef #{funcret}(*#{functype})(#{funcargs});" + parse_project[:typedefs] << "typedef #{funcret}(*#{functype})(#{funcargs});" funcname = "cmock_arg#{c += 1}" if funcname.empty? "#{functype} #{funconst}#{funcname}" end @@ -525,7 +528,7 @@ class CMockHeaderParser end end - def parse_declaration(declaration, namespace = [], classname = nil) + def parse_declaration(parse_project, declaration, namespace = [], classname = nil) decl = {} decl[:namespace] = namespace decl[:class] = classname @@ -581,7 +584,7 @@ class CMockHeaderParser else decl[:var_arg] = nil end - args = clean_args(args) + args = clean_args(args, parse_project) decl[:args_string] = args decl[:args] = parse_args(args) decl[:args_call] = decl[:args].map { |a| a[:name] }.join(', ') diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index ca72fe4..bb568d4 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -78,20 +78,26 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @config.expect :treat_inlines, :exclude @config.expect :exclude_setjmp_h, false @cmock_generator_strict = CMockGenerator.new(@config, @file_writer, @utils, @plugins) - @cmock_generator_strict.module_name = @module_name - @cmock_generator_strict.module_ext = '.h' - @cmock_generator_strict.mock_name = "Mock#{@module_name}" - @cmock_generator_strict.clean_mock_name = "Mock#{@module_name}" + + @test_project = { + :module_name => @module_name, + :module_ext => '.h', + :mock_name => "Mock#{@module_name}", + :clean_name => "Mock#{@module_name}", + :folder => nil, + :parsed_stuff => {}, + :skeleton => false + } end after do end - def helper_create_header_top_with_opt_incldues_form_config_and_plugin(ext) + def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext) @config.expect :mock_prefix, "Mock" @config.expect :mock_suffix, "" @config.expect :weak, "" - @cmock_generator.module_ext = ext + orig_filename = "PoutPoutFish#{ext}" define_name = "MOCKPOUTPOUTFISH_H" output = [] @@ -122,41 +128,26 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @config.expect :orig_header_include_fmt, "#include \"%s\"" @plugins.expect :run, "#include \"PluginRequiredHeader.h\"\n", [:include_files] - @cmock_generator.create_mock_header_header(output, "Mock#{orig_filename}") + # Update the extention for this test + test_project = @test_project.clone + test_project[:module_ext] = ext + + @cmock_generator.create_mock_header_header(output, "Mock#{orig_filename}", test_project) assert_equal(expected, output) end it "create the top of a header file with optional include files from config and include file from plugin" do ['.h','.hh','.hpp'].each do |ext| - helper_create_header_top_with_opt_incldues_form_config_and_plugin(ext) + helper_create_header_top_with_opt_includes_form_config_and_plugin(ext) end end it "handle dashes and spaces in the module name" do - #no strict handling - @config.expect :mock_prefix, "Mock" - @config.expect :mock_suffix, "" - @config.expect :weak, "" - @config.expect :enforce_strict_ordering, nil - @config.expect :framework, :unity - @config.expect :includes, ["ConfigRequiredHeader1.h","ConfigRequiredHeader2.h"] - @config.expect :includes_h_post_orig_header, nil - @config.expect :includes_c_pre_header, nil - @config.expect :includes_c_post_header, nil - @config.expect :subdir, nil - @config.expect :fail_on_unexpected_calls, true - @config.expect :treat_inlines, :exclude - @config.expect :exclude_setjmp_h, false - @cmock_generator2 = CMockGenerator.new(@config, @file_writer, @utils, @plugins) - @cmock_generator2.module_name = "Pout-Pout Fish" - @cmock_generator2.module_ext = '.h' - @cmock_generator2.mock_name = "MockPout-Pout Fish" - @cmock_generator2.clean_mock_name = "MockPout_Pout_Fish" - @config.expect :mock_prefix, "Mock" @config.expect :mock_suffix, "" @config.expect :weak, "" + orig_filename = "Pout-Pout Fish.h" define_name = "MOCKPOUT_POUT_FISH_H" output = [] @@ -187,7 +178,13 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @config.expect :orig_header_include_fmt, "#include \"%s\"" @plugins.expect :run, "#include \"PluginRequiredHeader.h\"\n", [:include_files] - @cmock_generator2.create_mock_header_header(output, "MockPout-Pout Fish.h") + # Create a project with some added challenges + test_project = @test_project.clone + test_project[:module_name] = "Pout-Pout Fish" + test_project[:mock_name] = "MockPout-Pout Fish" + test_project[:clean_name] = "MockPout_Pout_Fish" + + @cmock_generator.create_mock_header_header(output, "MockPout-Pout Fish.h", test_project) assert_equal(expected, output) end @@ -225,7 +222,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @config.expect :orig_header_include_fmt, "#include \"%s\"" @plugins.expect :run, '', [:include_files] - @cmock_generator.create_mock_header_header(output, "MockPoutPoutFish.h") + @cmock_generator.create_mock_header_header(output, "MockPoutPoutFish.h", @test_project) assert_equal(expected, output) end @@ -264,7 +261,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @config.expect :orig_header_include_fmt, "#include \"%s\"" @plugins.expect :run, "#include \"PluginRequiredHeader.h\"\n", [:include_files] - @cmock_generator.create_mock_header_header(output, "MockPoutPoutFish.h") + @cmock_generator.create_mock_header_header(output, "MockPoutPoutFish.h", @test_project) assert_equal(expected, output) end @@ -282,7 +279,8 @@ describe CMockGenerator, "Verify CMockGenerator Module" do "\n\n" ] - @cmock_generator.create_typedefs(output, typedefs) + @test_project[:parsed_stuff][:typedefs] = typedefs + @cmock_generator.create_typedefs(output, @test_project) assert_equal(expected, output.flatten) end @@ -296,7 +294,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do "void #{mock_name}_Verify(void);\n\n" ] - @cmock_generator.create_mock_header_service_call_declarations(output) + @cmock_generator.create_mock_header_service_call_declarations(output, @test_project) assert_equal(expected, output) end @@ -340,7 +338,8 @@ describe CMockGenerator, "Verify CMockGenerator Module" do "\n" ] - @cmock_generator.create_source_header_section(output, "MockPoutPoutFish.c", functions) + @test_project[:parsed_stuff][:functions] = functions + @cmock_generator.create_source_header_section(output, "MockPoutPoutFish.c", @test_project) assert_equal(expected, output) end @@ -354,7 +353,8 @@ describe CMockGenerator, "Verify CMockGenerator Module" do "} Mock;\n\n" ].join - @cmock_generator.create_instance_structure(output, functions) + @test_project[:parsed_stuff][:functions] = functions + @cmock_generator.create_instance_structure(output, @test_project) assert_equal(expected, output.join) end @@ -384,7 +384,8 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @plugins.expect :run, [" d1"], [:instance_structure, functions[0]] @plugins.expect :run, [" e1"," e2"," e3"], [:instance_structure, functions[1]] - @cmock_generator.create_instance_structure(output, functions) + @test_project[:parsed_stuff][:functions] = functions + @cmock_generator.create_instance_structure(output, @test_project) assert_equal(expected, output.join) end @@ -416,7 +417,8 @@ describe CMockGenerator, "Verify CMockGenerator Module" do output = [] expected = "void MockPoutPoutFish_Verify(void)\n{\n}\n\n" - @cmock_generator.create_mock_verify_function(output, functions) + @test_project[:parsed_stuff][:functions] = functions + @cmock_generator.create_mock_verify_function(output, @test_project) assert_equal(expected, output.join) end @@ -441,7 +443,8 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @plugins.expect :run, [" Uno_Second"," Dos_Second"], [:mock_verify, functions[1]] @cmock_generator.ordered = true - @cmock_generator.create_mock_verify_function(output, functions) + @test_project[:parsed_stuff][:functions] = functions + @cmock_generator.create_mock_verify_function(output, @test_project) assert_equal(expected, output.flatten) end @@ -453,7 +456,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do "}\n\n" ] - @cmock_generator.create_mock_init_function(output) + @cmock_generator.create_mock_init_function(output, @test_project) assert_equal(expected.join, output.join) end @@ -467,7 +470,8 @@ describe CMockGenerator, "Verify CMockGenerator Module" do "}\n\n" ] - @cmock_generator.create_mock_destroy_function(output, functions) + @test_project[:parsed_stuff][:functions] = functions + @cmock_generator.create_mock_destroy_function(output, @test_project) assert_equal(expected.join, output.join) end @@ -488,7 +492,8 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @plugins.expect :run, [], [:mock_destroy, functions[0]] @plugins.expect :run, [" uno"], [:mock_destroy, functions[1]] - @cmock_generator_strict.create_mock_destroy_function(output, functions) + @test_project[:parsed_stuff][:functions] = functions + @cmock_generator_strict.create_mock_destroy_function(output, @test_project) assert_equal(expected.join, output.join) end diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index cb535ef..bee6a24 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -13,7 +13,6 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do before do create_mocks :config - @test_name = 'test_file.h' @config.expect :strippables, ["STRIPPABLE"] @config.expect :attributes, ['__ramfunc', 'funky_attrib', 'SQLITE_API'] @config.expect :c_calling_conventions, ['__stdcall'] @@ -29,13 +28,20 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @config.expect :array_size_name, 'size|len' @parser = CMockHeaderParser.new(@config) + + @test_project = { + :module_name => 'test_file.h', + :typedefs => [], + :functions => [], + :normalized_source => nil + } end after do end it "create and initialize variables to defaults appropriately" do - assert_equal([], @parser.funcs) + assert_equal(nil, @parser.funcs) assert_equal(['const', '__ramfunc', 'funky_attrib', 'SQLITE_API'], @parser.c_attributes) assert_equal(['void','MY_FUNKY_VOID'], @parser.treat_as_void) end @@ -52,7 +58,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "who" ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "remove block comments" do @@ -85,7 +91,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "shown_because_line_above_ended_comment_this_time" ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "remove strippables from the beginning or end of function declarations" do @@ -105,7 +111,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "void universal_handler()" ] - assert_equal(expected, @parser.import_source(source)) + assert_equal(expected, @parser.import_source(source, @test_project)) end it "remove gcc's function __attribute__'s" do @@ -125,7 +131,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "void universal_handler()" ] - assert_equal(expected, @parser.import_source(source)) + assert_equal(expected, @parser.import_source(source, @test_project)) end it "remove preprocessor directives" do @@ -136,7 +142,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do expected = [] - assert_equal(expected, @parser.import_source(source)) + assert_equal(expected, @parser.import_source(source, @test_project)) end @@ -151,7 +157,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do expected = ["foo"] - assert_equal(expected, @parser.import_source(source)) + assert_equal(expected, @parser.import_source(source, @test_project)) end @@ -165,7 +171,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "hoo hah when" ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end @@ -178,7 +184,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do expected = ["but I'm here"] - assert_equal(expected, @parser.import_source(source)) + assert_equal(expected, @parser.import_source(source, @test_project)) end @@ -212,7 +218,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "this should remain!" ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end @@ -230,7 +236,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "} Thinger;\n" + "or me!!\n" - assert_equal(["don't delete me!! or me!!"], @parser.import_source(source).map!{|s|s.strip}) + assert_equal(["don't delete me!! or me!!"], @parser.import_source(source, @test_project).map!{|s|s.strip}) end @@ -248,7 +254,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "} Whatever;\n" + "me too!!\n" - assert_equal(["I want to live!! me too!!"], @parser.import_source(source).map!{|s|s.strip}) + assert_equal(["I want to live!! me too!!"], @parser.import_source(source, @test_project).map!{|s|s.strip}) end @@ -271,7 +277,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "I want to live!!\n" assert_equal(["void foo(void)", "struct THINGER foo(void)", "I want to live!!"], - @parser.import_source(source).map!{|s|s.strip}) + @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "remove externed and inline functions" do @@ -290,7 +296,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "uint32 funcinline(unsigned int)" ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "remove function definitions but keep function declarations" do @@ -312,7 +318,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "uint32 func_with_decl_b", #okay. it's not going to be interpretted as another function ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "remove function definitions with nested braces but keep function declarations" do @@ -354,7 +360,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "uint32 func_with_decl_c", #okay. it's not going to be interpretted as another function ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "remove a fully defined inline function" do @@ -371,7 +377,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal([], @parser.funcs) + assert_equal(nil, @parser.funcs) # verify exception message begin @@ -395,7 +401,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal([], @parser.funcs) + assert_equal(nil, @parser.funcs) # verify exception message begin @@ -423,7 +429,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal([], @parser.funcs) + assert_equal(nil, @parser.funcs) # verify exception message begin @@ -452,7 +458,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do ] @parser.treat_externs = :include - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "leave inline functions if inline to be included" do @@ -478,7 +484,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do ] @parser.treat_inlines = :include - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "leave inline and extern functions if inline and extern to be included" do @@ -507,7 +513,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.treat_externs = :include @parser.treat_inlines = :include - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "Include inline functions that contain user defined inline function formats" do @@ -538,7 +544,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.treat_inlines = :include @parser.inline_function_patterns = ['static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__', '\binline\b'] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end it "remove defines" do @@ -554,7 +560,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "void hello(void)", ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end @@ -567,7 +573,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "const int TheMatrix(int Trinity, unsigned int * Neo)", ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end @@ -593,7 +599,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do :args=>[{:type=>"int", :name=>"a", :ptr? => false, :const? => false, :const_ptr? => false}], :args_string=>"int a", :args_call=>"a"} - assert_equal(expected, @parser.parse_declaration(source)) + assert_equal(expected, @parser.parse_declaration(@test_project, source)) end it "handle odd case of typedef'd void as arg" do @@ -616,7 +622,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do :args=>[], :args_string=>"void", :args_call=>"" } - assert_equal(expected, @parser.parse_declaration(source)) + assert_equal(expected, @parser.parse_declaration(@test_project, source)) end it "handle odd case of typedef'd void as arg pointer" do @@ -639,7 +645,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do :args=>[{:type=>"MY_FUNKY_VOID*", :name=>"bluh", :ptr? => true, :const? => false, :const_ptr? => false}], :args_string=>"MY_FUNKY_VOID* bluh", :args_call=>"bluh" } - assert_equal(expected, @parser.parse_declaration(source)) + assert_equal(expected, @parser.parse_declaration(@test_project, source)) end @@ -652,7 +658,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "void Foo(int a, float b, char c, char* e)" ] - assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) end @@ -664,7 +670,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal([], @parser.funcs) + assert_equal(nil, @parser.funcs) # verify exception message begin @@ -693,7 +699,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal([], @parser.funcs) + assert_equal(nil, @parser.funcs) # verify exception message begin @@ -743,7 +749,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do ], :args_string=>"int a, unsigned int b", :args_call=>"a, b" } - assert_equal(expected, @parser.parse_declaration(source)) + assert_equal(expected, @parser.parse_declaration(@test_project, source)) end it "extract and return function declarations with no retval" do @@ -770,7 +776,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do ], :args_string=>"uint la, int de, bool da", :args_call=>"la, de, da" } - assert_equal(expected, @parser.parse_declaration(source)) + assert_equal(expected, @parser.parse_declaration(@test_project, source)) end it "extract and return function declarations with implied voids" do @@ -794,7 +800,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do :args=>[ ], :args_string=>"void", :args_call=>"" } - assert_equal(expected, @parser.parse_declaration(source)) + assert_equal(expected, @parser.parse_declaration(@test_project, source)) end it "extract modifiers properly" do @@ -820,7 +826,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do ], :args_string=>"int Trinity, unsigned int* Neo", :args_call=>"Trinity, Neo" } - assert_equal(expected, @parser.parse_declaration(source)) + assert_equal(expected, @parser.parse_declaration(@test_project, source)) end it "extract c calling conventions properly" do @@ -847,7 +853,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do ], :args_string=>"int Trinity, unsigned int* Neo", :args_call=>"Trinity, Neo" } - assert_equal(expected, @parser.parse_declaration(source)) + assert_equal(expected, @parser.parse_declaration(@test_project, source)) end it "extract and return function declarations inside namespace and class" do @@ -872,7 +878,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do ], :args_string=>"int a, unsigned int b", :args_call=>"a, b" } - assert_equal(expected, @parser.parse_declaration(source, ["ns1", "ns2"], "Bar")) + assert_equal(expected, @parser.parse_declaration(@test_project, source, ["ns1", "ns2"], "Bar")) end it "fully parse multiple prototypes" do @@ -2387,8 +2393,8 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "} }" ] - assert_equal(expected, @parser.import_source(source, cpp=true)) - refute_equal(expected, @parser.import_source(source)) + assert_equal(expected, @parser.import_source(source, @test_project, cpp=true)) + refute_equal(expected, @parser.import_source(source, @test_project)) end # only so parse_functions does not raise an error