Add ability to generate skeleton from header. woo!

This commit is contained in:
mvandervoord
2020-03-12 12:20:33 -04:00
parent 2eb209b2a8
commit f5abf20f4b
10 changed files with 233 additions and 13 deletions
+19 -1
View File
@@ -32,6 +32,12 @@ class CMock
end
end
def setup_skeletons(files)
[files].flatten.each do |src|
generate_skeleton src
end
end
private ###############################
def generate_mock(src)
@@ -39,6 +45,12 @@ class CMock
puts "Creating mock for #{name}..." unless @silent
@cm_generator.create_mock(name, @cm_parser.parse(name, File.read(src)))
end
def generate_skeleton(src)
name = File.basename(src, '.h')
puts "Creating skeleton for #{name}..." unless @silent
@cm_generator.create_skeleton(name, @cm_parser.parse(name, File.read(src)))
end
end
def option_maker(options, key, val)
@@ -75,6 +87,8 @@ if ($0 == __FILE__)
ARGV.each do |arg|
if (arg =~ /^-o\"?([a-zA-Z0-9._\\\/:\s]+)\"?/)
options.merge! CMockConfig.load_config_file_from_yaml( arg.gsub(/^-o/,'') )
elsif (arg == "--skeleton")
options[:skeleton] = true
elsif (arg =~ /^--([a-zA-Z0-9._\\\/:\s]+)=\"?([a-zA-Z0-9._\-\\\/:\s\;]+)\"?/)
options = option_maker(options, $1, $2)
else
@@ -82,5 +96,9 @@ if ($0 == __FILE__)
end
end
CMock.new(options).setup_mocks(filelist)
if (options[:skeleton])
CMock.new(options).setup_skeletons(filelist)
else
CMock.new(options).setup_mocks(filelist)
end
end
+1
View File
@@ -40,6 +40,7 @@ class CMockConfig
:orig_header_include_fmt => "#include \"%s\"",
:array_size_type => [],
:array_size_name => 'size|len',
:skeleton => false,
# Format to look for inline functions.
# This is a combination of "static" and "inline" keywords ("static inline", "inline static", "inline", "static")
+8
View File
@@ -33,6 +33,14 @@ class CMockFileWriter
update_file(full_file_name_done, full_file_name_temp)
end
def append_file(filename, subdir)
raise "Where's the block of data to create?" unless block_given?
full_file_name = "#{@config.mock_path}/#{subdir+'/' if subdir}#{filename}"
File.open(full_file_name, 'a') do |file|
yield(file, filename)
end
end
private ###################################
def update_file(dest, src)
+37 -1
View File
@@ -55,6 +55,11 @@ class CMockGenerator
create_mock_source_file(parsed_stuff)
end
def create_skeleton(module_name, parsed_stuff)
@module_name = module_name
create_skeleton_source_file(parsed_stuff)
end
private if $ThisIsOnlyATest.nil? ##############################
def create_mock_subdir()
@@ -94,6 +99,17 @@ class CMockGenerator
end
end
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?
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 = (@subdir ? @subdir + "/" : "") + @module_name + ".h"
@@ -146,7 +162,7 @@ class CMockGenerator
def create_source_header_section(file, filename, functions)
header_file = (@subdir ? @subdir + '/' : '') + filename.gsub(".c",".h")
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n"
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" unless functions.empty?
file << "#include <string.h>\n"
file << "#include <stdlib.h>\n"
file << "#include <setjmp.h>\n"
@@ -272,4 +288,24 @@ class CMockGenerator
file << @utils.code_add_argument_loader(function)
file << @plugins.run(:mock_interfaces, function)
end
def create_function_skeleton(file, function, existing)
# prepare return value and arguments
function_mod_and_rettype = (function[:modifier].empty? ? '' : "#{function[:modifier]} ") +
(function[:return][:type]) +
(function[:c_calling_convention] ? " #{function[:c_calling_convention]}" : '')
args_string = function[:args_string]
args_string += (", " + function[:var_arg]) unless (function[:var_arg].nil?)
decl = "#{function_mod_and_rettype} #{function[:name]}(#{args_string})"
unless (existing.include?(decl))
file << "#{decl}\n"
file << "{\n"
file << " //TODO: Implement Me!\n"
function[:args].each {|arg| file << " (void)#{arg[:name]};\n"}
file << " return (#{(function[:return][:type])})0;\n" unless (function[:return][:void?])
file << "}\n\n"
end
end
end