test/gensuitemaker.py
author Volker Birk <vb@pep.foundation>
Wed, 29 Aug 2018 23:01:09 +0200
branchsync
changeset 2907 92f22b19b09e
parent 2645 a1932fb654b8
child 2941 23726372aea0
permissions -rw-r--r--
a dangling else
krista@2645
     1
# This file is under GNU General Public License 3.0
krista@2645
     2
# see LICENSE.txt
krista@2645
     3
krista@2645
     4
from os import listdir, getcwd
krista@2645
     5
from os.path import isfile, join
krista@2645
     6
from re import sub
krista@2645
     7
import datetime
krista@2645
     8
krista@2645
     9
srcpath = join(getcwd(), "src/engine_tests")
krista@2645
    10
krista@2645
    11
ls = listdir(srcpath)
krista@2645
    12
krista@2645
    13
suites = []
krista@2645
    14
krista@2645
    15
indent = "    "
krista@2645
    16
krista@2645
    17
for f in ls:
krista@2645
    18
    if isfile(join(srcpath, f)):
krista@2645
    19
        if (f.endswith(".cc")):
krista@2645
    20
            suite = sub('\.cc$', '', f)
krista@2645
    21
            suites.append(suite)
krista@2645
    22
krista@2645
    23
license = (''
krista@2645
    24
'// This file is under GNU General Public License 3.0\n'
krista@2645
    25
'// see LICENSE.txt\n\n')
krista@2645
    26
krista@2645
    27
krista@2645
    28
header = license + "//\n// src/SuiteMaker.cc generated by gensuitemaker.py - changes may be overwritten. You've been warned!\n//\n\n"
krista@2645
    29
krista@2645
    30
header += (''
krista@2645
    31
'#include <cpptest.h>\n'
krista@2645
    32
'#include <cpptest-suite.h>\n'
krista@2645
    33
'#include <memory>\n'
krista@2645
    34
'#include <vector>\n'
krista@2645
    35
'#include "SuiteMaker.h"\n')
krista@2645
    36
krista@2645
    37
suiteincludes = ""
krista@2645
    38
krista@2645
    39
for s in suites:
krista@2645
    40
    suiteincludes += '#include "' + s + '.h"\n'
krista@2645
    41
krista@2645
    42
output = header + "\n// Begin where we generate stuff\n" + suiteincludes + "\n"
krista@2645
    43
krista@2645
    44
output += ("\n"
krista@2645
    45
"const char* SuiteMaker::all_suites[] = {\n")
krista@2645
    46
krista@2645
    47
first = True
krista@2645
    48
krista@2645
    49
for s in suites:
krista@2645
    50
    output += indent + '"' + s + '",\n'
krista@2645
    51
krista@2645
    52
output += "};\n\n"
krista@2645
    53
krista@2645
    54
output += (
krista@2645
    55
'// This file is generated, so magic constants are ok.\n'
krista@2645
    56
'int SuiteMaker::num_suites = ')
krista@2645
    57
krista@2645
    58
output += str(len(suites)) + ";\n\n"
krista@2645
    59
    
krista@2645
    60
output += "void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {\n"
krista@2645
    61
krista@2645
    62
first = True
krista@2645
    63
krista@2645
    64
for s in suites:
krista@2645
    65
    output += indent
krista@2645
    66
    
krista@2645
    67
    if not first:
krista@2645
    68
        output += "else "
krista@2645
    69
    else:
krista@2645
    70
        first = False
krista@2645
    71
    
krista@2645
    72
    output += ('if (strcmp(test_class_name, "' + s + '") == 0)\n' + 
krista@2645
    73
               indent + indent + '*test_suite = new ' + s + '(test_class_name, test_home);\n')
krista@2645
    74
        
krista@2645
    75
output += "}\n\n"
krista@2645
    76
krista@2645
    77
output += (
krista@2645
    78
'void SuiteMaker::suitemaker_buildlist(const char** test_class_names, int num_to_run, const char* test_home, std::vector<Test::Suite*>& test_suites) {\n'
krista@2645
    79
'    for (int i = 0; i < num_to_run; i++) {\n'
krista@2645
    80
'        Test::Suite* suite = NULL;\n'
krista@2645
    81
'        SuiteMaker::suitemaker_build(test_class_names[i], test_home, &suite);\n'
krista@2645
    82
'        if (!suite)\n'
krista@2645
    83
'            throw std::runtime_error("Could not create a test suite instance."); // FIXME, better error, cleanup, obviously\n'
krista@2645
    84
'        test_suites.push_back(suite);\n'
krista@2645
    85
'    }\n'    
krista@2645
    86
'}'
krista@2645
    87
'\n'
krista@2645
    88
'void SuiteMaker::suitemaker_buildall(const char* test_home, std::vector<Test::Suite*>& test_suites) {\n'
krista@2645
    89
'    SuiteMaker::suitemaker_buildlist(SuiteMaker::all_suites, SuiteMaker::num_suites, test_home, test_suites);\n'
krista@2645
    90
'}\n\n')
krista@2645
    91
krista@2645
    92
suitemaker = open("src/SuiteMaker.cc", 'w') 
krista@2645
    93
suitemaker.write(output)
krista@2645
    94
suitemaker.close()
krista@2645
    95
krista@2645
    96
generated_at = ("src/SuiteMaker.cc" + " generated at " + datetime.datetime.now().strftime('%c'))
krista@2645
    97
print(generated_at)