1
0
mirror of https://github.com/ThrowTheSwitch/Unity.git synced 2026-01-28 18:54:27 +01:00

the test runner generator now has the ability to also output a header file for the tests, which can get pulled into the test itself if desired.

This commit is contained in:
Mark VanderVoord
2015-07-21 15:35:33 -04:00
parent ab7e322a04
commit a7b85335be
12 changed files with 214 additions and 25 deletions

View File

@@ -55,8 +55,10 @@ class UnityTestRunnerGenerator
source = source.force_encoding("ISO-8859-1").encode("utf-8", :replace => nil) if ($QUICK_RUBY_VERSION > 10900)
tests = find_tests(source)
headers = find_includes(source)
testfile_includes = headers[:local] + headers[:system]
testfile_includes = (headers[:local] + headers[:system])
used_mocks = find_mocks(testfile_includes)
testfile_includes = (testfile_includes - used_mocks)
testfile_includes.delete_if{|inc| inc =~ /(unity|cmock)/}
#build runner file
generate(input_file, output_file, tests, used_mocks, testfile_includes)
@@ -77,6 +79,12 @@ class UnityTestRunnerGenerator
create_reset(output, used_mocks)
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)
end
end
end
def find_tests(source)
@@ -93,12 +101,13 @@ class UnityTestRunnerGenerator
arguments = $1
name = $2
call = $3
params = $4
args = nil
if (@options[:use_param_tests] and !arguments.empty?)
args = []
arguments.scan(/\s*TEST_CASE\s*\((.*)\)\s*$/) {|a| args << a[0]}
end
tests_and_line_numbers << { :test => name, :args => args, :call => call, :line_number => 0 }
tests_and_line_numbers << { :test => name, :args => args, :call => call, :params => params, :line_number => 0 }
end
end
tests_and_line_numbers.uniq! {|v| v[:test] }
@@ -148,17 +157,19 @@ class UnityTestRunnerGenerator
output.puts("\n//=======Automagically Detected Files To Include=====")
output.puts("#include \"#{@options[:framework].to_s}.h\"")
output.puts('#include "cmock.h"') unless (mocks.empty?)
@options[:includes].flatten.uniq.compact.each do |inc|
output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
end
output.puts('#include <setjmp.h>')
output.puts('#include <stdio.h>')
output.puts('#include "CException.h"') if @options[:plugins].include?(:cexception)
testfile_includes.delete_if{|inc| inc =~ /(unity|cmock)/}
testrunner_includes = testfile_includes - mocks
testrunner_includes.each do |inc|
output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
end
if (@options[:header_file] && !@options[:header_file].empty?)
output.puts("#include \"#{File.basename(@options[:header_file])}\"")
else
@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
end
mocks.each do |mock|
output.puts("#include \"#{mock.gsub('.h','')}.h\"")
end
@@ -296,8 +307,23 @@ class UnityTestRunnerGenerator
output.puts(" return #{@options[:suite_teardown].nil? ? "" : "suite_teardown"}(UnityEnd());")
output.puts("}")
end
end
def create_h_file(output, filename, tests, testfile_includes)
filename = filename.upcase.gsub(/(?:\/|\\|\.)*/,'_')
output.puts("/* AUTOGENERATED FILE. DO NOT EDIT. */")
output.puts("#ifndef _#{filename}")
output.puts("#define _#{filename}\n\n")
@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]});") }
output.puts("#endif\n\n")
end
end
if ($0 == __FILE__)
options = { :includes => [] }
@@ -335,6 +361,7 @@ if ($0 == __FILE__)
" --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"
].join("\n")
exit 1
end