generate_test_runner.rb 12.1 KB
Newer Older
1 2 3 4
# ==========================================
#   Unity Project - A Test Framework for C
#   Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
#   [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 }
8
File.expand_path(File.join(File.dirname(__FILE__),'colour_prompt'))
9 10 11

class UnityTestRunnerGenerator

12
  def initialize(options = nil)
13
    @options = { :includes => [], :plugins => [], :framework => :unity }
14 15
    case(options)
      when NilClass then @options
16 17
      when String   then @options.merge!(UnityTestRunnerGenerator.grab_config(options))
      when Hash     then @options.merge!(options)
18 19 20
      else          raise "If you specify arguments, it should be a filename or a hash of options"
    end
  end
21

M
mkarlesky 已提交
22
  def self.grab_config(config_file)
23
    options = { :includes => [], :plugins => [], :framework => :unity }
24 25
    unless (config_file.nil? or config_file.empty?)
      require 'yaml'
26
      yaml_guts = YAML.load_file(config_file)
27 28
      options.merge!(yaml_guts[:unity] ? yaml_guts[:unity] : yaml_guts[:cmock])
      raise "No :unity or :cmock section found in #{config_file}" unless options
29
    end
30
    return(options)
31 32
  end

33
  def run(input_file, output_file, options=nil)
34
    tests = []
35
    testfile_includes = []
36
    used_mocks = []
37

38
    @options.merge!(options) unless options.nil?
39
    module_name = File.basename(input_file)
40

41
    #pull required data from source file
42
    source = File.read(input_file)
43
    source = source.force_encoding("ISO-8859-1").encode("utf-8", :replace => nil) if ($QUICK_RUBY_VERSION > 10900)
44 45 46
    tests               = find_tests(source)
    testfile_includes   = find_includes(source)
    used_mocks          = find_mocks(testfile_includes)
47

48
    #build runner file
S
shellyniz 已提交
49
    generate(input_file, output_file, tests, used_mocks, testfile_includes)
50

51 52
    #determine which files were used to return them
    all_files_used = [input_file, output_file]
53
    all_files_used += testfile_includes.map {|filename| filename + '.c'} unless testfile_includes.empty?
54 55 56
    all_files_used += @options[:includes] unless @options[:includes].empty?
    return all_files_used.uniq
  end
57

S
shellyniz 已提交
58
  def generate(input_file, output_file, tests, used_mocks, testfile_includes)
59
    File.open(output_file, 'w') do |output|
S
shellyniz 已提交
60
      create_header(output, used_mocks, testfile_includes)
61 62
      create_externs(output, tests, used_mocks)
      create_mock_management(output, used_mocks)
63
      create_suite_setup_and_teardown(output)
64
      create_reset(output, used_mocks)
65
      create_main(output, input_file, tests)
66 67
    end
  end
68 69

  def find_tests(source)
70
    tests_raw = []
71
    tests_args = []
72
    tests_and_line_numbers = []
73 74

    source_scrubbed = source.gsub(/\/\/.*$/, '')               # remove line comments
75 76 77
    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
78 79

    lines.each_with_index do |line, index|
80 81
      #find tests
      if line =~ /^((?:\s*TEST_CASE\s*\(.*?\)\s*)*)\s*void\s+(test.*?)\s*\(\s*(.*)\s*\)/
82
        arguments = $1
83 84
        name = $2
        call = $3
85 86 87 88 89
        args = nil
        if (@options[:use_param_tests] and !arguments.empty?)
          args = []
          arguments.scan(/\s*TEST_CASE\s*\((.*)\)\s*$/) {|a| args << a[0]}
        end
90
        tests_and_line_numbers << { :test => name, :args => args, :call => call, :line_number => 0 }
91
        tests_args = []
92 93 94
      end
    end

95
    #determine line numbers and create tests to run
96
    source_lines = source.split("\n")
97
    source_index = 0;
98
    tests_and_line_numbers.size.times do |i|
99
      source_lines[source_index..-1].each_with_index do |line, index|
