Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions code/logic/fossil/test/testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,13 @@ void fossil_test_print_stack_trace(stack_frame_t *stack_trace);
*
* @param name The name of the test group.
*/
#ifdef __cplusplus
#define _FOSSIL_TEST_GROUP(name) \
extern "C" void name##_test_group(fossil_test_env_t *_env)
#else
#define _FOSSIL_TEST_GROUP(name) \
void name##_test_group(fossil_test_env_t *_env)
#endif

/**
* @brief Macro to export a test group.
Expand Down
15 changes: 5 additions & 10 deletions code/tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@ if get_option('with_test').enabled()
run_command(['python3', 'tools' / 'generate-runner.py'], check: true)

test_c = ['unit_runner.c']
test_cpp = ['unit_runner.cpp']
test_cases = [
'sample', 'bdd', 'tdd', 'ddd', 'mark'
]
test_cases = ['sample', 'bdd', 'tdd', 'ddd', 'mark']

foreach cases : test_cases
test_c += ['cases' / 'test_' + cases + '.c']
test_cpp += ['cases' / 'test_' + cases + '.cpp']
test_c += ['cases' / 'test_' + cases + '.cpp']
endforeach

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

test('fossil testing C ', pizza_c )
test('fossil testing Cpp', pizza_cpp)
endif
test('fossil testing C', pizza_c)
endif
103 changes: 25 additions & 78 deletions code/tests/tools/generate-runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,123 +8,70 @@ def __init__(self):
self.directory = os.path.join(os.getcwd(), "cases")

def find_test_groups(self):
c_test_groups = set()
cpp_test_groups = set()
test_groups = set()
pattern = r"FOSSIL_TEST_GROUP\((\w+)\)"

# Walk through files in the specified directory, 'cases'
for root, _, files in os.walk(self.directory):
for file in files:
# Search for C files
if file.startswith("test_") and file.endswith(".c"):
# Search for C and C++ files
if (file.startswith("test_") and file.endswith(".c")) or file.endswith(".cpp"):
with open(os.path.join(root, file), "r") as f:
content = f.read()
matches = re.findall(pattern, content)
c_test_groups.update(matches)
# Search for C++ files
elif file.startswith("test_") and file.endswith(".cpp"):
with open(os.path.join(root, file), "r") as f:
content = f.read()
matches = re.findall(pattern, content)
cpp_test_groups.update(matches)
test_groups.update(matches)

return list(c_test_groups), list(cpp_test_groups)
return list(test_groups)

def generate_c_runner(self, c_test_groups):
# Prepare header content for C test runner
def generate_c_runner(self, test_groups):
# Prepare header content for the test runner
header = """
// Generated Fossil Logic Test (C)
// Generated Fossil Logic Test Runner
#include <fossil/test/framework.h>

// * * * * * * * * * * * * * * * * * * * * * * * *
// * Fossil Logic Test List (C)
// * Fossil Logic Test List
// * * * * * * * * * * * * * * * * * * * * * * * *
"""

# Declare C test group externs within extern "C"
extern_c_pools = "\n".join(
[f"FOSSIL_TEST_EXPORT({group});" for group in c_test_groups]
# Declare test group externs
extern_test_groups = "\n".join(
[f"FOSSIL_TEST_EXPORT({group});" for group in test_groups]
)

# Prepare runner content for C
# Prepare runner content
runner = """\n
// * * * * * * * * * * * * * * * * * * * * * * * *
// * Fossil Logic Test Runner (C)
// * Fossil Logic Test Runner
// * * * * * * * * * * * * * * * * * * * * * * * *
int main(int argc, char **argv) {
FOSSIL_TEST_START(argc, argv);\n"""

# Import C test groups in the main function
import_c_pools = "\n".join(
[f" FOSSIL_TEST_IMPORT({group});" for group in c_test_groups]
# Import test groups in the main function
import_test_groups = "\n".join(
[f" FOSSIL_TEST_IMPORT({group});" for group in test_groups]
)

# Complete with footer
footer = """\n
FOSSIL_TEST_RUN();
FOSSIL_TEST_SUMMARY();
FOSSIL_TEST_END();
} // end of func
} // end of main
"""

# Write the generated C test runner to 'unit_runner_c.cpp'
# Write the generated test runner to 'unit_runner.c'
with open("unit_runner.c", "w") as file:
file.write(header)
file.write(extern_c_pools)
file.write(runner)
file.write(import_c_pools)
file.write(footer)

def generate_cpp_runner(self, cpp_test_groups):
# Prepare header content for C++ test runner
header = """
// Generated Fossil Logic Test (C++)
#include <fossil/test/framework.h>

// * * * * * * * * * * * * * * * * * * * * * * * *
// * Fossil Logic Test List (C++)
// * * * * * * * * * * * * * * * * * * * * * * * *
"""

# Declare C++ test group externs
extern_cpp_pools = "\n".join(
[f"FOSSIL_TEST_EXPORT({group});" for group in cpp_test_groups]
)

# Prepare runner content for C++
runner = """\n
// * * * * * * * * * * * * * * * * * * * * * * * *
// * Fossil Logic Test Runner (C++)
// * * * * * * * * * * * * * * * * * * * * * * * *
int main(int argc, char **argv) {
FOSSIL_TEST_START(argc, argv);\n"""

# Import C++ test groups in the main function
import_cpp_pools = "\n".join(
[f" FOSSIL_TEST_IMPORT({group});" for group in cpp_test_groups]
)

# Complete with footer
footer = """\n
FOSSIL_TEST_RUN();
FOSSIL_TEST_SUMMARY();
FOSSIL_TEST_END();
} // end of func
"""

# Write the generated C++ test runner to 'unit_runner_cpp.cpp'
with open("unit_runner.cpp", "w") as file:
file.write(header)
file.write(extern_cpp_pools)
file.write(extern_test_groups)
file.write(runner)
file.write(import_cpp_pools)
file.write(import_test_groups)
file.write(footer)


# Instantiate the generator, find test groups, and generate the test runners
# Instantiate the generator, find test groups, and generate the test runner
generator = TestRunnerGenerator()
c_test_groups, cpp_test_groups = generator.find_test_groups()
test_groups = generator.find_test_groups()

# Generate separate runners for C and C++
generator.generate_c_runner(c_test_groups)
generator.generate_cpp_runner(cpp_test_groups)
# Generate the test runner for C and C++ tests
generator.generate_c_runner(test_groups)