generate_test_runner.rb 15.0 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 = UnityTestRunnerGenerator.default_options
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
      else          raise "If you specify arguments, it should be a filename or a hash of options"
    end
20
    require "#{File.expand_path(File.dirname(__FILE__))}/type_sanitizer"
21
  end
22

23
  def self.default_options
24 25 26 27 28 29 30 31 32 33
    {
      :includes      => [],
      :plugins       => [],
      :framework     => :unity,
      :test_prefix   => "test|spec|should",
      :setup_name    => "setUp",
      :teardown_name => "tearDown",
    }
  end

M
mkarlesky 已提交
34
  def self.grab_config(config_file)
35
    options = self.default_options
36 37
    unless (config_file.nil? or config_file.empty?)
      require 'yaml'
38
      yaml_guts = YAML.load_file(config_file)
39
      options.merge!(yaml_guts[:unity] || yaml_guts[:cmock])
40
      raise "No :unity or :cmock section found in #{config_file}" unless options
41
    end
42
    return(options)
43 44
  end

45
  def run(input_file, output_file, options=nil)
46
    tests = []
47
    testfile_includes = []
48
    used_mocks = []
49

50
    @options.merge!(options) unless options.nil?
51
    module_name = File.basename(input_file)
52

53
    #pull required data from source file
54
    source = File.read(input_file)
55
    source = source.force_encoding("ISO-8859-1").encode("utf-8", :replace => nil) if ($QUICK_RUBY_VERSION > 10900)
56
    tests               = find_tests(source)
57
    headers             = find_includes(source)
58
    testfile_includes   = (headers[:local] + headers[:system])
59
    used_mocks          = find_mocks(testfile_includes)
60 61
    testfile_includes   = (testfile_includes - used_mocks)
    testfile_includes.delete_if{|inc| inc =~ /(unity|cmock)/}
62

63
    #build runner file
S
shellyniz 已提交
64
    generate(input_file, output_file, tests, used_mocks, testfile_includes)
65

66 67
    #determine which files were used to return them
    all_files_used = [input_file, output_file]
68
    all_files_used += testfile_includes.map {|filename| filename + '.c'} unless testfile_includes.empty?
69 70 71
    all_files_used += @options[:includes] unless @options[:includes].empty?
    return all_files_used.uniq
  end
72

S
shellyniz 已提交
73
  def generate(input_file, output_file, tests, used_mocks, testfile_includes)
74
    File.open(output_file, 'w') do |output|
S
shellyniz 已提交
75
      create_header(output, used_mocks, testfile_includes)
76 77
      create_externs(output, tests, used_mocks)
      create_mock_management(output, used_mocks)
78
      create_suite_setup_and_teardown(output)
79
      create_reset(output, used_mocks)
80
      create_main(output, input_file, tests, used_mocks)
81
    end
82 83 84 85 86 87

    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
88
  end
89 90

  def find_tests(source)
91
    tests_and_line_numbers = []
92 93

    source_scrubbed = source.gsub(/\/\/.*$/, '')               # remove line comments
94 95 96
    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
97 98

    lines.each_with_index do |line, index|
99
      #find tests