100
        if (line =~ /#{tests_and_line_numbers[i][:test]}/)
101
          source_index += index
102
          tests_and_line_numbers[i][:line_number] = source_index + 1
103 104
          break
        end
105 106
      end
    end
107

108
    return tests_and_line_numbers
109 110
  end

111 112
  def find_includes(source)

113 114
    #remove comments (block and line, in three steps to ensure correct precedence)
    source.gsub!(/\/\/(?:.+\/\*|\*(?:$|[^\/])).*$/, '')  # remove line comments that comment out the start of blocks
115
    source.gsub!(/\/\*.*?\*\//m, '')                     # remove block comments
116
    source.gsub!(/\/\/.*$/, '')                          # remove line comments (all that remain)
117

118
    #parse out includes
119 120 121 122
    includes = source.scan(/^\s*#include\s+\"\s*(.+)\.[hH]\s*\"/).flatten
    brackets_includes = source.scan(/^\s*#include\s+<\s*(.+)\s*>/).flatten
    brackets_includes.each { |inc| includes << '<' + inc +'>' }
    return includes
123
  end
124

125 126 127
  def find_mocks(includes)
    mock_headers = []
    includes.each do |include_file|
M
mvandervoord 已提交
128
      mock_headers << File.basename(include_file) if (include_file =~ /^mock/i)
129
    end
130
    return mock_headers
131
  end
132

S
shellyniz 已提交
133
  def create_header(output, mocks, testfile_includes)
134
    output.puts('/* AUTOGENERATED FILE. DO NOT EDIT. */')
135 136
    create_runtest(output, mocks)
    output.puts("\n//=======Automagically Detected Files To Include=====")
M
mvandervoord 已提交
137
    output.puts("#include \"#{@options[:framework].to_s}.h\"")
138
    output.puts('#include "cmock.h"') unless (mocks.empty?)
139
    @options[:includes].flatten.uniq.compact.each do |inc|
M
mvandervoord 已提交
140
      output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
141 142 143
    end
    output.puts('#include <setjmp.h>')
    output.puts('#include <stdio.h>')
144
    output.puts('#include "CException.h"') if @options[:plugins].include?(:cexception)
S
shellyniz 已提交
145
	testfile_includes.delete("unity").delete("cmock")
146 147 148
	testrunner_includes = testfile_includes - mocks
	testrunner_includes.each do |inc|
	  output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}")
S
shellyniz 已提交
149
	end
150 151 152
    mocks.each do |mock|
      output.puts("#include \"#{mock.gsub('.h','')}.h\"")
    end
153
    if @options[:enforce_strict_ordering]
154 155 156 157
      output.puts('')
      output.puts('int GlobalExpectCount;')
      output.puts('int GlobalVerifyOrder;')
      output.puts('char* GlobalOrderError;')
158
    end
159
  end
160

161
  def create_externs(output, tests, mocks)
162
    output.puts("\n//=======External Functions This Runner Calls=====")
163 164 165
    output.puts("extern void setUp(void);")
    output.puts("extern void tearDown(void);")
    tests.each do |test|
166
      output.puts("extern void #{test[:test]}(#{test[:call] || 'void'});")
167 168 169
    end
    output.puts('')
  end
170

171 172
  def create_mock_management(output, mocks)
    unless (mocks.empty?)
173
      output.puts("\n//=======Mock Management=====")
174 175
      output.puts("static void CMock_Init(void)")
      output.puts("{")
176
      if @options[:enforce_strict_ordering]
177
        output.puts("  GlobalExpectCount = 0;")
178 179
        output.puts("  GlobalVerifyOrder = 0;")
        output.puts("  GlobalOrderError = NULL;")
180
      end
181
      mocks.each do |mock|
182 183
        mock_clean = mock.gsub(/(?:-|\s+)/, "_")
        output.puts("  #{mock_clean}_Init();")
184 185 186 187 188 189
      end
      output.puts("}\n")

      output.puts("static void CMock_Verify(void)")
      output.puts("{")
      mocks.each do |mock|
190 191
        mock_clean = mock.gsub(/(?:-|\s+)/, "_")
        output.puts("  #{mock_clean}_Verify();")
192 193 194 195 196 197
      end
      output.puts("}\n")

      output.puts("static void CMock_Destroy(void)")
      output.puts("{")
      mocks.each do |mock|
198 199
        mock_clean = mock.gsub(/(?:-|\s+)/, "_")
        output.puts("  #{mock_clean}_Destroy();")
200 201 202 203
      end
      output.puts("}\n")
    end
  end
204

205 206
  def create_suite_setup_and_teardown(output)
    unless (@options[:suite_setup].nil?)
207
      output.puts("\n//=======Suite Setup=====")
208 209 210 211 212 213
      output.puts("static int suite_setup(void)")
      output.puts("{")
      output.puts(@options[:suite_setup])
      output.puts("}")
    end
    unless (@options[:suite_teardown].nil?)
214
      output.puts("\n//=======Suite Teardown=====")
215 216 217 218 219 220
      output.puts("static int suite_teardown(int num_failures)")
      output.puts("{")
      output.puts(@options[:suite_teardown])
      output.puts("}")
    end
  end
221

222
  def create_runtest(output, used_mocks)
223
    cexception = @options[:plugins].include? :cexception
224 225 226
    va_args1   = @options[:use_param_tests] ? ', ...' : ''
    va_args2   = @options[:use_param_tests] ? '__VA_ARGS__' : ''
    output.puts("\n//=======Test Runner Used To Run Each Test Below=====")
227
    output.puts("#define RUN_TEST_NO_ARGS") if @options[:use_param_tests]
228 229
    output.puts("#define RUN_TEST(TestFunc, TestLineNum#{va_args1}) \\")
    output.puts("{ \\")
M
mvandervoord 已提交
230
    output.puts("  Unity.CurrentTestName = #TestFunc#{va_args2.empty? ? '' : " \"(\" ##{va_args2} \")\""}; \\")
231 232
    output.puts("  Unity.CurrentTestLineNumber = TestLineNum; \\")
    output.puts("  Unity.NumberOfTests++; \\")
233
    output.puts("  CMock_Init(); \\") unless (used_mocks.empty?)
234 235 236 237 238 239 240 241 242 243 244
    output.puts("  if (TEST_PROTECT()) \\")
    output.puts("  { \\")
    output.puts("    CEXCEPTION_T e; \\") if cexception
    output.puts("    Try { \\") if cexception
    output.puts("      setUp(); \\")
    output.puts("      TestFunc(#{va_args2}); \\")
    output.puts("    } Catch(e) { TEST_ASSERT_EQUAL_HEX32_MESSAGE(CEXCEPTION_NONE, e, \"Unhandled Exception!\"); } \\") if cexception
    output.puts("  } \\")
    output.puts("  if (TEST_PROTECT() && !TEST_IS_IGNORED) \\")
    output.puts("  { \\")
    output.puts("    tearDown(); \\")
245
    output.puts("    CMock_Verify(); \\") unless (used_mocks.empty?)
246
    output.puts("  } \\")
247
    output.puts("  CMock_Destroy(); \\") unless (used_mocks.empty?)
248 249
    output.puts("  UnityConcludeTest(); \\")
    output.puts("}\n")
250
  end
251

252
  def create_reset(output, used_mocks)
253
    output.puts("\n//=======Test Reset Option=====")
254 255 256 257 258
    output.puts("void resetTest()")
    output.puts("{")
    output.puts("  CMock_Verify();") unless (used_mocks.empty?)
    output.puts("  CMock_Destroy();") unless (used_mocks.empty?)
    output.puts("  tearDown();")
259
    output.puts("  CMock_Init();") unless (used_mocks.empty?)
260 261 262
    output.puts("  setUp();")
    output.puts("}")
  end
263

264
  def create_main(output, filename, tests)
265
    output.puts("\n\n//=======MAIN=====")
266
    output.puts("int main(void)")
267
    output.puts("{")
268
    output.puts("  suite_setup();") unless @options[:suite_setup].nil?
269
    output.puts("  UnityBegin();")
270
    output.puts("  Unity.TestFile = \"#{filename}\";")
271 272 273
    if (@options[:use_param_tests])
      tests.each do |test|
        if ((test[:args].nil?) or (test[:args].empty?))
274
          output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]}, RUN_TEST_NO_ARGS);")
