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