#! /usr/bin/env python3 """Translate the language-independent tests to C++ code.""" import json import textwrap def main(): """Read canonical-data.json and print C++ tests.""" with open("canonical-data.json") as file: json_data = json.load(file) print_head(json_data) for i, case in enumerate(json_data['cases']): if i == 1: print('#if defined(EXERCISM_RUN_ALL_TESTS)') print() print_case(case) if len(json_data['cases']) > 1: print('#endif // EXERCISM_RUN_ALL_TESTS') def print_head(json_data): """Print the head of the test file.""" exercise = json_data['exercise'] print(textwrap.dedent(f'''\ #include "{exercise}.h" #ifdef EXERCISM_TEST_SUITE #include #else #include "test/catch.hpp" #endif ''')) def print_case(case): """Print a single test case.""" test_name = case['description'].replace(' ', '_') print(textwrap.dedent(f'''\ TEST_CASE("{test_name}") {{ ''')) for operation in case['input']['operations']: op = operation['operation'] value = operation.get('value', '') if 'expected' in operation: expected = operation['expected'] print(f' REQUIRE({expected} == llist.{op}({value}));') else: print(f' llist.{op}({value});') print('}\n'); if __name__ == '__main__': main()