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