mirror of
https://github.com/ThrowTheSwitch/CMock.git
synced 2026-08-15 15:47:50 +00:00
Support alternative header file extension support (thanks @Tuc-an)
This commit is contained in:
+4
-3
@@ -41,13 +41,14 @@ class CMock
|
||||
private ###############################
|
||||
|
||||
def generate_mock(src)
|
||||
name = File.basename(src, '.h')
|
||||
name = File.basename(src, '.*')
|
||||
ext = File.extname(src)
|
||||
puts "Creating mock for #{name}..." unless @silent
|
||||
@cm_generator.create_mock(name, @cm_parser.parse(name, File.read(src)))
|
||||
@cm_generator.create_mock(name, @cm_parser.parse(name, File.read(src)), ext)
|
||||
end
|
||||
|
||||
def generate_skeleton(src)
|
||||
name = File.basename(src, '.h')
|
||||
name = File.basename(src, '.*')
|
||||
puts "Creating skeleton for #{name}..." unless @silent
|
||||
@cm_generator.create_skeleton(name, @cm_parser.parse(name, File.read(src)))
|
||||
end
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
class CMockGenerator
|
||||
|
||||
attr_accessor :config, :file_writer, :module_name, :clean_mock_name, :mock_name, :utils, :plugins, :weak, :ordered
|
||||
attr_accessor :config, :file_writer, :module_name, :module_ext, :clean_mock_name, :mock_name, :utils, :plugins, :weak, :ordered
|
||||
|
||||
def initialize(config, file_writer, utils, plugins)
|
||||
@file_writer = file_writer
|
||||
@@ -46,8 +46,9 @@ class CMockGenerator
|
||||
|
||||
end
|
||||
|
||||
def create_mock(module_name, parsed_stuff)
|
||||
def create_mock(module_name, parsed_stuff, module_ext = nil)
|
||||
@module_name = module_name
|
||||
@module_ext = module_ext || '.h'
|
||||
@mock_name = @prefix + @module_name + @suffix
|
||||
@clean_mock_name = TypeSanitizer.sanitize_c_identifier(@mock_name)
|
||||
create_mock_subdir()
|
||||
@@ -68,12 +69,12 @@ class CMockGenerator
|
||||
|
||||
def create_mock_header_file(parsed_stuff)
|
||||
if @include_inline == :include
|
||||
@file_writer.create_file(@module_name + ".h", @subdir) do |file, filename|
|
||||
@file_writer.create_file(@module_name + (@module_ext || '.h'), @subdir) do |file, filename|
|
||||
file << parsed_stuff[:normalized_source]
|
||||
end
|
||||
end
|
||||
|
||||
@file_writer.create_file(@mock_name + ".h", @subdir) do |file, filename|
|
||||
@file_writer.create_file(@mock_name + (@module_ext || '.h'), @subdir) do |file, filename|
|
||||
create_mock_header_header(file, filename)
|
||||
create_mock_header_service_call_declarations(file)
|
||||
create_typedefs(file, parsed_stuff[:typedefs])
|
||||
@@ -102,8 +103,8 @@ class CMockGenerator
|
||||
def create_skeleton_source_file(parsed_stuff)
|
||||
filename = "#{@config.mock_path}/#{@subdir+'/' if @subdir}#{module_name}.c"
|
||||
existing = File.exists?(filename) ? File.read(filename) : ""
|
||||
@file_writer.append_file(@module_name + ".c", @subdir) do |file, filename|
|
||||
create_source_header_section(file, filename, []) if existing.empty?
|
||||
@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|
|
||||
create_function_skeleton(file, function, existing)
|
||||
end
|
||||
@@ -112,7 +113,7 @@ class CMockGenerator
|
||||
|
||||
def create_mock_header_header(file, filename)
|
||||
define_name = @clean_mock_name.upcase
|
||||
orig_filename = (@subdir ? @subdir + "/" : "") + @module_name + ".h"
|
||||
orig_filename = (@subdir ? @subdir + "/" : "") + @module_name + (@module_ext || '.h')
|
||||
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n"
|
||||
file << "#ifndef _#{define_name}_H\n"
|
||||
file << "#define _#{define_name}_H\n\n"
|
||||
@@ -161,7 +162,7 @@ class CMockGenerator
|
||||
end
|
||||
|
||||
def create_source_header_section(file, filename, functions)
|
||||
header_file = (@subdir ? @subdir + '/' : '') + filename.gsub(".c",".h")
|
||||
header_file = (@subdir ? @subdir + '/' : '') + filename.gsub(".c",(@module_ext || '.h'))
|
||||
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" unless functions.empty?
|
||||
file << "#include <string.h>\n"
|
||||
file << "#include <stdlib.h>\n"
|
||||
|
||||
@@ -61,7 +61,7 @@ File.open(TEST_MAKEFILE, "w") do |mkfile|
|
||||
generator = UnityTestRunnerGenerator.new
|
||||
|
||||
# headers that begin with prefix or end with suffix are not included
|
||||
all_headers = Dir["#{SRC_DIR}/**/*.h"]
|
||||
all_headers = Dir["#{SRC_DIR}/**/*.h*"]
|
||||
|
||||
def reject_mock_files(file)
|
||||
extn = File.extname file
|
||||
@@ -104,8 +104,8 @@ File.open(TEST_MAKEFILE, "w") do |mkfile|
|
||||
linkonly = cfg[:includes][:linkonly]
|
||||
linkonly_objs = []
|
||||
linkonly.each do |linkonlyfile|
|
||||
linkonlybase = File.basename(linkonlyfile)
|
||||
linkonlymodule_src = File.join(SRC_DIR, "#{linkonlyfile}.c")
|
||||
linkonlybase = File.basename(linkonlyfile,".*")
|
||||
linkonlymodule_src = File.join(SRC_DIR, "#{linkonlyfile}")
|
||||
linkonlymodule_obj = File.join(OBJ_DIR, "#{linkonlybase}.o")
|
||||
linkonly_objs.push(linkonlymodule_obj)
|
||||
#only create the target if we didn't already
|
||||
@@ -175,7 +175,7 @@ File.open(TEST_MAKEFILE, "w") do |mkfile|
|
||||
# Generate and build mocks
|
||||
all_headers_to_mock.each do |hdr|
|
||||
mock_name = MOCK_PREFIX + File.basename(hdr, '.*')
|
||||
mock_header = File.join(MOCKS_DIR, mock_name + '.h')
|
||||
mock_header = File.join(MOCKS_DIR, mock_name + File.extname(hdr))
|
||||
mock_src = File.join(MOCKS_DIR, mock_name + '.c')
|
||||
mock_obj = File.join(MOCKS_DIR, mock_name + '.o')
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do
|
||||
@config.expect :treat_inlines, :exclude
|
||||
@cmock_generator = CMockGenerator.new(@config, @file_writer, @utils, @plugins)
|
||||
@cmock_generator.module_name = @module_name
|
||||
@cmock_generator.module_ext = '.h'
|
||||
@cmock_generator.mock_name = "Mock#{@module_name}"
|
||||
@cmock_generator.clean_mock_name = "Mock#{@module_name}"
|
||||
|
||||
@@ -76,6 +77,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do
|
||||
@config.expect :treat_inlines, :exclude
|
||||
@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}"
|
||||
end
|
||||
@@ -83,11 +85,12 @@ describe CMockGenerator, "Verify CMockGenerator Module" do
|
||||
after do
|
||||
end
|
||||
|
||||
it "create the top of a header file with optional include files from config and include file from plugin" do
|
||||
def helper_create_header_top_with_opt_incldues_form_config_and_plugin(ext)
|
||||
@config.expect :mock_prefix, "Mock"
|
||||
@config.expect :mock_suffix, ""
|
||||
@config.expect :weak, ""
|
||||
orig_filename = "PoutPoutFish.h"
|
||||
@cmock_generator.module_ext = ext
|
||||
orig_filename = "PoutPoutFish#{ext}"
|
||||
define_name = "MOCKPOUTPOUTFISH_H"
|
||||
output = []
|
||||
expected = [
|
||||
@@ -117,11 +120,17 @@ 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, "Mock#{orig_filename}")
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
it "handle dashes and spaces in the module name" do
|
||||
#no strict handling
|
||||
@config.expect :mock_prefix, "Mock"
|
||||
@@ -138,6 +147,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do
|
||||
@config.expect :treat_inlines, :exclude
|
||||
@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"
|
||||
|
||||
|
||||
@@ -2091,7 +2091,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
|
||||
"{\n" +
|
||||
" return dummy_func_decl(1, 1, 1);\n" +
|
||||
"}\n" +
|
||||
"struct my_struct_with_inline_in_it\n" # struct definition in between to mess with the parser
|
||||
"struct my_struct_with_inline_in_it\n" + # struct definition in between to mess with the parser
|
||||
"{\n" +
|
||||
" int a;\n" +
|
||||
" char b;\n" +
|
||||
@@ -2106,7 +2106,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
|
||||
"int dummy_func_decl(int a, char b, float c);\n" +
|
||||
"int dummy_func_decl2(int a, char b, float c)\n\n\n\n\n\n;\n" + # Second declaration with a lot of newlines until the semicolon to mess with the parser
|
||||
"int staticinlinefunc(struct my_struct *s);\n" +
|
||||
"struct my_struct_with_inline_in_it\n"
|
||||
"struct my_struct_with_inline_in_it\n" +
|
||||
"{\n" +
|
||||
" int a;\n" +
|
||||
" char b;\n" +
|
||||
|
||||
Reference in New Issue
Block a user