diff --git a/lib/cmock_config.rb b/lib/cmock_config.rb index c196337..9a0f8c0 100644 --- a/lib/cmock_config.rb +++ b/lib/cmock_config.rb @@ -12,7 +12,6 @@ class CMockConfig :mock_path => 'mocks', :mock_prefix => 'Mock', :plugins => [], - :includes => [], :strippables => ['(?:__attribute__\s*\(+.*?\)+)'], :attributes => ['__ramfunc', '__irq', '__fiq', 'register', 'extern'], :enforce_strict_ordering => false, @@ -27,6 +26,11 @@ class CMockConfig :ignore => :args_and_calls, #the options being :args_and_calls or :args_only :callback_include_count => true, :callback_after_arg_check => false, + :includes => nil, + :includes_h_pre_orig_header => nil, + :includes_h_post_orig_header => nil, + :includes_c_pre_header => nil, + :includes_c_post_header => nil, } def initialize(options=nil) @@ -38,12 +42,18 @@ class CMockConfig end #do some quick type verification - [:plugins, :includes, :attributes, :treat_as_void].each do |opt| + [:plugins, :attributes, :treat_as_void].each do |opt| unless (options[opt].class == Array) options[opt] = [] puts "WARNING: :#{opt.to_s} should be an array." unless (options[:verbosity] < 1) end end + [:includes, :includes_h_pre_orig_header, :includes_h_post_orig_header, :includes_c_pre_header, :includes_c_post_header].each do |opt| + unless (options[opt].nil? or (options[opt].class == Array)) + options[opt] = [] + puts "WARNING: :#{opt.to_s} should be an array." unless (options[:verbosity] < 1) + end + end options[:plugins].compact! options[:plugins].map! {|p| p.to_sym} @options = options diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index f5ff369..5bef603 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -18,6 +18,11 @@ class CMockGenerator @prefix = @config.mock_prefix @ordered = @config.enforce_strict_ordering @framework = @config.framework.to_s + + @includes_h_pre_orig_header = @config.includes || @config.includes_h_pre_orig_header || [] + @includes_h_post_orig_header = @config.includes_h_post_orig_header || [] + @includes_c_pre_header = @config.includes_c_pre_header || [] + @includes_c_post_header = @config.includes_c_post_header || [] end def create_mock(module_name, parsed_stuff) @@ -62,9 +67,9 @@ class CMockGenerator file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" file << "#ifndef _#{define_name}\n" file << "#define _#{define_name}\n\n" - config_includes = @config.includes - config_includes.each {|inc| file << "#include \"#{inc}\"\n"} if (!config_includes.nil?) + @includes_h_pre_orig_header.each {|inc| file << "#include \"#{inc}\"\n"} file << "#include \"#{orig_filename}\"\n" + @includes_h_post_orig_header.each {|inc| file << "#include \"#{inc}\"\n"} plugin_includes = @plugins.run(:include_files) file << plugin_includes if (!plugin_includes.empty?) file << "\n" @@ -94,7 +99,10 @@ class CMockGenerator file << "#include \n" file << "#include \"#{@framework}.h\"\n" file << "#include \"cmock.h\"\n" - file << "#include \"#{header_file}\"\n\n" + @includes_c_pre_header.each {|inc| file << "#include \"#{inc}\"\n"} + file << "#include \"#{header_file}\"\n" + @includes_c_post_header.each {|inc| file << "#include \"#{inc}\"\n"} + file << "\n" end def create_instance_structure(file, functions) diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index f0c5907..7c45a11 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -13,7 +13,7 @@ class CMockHeaderParser @c_strippables = cfg.strippables @c_attributes = (['const'] + cfg.attributes).uniq @treat_as_void = (['void'] + cfg.treat_as_void).uniq - @declaration_parse_matcher = /([\d\w\s\*\(\),\[\]]+??)\(([\d\w\s\*\(\),\.\[\]]*)\)$/m + @declaration_parse_matcher = /([\d\w\s\*\(\),\[\]]+??)\(([\d\w\s\*\(\),\.\[\]+-]*)\)$/m @standards = (['int','short','char','long','unsigned','signed'] + cfg.treat_as.keys).uniq @when_no_prototypes = cfg.when_no_prototypes @local_as_void = @treat_as_void @@ -143,9 +143,9 @@ class CMockHeaderParser return 'void' else c=0 - arg_list.gsub!(/(\w+)(?:\s*\[[\s\d\w]*\])+/,'*\1') # magically turn brackets into asterisks - arg_list.gsub!(/\s+\*/,'*') # remove space to place asterisks with type (where they belong) - arg_list.gsub!(/\*(\w)/,'* \1') # pull asterisks away from arg to place asterisks with type (where they belong) + arg_list.gsub!(/(\w+)(?:\s*\[[\s\d\w+-]*\])+/,'*\1') # magically turn brackets into asterisks + arg_list.gsub!(/\s+\*/,'*') # remove space to place asterisks with type (where they belong) + arg_list.gsub!(/\*(\w)/,'* \1') # pull asterisks away from arg to place asterisks with type (where they belong) #scan argument list for function pointers and replace them with custom types arg_list.gsub!(/([\w\s]+)\(*\(\s*\*([\w\s\*]+)\)\s*\(([\w\s\*,]*)\)\)*/) do |m| diff --git a/test/system/test_interactions/array_and_pointer_handling.yml b/test/system/test_interactions/array_and_pointer_handling.yml index 492dc2c..dfa7d10 100644 --- a/test/system/test_interactions/array_and_pointer_handling.yml +++ b/test/system/test_interactions/array_and_pointer_handling.yml @@ -15,7 +15,7 @@ :mockable: | void foo(POINT_T* a); POINT_T* bar(void); - void fooa(POINT_T a[ARRAY_A_SIZE]); + void fooa(POINT_T a[ARRAY_A_SIZE+1-1]); void foos(const char * a); const char * bars(void); void no_pointers(int a, char* b); diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index 9191dd6..11a8846 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -44,6 +44,11 @@ class CMockGeneratorTest < Test::Unit::TestCase @config.expect.mock_prefix.returns("Mock") @config.expect.enforce_strict_ordering.returns(nil) @config.expect.framework.returns(:unity) + @config.expect.includes.returns(["ConfigRequiredHeader1.h","ConfigRequiredHeader2.h"]) + #@config.expect.includes_h_pre_orig_header.returns(nil) #not called because includes called + @config.expect.includes_h_post_orig_header.returns(nil) + @config.expect.includes_c_pre_header.returns(nil) + @config.expect.includes_c_post_header.returns(nil) @cmock_generator = CMockGenerator.new(@config, @file_writer, @utils, @plugins) @cmock_generator.module_name = @module_name @cmock_generator.mock_name = "Mock#{@module_name}" @@ -52,6 +57,11 @@ class CMockGeneratorTest < Test::Unit::TestCase @config.expect.mock_prefix.returns("Mock") @config.expect.enforce_strict_ordering.returns(true) @config.expect.framework.returns(:unity) + @config.expect.includes.returns(nil) + @config.expect.includes_h_pre_orig_header.returns(nil) + @config.expect.includes_h_post_orig_header.returns(nil) + @config.expect.includes_c_pre_header.returns(nil) + @config.expect.includes_c_post_header.returns(nil) @cmock_generator_strict = CMockGenerator.new(@config, @file_writer, @utils, @plugins) @cmock_generator_strict.module_name = @module_name @cmock_generator_strict.mock_name = "Mock#{@module_name}" @@ -83,7 +93,6 @@ class CMockGeneratorTest < Test::Unit::TestCase "\n" ] - @config.expect.includes.returns(["ConfigRequiredHeader1.h","ConfigRequiredHeader2.h"]) @plugins.expect.run(:include_files).returns("#include \"PluginRequiredHeader.h\"\n") @cmock_generator.create_mock_header_header(output, "MockPoutPoutFish.h") @@ -106,7 +115,6 @@ class CMockGeneratorTest < Test::Unit::TestCase "\n" ] - @config.expect.includes.returns(["ConfigRequiredHeader1.h","ConfigRequiredHeader2.h"]) @plugins.expect.run(:include_files).returns('') @cmock_generator.create_mock_header_header(output, "MockPoutPoutFish.h") @@ -123,12 +131,13 @@ class CMockGeneratorTest < Test::Unit::TestCase expected = [ "/* AUTOGENERATED FILE. DO NOT EDIT. */\n", "#ifndef _#{define_name}\n", "#define _#{define_name}\n\n", + "#include \"ConfigRequiredHeader1.h\"\n", + "#include \"ConfigRequiredHeader2.h\"\n", "#include \"#{orig_filename}\"\n", "#include \"PluginRequiredHeader.h\"\n", "\n" ] - @config.expect.includes.returns(nil) @plugins.expect.run(:include_files).returns("#include \"PluginRequiredHeader.h\"\n") @cmock_generator.create_mock_header_header(output, "MockPoutPoutFish.h") @@ -185,7 +194,8 @@ class CMockGeneratorTest < Test::Unit::TestCase "#include \n", "#include \"unity.h\"\n", "#include \"cmock.h\"\n", - "#include \"MockPoutPoutFish.h\"\n\n" + "#include \"MockPoutPoutFish.h\"\n", + "\n" ] @cmock_generator.create_source_header_section(output, "MockPoutPoutFish.c")