test/gen_test_skel.py
author Jorg Knobloch
Thu, 30 Jul 2020 22:50:42 +0200
branchENGINE-780-take2
changeset 4910 347f8b793204
parent 4170 14e0ece2e488
child 4956 ccaf0fbc5006
permissions -rw-r--r--
ENGINE-780, take 2: After normalizing to UTC, forget the offset.
krista@4034
     1
import argparse
krista@4034
     2
import os
krista@4034
     3
import subprocess
krista@4034
     4
import re
krista@4034
     5
krista@4034
     6
def decamel(name):
krista@4034
     7
    retval = re.sub('([A-Z])', r'_\1', name).lower()
krista@4034
     8
    return re.sub('^_', r'', retval) 
krista@4034
     9
krista@4034
    10
def tb(n):
krista@4034
    11
    retval = ""
krista@4034
    12
    for i in range(n):
krista@4034
    13
        retval = retval + "    "
krista@4034
    14
    return retval
krista@4034
    15
krista@4034
    16
parser = argparse.ArgumentParser()
krista@4034
    17
parser.add_argument("suite_name", help="(convention is <NameInUpperCamelCase>, e.g. StringpairList - suite created will then be StringpairListTests)")
krista@4034
    18
parser.add_argument("--clobber", "-c", help="Overwrite extant files (must be explicitly indicated)", action='store_true')
krista@4034
    19
krista@4034
    20
args = parser.parse_args()
krista@4034
    21
krista@4034
    22
suitename = args.suite_name
krista@4034
    23
test_suite = suitename + "Test"
krista@4034
    24
krista@4034
    25
nspace = "using namespace std;\n\n"
krista@4034
    26
krista@4034
    27
newfile = open(os.path.join("src", test_suite + ".cc"), 'w')
krista@4034
    28
krista@4034
    29
license = ("// This file is under GNU General Public License 3.0\n"
krista@4034
    30
           "// see LICENSE.txt\n\n")
krista@4034
    31
krista@4034
    32
newfile.write("#include <stdlib.h>\n")
krista@4034
    33
newfile.write("#include <string>\n")
krista@4034
    34
newfile.write("#include <cstring>\n\n")
krista@4034
    35
newfile.write("#include \"pEpEngine.h\"\n")
krista@4034
    36
newfile.write("#include \"test_util.h\"\n")
krista@4034
    37
newfile.write("#include \"TestConstants.h\"\n")
krista@4034
    38
newfile.write("#include \"Engine.h\"\n\n")                    
krista@4034
    39
newfile.write("#include <gtest/gtest.h>\n\n\n")
krista@4034
    40
newfile.write("namespace {\n\n\t//The fixture for " + test_suite + "\n")
krista@4034
    41
newfile.write(tb(1) + "class " + test_suite + " : public ::testing::Test {\n")
krista@4034
    42
newfile.write(tb(2) + "public:\n")
krista@4034
    43
newfile.write(tb(3) + "Engine* engine;\n")
krista@4034
    44
newfile.write(tb(3) + "PEP_SESSION session;\n\n")
krista@4034
    45
newfile.write(tb(2) + "protected:\n")
krista@4034
    46
newfile.write(tb(3) + "// You can remove any or all of the following functions if its body\n")
krista@4034
    47
newfile.write(tb(3) + "// is empty.\n")
krista@4034
    48
newfile.write(tb(3) + test_suite + "() {\n")
krista@4034
    49
newfile.write(tb(4) + "// You can do set-up work for each test here.\n")
krista@4170
    50
newfile.write(tb(4) + "test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();\n")
krista@4034
    51
newfile.write(tb(4) + "test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();\n")
krista@4034
    52
newfile.write(tb(4) + "test_path = get_main_test_home_dir() + \"/\" + test_suite_name + \"/\" + test_name;\n")
krista@4034
    53
newfile.write(tb(3) + "}\n\n")
krista@4034
    54
