Skip to content

Commit 79f89f4

Browse files
Luiskyrzr
authored andcommitted
libs2/tf: SWPROT-8720: rewrote gen_test_runner.py for greater clarity (#57)
(cherry picked from commit 81b502d3c534f964958a4e9d76f6943d5a37562c) Forwarded: #57 Relate-to: #50 Signed-off-by: Philippe Coval <philippe.coval@silabs.com>
1 parent 2a866b9 commit 79f89f4

File tree

1 file changed

+62
-55
lines changed
  • applications/zpc/components/zwave/zwave_transports/s2/libs/zw-libs2/TestFramework

1 file changed

+62
-55
lines changed

applications/zpc/components/zwave/zwave_transports/s2/libs/zw-libs2/TestFramework/gen_test_runner.py

+62-55
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,20 @@
66
import os
77
import re
88
from io import open
9+
from string import Template
910

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 = '''
2412
/* 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-
''')
4513
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}
4919
50-
for f in funcs:
51-
print("void {}();".format(f[0]))
20+
int verbose;
5221
53-
print('''
22+
${FUNCTION_PROTOTYPES}
5423
5524
// Inspired by how Unity creates the setUp and tearDown functions
5625
// Purpose is a setup and teardown method called before suite is run,
@@ -61,9 +30,9 @@
6130
UNITY_WEAK_ATTRIBUTE void setUpSuite(void) { }
6231
UNITY_WEAK_ATTRIBUTE void tearDownSuite(void) { }
6332
#elif defined(UNITY_WEAK_PRAGMA)
64-
# pragma weak setUpSuite
33+
#pragma weak setUpSuite
6534
void setUpSuite(void);
66-
# pragma weak tearDownSuite
35+
#pragma weak tearDownSuite
6736
void tearDownSuite(void);
6837
#else
6938
void setUpSuite(void);
@@ -73,28 +42,66 @@
7342
int main(int argc, char** argp) {
7443
int ret;
7544
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+
7751
UNITY_BEGIN();
7852
7953
8054
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+
8659
} else {
8760
88-
''')
89-
for f in funcs:
90-
print(' UnityDefaultTestRun(&{}, "{}", {});'.format(f[0], f[0], f[1]))
91-
print('''
61+
${FUNCTION_CALLS}
62+
9263
}
9364
9465
ret = UNITY_END();
95-
unity_print_close();
9666
tearDownSuite();
9767
return ret;
9868
}
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

Comments
 (0)