transpile_tests()   C
last analyzed

Complexity

Conditions 7

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
dl 0
loc 24
rs 5.5
1
#!/usr/bin/env python
2
import os
3
from os.path import join, dirname, realpath, splitext
4
5
from py14.transpiler import transpile
6
7
8
def is_test(filename):
9
    return filename.startswith("test") and filename.endswith(".py")
10
11
12
def transpile_tests(tests_dir):
13
    """
14
    Transpile all python regression tests in tests_dir into C++ header
15
    files and include them in a single cpp file
16
    """
17
    hpp_files = []
18
    py_files = [p for p in os.listdir(tests_dir) if is_test(p)]
19
20
    for py_file in py_files:
21
        hpp_file = splitext(py_file)[0] + ".hpp"
22
        hpp_path = join(tests_dir, hpp_file)
23
24
        with open(hpp_path, "w") as cpp_file:
25
            source = open(join(tests_dir, py_file)).read()
26
            cpp = transpile(source, headers=True, testing=True)
27
            cpp_file.write(cpp)
28
29
        hpp_files.append(hpp_file)
30
31
    with open(join(tests_dir, "main.cpp"), "w") as main:
32
        main.write("#define CATCH_CONFIG_MAIN\n")
33
        main.write('#include "catch.hpp"\n')
34
        for hpp_file in hpp_files:
35
            main.write('#include "{0}"\n'.format(hpp_file))
36
37
38
if __name__ == "__main__":
39
    tests_dir = join(dirname(realpath(__file__)), "regtests")
40
    transpile_tests(tests_dir)
41