Skip to content

Commit c82c22a

Browse files
Merge pull request #28 from dreamer-coding/pr_test_group_extern_cpp
Pull request to allow test group to use extern cpp for mixed test runners
2 parents 491fe70 + 95fbebe commit c82c22a

File tree

3 files changed

+35
-88
lines changed

3 files changed

+35
-88
lines changed

code/logic/fossil/test/testing.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,13 @@ void fossil_test_print_stack_trace(stack_frame_t *stack_trace);
362362
*
363363
* @param name The name of the test group.
364364
*/
365+
#ifdef __cplusplus
366+
#define _FOSSIL_TEST_GROUP(name) \
367+
extern "C" void name##_test_group(fossil_test_env_t *_env)
368+
#else
365369
#define _FOSSIL_TEST_GROUP(name) \
366370
void name##_test_group(fossil_test_env_t *_env)
371+
#endif
367372

368373
/**
369374
* @brief Macro to export a test group.

code/tests/meson.build

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ if get_option('with_test').enabled()
22
run_command(['python3', 'tools' / 'generate-runner.py'], check: true)
33

44
test_c = ['unit_runner.c']
5-
test_cpp = ['unit_runner.cpp']
6-
test_cases = [
7-
'sample', 'bdd', 'tdd', 'ddd', 'mark'
8-
]
5+
test_cases = ['sample', 'bdd', 'tdd', 'ddd', 'mark']
96

107
foreach cases : test_cases
118
test_c += ['cases' / 'test_' + cases + '.c']
12-
test_cpp += ['cases' / 'test_' + cases + '.cpp']
9+
test_c += ['cases' / 'test_' + cases + '.cpp']
1310
endforeach
1411

15-
pizza_c = executable('testbed-c', test_c, include_directories: dir, dependencies: [fossil_test_dep])
16-
pizza_cpp = executable('testbed-cpp', test_cpp, include_directories: dir, dependencies: [fossil_test_dep])
12+
pizza_c = executable('testbed-c', test_c, include_directories: dir, dependencies: [fossil_test_dep])
1713

18-
test('fossil testing C ', pizza_c )
19-
test('fossil testing Cpp', pizza_cpp)
20-
endif
14+
test('fossil testing C', pizza_c)
15+
endif

code/tests/tools/generate-runner.py

Lines changed: 25 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,123 +8,70 @@ def __init__(self):
88
self.directory = os.path.join(os.getcwd(), "cases")
99

1010
def find_test_groups(self):
11-
c_test_groups = set()
12-
cpp_test_groups = set()
11+
test_groups = set()
1312
pattern = r"FOSSIL_TEST_GROUP\((\w+)\)"
1413

1514
# Walk through files in the specified directory, 'cases'
1615
for root, _, files in os.walk(self.directory):
1716
for file in files:
18-
# Search for C files
19-
if file.startswith("test_") and file.endswith(".c"):
17+
# Search for C and C++ files
18+
if (file.startswith("test_") and file.endswith(".c")) or file.endswith(".cpp"):
2019
with open(os.path.join(root, file), "r") as f:
2120
content = f.read()
2221
matches = re.findall(pattern, content)
23-
c_test_groups.update(matches)
24-
# Search for C++ files
25-
elif file.startswith("test_") and file.endswith(".cpp"):
26-
with open(os.path.join(root, file), "r") as f:
27-
content = f.read()
28-
matches = re.findall(pattern, content)
29-
cpp_test_groups.update(matches)
22+
test_groups.update(matches)
3023

31-
return list(c_test_groups), list(cpp_test_groups)
24+
return list(test_groups)
3225

33-
def generate_c_runner(self, c_test_groups):
34-
# Prepare header content for C test runner
26+
def generate_c_runner(self, test_groups):
27+
# Prepare header content for the test runner
3528
header = """
36-
// Generated Fossil Logic Test (C)
29+
// Generated Fossil Logic Test Runner
3730
#include <fossil/test/framework.h>
3831
3932
// * * * * * * * * * * * * * * * * * * * * * * * *
40-
// * Fossil Logic Test List (C)
33+
// * Fossil Logic Test List
4134
// * * * * * * * * * * * * * * * * * * * * * * * *
4235
"""
4336

44-
# Declare C test group externs within extern "C"
45-
extern_c_pools = "\n".join(
46-
[f"FOSSIL_TEST_EXPORT({group});" for group in c_test_groups]
37+
# Declare test group externs
38+
extern_test_groups = "\n".join(
39+
[f"FOSSIL_TEST_EXPORT({group});" for group in test_groups]
4740
)
4841

49-
# Prepare runner content for C
42+
# Prepare runner content
5043
runner = """\n
5144
// * * * * * * * * * * * * * * * * * * * * * * * *
52-
// * Fossil Logic Test Runner (C)
45+
// * Fossil Logic Test Runner
5346
// * * * * * * * * * * * * * * * * * * * * * * * *
5447
int main(int argc, char **argv) {
5548
FOSSIL_TEST_START(argc, argv);\n"""
5649

57-
# Import C test groups in the main function
58-
import_c_pools = "\n".join(
59-
[f" FOSSIL_TEST_IMPORT({group});" for group in c_test_groups]
50+
# Import test groups in the main function
51+
import_test_groups = "\n".join(
52+
[f" FOSSIL_TEST_IMPORT({group});" for group in test_groups]
6053
)
6154

6255
# Complete with footer
6356
footer = """\n
6457
FOSSIL_TEST_RUN();
6558
FOSSIL_TEST_SUMMARY();
6659
FOSSIL_TEST_END();
67-
} // end of func
60+
} // end of main
6861
"""
6962

70-
# Write the generated C test runner to 'unit_runner_c.cpp'
63+
# Write the generated test runner to 'unit_runner.c'
7164
with open("unit_runner.c", "w") as file:
7265
file.write(header)
73-
file.write(extern_c_pools)
74-
file.write(runner)
75-
file.write(import_c_pools)
76-
file.write(footer)
77-
78-
def generate_cpp_runner(self, cpp_test_groups):
79-
# Prepare header content for C++ test runner
80-
header = """
81-
// Generated Fossil Logic Test (C++)
82-
#include <fossil/test/framework.h>
83-
84-
// * * * * * * * * * * * * * * * * * * * * * * * *
85-
// * Fossil Logic Test List (C++)
86-
// * * * * * * * * * * * * * * * * * * * * * * * *
87-
"""
88-
89-
# Declare C++ test group externs
90-
extern_cpp_pools = "\n".join(
91-
[f"FOSSIL_TEST_EXPORT({group});" for group in cpp_test_groups]
92-
)
93-
94-
# Prepare runner content for C++
95-
runner = """\n
96-
// * * * * * * * * * * * * * * * * * * * * * * * *
97-
// * Fossil Logic Test Runner (C++)
98-
// * * * * * * * * * * * * * * * * * * * * * * * *
99-
int main(int argc, char **argv) {
100-
FOSSIL_TEST_START(argc, argv);\n"""
101-
102-
# Import C++ test groups in the main function
103-
import_cpp_pools = "\n".join(
104-
[f" FOSSIL_TEST_IMPORT({group});" for group in cpp_test_groups]
105-
)
106-
107-
# Complete with footer
108-
footer = """\n
109-
FOSSIL_TEST_RUN();
110-
FOSSIL_TEST_SUMMARY();
111-
FOSSIL_TEST_END();
112-
} // end of func
113-
"""
114-
115-
# Write the generated C++ test runner to 'unit_runner_cpp.cpp'
116-
with open("unit_runner.cpp", "w") as file:
117-
file.write(header)
118-
file.write(extern_cpp_pools)
66+
file.write(extern_test_groups)
11967
file.write(runner)
120-
file.write(import_cpp_pools)
68+
file.write(import_test_groups)
12169
file.write(footer)
12270

12371

124-
# Instantiate the generator, find test groups, and generate the test runners
72+
# Instantiate the generator, find test groups, and generate the test runner
12573
generator = TestRunnerGenerator()
126-
c_test_groups, cpp_test_groups = generator.find_test_groups()
74+
test_groups = generator.find_test_groups()
12775

128-
# Generate separate runners for C and C++
129-
generator.generate_c_runner(c_test_groups)
130-
generator.generate_cpp_runner(cpp_test_groups)
76+
# Generate the test runner for C and C++ tests
77+
generator.generate_c_runner(test_groups)

0 commit comments

Comments
 (0)