|
6 | 6 | import os
|
7 | 7 | import re
|
8 | 8 | from io import open
|
| 9 | +from string import Template |
9 | 10 |
|
10 |
| -n=0 |
11 |
| -regexp = r'^void (test_[A-Za-z0-9_]+)\(.*\).*' |
12 |
| - |
13 |
| -filename, file_type = os.path.splitext(sys.argv[1]) |
14 |
| - |
15 |
| -funcs = [] |
16 |
| - |
17 |
| -for l in open(sys.argv[1], encoding='utf-8'): |
18 |
| - m = re.search(regexp, l) |
19 |
| - if(m): |
20 |
| - funcs.append( (m.group(1).strip(),n) ) |
21 |
| - n=n+1 |
22 |
| - |
23 |
| -print(''' |
| 11 | +test_runner_template = ''' |
24 | 12 | /* AUTOGENERATED FILE. DO NOT EDIT. */
|
25 |
| -''') |
26 |
| - |
27 |
| -if(file_type == '.cpp'): |
28 |
| - print(''' |
29 |
| -extern "C" { |
30 |
| -''') |
31 |
| - |
32 |
| -print(''' |
33 |
| - #include "unity.h" |
34 |
| - #include "unity_print.h" |
35 |
| - #include "unity_internals.h" |
36 |
| -#ifndef __codasip__ |
37 |
| - #include "string.h" |
38 |
| -#endif /* __codasip__ */ |
39 |
| -''') |
40 |
| - |
41 |
| -if(file_type == '.cpp'): |
42 |
| - print(''' |
43 |
| -} |
44 |
| -''') |
45 | 13 |
|
46 |
| -print(''' |
47 |
| -int verbose; |
48 |
| -''') |
| 14 | +${EXTERN_C_CPP_START} |
| 15 | +#include "unity.h" |
| 16 | +#include "unity_internals.h" |
| 17 | +#include "string.h" |
| 18 | +${EXTERN_C_CPP_END} |
49 | 19 |
|
50 |
| -for f in funcs: |
51 |
| - print("void {}();".format(f[0])) |
| 20 | +int verbose; |
52 | 21 |
|
53 |
| -print(''' |
| 22 | +${FUNCTION_PROTOTYPES} |
54 | 23 |
|
55 | 24 | // Inspired by how Unity creates the setUp and tearDown functions
|
56 | 25 | // Purpose is a setup and teardown method called before suite is run,
|
|
61 | 30 | UNITY_WEAK_ATTRIBUTE void setUpSuite(void) { }
|
62 | 31 | UNITY_WEAK_ATTRIBUTE void tearDownSuite(void) { }
|
63 | 32 | #elif defined(UNITY_WEAK_PRAGMA)
|
64 |
| -# pragma weak setUpSuite |
| 33 | +#pragma weak setUpSuite |
65 | 34 | void setUpSuite(void);
|
66 |
| -# pragma weak tearDownSuite |
| 35 | +#pragma weak tearDownSuite |
67 | 36 | void tearDownSuite(void);
|
68 | 37 | #else
|
69 | 38 | void setUpSuite(void);
|
|
73 | 42 | int main(int argc, char** argp) {
|
74 | 43 | int ret;
|
75 | 44 | setUpSuite();
|
76 |
| - unity_print_init(); |
| 45 | +
|
| 46 | + // Disable all print buffering. This ensures output is written to stdout as early as possible |
| 47 | + // and minimizes risk of lost print messages if program seg faults. |
| 48 | + setvbuf(stdout, NULL, _IONBF, 0); |
| 49 | + setvbuf(stderr, NULL, _IONBF, 0); |
| 50 | +
|
77 | 51 | UNITY_BEGIN();
|
78 | 52 |
|
79 | 53 |
|
80 | 54 | if(argc==2) {
|
81 |
| - verbose=1; |
82 |
| -''') |
83 |
| -for f in funcs: |
84 |
| - print(' if(strcmp(argp[1],"{}") == 0) UnityDefaultTestRun(&{}, "{}", {});'.format(f[0], f[0], f[0], f[1])) |
85 |
| -print(''' |
| 55 | + verbose=1; |
| 56 | +
|
| 57 | +${FUNCTION_CALLS_VERBOSE} |
| 58 | +
|
86 | 59 | } else {
|
87 | 60 |
|
88 |
| -''') |
89 |
| -for f in funcs: |
90 |
| - print(' UnityDefaultTestRun(&{}, "{}", {});'.format(f[0], f[0], f[1])) |
91 |
| -print(''' |
| 61 | +${FUNCTION_CALLS} |
| 62 | +
|
92 | 63 | }
|
93 | 64 |
|
94 | 65 | ret = UNITY_END();
|
95 |
| - unity_print_close(); |
96 | 66 | tearDownSuite();
|
97 | 67 | return ret;
|
98 | 68 | }
|
99 |
| -
|
100 |
| -''') |
| 69 | +''' |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + |
| 73 | + if len(sys.argv) != 2: |
| 74 | + exit(f"usage: {sys.argv[0]} <filename>") |
| 75 | + |
| 76 | + filename, file_type = os.path.splitext(sys.argv[1]) |
| 77 | + |
| 78 | + funcs = [] |
| 79 | + function_prototypes = '' |
| 80 | + function_calls_verbose = '' |
| 81 | + function_calls = '' |
| 82 | + |
| 83 | + regexp = r'^void (test_[A-Za-z0-9_]+)\(.*\).*' |
| 84 | + |
| 85 | + line = 0 |
| 86 | + for l in open(sys.argv[1], encoding='utf-8'): |
| 87 | + m = re.search(regexp, l) |
| 88 | + if(m): |
| 89 | + # adds a tuple (function_name, line number) |
| 90 | + funcs.append( (m.group(1).strip(), line) ) |
| 91 | + line+=1 |
| 92 | + |
| 93 | + for f in funcs: |
| 94 | + # f[0] is the function name, f[1] is the line number |
| 95 | + function_prototypes += f"void {f[0]}();\n" |
| 96 | + function_calls_verbose += f' if(strcmp(argp[1],"{f[0]}") == 0) UnityDefaultTestRun(&{f[0]}, "{f[0]}", {f[1]});\n' |
| 97 | + function_calls += f' UnityDefaultTestRun(&{f[0]}, "{f[0]}", {f[1]});\n' |
| 98 | + |
| 99 | + t = Template(test_runner_template).substitute({ |
| 100 | + 'EXTERN_C_CPP_START': 'extern "C" {' if file_type == '.cpp' else '', |
| 101 | + 'EXTERN_C_CPP_END': '}' if file_type == '.cpp' else '', |
| 102 | + 'FUNCTION_PROTOTYPES': function_prototypes, |
| 103 | + 'FUNCTION_CALLS_VERBOSE': function_calls_verbose, |
| 104 | + 'FUNCTION_CALLS': function_calls |
| 105 | + }) |
| 106 | + |
| 107 | + print(t) |
0 commit comments