xref: /freebsd/contrib/ntp/sntp/unity/auto/generate_test_runner.rb (revision 32100375a661c1e16588ddfa7b90ca8d26cb9786)
1# ==========================================
2#   Unity Project - A Test Framework for C
3#   Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4#   [Released under MIT License. Please refer to license.txt for details]
5# ==========================================
6
7$QUICK_RUBY_VERSION = RUBY_VERSION.split('.').inject(0){|vv,v| vv * 100 + v.to_i }
8File.expand_path(File.join(File.dirname(__FILE__),'colour_prompt'))
9
10class UnityTestRunnerGenerator
11
12  def initialize(options = nil)
13    @options = UnityTestRunnerGenerator.default_options
14
15    case(options)
16      when NilClass then @options
17      when String   then @options.merge!(UnityTestRunnerGenerator.grab_config(options))
18      when Hash     then @options.merge!(options)
19      else          raise "If you specify arguments, it should be a filename or a hash of options"
20    end
21    require "#{File.expand_path(File.dirname(__FILE__))}/type_sanitizer"
22  end
23
24  def self.default_options
25    {
26      :includes      => [],
27      :plugins       => [],
28      :framework     => :unity,
29      :test_prefix   => "test|spec|should",
30      :setup_name    => "setUp",
31      :teardown_name => "tearDown",
32    }
33  end
34
35
36  def self.grab_config(config_file)
37    options = self.default_options
38
39    unless (config_file.nil? or config_file.empty?)
40      require 'yaml'
41      yaml_guts = YAML.load_file(config_file)
42      options.merge!(yaml_guts[:unity] || yaml_guts[:cmock])
43      raise "No :unity or :cmock section found in #{config_file}" unless options
44    end
45    return(options)
46  end
47
48  def run(input_file, output_file, options=nil)
49    tests = []
50    testfile_includes = []
51    used_mocks = []
52
53
54    @options.merge!(options) unless options.nil?
55    module_name = File.basename(input_file)
56
57
58    #pull required data from source file
59    source = File.read(input_file)
60    source = source.force_encoding("ISO-8859-1").encode("utf-8", :replace => nil) if ($QUICK_RUBY_VERSION > 10900)
61    tests               = find_tests(source)
62    headers             = find_includes(source)
63    testfile_includes   = headers[:local] + headers[:system]
64    used_mocks          = find_mocks(testfile_includes)
65
66
67    #build runner file
68    generate(input_file, output_file, tests, used_mocks, testfile_includes)
69
70    #determine which files were used to return them
71    all_files_used = [input_file, output_file]
72    all_files_used += testfile_includes.map {|filename| filename + '.c'} unless testfile_includes.empty?
73    all_files_used += @options[:includes] unless @options[:includes].empty?
74    return all_files_used.uniq
75  end
76
77  def generate(input_file, output_file, tests, used_mocks, testfile_includes)
78    File.open(output_file, 'w') do |output|
79      create_header(output, used_mocks, testfile_includes)
80      create_externs(output, tests, used_mocks)
81      create_mock_management(output, used_mocks)
82      create_suite_setup_and_teardown(output)
83      create_reset(output, used_mocks)
84      create_main(output, input_file, tests, used_mocks)
85    end
86
87
88
89
90
91  end
92
93
94  def find_tests(source)
95
96
97    tests_and_line_numbers = []
98
99
100
101
102    source_scrubbed = source.gsub(/\/\/.*$/, '')               # remove line comments
103    source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments
104    lines = source_scrubbed.split(/(^\s*\#.*$)                 # Treat preprocessor directives as a logical line
105                              | (;|\{|\}) /x)                  # Match ;, {, and } as end of lines
106
107    lines.each_with_index do |line, index|
108      #find tests
109      if line =~ /^((?:\s*TEST_CASE\s*\(.*?\)\s*)*)\s*void\s+((?:#{@options[:test_prefix]}).*)\s*\(\s*(.*)\s*\)/
110        arguments = $1
111        name = $2
112        call = $3
113        args = nil
114        if (@options[:use_param_tests] and !arguments.empty?)
115          args = []
116          arguments.scan(/\s*TEST_CASE\s*\((.*)\)\s*$/) {|a| args << a[0]}
117        end
118        tests_and_line_numbers << { :test => name, :args => args, :call => call, :line_number => 0 }
119
120      end
121    end
122    tests_and_line_numbers.uniq! {|v| v[:test] }
123
124    #determine line numbers and create tests to run
125    source_lines = source.split("\n")
126    source_index = 0;
127    tests_and_line_numbers.size.times do |i|
128      source_lines[source_index..-1].each_with_index do |line, index|
129        if (line =~ /#{tests_and_line_numbers[i][:test]}/)
130          source_index += index
131          tests_and_line_numbers[i][:line_number] = source_index + 1
132          break
133        end
134      end
135    end
136
137
138    return tests_and_line_numbers
139  end
140
141  def find_includes(source)
142
143    #remove comments (block and line, in three steps to ensure correct precedence)
144    source.gsub!(/\/\/(?:.+\/\*|\*(?:$|[^\/])).*$/, '')  # remove line comments that comment out the start of blocks
145    source.gsub!(/\/\*.*?\*\//m, '')                     # remove block comments
146    source.gsub!(/\/\/.*$/, '')                          # remove line comments (all that remain)
147
148    #parse out includes
149
150    includes = {
151
152      :local => source.scan(/^\s*#include\s+\"\s*(.+)\.[hH]\s*\"/).flatten,
153      :system => source.scan(/^\s*#include\s+<\s*(.+)\s*>/).flatten.map { |inc| "<#{inc}>" }
154    }
155
156
157    return includes
158  end
159
160
161  def find_mocks(includes)
162    mock_headers = []
163    includes.each do |include_file|
164      mock_headers << File.basename(include_file) if (include_file =~ /^mock/i)
165    end
166    return mock_headers
167  end
168
169
170  def create_header(output, mocks, testfile_includes=[])
171    output.puts('/* AUTOGENERATED FILE. DO NOT EDIT. */')
172    create_runtest(output, mocks)
173    output.puts("\n//=======Automagically Detected Files To Include=====")
174    output.puts("#include \"#{@options[:framework].to_s}.h\"")
175    output.puts('#include "cmock.h"') unless (mocks.empty?)
176    @options[:includes].flatten.uniq.compact.each do |inc|
177      output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
178    end
179    output.puts('#include <setjmp.h>')
180    output.puts('#include <stdio.h>')
181    output.puts('#include "CException.h"') if @options[:plugins].include?(:cexception)
182    testfile_includes.delete_if{|inc| inc =~ /(unity|cmock)/}
183    testrunner_includes = testfile_includes - mocks
184    testrunner_includes.each do |inc|
185    output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
186  end
187    mocks.each do |mock|
188      output.puts("#include \"#{mock.gsub('.h','')}.h\"")
189    end
190    if @options[:enforce_strict_ordering]
191      output.puts('')
192      output.puts('int GlobalExpectCount;')
193      output.puts('int GlobalVerifyOrder;')
194      output.puts('char* GlobalOrderError;')
195    end
196  end
197
198
199  def create_externs(output, tests, mocks)
200    output.puts("\n//=======External Functions This Runner Calls=====")
201    output.puts("extern void #{@options[:setup_name]}(void);")
202    output.puts("extern void #{@options[:teardown_name]}(void);")
203
204    tests.each do |test|
205      output.puts("extern void #{test[:test]}(#{test[:call] || 'void'});")
206    end
207    output.puts('')
208  end
209
210
211  def create_mock_management(output, mocks)
212    unless (mocks.empty?)
213      output.puts("\n//=======Mock Management=====")
214      output.puts("static void CMock_Init(void)")
215      output.puts("{")
216      if @options[:enforce_strict_ordering]
217        output.puts("  GlobalExpectCount = 0;")
218        output.puts("  GlobalVerifyOrder = 0;")
219        output.puts("  GlobalOrderError = NULL;")
220      end
221      mocks.each do |mock|
222        mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
223        output.puts("  #{mock_clean}_Init();")
224      end
225      output.puts("}\n")
226
227      output.puts("static void CMock_Verify(void)")
228      output.puts("{")
229      mocks.each do |mock|
230        mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
231        output.puts("  #{mock_clean}_Verify();")
232      end
233      output.puts("}\n")
234
235      output.puts("static void CMock_Destroy(void)")
236      output.puts("{")
237      mocks.each do |mock|
238        mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
239        output.puts("  #{mock_clean}_Destroy();")
240      end
241      output.puts("}\n")
242    end
243  end
244
245
246  def create_suite_setup_and_teardown(output)
247    unless (@options[:suite_setup].nil?)
248      output.puts("\n//=======Suite Setup=====")
249      output.puts("static void suite_setup(void)")
250      output.puts("{")
251      output.puts(@options[:suite_setup])
252      output.puts("}")
253    end
254    unless (@options[:suite_teardown].nil?)
255      output.puts("\n//=======Suite Teardown=====")
256      output.puts("static int suite_teardown(int num_failures)")
257      output.puts("{")
258      output.puts(@options[:suite_teardown])
259      output.puts("}")
260    end
261  end
262
263
264  def create_runtest(output, used_mocks)
265    cexception = @options[:plugins].include? :cexception
266    va_args1   = @options[:use_param_tests] ? ', ...' : ''
267    va_args2   = @options[:use_param_tests] ? '__VA_ARGS__' : ''
268    output.puts("\n//=======Test Runner Used To Run Each Test Below=====")
269    output.puts("#define RUN_TEST_NO_ARGS") if @options[:use_param_tests]
270    output.puts("#define RUN_TEST(TestFunc, TestLineNum#{va_args1}) \\")
271    output.puts("{ \\")
272    output.puts("  Unity.CurrentTestName = #TestFunc#{va_args2.empty? ? '' : " \"(\" ##{va_args2} \")\""}; \\")
273    output.puts("  Unity.CurrentTestLineNumber = TestLineNum; \\")
274    output.puts("  Unity.NumberOfTests++; \\")
275    output.puts("  CMock_Init(); \\") unless (used_mocks.empty?)
276    output.puts("  if (TEST_PROTECT()) \\")
277    output.puts("  { \\")
278    output.puts("    CEXCEPTION_T e; \\") if cexception
279    output.puts("    Try { \\") if cexception
280    output.puts("      #{@options[:setup_name]}(); \\")
281
282
283    output.puts("      TestFunc(#{va_args2}); \\")
284
285    output.puts("    } Catch(e) { TEST_ASSERT_EQUAL_HEX32_MESSAGE(CEXCEPTION_NONE, e, \"Unhandled Exception!\"); } \\") if cexception
286    output.puts("  } \\")
287
288    output.puts("  if (TEST_PROTECT() && !TEST_IS_IGNORED) \\")
289    output.puts("  { \\")
290    output.puts("    #{@options[:teardown_name]}(); \\")
291    output.puts("    CMock_Verify(); \\") unless (used_mocks.empty?)
292
293    output.puts("  } \\")
294    output.puts("  CMock_Destroy(); \\") unless (used_mocks.empty?)
295    output.puts("  UnityConcludeTest(); \\")
296    output.puts("}\n")
297  end
298
299
300  def create_reset(output, used_mocks)
301    output.puts("\n//=======Test Reset Option=====")
302    output.puts("void resetTest(void);")
303    output.puts("void resetTest(void)")
304
305    output.puts("{")
306    output.puts("  CMock_Verify();") unless (used_mocks.empty?)
307    output.puts("  CMock_Destroy();") unless (used_mocks.empty?)
308    output.puts("  #{@options[:teardown_name]}();")
309
310    output.puts("  CMock_Init();") unless (used_mocks.empty?)
311    output.puts("  #{@options[:setup_name]}();")
312
313    output.puts("}")
314  end
315
316
317  def create_main(output, filename, tests, used_mocks)
318    output.puts("\nchar const *progname;\n")
319    output.puts("\n\n//=======MAIN=====")
320
321    output.puts("int main(int argc, char *argv[])")
322    output.puts("{")
323    output.puts("  progname = argv[0];\n")
324
325
326    modname = filename.split(/[\/\\]/).last
327
328
329
330    output.puts("  suite_setup();") unless @options[:suite_setup].nil?
331
332    output.puts("  UnityBegin(\"#{modname}\");")
333
334    if (@options[:use_param_tests])
335      tests.each do |test|
336        if ((test[:args].nil?) or (test[:args].empty?))
337          output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]}, RUN_TEST_NO_ARGS);")
338        else
339          test[:args].each {|args| output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]}, #{args});")}
340        end
341      end
342    else
343        tests.each { |test| output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]});") }
344    end
345    output.puts()
346    output.puts("  CMock_Guts_MemFreeFinal();") unless used_mocks.empty?
347    output.puts("  return #{@options[:suite_teardown].nil? ? "" : "suite_teardown"}(UnityEnd());")
348    output.puts("}")
349  end
350end
351
352
353if ($0 == __FILE__)
354  options = { :includes => [] }
355  yaml_file = nil
356
357
358  #parse out all the options first (these will all be removed as we go)
359  ARGV.reject! do |arg|
360    case(arg)
361      when '-cexception'
362        options[:plugins] = [:cexception]; true
363      when /\.*\.ya?ml/
364
365        options = UnityTestRunnerGenerator.grab_config(arg); true
366      when /\.*\.h/
367        options[:includes] << arg; true
368      when /--(\w+)=\"?(.*)\"?/
369        options[$1.to_sym] = $2; true
370      else false
371    end
372  end
373
374
375  #make sure there is at least one parameter left (the input file)
376  if !ARGV[0]
377    puts ["\nusage: ruby #{__FILE__} (files) (options) input_test_file (output)",
378           "\n  input_test_file         - this is the C file you want to create a runner for",
379           "  output                  - this is the name of the runner file to generate",
380           "                            defaults to (input_test_file)_Runner",
381           "  files:",
382           "    *.yml / *.yaml        - loads configuration from here in :unity or :cmock",
383           "    *.h                   - header files are added as #includes in runner",
384           "  options:",
385
386           "    -cexception           - include cexception support",
387           "    --setup_name=\"\"       - redefine setUp func name to something else",
388           "    --teardown_name=\"\"    - redefine tearDown func name to something else",
389           "    --test_prefix=\"\"      - redefine test prefix from default test|spec|should",
390           "    --suite_setup=\"\"      - code to execute for setup of entire suite",
391           "    --suite_teardown=\"\"   - code to execute for teardown of entire suite",
392           "    --use_param_tests=1   - enable parameterized tests (disabled by default)",
393          ].join("\n")
394    exit 1
395  end
396
397
398  #create the default test runner name if not specified
399  ARGV[1] = ARGV[0].gsub(".c","_Runner.c") if (!ARGV[1])
400
401
402
403
404
405
406  UnityTestRunnerGenerator.new(options).run(ARGV[0], ARGV[1])
407end
408
409