Debian Codegraph @

scripts/convertToUnity/cpp_u_test_to_unity_utils_tests.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2
3 require File.join(File.dirname(__FILE__), 'cpp_u_test_to_unity_utils.rb')
4 include CppUTestToUnityUtils
5
6 test_lines = Array.new
7 expected_lines = Array.new
8
9
10 def check(theTest, expected, actual)
11 unless (expected == actual)
12 puts theTest + " failed"
13 puts "Actual:\n"
14 show(actual)
15 puts "unmatched lines"
16 show(expected - actual)
17 puts("Expected: " + expected.inspect)
18 puts(" Actual: " + actual.inspect)
19 end
20 end
21
22 #---------------------------------------------------
23
24 test_lines =
25 [
26 "\n",
27 "TEST_GROUP(mygroup)\n",
28 "{\n",
29 "}\n",
30 "\n",
31 "\n",
32 "TEST_GROUP(yourgroup)\n",
33 "{\n",
34 "}\n",
35 "\n"
36 ]
37
38
39 groups = get_test_groups(test_lines)
40 unless /mygroup/ =~ groups[0]
41 puts "Expected mygroup, but got #{groups[0]}"
42 end
43
44 unless /yourgroup/ =~ groups[1]
45 puts "Expected yourgroup, but got #{groups[1]}"
46 end
47
48 #---------------------------------------------------
49
50 test_lines =
51 ["\n",
52 "TEST_GROUP(mygroup)\n",
53 "{\n",
54 " int xxxx;\n",
55 " void yyyy()\n",
56 " {\n",
57 " int i = 0;\n",
58 " }\n",
59 "};\n",
60 "\n",
61 " void f()\n"
62 ]
63
64 expected_lines =
65 ["\n",
66 "TEST_GROUP(mygroup);\n",
67 "\n",
68 "static int xxxx;\n",
69 "static void yyyy()\n",
70 "{\n",
71 " int i = 0;\n",
72 "}\n",
73 "\n",
74 "\n",
75 " void f()\n"
76 ]
77
78 def show(lines)
79 lines.each do | line |
80 puts line
81 end
82 end
83
84
85 demote_group(/TEST_GROUP/, test_lines)
86 check("demote_group", expected_lines, test_lines)
87 #---------------------------------
88
89 test_lines =
90 [
91 "\n",
92 "TEST_GROUP(mygroup)\n",
93 "{\n",
94 "}\n",
95 ";\n",
96 "\n",
97 "TEST_GROUP(yourgroup)\n",
98 "{\n",
99 "}\n",
100 ";\n"
101 ]
102
103 expected_lines =
104 [
105 "\n",
106 "TEST_GROUP(mygroup);\n",
107 "\n",
108 "\n",
109 ";\n",
110 "\n",
111 "TEST_GROUP(yourgroup);\n",
112 "\n",
113 "\n",
114 ";\n"
115 ]
116
117 demote_group(/TEST_GROUP/, test_lines)
118 check("demote_group", expected_lines, test_lines)
119
120 #---------------------------------
121
122 test_lines =
123 ["\n",
124 "TEST_GROUP(mygroup)\n",
125 "{\n",
126 " int xxxx;\n",
127 " void yyyy()\n",
128 " {\n",
129 " int i = 0;\n",
130 " }\n",
131 "}; //End TEST_GROUP\n",
132 "\n"
133 ]
134
135 expected_lines =
136 ["\n",
137 "TEST_GROUP(mygroup);\n",
138 "\n",
139 "static int xxxx;\n",
140 "static void yyyy()\n",
141 "{\n",
142 " int i = 0;\n",
143 "}\n",
144 "//End TEST_GROUP\n",
145 "\n"
146 ]
147
148 demote_group(/TEST_GROUP/, test_lines)
149 check("demote_group", expected_lines, test_lines)
150
151 #---------------------------------------------------
152
153 test_lines =
154 ["\n",
155 "extern \"C\"\n",
156 "{\n",
157 "\#include \"heythere.h\"\n",
158 "int xxxx;\n",
159 "void yyyy()\n",
160 "{\n",
161 " int i = 0;\n",
162 "}\n",
163 "}\n",
164 "int foo()\n",
165 "{\n",
166 " int aaaa;\n",
167 "}\n"
168 ]
169
170 expected_lines =
171 ["\n",
172 "\n",
173 "\n",
174 "\#include \"heythere.h\"\n",
175 "int xxxx;\n",
176 "void yyyy()\n",
177 "{\n",
178 " int i = 0;\n",
179 "}\n",
180 "\n",
181 "int foo()\n",
182 "{\n",
183 " int aaaa;\n",
184 "}\n"
185 ]
186
187 remove_extern_c(test_lines)
188 check("remove_extern_c", expected_lines, test_lines)
189
190 #---------------------------------------------------
191
192 test_lines =
193 ["\n",
194 "extern \"C\"\n",
195 "{\n",
196 "#include \"LightScheduler.h\"\n",
197 "#include \"FakeLightController.h\"\n",
198 "#include \"FakeTimeService.h\"\n",
199 "#include \"FakeRandomMinute.h\"\n",
200 "}\n",
201 "#include \"CppUTest/TestHarness.h\"\n"
202 ]
203
204 expected_lines =
205 ["\n",
206 "\n",
207 "\n",
208 "#include \"LightScheduler.h\"\n",
209 "#include \"FakeLightController.h\"\n",
210 "#include \"FakeTimeService.h\"\n",
211 "#include \"FakeRandomMinute.h\"\n",
212 "\n",
213 "#include \"CppUTest/TestHarness.h\"\n"
214 ]
215
216 remove_extern_c(test_lines)
217 check("remove_extern_c", expected_lines, test_lines)
218
219 #---------------------------------------------------
220
221 test_lines =
222 [
223 "\n",
224 "#include \"unity_fixture.h\" expected\n",
225 "#include \"CppUTest\/TestHarness.h\"\n",
226 "\n",
227 "TEST_SETUP(theGroup) expected\n",
228 "static void setup()\n",
229 "{\n",
230 " x = 1;\n",
231 "}\n",
232 "TEST_TEAR_DOWN(theGroup) expected\n",
233 "static void teardown()\n",
234 "{\n",
235 " y = 0;\n",
236 "}\n",
237 "TEST(LedDriver, Create)\n",
238 "{\n",
239 " FAIL(\"Start here\");\n",
240 "}\n",
241 "\n",
242 "IGNORE_TEST(LedDriver, ignore)\n",
243 "{\n",
244 " TEST_ASSERT_TRUE(0 == 0); expected\n",
245 " CHECK(0 == 0);\n",
246 "\n",
247 " TEST_ASSERT_TRUE(0 == 0); expected\n",
248 " CHECK_TRUE(0 == 0);\n",
249 "\n",
250 " TEST_ASSERT_FALSE(0 != 0); expected\n",
251 " CHECK_FALSE(0 != 0);\n",
252 "\n",
253 " TEST_ASSERT_EQUAL(1,1); expected\n",
254 " LONGS_EQUAL(1,1);\n",
255 "\n",
256 " TEST_ASSERT_EQUAL_HEX8(0xab,0xab); expected\n",
257 " BYTES_EQUAL(0xab,0xab);\n",
258 "\n",
259 " TEST_ASSERT_EQUAL(100,100); expected\n",
260 " CHECK_EQUAL(100,100);\n",
261 "\n",
262 " TEST_ASSERT_TRUE(true); expected\n",
263 " CHECK_EQUAL(true,true);\n",
264 "\n",
265 " TEST_ASSERT_FALSE(false); expected\n",
266 " CHECK_EQUAL(false,false);\n",
267 "\n",
268 " TEST_ASSERT_EQUAL_STRING(\"THIS\", \"THIS\"); expected\n",
269 " STRCMP_EQUAL(\"THIS\", \"THIS\");\n",
270 "\n",
271 " TEST_ASSERT_FLOAT_WITHIN(1.0, 1.0, .01); expected\n",
272 " DOUBLES_EQUAL(1.0, 1.0, .01);\n",
273 "\n",
274 " TEST_ASSERT_POINTERS_EQUAL(this, this); expected\n",
275 " POINTERS_EQUAL(this, this);\n",
276 "}\n"
277 ]
278
279 expected_lines =
280 [
281 "\n",
282 "#include \"unity_fixture.h\" expected\n",
283 "#include \"unity_fixture.h\"\n",
284 "\n",
285 "TEST_SETUP(theGroup) expected\n",
286 "TEST_SETUP(theGroup)\n",
287 "{\n",
288 " x = 1;\n",
289 "}\n",
290 "TEST_TEAR_DOWN(theGroup) expected\n",
291 "TEST_TEAR_DOWN(theGroup)\n",
292 "{\n",
293 " y = 0;\n",
294 "}\n",
295 "TEST(LedDriver, Create)\n",
296 "{\n",
297 " TEST_FAIL(\"Start here\");\n",
298 "}\n",
299 "\n",
300 "IGNORE_TEST(LedDriver, ignore)\n",
301 "{\n",
302 " TEST_ASSERT_TRUE(0 == 0); expected\n",
303 " TEST_ASSERT_TRUE(0 == 0);\n",
304 "\n",
305 " TEST_ASSERT_TRUE(0 == 0); expected\n",
306 " TEST_ASSERT_TRUE(0 == 0);\n",
307 "\n",
308 " TEST_ASSERT_FALSE(0 != 0); expected\n",
309 " TEST_ASSERT_FALSE(0 != 0);\n",
310 "\n",
311 " TEST_ASSERT_EQUAL(1,1); expected\n",
312 " TEST_ASSERT_EQUAL(1,1);\n",
313 "\n",
314 " TEST_ASSERT_EQUAL_HEX8(0xab,0xab); expected\n",
315 " TEST_ASSERT_EQUAL_HEX8(0xab,0xab);\n",
316 "\n",
317 " TEST_ASSERT_EQUAL(100,100); expected\n",
318 " TEST_ASSERT_EQUAL(100,100);\n",
319 "\n",
320 " TEST_ASSERT_TRUE(true); expected\n",
321 " TEST_ASSERT_TRUE(true);\n",
322 "\n",
323 " TEST_ASSERT_FALSE(false); expected\n",
324 " TEST_ASSERT_FALSE(false);\n",
325 "\n",
326 " TEST_ASSERT_EQUAL_STRING(\"THIS\", \"THIS\"); expected\n",
327 " TEST_ASSERT_EQUAL_STRING(\"THIS\", \"THIS\");\n",
328 "\n",
329 " TEST_ASSERT_FLOAT_WITHIN(1.0, 1.0, .01); expected\n",
330 " TEST_ASSERT_FLOAT_WITHIN(1.0, 1.0, .01);\n",
331 "\n",
332 " TEST_ASSERT_POINTERS_EQUAL(this, this); expected\n",
333 " TEST_ASSERT_POINTERS_EQUAL(this, this);\n",
334 "}\n"
335 ]
336
337 convert_macros(test_lines, ["theGroup"])
338 check("convert_macros", expected_lines, test_lines)
339
340 #---------------------------------------------------
341
342 test_lines =
343 [
344 "TEST_SETUP(group1) expected\n",
345 "static void setup()\n",
346 "TEST_TEAR_DOWN(group1) expected\n",
347 "static void teardown()\n",
348 "TEST(group1, Create)\n",
349 "IGNORE_TEST(group1, ignore)\n",
350 "TEST_SETUP(group2) expected\n",
351 "static void setup()\n",
352 "TEST_TEAR_DOWN(group2) expected\n",
353 "static void teardown()\n",
354 "TEST(group2, Create)\n",
355 "IGNORE_TEST(group2, ignore)\n"
356 ]
357
358 expected_lines =
359 [
360 "TEST_SETUP(group1) expected\n",
361 "TEST_SETUP(group1)\n",
362 "TEST_TEAR_DOWN(group1) expected\n",
363 "TEST_TEAR_DOWN(group1)\n",
364 "TEST(group1, Create)\n",
365 "IGNORE_TEST(group1, ignore)\n",
366 "TEST_SETUP(group2) expected\n",
367 "TEST_SETUP(group2)\n",
368 "TEST_TEAR_DOWN(group2) expected\n",
369 "TEST_TEAR_DOWN(group2)\n",
370 "TEST(group2, Create)\n",
371 "IGNORE_TEST(group2, ignore)\n",
372 ]
373
374 convert_macros(test_lines, ["group1", "group2"])
375 check("convert_macros", expected_lines, test_lines)
376
377 #---------------------------------------------------
378
379 test_lines =
380 [
381 " aaaa expected\n",
382 "\taaa\n",
383 " bbbb expected\n",
384 "\t\t\t\tbbbb\n",
385 " int a3; expected\n",
386 " int a3;\n",
387 " int a4; expected\n",
388 " int a4;\n"
389 ]
390
391 expected_lines =
392 [
393 " aaaa expected\n",
394 " aaa\n",
395 " bbbb expected\n",
396 " bbbb\n",
397 " int a3; expected\n",
398 " int a3;\n",
399 " int a4; expected\n",
400 " int a4;\n"
401 ]
402
403 adjust_tabs(test_lines)
404 check("adjust_tabs", expected_lines, test_lines)
405
406
407 #---------------------------------------------------
408
409 test_lines =
410 [
411 "TEST(LedDriver, Create)\n",
412 "{\n",
413 " FAIL(\"Start here\");\n",
414 "}\n",
415 "\n",
416 "TEST(LedDriver, XXXXX)\n",
417 "{\n",
418 " FAIL(\"Start here\");\n",
419 "}\n",
420 "\n",
421 "IGNORE_TEST(LedDriver, ignore)\n",
422 "{\n",
423 " TEST_ASSERT_TRUE(0 == 0); expected\n",
424 "}\n"
425 ]
426
427 expected_group_runner =
428 [
429 "/* Make sure you invoke RUN_TEST_GROUP(LedDriver) from unity main */\n\n",
430 "TEST_GROUP_RUNNER(LedDriver)\n",
431 "{\n",
432 " RUN_TEST_CASE(LedDriver, Create);\n",
433 " RUN_TEST_CASE(LedDriver, XXXXX);\n",
434 " RUN_TEST_CASE(LedDriver, ignore);\n",
435 "}\n\n"
436 ]
437
438 group_runner = generate_group_runner("LedDriver", test_lines)
439 check("generate_group_runner", expected_group_runner, group_runner)
440
441
442 #---------------------------------------------------
443
444 test_lines =
445 [
446 "TEST(LedDriverGroup1, Create)\n",
447 "{\n",
448 " FAIL(\"Start here\");\n",
449 "}\n",
450 "\n",
451 "TEST(LedDriverGroup2, XXXXX)\n",
452 "{\n",
453 " FAIL(\"Start here\");\n",
454 "}\n",
455 "\n"
456 ]
457
458 expected_group_runners =
459 [
460 "/* Generated code, edit at your own risk */\n\n",
461 "#include \"unity_fixture.h\"\n\n",
462 "/* Make sure you invoke RUN_TEST_GROUP(LedDriverGroup1) from unity main */\n\n",
463 "TEST_GROUP_RUNNER(LedDriverGroup1)\n",
464 "{\n",
465 " RUN_TEST_CASE(LedDriverGroup1, Create);\n",
466 "}\n\n",
467 "/* Make sure you invoke RUN_TEST_GROUP(LedDriverGroup2) from unity main */\n\n",
468 "TEST_GROUP_RUNNER(LedDriverGroup2)\n",
469 "{\n",
470 " RUN_TEST_CASE(LedDriverGroup2, XXXXX);\n",
471 "}\n\n"
472 ]
473
474 runners = generate_group_runners(["LedDriverGroup1", "LedDriverGroup2"], test_lines)
475 check("generate_group_runners", expected_group_runners, runners)
476
477
478 #---------------------------------------------------
479
480 test_filename = "prefix/tests/pooltable/EightballTest.cpp"
481 expected_unity_filename = "prefix/unity/pooltable/EightballTest.c"
482
483 unity_filename = convert_test_filename_to_unity_filename(test_filename)
484
485 if expected_unity_filename != unity_filename
486 puts("Failure in: convert_test_path_to_unity_path")
487 puts("Expected: " + expected_unity_filename.inspect)
488 puts(" Actual: " + unity_filename.inspect)
489 end
490
491 #---------------------------------------------------
492
493 test_filename = "tests/pool/table/EightballTest.cpp"
494 expected_unity_runner_filename = "unity/pool/table/EightballTest_runner.c"
495
496 unity_runner_filename = convert_test_filename_to_unity_testrunner_filename(test_filename)
497
498 if expected_unity_runner_filename != unity_runner_filename
499 puts("Failure in: convert_test_path_to_unity_path")
500 puts("Expected: " + expected_unity_runner_filename.inspect)
501 puts(" Actual: " + unity_runner_filename.inspect)
502 end
503
504
505
506