test/gentestshell.py
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 18 Nov 2019 15:34:21 +0100
changeset 4205 f75e9274551f
parent 2654 be057a8a002e
child 3249 03119f76ea35
permissions -rw-r--r--
Ugh. Old API.
     1 import argparse
     2 import os
     3 import subprocess
     4 import re
     5 
     6 def decamel(name):
     7     retval = re.sub('([A-Z])', r'_\1', name).lower()
     8     return re.sub('^_', r'', retval) 
     9 
    10 parser = argparse.ArgumentParser()
    11 parser.add_argument("suite_name", help="(convention is <NameInUpperCamelCase>, e.g. StringpairList - suite created will then be StringpairListTests)")
    12 parser.add_argument("--clobber", "-c", help="Overwrite extant files (must be explicitly indicated)", action='store_true')
    13 
    14 gengroup = parser.add_mutually_exclusive_group()
    15 gengroup.add_argument("--no_src", help="Generate header only, no source", action='store_true')
    16 gengroup.add_argument("--no_hdr", help="Generate source only, no header", action='store_true')
    17 
    18 typegroup = parser.add_mutually_exclusive_group()
    19 typegroup.add_argument("--test", "-t", help="Just generate a generic test suite (no engine initialisation or db/gpg setup/teardown - default)", action='store_true')
    20 typegroup.add_argument("--indiv", "-i", help="Generate a test suite with engine initialisation/release and gpg/db teardown between each test function", action='store_true')
    21 typegroup.add_argument("--session", "-s", help="Generate a test suite with engine initialisation/release and gpg/db teardown only at the beginning and end of the test suite", action='store_true')
    22 
    23 args = parser.parse_args()
    24 
    25 suitename = args.suite_name
    26 test_suite = suitename + "Tests"
    27 
    28 superclass = "EngineTestSuite"
    29 
    30 if args.indiv: 
    31     superclass = "EngineTestIndividualSuite"
    32 elif args.session:
    33     superclass = "EngineTestSessionSuite"
    34 
    35 print("\nCreating " + test_suite + " as an " + superclass + "\n")
    36 
    37 uncamel = decamel(suitename)
    38 print(uncamel)
    39 
    40 
    41 nspace = "using namespace std;\n\n"
    42 
    43 license = ("// This file is under GNU General Public License 3.0\n"
    44            "// see LICENSE.txt\n\n")
    45 
    46 default_single_testname = "check_" + re.sub('_tests$', r'', uncamel) 
    47 
    48 if not args.no_hdr:
    49     
    50     header_def = uncamel.upper() + "_H"
    51 
    52     deftxt = "#ifndef " + header_def + "\n#define " + header_def + "\n\n"
    53 
    54     header_inc = ("#include <string>\n" 
    55                   "#include \"" + superclass + ".h\"\n\n")
    56 
    57     header = license + deftxt + header_inc + nspace
    58 
    59 
    60     classdef = "class " + test_suite + " : public " + superclass + " {\n" + \
    61                "    public:\n" + \
    62                "        " + test_suite + "(string test_suite, string test_home_dir);\n" + \
    63                "    private:\n" \
    64                "        void " + default_single_testname + "();\n" + \
    65                "};\n" 
    66 
    67     header_file = header + classdef + "\n#endif\n"
    68 
    69     #print(header_file)
    70 
    71     do_write = True
    72     hfile_name = test_suite + ".h"
    73     hfile_path = os.path.join(os.path.join(os.getcwd(), "include"), hfile_name)
    74 
    75     if not args.clobber:
    76         if (os.path.isfile(hfile_path)):
    77             print(hfile_path + " exists. Not writing header file. Use --clobber to overwrite.")
    78             do_write = False
    79 
    80     if do_write:
    81         header_out = open(hfile_path, 'w')
    82         header_out.write(header_file)
    83         header_out.close()
    84 
    85 if not args.no_src:
    86     src_inc = ('#include <stdlib.h>\n'
    87                '#include <string>\n\n'
    88                '#include "pEpEngine.h"\n\n'
    89                '#include "' + superclass +'.h"\n'
    90                '#include "' + test_suite + '.h"\n\n')
    91 
    92     test_suite_prefix = test_suite + "::"
    93     fname = test_suite_prefix + default_single_testname
    94 
    95     constructor = test_suite_prefix + test_suite + "(string suitename, string test_home_dir) :\n" + \
    96                   "    " + superclass + "::" + superclass + "(suitename, test_home_dir) {\n" + \
    97                   "    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string(\"" + fname + "\"),\n" + \
    98                   "                                                                      static_cast<Func>(&" + fname + ")));\n" + \
    99                   "}\n\n"
   100                   
   101     def_funct = "void " + test_suite_prefix + default_single_testname + "() {\n" + \
   102                 "    TEST_ASSERT(true);\n" + \
   103                 "}\n\n"
   104 
   105     src_file = license + src_inc + nspace + constructor + def_funct
   106 
   107     do_write = True
   108     sfile_name = test_suite + ".cc"
   109     sfile_path = os.path.join(os.path.join(os.getcwd(), "src/engine_tests"), sfile_name)
   110 
   111     if not args.clobber:
   112         if (os.path.isfile(sfile_path)):
   113             print(sfile_path + " exists. Not writing source file. Use --clobber to overwrite.")
   114             do_write = False
   115 
   116     if do_write:
   117         src_out = open(sfile_path, 'w')
   118         src_out.write(src_file)
   119         src_out.close()