xref: /freebsd-src/contrib/ntp/sntp/unity/auto/generate_test_runner.rb (revision 091002585974d17c9533f943ec351c13a69788ab)
1276da39aSCy Schubert# ==========================================
2276da39aSCy Schubert#   Unity Project - A Test Framework for C
3276da39aSCy Schubert#   Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4276da39aSCy Schubert#   [Released under MIT License. Please refer to license.txt for details]
5276da39aSCy Schubert# ==========================================
6276da39aSCy Schubert
79034852cSGleb Smirnoff$QUICK_RUBY_VERSION = RUBY_VERSION.split('.').inject(0){|vv,v| vv * 100 + v.to_i }
8276da39aSCy SchubertFile.expand_path(File.join(File.dirname(__FILE__),'colour_prompt'))
9276da39aSCy Schubert
10276da39aSCy Schubertclass UnityTestRunnerGenerator
11276da39aSCy Schubert
12276da39aSCy Schubert  def initialize(options = nil)
139034852cSGleb Smirnoff    @options = UnityTestRunnerGenerator.default_options
149034852cSGleb Smirnoff
15276da39aSCy Schubert    case(options)
16276da39aSCy Schubert      when NilClass then @options
17276da39aSCy Schubert      when String   then @options.merge!(UnityTestRunnerGenerator.grab_config(options))
18276da39aSCy Schubert      when Hash     then @options.merge!(options)
19276da39aSCy Schubert      else          raise "If you specify arguments, it should be a filename or a hash of options"
20276da39aSCy Schubert    end
219034852cSGleb Smirnoff    require "#{File.expand_path(File.dirname(__FILE__))}/type_sanitizer"
22276da39aSCy Schubert  end
23276da39aSCy Schubert
249034852cSGleb Smirnoff  def self.default_options
259034852cSGleb Smirnoff    {
269034852cSGleb Smirnoff      :includes      => [],
279034852cSGleb Smirnoff      :plugins       => [],
289034852cSGleb Smirnoff      :framework     => :unity,
299034852cSGleb Smirnoff      :test_prefix   => "test|spec|should",
309034852cSGleb Smirnoff      :setup_name    => "setUp",
319034852cSGleb Smirnoff      :teardown_name => "tearDown",
329034852cSGleb Smirnoff    }
339034852cSGleb Smirnoff  end
349034852cSGleb Smirnoff
359034852cSGleb Smirnoff
36276da39aSCy Schubert  def self.grab_config(config_file)
379034852cSGleb Smirnoff    options = self.default_options
389034852cSGleb Smirnoff
39276da39aSCy Schubert    unless (config_file.nil? or config_file.empty?)
40276da39aSCy Schubert      require 'yaml'
41276da39aSCy Schubert      yaml_guts = YAML.load_file(config_file)
429034852cSGleb Smirnoff      options.merge!(yaml_guts[:unity] || yaml_guts[:cmock])
43276da39aSCy Schubert      raise "No :unity or :cmock section found in #{config_file}" unless options
44276da39aSCy Schubert    end
45276da39aSCy Schubert    return(options)
46276da39aSCy Schubert  end
47276da39aSCy Schubert
48276da39aSCy Schubert  def run(input_file, output_file, options=nil)
49276da39aSCy Schubert    tests = []
509034852cSGleb Smirnoff    testfile_includes = []
51276da39aSCy Schubert    used_mocks = []
52276da39aSCy Schubert
539034852cSGleb Smirnoff
54276da39aSCy Schubert    @options.merge!(options) unless options.nil?
55276da39aSCy Schubert    module_name = File.basename(input_file)
56276da39aSCy Schubert
579034852cSGleb Smirnoff
58276da39aSCy Schubert    #pull required data from source file
599034852cSGleb Smirnoff    source = File.read(input_file)
609034852cSGleb Smirnoff    source = source.force_encoding("ISO-8859-1").encode("utf-8", :replace => nil) if ($QUICK_RUBY_VERSION > 10900)
619034852cSGleb Smirnoff    tests               = find_tests(source)
629034852cSGleb Smirnoff    headers             = find_includes(source)
639034852cSGleb Smirnoff    testfile_includes   = headers[:local] + headers[:system]
649034852cSGleb Smirnoff    used_mocks          = find_mocks(testfile_includes)
659034852cSGleb Smirnoff
66276da39aSCy Schubert
67276da39aSCy Schubert    #build runner file
689034852cSGleb Smirnoff    generate(input_file, output_file, tests, used_mocks, testfile_includes)
69276da39aSCy Schubert
709034852cSGleb Smirnoff    #determine which files were used to return them
71276da39aSCy Schubert    all_files_used = [input_file, output_file]
729034852cSGleb Smirnoff    all_files_used += testfile_includes.map {|filename| filename + '.c'} unless testfile_includes.empty?
73276da39aSCy Schubert    all_files_used += @options[:includes] unless @options[:includes].empty?
74276da39aSCy Schubert    return all_files_used.uniq
75276da39aSCy Schubert  end
76276da39aSCy Schubert
779034852cSGleb Smirnoff  def generate(input_file, output_file, tests, used_mocks, testfile_includes)
789034852cSGleb Smirnoff    File.open(output_file, 'w') do |output|
799034852cSGleb Smirnoff      create_header(output, used_mocks, testfile_includes)
809034852cSGleb Smirnoff      create_externs(output, tests, used_mocks)
819034852cSGleb Smirnoff      create_mock_management(output, used_mocks)
829034852cSGleb Smirnoff      create_suite_setup_and_teardown(output)
839034852cSGleb Smirnoff      create_reset(output, used_mocks)
849034852cSGleb Smirnoff      create_main(output, input_file, tests, used_mocks)
859034852cSGleb Smirnoff    end
869034852cSGleb Smirnoff
879034852cSGleb Smirnoff
889034852cSGleb Smirnoff
899034852cSGleb Smirnoff
909034852cSGleb Smirnoff
919034852cSGleb Smirnoff  end
929034852cSGleb Smirnoff
939034852cSGleb Smirnoff
949034852cSGleb Smirnoff  def find_tests(source)
959034852cSGleb Smirnoff
969034852cSGleb Smirnoff
97276da39aSCy Schubert    tests_and_line_numbers = []
98276da39aSCy Schubert
999034852cSGleb Smirnoff
1009034852cSGleb Smirnoff
1019034852cSGleb Smirnoff
1029034852cSGleb Smirnoff    source_scrubbed = source.gsub(/\/\/.*$/, '')               # remove line comments
103276da39aSCy Schubert    source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments
104276da39aSCy Schubert    lines = source_scrubbed.split(/(^\s*\#.*$)                 # Treat preprocessor directives as a logical line
105276da39aSCy Schubert                              | (;|\{|\}) /x)                  # Match ;, {, and } as end of lines
106276da39aSCy Schubert
107276da39aSCy Schubert    lines.each_with_index do |line, index|
108276da39aSCy Schubert      #find tests
1099034852cSGleb Smirnoff      if line =~ /^((?:\s*TEST_CASE\s*\(.*?\)\s*)*)\s*void\s+((?:#{@options[:test_prefix]}).*)\s*\(\s*(.*)\s*\)/
1109034852cSGleb Smirnoff        arguments = $1
111276da39aSCy Schubert        name = $2
112276da39aSCy Schubert        call = $3
1139034852cSGleb Smirnoff        args = nil
1149034852cSGleb Smirnoff        if (@options[:use_param_tests] and !arguments.empty?)
1159034852cSGleb Smirnoff          args = []
1169034852cSGleb Smirnoff          arguments.scan(/\s*TEST_CASE\s*\((.*)\)\s*$/) {|a| args << a[0]}
1179034852cSGleb Smirnoff        end
1189034852cSGleb Smirnoff        tests_and_line_numbers << { :test => name, :args => args, :call => call, :line_number => 0 }
1199034852cSGleb Smirnoff
120276da39aSCy Schubert      end
121276da39aSCy Schubert    end
1229034852cSGleb Smirnoff    tests_and_line_numbers.uniq! {|v| v[:test] }
123276da39aSCy Schubert
124276da39aSCy Schubert    #determine line numbers and create tests to run
1259034852cSGleb Smirnoff    source_lines = source.split("\n")
126276da39aSCy Schubert    source_index = 0;
127276da39aSCy Schubert    tests_and_line_numbers.size.times do |i|
128276da39aSCy Schubert      source_lines[source_index..-1].each_with_index do |line, index|
1299034852cSGleb Smirnoff        if (line =~ /#{tests_and_line_numbers[i][:test]}/)
130276da39aSCy Schubert          source_index += index
131276da39aSCy Schubert          tests_and_line_numbers[i][:line_number] = source_index + 1
132276da39aSCy Schubert          break
133276da39aSCy Schubert        end
134276da39aSCy Schubert      end
135276da39aSCy Schubert    end
136276da39aSCy Schubert
1379034852cSGleb Smirnoff
138276da39aSCy Schubert    return tests_and_line_numbers
139276da39aSCy Schubert  end
140276da39aSCy Schubert
1419034852cSGleb Smirnoff  def find_includes(source)
1429034852cSGleb Smirnoff
1439034852cSGleb Smirnoff    #remove comments (block and line, in three steps to ensure correct precedence)
1449034852cSGleb Smirnoff    source.gsub!(/\/\/(?:.+\/\*|\*(?:$|[^\/])).*$/, '')  # remove line comments that comment out the start of blocks
1459034852cSGleb Smirnoff    source.gsub!(/\/\*.*?\*\//m, '')                     # remove block comments
1469034852cSGleb Smirnoff    source.gsub!(/\/\/.*$/, '')                          # remove line comments (all that remain)
1479034852cSGleb Smirnoff
1489034852cSGleb Smirnoff    #parse out includes
1499034852cSGleb Smirnoff
1509034852cSGleb Smirnoff    includes = {
1519034852cSGleb Smirnoff
1529034852cSGleb Smirnoff      :local => source.scan(/^\s*#include\s+\"\s*(.+)\.[hH]\s*\"/).flatten,
1539034852cSGleb Smirnoff      :system => source.scan(/^\s*#include\s+<\s*(.+)\s*>/).flatten.map { |inc| "<#{inc}>" }
1549034852cSGleb Smirnoff    }
1559034852cSGleb Smirnoff
1569034852cSGleb Smirnoff
157276da39aSCy Schubert    return includes
158276da39aSCy Schubert  end
159276da39aSCy Schubert
1609034852cSGleb Smirnoff
161276da39aSCy Schubert  def find_mocks(includes)
162276da39aSCy Schubert    mock_headers = []
163276da39aSCy Schubert    includes.each do |include_file|
164276da39aSCy Schubert      mock_headers << File.basename(include_file) if (include_file =~ /^mock/i)
165276da39aSCy Schubert    end
166276da39aSCy Schubert    return mock_headers
167276da39aSCy Schubert  end
168276da39aSCy Schubert
1699034852cSGleb Smirnoff
1709034852cSGleb Smirnoff  def create_header(output, mocks, testfile_includes=[])
171276da39aSCy Schubert    output.puts('/* AUTOGENERATED FILE. DO NOT EDIT. */')
172276da39aSCy Schubert    create_runtest(output, mocks)
173276da39aSCy Schubert    output.puts("\n//=======Automagically Detected Files To Include=====")
174276da39aSCy Schubert    output.puts("#include \"#{@options[:framework].to_s}.h\"")
175276da39aSCy Schubert    output.puts('#include "cmock.h"') unless (mocks.empty?)
1769034852cSGleb Smirnoff    @options[:includes].flatten.uniq.compact.each do |inc|
1779034852cSGleb Smirnoff      output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
178276da39aSCy Schubert    end
179276da39aSCy Schubert    output.puts('#include <setjmp.h>')
180276da39aSCy Schubert    output.puts('#include <stdio.h>')
181276da39aSCy Schubert    output.puts('#include "CException.h"') if @options[:plugins].include?(:cexception)
1829034852cSGleb Smirnoff    testfile_includes.delete_if{|inc| inc =~ /(unity|cmock)/}
1839034852cSGleb Smirnoff    testrunner_includes = testfile_includes - mocks
1849034852cSGleb Smirnoff    testrunner_includes.each do |inc|
1859034852cSGleb Smirnoff    output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
1869034852cSGleb Smirnoff  end
187276da39aSCy Schubert    mocks.each do |mock|
188276da39aSCy Schubert      output.puts("#include \"#{mock.gsub('.h','')}.h\"")
189276da39aSCy Schubert    end
190276da39aSCy Schubert    if @options[:enforce_strict_ordering]
191276da39aSCy Schubert      output.puts('')
192276da39aSCy Schubert      output.puts('int GlobalExpectCount;')
193276da39aSCy Schubert      output.puts('int GlobalVerifyOrder;')
194276da39aSCy Schubert      output.puts('char* GlobalOrderError;')
195276da39aSCy Schubert    end
196276da39aSCy Schubert  end
197276da39aSCy Schubert
1989034852cSGleb Smirnoff
199276da39aSCy Schubert  def create_externs(output, tests, mocks)
200276da39aSCy Schubert    output.puts("\n//=======External Functions This Runner Calls=====")
2019034852cSGleb Smirnoff    output.puts("extern void #{@options[:setup_name]}(void);")
2029034852cSGleb Smirnoff    output.puts("extern void #{@options[:teardown_name]}(void);")
2039034852cSGleb Smirnoff
204276da39aSCy Schubert    tests.each do |test|
2059034852cSGleb Smirnoff      output.puts("extern void #{test[:test]}(#{test[:call] || 'void'});")
206276da39aSCy Schubert    end
207276da39aSCy Schubert    output.puts('')
208276da39aSCy Schubert  end
209276da39aSCy Schubert
2109034852cSGleb Smirnoff
211276da39aSCy Schubert  def create_mock_management(output, mocks)
212276da39aSCy Schubert    unless (mocks.empty?)
213276da39aSCy Schubert      output.puts("\n//=======Mock Management=====")
214276da39aSCy Schubert      output.puts("static void CMock_Init(void)")
215276da39aSCy Schubert      output.puts("{")
216276da39aSCy Schubert      if @options[:enforce_strict_ordering]
217276da39aSCy Schubert        output.puts("  GlobalExpectCount = 0;")
218276da39aSCy Schubert        output.puts("  GlobalVerifyOrder = 0;")
219276da39aSCy Schubert        output.puts("  GlobalOrderError = NULL;")
220276da39aSCy Schubert      end
221276da39aSCy Schubert      mocks.each do |mock|
2229034852cSGleb Smirnoff        mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
2239034852cSGleb Smirnoff        output.puts("  #{mock_clean}_Init();")
224276da39aSCy Schubert      end
225276da39aSCy Schubert      output.puts("}\n")
226276da39aSCy Schubert
227276da39aSCy Schubert      output.puts("static void CMock_Verify(void)")
228276da39aSCy Schubert      output.puts("{")
229276da39aSCy Schubert      mocks.each do |mock|
2309034852cSGleb Smirnoff        mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
2319034852cSGleb Smirnoff        output.puts("  #{mock_clean}_Verify();")
232276da39aSCy Schubert      end
233276da39aSCy Schubert      output.puts("}\n")
234276da39aSCy Schubert
235276da39aSCy Schubert      output.puts("static void CMock_Destroy(void)")
236276da39aSCy Schubert      output.puts("{")
237276da39aSCy Schubert      mocks.each do |mock|
2389034852cSGleb Smirnoff        mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
2399034852cSGleb Smirnoff        output.puts("  #{mock_clean}_Destroy();")
240276da39aSCy Schubert      end
241276da39aSCy Schubert      output.puts("}\n")
242276da39aSCy Schubert    end
243276da39aSCy Schubert  end
244276da39aSCy Schubert
2459034852cSGleb Smirnoff
246276da39aSCy Schubert  def create_suite_setup_and_teardown(output)
247276da39aSCy Schubert    unless (@options[:suite_setup].nil?)
248276da39aSCy Schubert      output.puts("\n//=======Suite Setup=====")
249*09100258SXin LI      output.puts("static void suite_setup(void)")
250276da39aSCy Schubert      output.puts("{")
251276da39aSCy Schubert      output.puts(@options[:suite_setup])
252276da39aSCy Schubert      output.puts("}")
253276da39aSCy Schubert    end
254276da39aSCy Schubert    unless (@options[:suite_teardown].nil?)
255276da39aSCy Schubert      output.puts("\n//=======Suite Teardown=====")
256276da39aSCy Schubert      output.puts("static int suite_teardown(int num_failures)")
257276da39aSCy Schubert      output.puts("{")
258276da39aSCy Schubert      output.puts(@options[:suite_teardown])
259276da39aSCy Schubert      output.puts("}")
260276da39aSCy Schubert    end
261276da39aSCy Schubert  end
262276da39aSCy Schubert
2639034852cSGleb Smirnoff
264276da39aSCy Schubert  def create_runtest(output, used_mocks)
265276da39aSCy Schubert    cexception = @options[:plugins].include? :cexception
266276da39aSCy Schubert    va_args1   = @options[:use_param_tests] ? ', ...' : ''
267276da39aSCy Schubert    va_args2   = @options[:use_param_tests] ? '__VA_ARGS__' : ''
268276da39aSCy Schubert    output.puts("\n//=======Test Runner Used To Run Each Test Below=====")
269276da39aSCy Schubert    output.puts("#define RUN_TEST_NO_ARGS") if @options[:use_param_tests]
270276da39aSCy Schubert    output.puts("#define RUN_TEST(TestFunc, TestLineNum#{va_args1}) \\")
271276da39aSCy Schubert    output.puts("{ \\")
272276da39aSCy Schubert    output.puts("  Unity.CurrentTestName = #TestFunc#{va_args2.empty? ? '' : " \"(\" ##{va_args2} \")\""}; \\")
273276da39aSCy Schubert    output.puts("  Unity.CurrentTestLineNumber = TestLineNum; \\")
274276da39aSCy Schubert    output.puts("  Unity.NumberOfTests++; \\")
2759034852cSGleb Smirnoff    output.puts("  CMock_Init(); \\") unless (used_mocks.empty?)
276276da39aSCy Schubert    output.puts("  if (TEST_PROTECT()) \\")
277276da39aSCy Schubert    output.puts("  { \\")
278276da39aSCy Schubert    output.puts("    CEXCEPTION_T e; \\") if cexception
279276da39aSCy Schubert    output.puts("    Try { \\") if cexception
2809034852cSGleb Smirnoff    output.puts("      #{@options[:setup_name]}(); \\")
2819034852cSGleb Smirnoff
2829034852cSGleb Smirnoff
283276da39aSCy Schubert    output.puts("      TestFunc(#{va_args2}); \\")
2849034852cSGleb Smirnoff
285276da39aSCy Schubert    output.puts("    } Catch(e) { TEST_ASSERT_EQUAL_HEX32_MESSAGE(CEXCEPTION_NONE, e, \"Unhandled Exception!\"); } \\") if cexception
286276da39aSCy Schubert    output.puts("  } \\")
2879034852cSGleb Smirnoff
288276da39aSCy Schubert    output.puts("  if (TEST_PROTECT() && !TEST_IS_IGNORED) \\")
289276da39aSCy Schubert    output.puts("  { \\")
2909034852cSGleb Smirnoff    output.puts("    #{@options[:teardown_name]}(); \\")
2919034852cSGleb Smirnoff    output.puts("    CMock_Verify(); \\") unless (used_mocks.empty?)
2929034852cSGleb Smirnoff
293276da39aSCy Schubert    output.puts("  } \\")
2949034852cSGleb Smirnoff    output.puts("  CMock_Destroy(); \\") unless (used_mocks.empty?)
295276da39aSCy Schubert    output.puts("  UnityConcludeTest(); \\")
296276da39aSCy Schubert    output.puts("}\n")
297276da39aSCy Schubert  end
298276da39aSCy Schubert
2999034852cSGleb Smirnoff
300276da39aSCy Schubert  def create_reset(output, used_mocks)
301276da39aSCy Schubert    output.puts("\n//=======Test Reset Option=====")
3029034852cSGleb Smirnoff    output.puts("void resetTest(void);")
3039034852cSGleb Smirnoff    output.puts("void resetTest(void)")
3049034852cSGleb Smirnoff
305276da39aSCy Schubert    output.puts("{")
306276da39aSCy Schubert    output.puts("  CMock_Verify();") unless (used_mocks.empty?)
307276da39aSCy Schubert    output.puts("  CMock_Destroy();") unless (used_mocks.empty?)
3089034852cSGleb Smirnoff    output.puts("  #{@options[:teardown_name]}();")
3099034852cSGleb Smirnoff
310276da39aSCy Schubert    output.puts("  CMock_Init();") unless (used_mocks.empty?)
3119034852cSGleb Smirnoff    output.puts("  #{@options[:setup_name]}();")
3129034852cSGleb Smirnoff
313276da39aSCy Schubert    output.puts("}")
314276da39aSCy Schubert  end
315276da39aSCy Schubert
3169034852cSGleb Smirnoff
3179034852cSGleb Smirnoff  def create_main(output, filename, tests, used_mocks)
3189034852cSGleb Smirnoff    output.puts("\nchar const *progname;\n")
319276da39aSCy Schubert    output.puts("\n\n//=======MAIN=====")
320276da39aSCy Schubert
321276da39aSCy Schubert    output.puts("int main(int argc, char *argv[])")
322276da39aSCy Schubert    output.puts("{")
323276da39aSCy Schubert    output.puts("  progname = argv[0];\n")
3249034852cSGleb Smirnoff
3259034852cSGleb Smirnoff
326*09100258SXin LI    modname = filename.split(/[\/\\]/).last
3279034852cSGleb Smirnoff
3289034852cSGleb Smirnoff
3299034852cSGleb Smirnoff
330276da39aSCy Schubert    output.puts("  suite_setup();") unless @options[:suite_setup].nil?
3319034852cSGleb Smirnoff
332*09100258SXin LI    output.puts("  UnityBegin(\"#{modname}\");")
333276da39aSCy Schubert
334276da39aSCy Schubert    if (@options[:use_param_tests])
335276da39aSCy Schubert      tests.each do |test|
336276da39aSCy Schubert        if ((test[:args].nil?) or (test[:args].empty?))
3379034852cSGleb Smirnoff          output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]}, RUN_TEST_NO_ARGS);")
338276da39aSCy Schubert        else
3399034852cSGleb Smirnoff          test[:args].each {|args| output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]}, #{args});")}
340276da39aSCy Schubert        end
341276da39aSCy Schubert      end
342276da39aSCy Schubert    else
3439034852cSGleb Smirnoff        tests.each { |test| output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]});") }
344276da39aSCy Schubert    end
345276da39aSCy Schubert    output.puts()
3469034852cSGleb Smirnoff    output.puts("  CMock_Guts_MemFreeFinal();") unless used_mocks.empty?
347276da39aSCy Schubert    output.puts("  return #{@options[:suite_teardown].nil? ? "" : "suite_teardown"}(UnityEnd());")
348276da39aSCy Schubert    output.puts("}")
349276da39aSCy Schubert  end
350276da39aSCy Schubertend
351276da39aSCy Schubert
352276da39aSCy Schubert
353276da39aSCy Schubertif ($0 == __FILE__)
354276da39aSCy Schubert  options = { :includes => [] }
355276da39aSCy Schubert  yaml_file = nil
356276da39aSCy Schubert
3579034852cSGleb Smirnoff
3589034852cSGleb Smirnoff  #parse out all the options first (these will all be removed as we go)
359276da39aSCy Schubert  ARGV.reject! do |arg|
360276da39aSCy Schubert    case(arg)
361276da39aSCy Schubert      when '-cexception'
362276da39aSCy Schubert        options[:plugins] = [:cexception]; true
3639034852cSGleb Smirnoff      when /\.*\.ya?ml/
3649034852cSGleb Smirnoff
365276da39aSCy Schubert        options = UnityTestRunnerGenerator.grab_config(arg); true
3669034852cSGleb Smirnoff      when /\.*\.h/
3679034852cSGleb Smirnoff        options[:includes] << arg; true
3689034852cSGleb Smirnoff      when /--(\w+)=\"?(.*)\"?/
3699034852cSGleb Smirnoff        options[$1.to_sym] = $2; true
370276da39aSCy Schubert      else false
371276da39aSCy Schubert    end
372276da39aSCy Schubert  end
373276da39aSCy Schubert
3749034852cSGleb Smirnoff
375276da39aSCy Schubert  #make sure there is at least one parameter left (the input file)
376276da39aSCy Schubert  if !ARGV[0]
3779034852cSGleb Smirnoff    puts ["\nusage: ruby #{__FILE__} (files) (options) input_test_file (output)",
3789034852cSGleb Smirnoff           "\n  input_test_file         - this is the C file you want to create a runner for",
3799034852cSGleb Smirnoff           "  output                  - this is the name of the runner file to generate",
3809034852cSGleb Smirnoff           "                            defaults to (input_test_file)_Runner",
3819034852cSGleb Smirnoff           "  files:",
3829034852cSGleb Smirnoff           "    *.yml / *.yaml        - loads configuration from here in :unity or :cmock",
3839034852cSGleb Smirnoff           "    *.h                   - header files are added as #includes in runner",
3849034852cSGleb Smirnoff           "  options:",
3859034852cSGleb Smirnoff
3869034852cSGleb Smirnoff           "    -cexception           - include cexception support",
3879034852cSGleb Smirnoff           "    --setup_name=\"\"       - redefine setUp func name to something else",
3889034852cSGleb Smirnoff           "    --teardown_name=\"\"    - redefine tearDown func name to something else",
3899034852cSGleb Smirnoff           "    --test_prefix=\"\"      - redefine test prefix from default test|spec|should",
3909034852cSGleb Smirnoff           "    --suite_setup=\"\"      - code to execute for setup of entire suite",
3919034852cSGleb Smirnoff           "    --suite_teardown=\"\"   - code to execute for teardown of entire suite",
3929034852cSGleb Smirnoff           "    --use_param_tests=1   - enable parameterized tests (disabled by default)",
3939034852cSGleb Smirnoff          ].join("\n")
394276da39aSCy Schubert    exit 1
395276da39aSCy Schubert  end
396276da39aSCy Schubert
3979034852cSGleb Smirnoff
398276da39aSCy Schubert  #create the default test runner name if not specified
399276da39aSCy Schubert  ARGV[1] = ARGV[0].gsub(".c","_Runner.c") if (!ARGV[1])
400276da39aSCy Schubert
4019034852cSGleb Smirnoff
4029034852cSGleb Smirnoff
4039034852cSGleb Smirnoff
4049034852cSGleb Smirnoff
405276da39aSCy Schubert
406276da39aSCy Schubert  UnityTestRunnerGenerator.new(options).run(ARGV[0], ARGV[1])
407276da39aSCy Schubertend
4089034852cSGleb Smirnoff
409