1module CppUTestToUnityUtils
2
3  def convert_test_filename_to_unity_filename(testpath)
4    testpath.sub(/tests\/(.*)\.cpp/, "unity/\\1.c")
5  end
6
7  def convert_test_filename_to_unity_testrunner_filename(testpath)
8    testpath.sub(/tests\/(.*)\.cpp/, "unity/\\1_runner.c")
9  end
10
11  def convert_one_liners(line, group)
12    line.sub!(/#include "CppUTest\/TestHarness.h\"/, "#include \"unity_fixture.h\"" )
13    line.sub!(/FAIL\(/, 'TEST_FAIL(')
14    line.sub!(/CHECK\(/, "TEST_ASSERT_TRUE(")
15    line.sub!(/CHECK_TRUE\(/, "TEST_ASSERT_TRUE(")
16    line.sub!(/CHECK_FALSE\(/, "TEST_ASSERT_FALSE(")
17    line.sub!(/LONGS_EQUAL\(/, "TEST_ASSERT_EQUAL(")
18    line.sub!(/BYTES_EQUAL\(/, "TEST_ASSERT_EQUAL_HEX8(")
19    line.sub!(/STRCMP_EQUAL\(/, "TEST_ASSERT_EQUAL_STRING(")
20    line.sub!(/DOUBLES_EQUAL\(/, "TEST_ASSERT_FLOAT_WITHIN(")
21    line.sub!(/ POINTERS_EQUAL\(/, " TEST_ASSERT_POINTERS_EQUAL(")
22    line.sub!(/CHECK_EQUAL\(true,/, "TEST_ASSERT_TRUE(")
23    line.sub!(/CHECK_EQUAL\(false,/, "TEST_ASSERT_FALSE(")
24    line.sub!(/CHECK_EQUAL\(/, "TEST_ASSERT_EQUAL(")
25    #line.sub!(/static void setup\(/, "TEST_SETUP(" + group)
26    #line.sub!(/static void teardown\(/, "TEST_TEAR_DOWN(" + group)
27  end
28
29  def convert_setup(lines, group)
30    lines.each do | line |
31        if line.sub!(/static void setup\(/, "TEST_SETUP(" + group)
32          return
33        end
34    end
35  end
36
37  def convert_teardown(lines, group)
38    lines.each do | line |
39      if line.sub!(/static void teardown\(/, "TEST_TEAR_DOWN(" + group)
40        return
41      end
42    end
43  end
44
45  def convert_macros(lines, groups)
46    groups.each do | group |
47      lines.each do | line |
48        convert_one_liners(line, group)
49      end
50      convert_setup(lines, group)
51      convert_teardown(lines, group)
52    end
53  end
54
55  def get_test_group(lines)
56    @test_group = "None"
57    lines.each do | line |
58      if /TEST_GROUP/ =~ line
59        @test_group = line.split(/[()]/)[1]
60      end
61    end
62    @test_group
63  end
64
65  def get_test_groups(lines)
66    @test_groups = []
67    i = 0
68    lines.each do | line |
69      if /TEST_GROUP/ =~ line
70        @test_groups[i] = line.split(/[()]/)[1]
71        i = i + 1
72      end
73    end
74    @test_groups
75  end
76
77  def adjust_tabs(lines)
78    lines.each do | line |
79
80    line.gsub!(/^ {2,3}(\w+)/,  "    \\1")
81    line.gsub!(/\t/, "    ")
82  end
83  end
84
85  def include_line?(line)
86    /\#include/ =~ line
87  end
88
89  def convert_member_to_static(line)
90    line.gsub!(/^\s*(\w)/, "static \\1")
91  end
92
93  def add_semicolon_to_end_of_test_group_line(line)
94    line.gsub!(/\)/, ");")
95  end
96
97  def consume_closing_curley_brace(line)
98    line.gsub!(/\}\;*[ \t]*(.*)/, "\\1")
99  end
100
101  def outdent(line)
102    line.gsub!(/^    /, "")
103  end
104
105  def consume_opening_curley_brace(line)
106   line.gsub!(/\{/, "")
107  end
108
109  def demote_group(group, lines)
110    test_group = "None"
111    scope_level = 0
112    in_test_group = false
113    lines.each do | line |
114      next if include_line?(line)
115
116      if !in_test_group
117        if line.match(group)
118          add_semicolon_to_end_of_test_group_line(line)
119          in_test_group = true
120        end
121        next
122      end
123
124      if line.include?("}")
125        scope_level -= 1
126      elsif line.include?("{")
127        scope_level += 1
128      end
129
130      outdent(line)
131
132      if scope_level == 1
133        convert_member_to_static(line)
134        consume_opening_curley_brace(line)
135      end
136      if scope_level == 0
137        consume_closing_curley_brace(line)
138        in_test_group = false
139      end
140    end
141  end
142
143def search_and_destroy(pattern, lines)
144  lines.each do | line |
145    line.gsub!(pattern, "")
146    end
147end
148
149def extern_c_line?(line)
150  line.match(/extern \"C\"/)
151end
152
153def  remove_extern_c(lines)
154  in_extern_c = false;
155  scope_level = 0
156
157  lines.each do | line |
158    if !in_extern_c
159      if extern_c_line?(line)
160        in_extern_c = true
161        line.gsub!(/extern \"C\"/, "")
162      end
163      next
164    end
165
166    # next if ! in_extern_c and ! extern_c_line?(line)
167    #
168    # in_extern_c = true
169    if line.include?("{")
170      scope_level += 1
171    elsif line.include?("}")
172      scope_level -= 1
173    end
174
175    if scope_level == 1
176      consume_opening_curley_brace(line)
177    end
178
179    if scope_level == 0 and line.include?("}")
180      in_extern_c = false
181      consume_closing_curley_brace(line)
182    end
183  end
184end
185
186
187  def test_declaration?(line, group)
188    /TEST\(#{group}/ =~ line
189  end
190
191  def generate_group_runner(group, lines)
192    group_runner = []
193    group_runner << "/* Make sure you invoke RUN_TEST_GROUP(" + group + ") from unity main */\n\n"
194    group_runner << "TEST_GROUP_RUNNER(" + group + ")\n"
195    group_runner << "{\n"
196    lines.each do | line |
197      if test_declaration?(line, group)
198         temp = line.clone
199         temp.sub!(/\n/, "")
200         temp.sub!(/^IGNORE_/, "")
201         temp.sub!(/^TEST/, "    RUN_TEST_CASE")
202         group_runner <<  temp + ";\n"
203      end
204    end
205      group_runner << "}\n\n"
206  end
207
208  def generate_group_runners(groups, lines)
209    group_runners = []
210    group_runners << "/* Generated code, edit at your own risk */\n\n"
211    group_runners << "#include \"unity_fixture.h\"\n\n"
212    groups.each do | group |
213      group_runners.concat generate_group_runner(group, lines)
214    end
215    group_runners
216  end
217
218  def generate_group_runner_plainUnity(group, lines)
219    prototypes = []
220    callers = []
221    group_runner = []
222    lines.each do | line |
223      if /void test.*\(.*\)/ =~ line
224        temp = line.clone
225        temp.sub!(/\n/, "")
226        prototypes << temp + ";"
227        temp.sub!(/void /, "    ")
228        temp.sub!(/\(void\)/, "()")
229        callers <<  temp
230      end
231    end
232    group_runner << "\n"
233    group_runner << "//Generated code, edit at your own risk\n\n"
234    group_runner << "\#include \"unity_fixture.h\"\n\n"
235    group_runner << prototypes
236    group_runner << "\n\nTEST_GROUP_RUNNER(" + group + ")\n"
237    group_runner << "{\n"
238    group_runner << callers
239    group_runner << "}\n"
240  end
241
242  def write_lines_to_file(file, lines)
243    lines.each do | line |
244      file.write(line)
245    end
246  end
247end