275
        else
276
          test[:args].each {|args| output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]}, #{args});")}
277
        end
278
      end
279
    else
280
        tests.each { |test| output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]});") }
281 282
    end
    output.puts()
283
    output.puts("  return #{@options[:suite_teardown].nil? ? "" : "suite_teardown"}(UnityEnd());")
284 285 286 287 288 289
    output.puts("}")
  end
end


if ($0 == __FILE__)
290 291
  options = { :includes => [] }
  yaml_file = nil
292

293
  #parse out all the options first
294
  ARGV.reject! do |arg|
295
    case(arg)
296
      when '-cexception'
M
mvandervoord 已提交
297
        options[:plugins] = [:cexception]; true
M
mvandervoord 已提交
298
      when /\.*\.yml/
M
mvandervoord 已提交
299
        options = UnityTestRunnerGenerator.grab_config(arg); true
300
      else false
301
    end
302 303
  end

304
  #make sure there is at least one parameter left (the input file)
305
  if !ARGV[0]
306
    puts ["usage: ruby #{__FILE__} (yaml) (options) input_test_file output_test_runner (includes)",
307
           "  blah.yml    - will use config options in the yml file (see docs)",
308
           "  -cexception - include cexception support"].join("\n")
309 310
    exit 1
  end
311

312
  #create the default test runner name if not specified
313
  ARGV[1] = ARGV[0].gsub(".c","_Runner.c") if (!ARGV[1])
314

315
  #everything else is an include file
M
mvandervoord 已提交
316
  options[:includes] ||= (ARGV.slice(2..-1).flatten.compact) if (ARGV.size > 2)
317

318
  UnityTestRunnerGenerator.new(options).run(ARGV[0], ARGV[1])
319
end