test/gensuitemaker.py
author Volker Birk <vb@pep-project.org>
Fri, 17 May 2019 17:59:06 +0200
branchsync
changeset 3720 9ed76a79d784
parent 3249 03119f76ea35
permissions -rw-r--r--
...
     1 #! /usr/bin/env python3
     2 
     3 # This file is under GNU General Public License 3.0
     4 # see LICENSE.txt
     5 
     6 import argparse
     7 from os import listdir, getcwd
     8 from os.path import isfile, join
     9 from re import sub
    10 import datetime
    11 
    12 srcpath = join(getcwd(), "src/engine_tests")
    13 
    14 ls = listdir(srcpath)
    15 
    16 suites = []
    17 
    18 indent = "    "
    19 
    20 parser = argparse.ArgumentParser()
    21 parser.add_argument("--exclude", "-x", help="Comma-separated list of tests to exclude from test suite build")
    22 
    23 args = parser.parse_args()
    24 
    25 exclude_arg = args.exclude
    26 exclude_list = []
    27 if exclude_arg:
    28     exclude_list = exclude_arg.split(",")
    29     for x in exclude_list:
    30         x.replace(" ","")
    31 for f in ls:
    32     if isfile(join(srcpath, f)):
    33         if (f.endswith(".cc")):
    34             suite = sub('\.cc$', '', f)
    35             if not suite in exclude_list:
    36                 suites.append(suite)
    37 
    38 license = (''
    39 '// This file is under GNU General Public License 3.0\n'
    40 '// see LICENSE.txt\n\n')
    41 
    42 
    43 header = license + "//\n// src/SuiteMaker.cc generated by gensuitemaker.py - changes may be overwritten. You've been warned!\n//\n\n"
    44 
    45 header += (''
    46 '#include <cpptest.h>\n'
    47 '#include <cpptest-suite.h>\n'
    48 '#include <memory>\n'
    49 '#include <vector>\n'
    50 '#include "SuiteMaker.h"\n')
    51 
    52 suiteincludes = ""
    53 
    54 for s in suites:
    55     suiteincludes += '#include "' + s + '.h"\n'
    56 
    57 output = header + "\n// Begin where we generate stuff\n" + suiteincludes + "\n"
    58 
    59 output += ("\n"
    60 "const char* SuiteMaker::all_suites[] = {\n")
    61 
    62 first = True
    63 
    64 for s in suites:
    65     output += indent + '"' + s + '",\n'
    66 
    67 output += "};\n\n"
    68 
    69 output += (
    70 '// This file is generated, so magic constants are ok.\n'
    71 'int SuiteMaker::num_suites = ')
    72 
    73 output += str(len(suites)) + ";\n\n"
    74     
    75 output += "void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {\n"
    76 
    77 first = True
    78 
    79 for s in suites:
    80     output += indent
    81     
    82     if not first:
    83         output += "else "
    84     else:
    85         first = False
    86     
    87     output += ('if (strcmp(test_class_name, "' + s + '") == 0)\n' + 
    88                indent + indent + '*test_suite = new ' + s + '(test_class_name, test_home);\n')
    89         
    90 output += "}\n\n"
    91 
    92 output += (
    93 'void SuiteMaker::suitemaker_buildlist(const char** test_class_names, int num_to_run, const char* test_home, std::vector<Test::Suite*>& test_suites) {\n'
    94 '    for (int i = 0; i < num_to_run; i++) {\n'
    95 '        Test::Suite* suite = NULL;\n'
    96 '        SuiteMaker::suitemaker_build(test_class_names[i], test_home, &suite);\n'
    97 '        if (!suite)\n'
    98 '            throw std::runtime_error("Could not create a test suite instance."); // FIXME, better error, cleanup, obviously\n'
    99 '        test_suites.push_back(suite);\n'
   100 '    }\n'    
   101 '}'
   102 '\n'
   103 'void SuiteMaker::suitemaker_buildall(const char* test_home, std::vector<Test::Suite*>& test_suites) {\n'
   104 '    SuiteMaker::suitemaker_buildlist(SuiteMaker::all_suites, SuiteMaker::num_suites, test_home, test_suites);\n'
   105 '}\n\n')
   106 
   107 suitemaker = open("src/SuiteMaker.cc", 'w') 
   108 suitemaker.write(output)
   109 suitemaker.close()
   110 
   111 generated_at = ("src/SuiteMaker.cc" + " generated at " + datetime.datetime.now().strftime('%c'))
   112 print(generated_at)