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