newfile.write(tb(3) + "~" + test_suite + "() override {\n")
krista@4034
    55
newfile.write(tb(4) + "// You can do clean-up work that doesn't throw exceptions here.\n")
krista@4034
    56
newfile.write(tb(3) + "}\n\n")
krista@4034
    57
newfile.write(tb(3) + "// If the constructor and destructor are not enough for setting up\n")
krista@4034
    58
newfile.write(tb(3) + "// and cleaning up each test, you can define the following methods:\n\n")
krista@4034
    59
newfile.write(tb(3) + "void SetUp() override {\n")
krista@4034
    60
newfile.write(tb(4) + "// Code here will be called immediately after the constructor (right\n")
krista@4034
    61
newfile.write(tb(4) + "// before each test).\n")
krista@4034
    62
newfile.write("\n" + tb(4) + "// Leave this empty if there are no files to copy to the home directory path\n")
krista@4034
    63
newfile.write(tb(4) + "std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();\n")                                        
krista@4034
    64
newfile.write("\n" + tb(4) + "// Get a new test Engine.\n")                    
krista@4034
    65
newfile.write(tb(4) + "engine = new Engine(test_path);\n");
krista@4034
    66
newfile.write(tb(4) + "ASSERT_NE(engine, nullptr);\n")
krista@4034
    67
newfile.write("\n" + tb(4) + "// Ok, let's initialize test directories etc.\n")                                        
krista@4034
    68
newfile.write(tb(4) + "engine->prep(NULL, NULL, init_files);\n")
krista@4034
    69
newfile.write("\n" + tb(4) + "// Ok, try to start this bugger.\n")                    
krista@4034
    70
newfile.write(tb(4) + "engine->start();\n")                    
krista@4034
    71
newfile.write(tb(4) + "ASSERT_NE(engine->session, nullptr);\n")                    
krista@4034
    72
newfile.write(tb(4) + "session = engine->session;\n") 
krista@4034
    73
newfile.write("\n" + tb(4) + "// Engine is up. Keep on truckin\'\n");                                                            
krista@4034
    74
newfile.write(tb(3) + "}\n\n")
krista@4034
    75
newfile.write(tb(3) + "void TearDown() override {\n")
krista@4034
    76
newfile.write(tb(4) + "// Code here will be called immediately after each test (right\n")
krista@4034
    77
newfile.write(tb(4) + "// before the destructor).\n")   
krista@4034
    78
newfile.write(tb(4) + "engine->shut_down();\n")
krista@4034
    79
newfile.write(tb(4) + "delete engine;\n")                    
krista@4034
    80
newfile.write(tb(4) + "engine = NULL;\n")                    
krista@4034
    81
newfile.write(tb(4) + "session = NULL;\n")                    
krista@4034
    82
newfile.write(tb(3) + "}\n\n")
krista@4034
    83
newfile.write(tb(2) + "private:\n");
krista@4034
    84
newfile.write(tb(3) + "const char* test_suite_name;\n")
krista@4034
    85
newfile.write(tb(3) + "const char* test_name;\n")                                                            
krista@4034
    86
newfile.write(tb(3) + "string test_path;\n") 
krista@4034
    87
newfile.write(tb(3) + "// Objects declared here can be used by all tests in the " + test_suite + " suite.\n\n")
krista@4034
    88
newfile.write(tb(1) + "};\n\n")
krista@4034
    89
newfile.write("}  // namespace\n\n\n")
krista@4034
    90
newfile.write("TEST_F(" + test_suite + ", check_" + decamel(suitename) + ") {\n")
krista@4034
    91
newfile.write(tb(1) + "// This is just a dummy test case. The convention is check_whatever_you_are_checking\n")
krista@4034
    92
newfile.write(tb(1) + "// so for multiple test cases in a suite, be more explicit ;)\n")
krista@4034
    93
newfile.write("}\n")