vendor/cmock/vendor/unity/auto/generate_test_runner.rb in ceedling-0.19.0 vs vendor/cmock/vendor/unity/auto/generate_test_runner.rb in ceedling-0.20.2
- old
+ new
@@ -26,10 +26,11 @@
:plugins => [],
:framework => :unity,
:test_prefix => "test|spec|should",
:setup_name => "setUp",
:teardown_name => "tearDown",
+ :main_name => "main",
}
end
def self.grab_config(config_file)
options = self.default_options
@@ -80,19 +81,21 @@
create_main(output, input_file, tests, used_mocks)
end
if (@options[:header_file] && !@options[:header_file].empty?)
File.open(@options[:header_file], 'w') do |output|
- create_h_file(output, @options[:header_file], tests, testfile_includes)
+ create_h_file(output, @options[:header_file], tests, testfile_includes, used_mocks)
end
end
end
def find_tests(source)
tests_and_line_numbers = []
- source_scrubbed = source.gsub(/\/\/.*$/, '') # remove line comments
+ source_scrubbed = source.clone
+ source_scrubbed = source_scrubbed.gsub(/"[^"]*"/, '') # remove things in strings
+ source_scrubbed = source_scrubbed.gsub(/\/\/.*$/, '') # remove line comments
source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments
lines = source_scrubbed.split(/(^\s*\#.*$) # Treat preprocessor directives as a logical line
| (;|\{|\}) /x) # Match ;, {, and } as end of lines
lines.each_with_index do |line, index|
@@ -143,12 +146,13 @@
return includes
end
def find_mocks(includes)
mock_headers = []
- includes.each do |include_file|
- mock_headers << File.basename(include_file) if (include_file =~ /^mock/i)
+ includes.each do |include_path|
+ include_file = File.basename(include_path)
+ mock_headers << include_path if (include_file =~ /^mock/i)
end
return mock_headers
end
def create_header(output, mocks, testfile_includes=[])
@@ -189,20 +193,21 @@
output.puts("extern void #{test[:test]}(#{test[:call] || 'void'});")
end
output.puts('')
end
- def create_mock_management(output, mocks)
- unless (mocks.empty?)
+ def create_mock_management(output, mock_headers)
+ unless (mock_headers.empty?)
output.puts("\n//=======Mock Management=====")
output.puts("static void CMock_Init(void)")
output.puts("{")
if @options[:enforce_strict_ordering]
output.puts(" GlobalExpectCount = 0;")
output.puts(" GlobalVerifyOrder = 0;")
output.puts(" GlobalOrderError = NULL;")
end
+ mocks = mock_headers.map {|mock| File.basename(mock)}
mocks.each do |mock|
mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
output.puts(" #{mock_clean}_Init();")
end
output.puts("}\n")
@@ -286,11 +291,14 @@
output.puts("}")
end
def create_main(output, filename, tests, used_mocks)
output.puts("\n\n//=======MAIN=====")
- output.puts("int main(void)")
+ if (@options[:main_name] != "main")
+ output.puts("int #{@options[:main_name]}(void);")
+ end
+ output.puts("int #{@options[:main_name]}(void)")
output.puts("{")
output.puts(" suite_setup();") unless @options[:suite_setup].nil?
output.puts(" UnityBegin(\"#{filename.gsub(/\\/,'\\\\')}\");")
if (@options[:use_param_tests])
tests.each do |test|
@@ -307,23 +315,31 @@
output.puts(" CMock_Guts_MemFreeFinal();") unless used_mocks.empty?
output.puts(" return #{@options[:suite_teardown].nil? ? "" : "suite_teardown"}(UnityEnd());")
output.puts("}")
end
- def create_h_file(output, filename, tests, testfile_includes)
- filename = filename.upcase.gsub(/(?:\/|\\|\.)*/,'_')
+ def create_h_file(output, filename, tests, testfile_includes, used_mocks)
+ filename = File.basename(filename).gsub(/[-\/\\\.\,\s]/, "_").upcase
output.puts("/* AUTOGENERATED FILE. DO NOT EDIT. */")
output.puts("#ifndef _#{filename}")
output.puts("#define _#{filename}\n\n")
+ output.puts("#include \"#{@options[:framework].to_s}.h\"")
+ output.puts('#include "cmock.h"') unless (used_mocks.empty?)
@options[:includes].flatten.uniq.compact.each do |inc|
output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
end
testfile_includes.each do |inc|
output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
end
output.puts "\n"
- tests.each {|test| output.puts("void #{test[:test]}(#{test[:params]});") }
+ tests.each do |test|
+ if ((test[:params].nil?) or (test[:params].empty?))
+ output.puts("void #{test[:test]}(void);")
+ else
+ output.puts("void #{test[:test]}(#{test[:params]});")
+ end
+ end
output.puts("#endif\n\n")
end
end
if ($0 == __FILE__)
@@ -335,14 +351,14 @@
case(arg)
when '-cexception'
options[:plugins] = [:cexception]; true
when /\.*\.ya?ml/
options = UnityTestRunnerGenerator.grab_config(arg); true
- when /\.*\.h/
- options[:includes] << arg; true
when /--(\w+)=\"?(.*)\"?/
options[$1.to_sym] = $2; true
+ when /\.*\.h/
+ options[:includes] << arg; true
else false
end
end
#make sure there is at least one parameter left (the input file)
@@ -356,10 +372,11 @@
" *.h - header files are added as #includes in runner",
" options:",
" -cexception - include cexception support",
" --setup_name=\"\" - redefine setUp func name to something else",
" --teardown_name=\"\" - redefine tearDown func name to something else",
+ " --main_name=\"\" - redefine main func name to something else",
" --test_prefix=\"\" - redefine test prefix from default test|spec|should",
" --suite_setup=\"\" - code to execute for setup of entire suite",
" --suite_teardown=\"\" - code to execute for teardown of entire suite",
" --use_param_tests=1 - enable parameterized tests (disabled by default)",
" --header_file=\"\" - path/name of test header file to generate too"
@@ -368,7 +385,7 @@
end
#create the default test runner name if not specified
ARGV[1] = ARGV[0].gsub(".c","_Runner.c") if (!ARGV[1])
- puts UnityTestRunnerGenerator.new(options).run(ARGV[0], ARGV[1]).inspect
+ UnityTestRunnerGenerator.new(options).run(ARGV[0], ARGV[1])
end