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