Merge pull request #57 from ThrowTheSwitch/bug/encoding

Support different encoding styles and force to something we can work with
This commit is contained in:
Mark VanderVoord
2014-03-07 14:07:30 -05:00

View File

@ -4,6 +4,7 @@
# [Released under MIT License. Please refer to license.txt for details] # [Released under MIT License. Please refer to license.txt for details]
# ========================================== # ==========================================
$QUICK_RUBY_VERSION = RUBY_VERSION.split('.').inject(0){|vv,v| vv * 100 + v.to_i }
File.expand_path(File.join(File.dirname(__FILE__),'colour_prompt')) File.expand_path(File.join(File.dirname(__FILE__),'colour_prompt'))
class UnityTestRunnerGenerator class UnityTestRunnerGenerator
@ -38,11 +39,11 @@ class UnityTestRunnerGenerator
module_name = File.basename(input_file) module_name = File.basename(input_file)
#pull required data from source file #pull required data from source file
File.open(input_file, 'r') do |input| source = File.read(input_file)
tests = find_tests(input) source = source.force_encoding("ISO-8859-1").encode("utf-8", replace: nil) if ($QUICK_RUBY_VERSION > 10900)
testfile_includes = find_includes(input) tests = find_tests(source)
used_mocks = find_mocks(testfile_includes) testfile_includes = find_includes(source)
end used_mocks = find_mocks(testfile_includes)
#build runner file #build runner file
generate(input_file, output_file, tests, used_mocks, testfile_includes) generate(input_file, output_file, tests, used_mocks, testfile_includes)
@ -65,14 +66,12 @@ class UnityTestRunnerGenerator
end end
end end
def find_tests(input_file) def find_tests(source)
tests_raw = [] tests_raw = []
tests_args = [] tests_args = []
tests_and_line_numbers = [] tests_and_line_numbers = []
input_file.rewind source_scrubbed = source.gsub(/\/\/.*$/, '') # remove line comments
source_raw = input_file.read
source_scrubbed = source_raw.gsub(/\/\/.*$/, '') # remove line comments
source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments
lines = source_scrubbed.split(/(^\s*\#.*$) # Treat preprocessor directives as a logical line lines = source_scrubbed.split(/(^\s*\#.*$) # Treat preprocessor directives as a logical line
| (;|\{|\}) /x) # Match ;, {, and } as end of lines | (;|\{|\}) /x) # Match ;, {, and } as end of lines
@ -94,7 +93,7 @@ class UnityTestRunnerGenerator
end end
#determine line numbers and create tests to run #determine line numbers and create tests to run
source_lines = source_raw.split("\n") source_lines = source.split("\n")
source_index = 0; source_index = 0;
tests_and_line_numbers.size.times do |i| tests_and_line_numbers.size.times do |i|
source_lines[source_index..-1].each_with_index do |line, index| source_lines[source_index..-1].each_with_index do |line, index|
@ -109,11 +108,7 @@ class UnityTestRunnerGenerator
return tests_and_line_numbers return tests_and_line_numbers
end end
def find_includes(input_file) def find_includes(source)
input_file.rewind
#read in file
source = input_file.read
#remove comments (block and line, in three steps to ensure correct precedence) #remove comments (block and line, in three steps to ensure correct precedence)
source.gsub!(/\/\/(?:.+\/\*|\*(?:$|[^\/])).*$/, '') # remove line comments that comment out the start of blocks source.gsub!(/\/\/(?:.+\/\*|\*(?:$|[^\/])).*$/, '') # remove line comments that comment out the start of blocks
@ -122,9 +117,9 @@ class UnityTestRunnerGenerator
#parse out includes #parse out includes
includes = source.scan(/^\s*#include\s+\"\s*(.+)\.[hH]\s*\"/).flatten includes = source.scan(/^\s*#include\s+\"\s*(.+)\.[hH]\s*\"/).flatten
brackets_includes = source.scan(/^\s*#include\s+<\s*(.+)\s*>/).flatten brackets_includes = source.scan(/^\s*#include\s+<\s*(.+)\s*>/).flatten
brackets_includes.each { |inc| includes << '<' + inc +'>' } brackets_includes.each { |inc| includes << '<' + inc +'>' }
return includes return includes
end end
def find_mocks(includes) def find_mocks(includes)