100
      if line =~ /^((?:\s*TEST_CASE\s*\(.*?\)\s*)*)\s*void\s+((?:#{@options[:test_prefix]}).*)\s*\(\s*(.*)\s*\)/
101
        arguments = $1
102 103
        name = $2
        call = $3
104
        params = $4
105 106 107 108 109
        args = nil
        if (@options[:use_param_tests] and !arguments.empty?)
          args = []
          arguments.scan(/\s*TEST_CASE\s*\((.*)\)\s*$/) {|a| args << a[0]}
        end
110
        tests_and_line_numbers << { :test => name, :args => args, :call => call, :params => params, :line_number => 0 }
111 112
      end
    end
113
    tests_and_line_numbers.uniq! {|v| v[:test] }
114

115
    #determine line numbers and create tests to run
116
    source_lines = source.split("\n")
117
    source_index = 0;
118
    tests_and_line_numbers.size.times do |i|
119
      source_lines[source_index..-1].each_with_index do |line, index|
120
        if (line =~ /#{tests_and_line_numbers[i][:test]}/)
121
          source_index += index
122
          tests_and_line_numbers[i][:line_number] = source_index + 1
123 124
          break
        end
125 126
      end
    end
127

128
    return tests_and_line_numbers
129 130
  end

131 132
  def find_includes(source)

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

138
    #parse out includes
139
    includes = {
140 141
      :local => source.scan(/^\s*#include\s+\"\s*(.+)\.[hH]\s*\"/).flatten,
      :system => source.scan(/^\s*#include\s+<\s*(.+)\s*>/).flatten.map { |inc| "<#{inc}>" }
142
    }
143
    return includes
144
  end
145

146 147 148
  def find_mocks(includes)
    mock_headers = []
    includes.each do |include_file|
M
mvandervoord 已提交
149
      mock_headers << File.basename(include_file) if (include_file =~ /^mock/i)
150
    end
151
    return mock_headers
152
  end
153

154
  def create_header(output, mocks, testfile_includes=[])
155
    output.puts('/* AUTOGENERATED FILE. DO NOT EDIT. */')
156 157
    create_runtest(output, mocks)
    output.puts("\n//=======Automagically Detected Files To Include=====")
M
mvandervoord 已提交
158
    output.puts("#include \"#{@options[:framework].to_s}.h\"")
159
    output.puts('#include "cmock.h"') unless (mocks.empty?)
160 161
    output.puts('#include <setjmp.h>')
    output.puts('#include <stdio.h>')
162
    output.puts('#include "CException.h"') if @options[:plugins].include?(:cexception)
163 164 165 166 167 168 169 170 171 172
    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
173 174 175
    mocks.each do |mock|
      output.puts("#include \"#{mock.gsub('.h','')}.h\"")
    end
176
    if @options[:enforce_strict_ordering]
177 178 179 180
      output.puts('')
      output.puts('int GlobalExpectCount;')
      output.puts('int GlobalVerifyOrder;')
      output.puts('char* GlobalOrderError;')
181
    end
182
  end
183

184
  def create_externs(output, tests, mocks)
185
    output.puts("\n//=======External Functions This Runner Calls=====")
186 187
    output.puts("extern void #{@options[:setup_name]}(void);")
    output.puts("extern void #{@options[:teardown_name]}(void);")
188
    tests.each do |test|
189
      output.puts("extern void #{test[:test]}(#{test[:call] || 'void'});")
190 191 192
    end
    output.puts('')
  end
193

194 195
  def create_mock_management(output, mocks)
    unless (mocks.empty?)
196
      output.puts("\n//=======Mock Management=====")
197 198
      output.puts("static void CMock_Init(void)")
      output.puts("{")
199
      if @options[:enforce_strict_ordering]
200
        output.puts("  GlobalExpectCount = 0;")
201 202
        output.puts("  GlobalVerifyOrder = 0;")
        output.puts("  GlobalOrderError = NULL;")
203
      end
204
      mocks.each do |mock|
205
        mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
206
        output.puts("  #{mock_clean}_Init();")
207 208 209 210 211 212
      end
      output.puts("}\n")

      output.puts("static void CMock_Verify(void)")
      output.puts("{")
      mocks.each do |mock|
213
        mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
214
        output.puts("  #{mock_clean}_Verify();")
215 216 217 218 219 220
      end
      output.puts("}\n")

      output.puts("static void CMock_Destroy(void)")
      output.puts("{")
      mocks.each do |mock|
221
        mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
222
        output.puts("  #{mock_clean}_Destroy();")
223 224 225 226
      end
      output.puts("}\n")
    end
  end
227

228 229
  def create_suite_setup_and_teardown(output)
    unless (@options[:suite_setup].nil?)
230
      output.puts("\n//=======Suite Setup=====")
231 232 233 234 235 236
      output.puts("static int suite_setup(void)")
      output.puts("{")
      output.puts(@options[:suite_setup])
      output.puts("}")
    end
    unless (@options[:suite_teardown].nil?)
237
      output.puts("\n//=======Suite Teardown=====")
238 239 240 241 242 243
      output.puts("static int suite_teardown(int num_failures)")
      output.puts("{")
      output.puts(@options[:suite_teardown])
      output.puts("}")
    end
  end
244

245
  def create_runtest(output, used_mocks)
246
    cexception = @options[:plugins].include? :cexception
247 248 249
    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=====")
250
    output.puts("#define RUN_TEST_NO_ARGS") if @options[:use_param_tests]
251 252
    output.puts("#define RUN_TEST(TestFunc, TestLineNum#{va_args1}) \\")
    output.puts("{ \\")
M
mvandervoord 已提交
253
    output.puts("  Unity.CurrentTestName = #TestFunc#{va_args2.empty? ? '' : " \"(\" ##{va_args2} \")\""}; \\")
254 255
    output.puts("  Unity.CurrentTestLineNumber = TestLineNum; \\")
    output.puts("  Unity.NumberOfTests++; \\")
256
    output.puts("  CMock_Init(); \\") unless (used_mocks.empty?)
257
    output.puts("  UNITY_CLR_DETAILS(); \\") unless (used_mocks.empty?)
258 259 260 261
    output.puts("  if (TEST_PROTECT()) \\")
    output.puts("  { \\")
    output.puts("    CEXCEPTION_T e; \\") if cexception
    output.puts("    Try { \\") if cexception
262
    output.puts("      #{@options[:setup_name]}(); \\")
263 264 265 266 267
    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("  { \\")
268
    output.puts("    #{@options[:teardown_name]}(); \\")
269
    output.puts("    CMock_Verify(); \\") unless (used_mocks.empty?)
270
    output.puts("  } \\")
271
    output.puts("  CMock_Destroy(); \\") unless (used_mocks.empty?)
272 273
    output.puts("  UnityConcludeTest(); \\")
    output.puts("}\n")
274
  end
275

276
  def create_reset(output, used_mocks)
277
    output.puts("\n//=======Test Reset Option=====")
278 279
    output.puts("void resetTest(void);")
    output.puts("void resetTest(void)")
280 281 282
    output.puts("{")
    output.puts("  CMock_Verify();") unless (used_mocks.empty?)
    output.puts("  CMock_Destroy();") unless (used_mocks.empty?)
283
    output.puts("  #{@options[:teardown_name]}();")
284
    output.puts("  CMock_Init();") unless (used_mocks.empty?)
285
    output.puts("  #{@options[:setup_name]}();")
286 287
    output.puts("}")
  end
288

289
  def create_main(output, filename, tests, used_mocks)
290
    output.puts("\n\n//=======MAIN=====")
291
    output.puts("int main(void)")
292
    output.puts("{")
293
    output.puts("  suite_setup();") unless @options[:suite_setup].nil?
294
    output.puts("  UnityBegin(\"#{filename.gsub(/\\/,'\\\\')}\");")
295 296 297
    if (@options[:use_param_tests])
      tests.each do |test|
        if ((test[:args].nil?) or (test[:args].empty?))
298
          output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]}, RUN_TEST_NO_ARGS);")
299
        else
300
          test[:args].each {|args| output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]}, #{args});")}
301
        end
302
      end
303
    else
304
        tests.each { |test| output.puts("  RUN_TEST(#{test[:test]}, #{test[:line_number]});") }
305 306
    end
    output.puts()
307
    output.puts("  CMock_Guts_MemFreeFinal();") unless used_mocks.empty?
308
    output.puts("  return #{@options[:suite_teardown].nil? ? "" : "suite_teardown"}(UnityEnd());")
309 310 311
    output.puts("}")
  end

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  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
328 329

if ($0 == __FILE__)
330 331
  options = { :includes => [] }
  yaml_file = nil
332

333
  #parse out all the options first (these will all be removed as we go)
334
  ARGV.reject! do |arg|
335
    case(arg)
336
      when '-cexception'
M
mvandervoord 已提交
337
        options[:plugins] = [:cexception]; true
338
      when /\.*\.ya?ml/
M
mvandervoord 已提交
339
        options = UnityTestRunnerGenerator.grab_config(arg); true
340 341
      when /\.*\.h/
        options[:includes] << arg; true
342 343
      when /--(\w+)=\"?(.*)\"?/
        options[$1.to_sym] = $2; true
344
      else false
345
    end
346 347
  end

348
  #make sure there is at least one parameter left (the input file)
349
  if !ARGV[0]
350 351 352 353 354 355 356
    puts ["\nusage: ruby #{__FILE__} (files) (options) input_test_file (output)",
           "\n  input_test_file         - this is the C file you want to create a runner for",
           "  output                  - this is the name of the runner file to generate",
           "                            defaults to (input_test_file)_Runner",
           "  files:",
           "    *.yml / *.yaml        - loads configuration from here in :unity or :cmock",
           "    *.h                   - header files are added as #includes in runner",
357
           "  options:",
358 359 360 361 362 363 364
           "    -cexception           - include cexception support",
           "    --setup_name=\"\"       - redefine setUp func name to something else",
           "    --teardown_name=\"\"    - redefine tearDown 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)",
365
           "    --header_file=\"\"      - path/name of test header file to generate too"
366
          ].join("\n")
367 368
    exit 1
  end
369

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

373
  puts UnityTestRunnerGenerator.new(options).run(ARGV[0], ARGV[1]).inspect
374
end