test/gtest_firstpass.py
author Jorg Knobloch
Thu, 30 Jul 2020 22:50:42 +0200
branchENGINE-780-take2
changeset 4910 347f8b793204
parent 4170 14e0ece2e488
child 4973 48a379cf24ff
permissions -rw-r--r--
ENGINE-780, take 2: After normalizing to UTC, forget the offset.
     1 import re 
     2 import sys
     3 import os
     4 
     5 def tb(n):
     6     retval = ""
     7     for i in range(n):
     8         retval = retval + "    "
     9     return retval
    10         
    11 #filename = sys.argv[1]
    12 #outfile = sys.argv[2]
    13 
    14 for filename in os.listdir("src"):
    15     fixture_in = False 
    16     removing_old_constructor = False
    17     constructor_done = False
    18     modline = None
    19     eat_next_line = False;
    20     
    21     if not filename.endswith("Tests.cc"):
    22         continue
    23         
    24     outfile = filename[:-4] + ".cc"
    25     
    26     print("Output file is " + outfile)
    27         
    28     newfile = open(os.path.join("src", outfile),'w')
    29 
    30     with open(os.path.join("src", filename)) as fp: 
    31         for line in fp:
    32             if (eat_next_line):
    33                 eat_next_line = False;
    34                 continue;
    35             line = line.rstrip();
    36             
    37             if not fixture_in:
    38                 if (removing_old_constructor):
    39                     if "}" in line:
    40                         removing_old_constructor = False  
    41                         constructor_done = True 
    42                     continue
    43                 else:        
    44                     if (line.find("namespace") >= 0):
    45                         continue
    46                     if (line.find("Tests.h") >= 0):
    47                         continue
    48                     if (line.find("cpptest") >= 0):
    49                         continue 
    50                     if (line.find("EngineTestSuite.h") >= 0 or line.find("EngineTestIndividualSuite.h") >= 0 or line.find("EngineTestSessionSuite.h") >= 0):
    51                         continue
    52                     
    53                     if (modline == None):                
    54                         modline = re.sub(r'(.*)Tests::(.*)Tests\(string suitename, string test_home_dir\) :', r'\1Test', line)                
    55                         
    56                     if(modline == line):
    57                         newfile.write(line + "\n")
    58                         modline = None
    59                         continue
    60                     else:
    61                         if not (constructor_done):
    62                             removing_old_constructor = True
    63                             continue    
    64                             
    65                         #*Tests::*Tests(string suitename, string test_home_dir)
    66                         # Put in fixture blob
    67                         # - delete through first }
    68                         #print(modline) 
    69                         newfile.write("#include \"Engine.h\"\n\n")                    
    70                         newfile.write("#include <gtest/gtest.h>\n\n\n")
    71                         newfile.write("namespace {\n\n\t//The fixture for " + modline + "\n")
    72                         newfile.write(tb(1) + "class " + modline + " : public ::testing::Test {\n")
    73                         newfile.write(tb(2) + "public:\n")
    74                         newfile.write(tb(3) + "Engine* engine;\n")
    75                         newfile.write(tb(3) + "PEP_SESSION session;\n\n")
    76                         newfile.write(tb(2) + "protected:\n")
    77                         newfile.write(tb(3) + "// You can remove any or all of the following functions if its body\n")
    78                         newfile.write(tb(3) + "// is empty.\n")
    79                         newfile.write(tb(3) + "" + modline + "() {\n")
    80                         newfile.write(tb(4) + "// You can do set-up work for each test here.\n")
    81                         newfile.write(tb(4) + "test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();\n")
    82                         newfile.write(tb(4) + "test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();\n")
    83                         newfile.write(tb(4) + "test_path = get_main_test_home_dir() + \"/\" + test_suite_name + \"/\" + test_name;\n")
    84                         newfile.write(tb(3) + "}\n\n")
    85                         newfile.write(tb(3) + "~" + modline + "() override {\n")
    86                         newfile.write(tb(4) + "// You can do clean-up work that doesn't throw exceptions here.\n")
    87                         newfile.write(tb(3) + "}\n\n")
    88                         newfile.write(tb(3) + "// If the constructor and destructor are not enough for setting up\n")
    89                         newfile.write(tb(3) + "// and cleaning up each test, you can define the following methods:\n\n")
    90                         newfile.write(tb(3) + "void SetUp() override {\n")
    91                         newfile.write(tb(4) + "// Code here will be called immediately after the constructor (right\n")
    92                         newfile.write(tb(4) + "// before each test).\n")
    93                         newfile.write("\n" + tb(4) + "// Leave this empty if there are no files to copy to the home directory path\n")
    94                         newfile.write(tb(4) + "std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();\n")                                        
    95                         newfile.write("\n" + tb(4) + "// Get a new test Engine.\n")                    
    96                         newfile.write(tb(4) + "engine = new Engine(test_path);\n");
    97                         newfile.write(tb(4) + "ASSERT_NE(engine, nullptr);\n")
    98                         newfile.write("\n" + tb(4) + "// Ok, let's initialize test directories etc.\n")                                        
    99                         newfile.write(tb(4) + "engine->prep(NULL, NULL, init_files);\n")
   100                         newfile.write("\n" + tb(4) + "// Ok, try to start this bugger.\n")                    
   101                         newfile.write(tb(4) + "engine->start();\n")                    
   102                         newfile.write(tb(4) + "ASSERT_NE(engine->session, nullptr);\n")                    
   103                         newfile.write(tb(4) + "session = engine->session;\n") 
   104                         newfile.write("\n" + tb(4) + "// Engine is up. Keep on truckin\'\n");                                                            
   105                         newfile.write(tb(3) + "}\n\n")
   106                         newfile.write(tb(3) + "void TearDown() override {\n")
   107                         newfile.write(tb(4) + "// Code here will be called immediately after each test (right\n")
   108                         newfile.write(tb(4) + "// before the destructor).\n")   
   109                         newfile.write(tb(4) + "engine->shut_down();\n")
   110                         newfile.write(tb(4) + "delete engine;\n")                    
   111                         newfile.write(tb(4) + "engine = NULL;\n")                    
   112                         newfile.write(tb(4) + "session = NULL;\n")                    
   113                         newfile.write(tb(3) + "}\n\n")
   114                         newfile.write(tb(2) + "private:\n");
   115                         newfile.write(tb(3) + "const char* test_suite_name;\n")
   116                         newfile.write(tb(3) + "const char* test_name;\n")                                                            
   117                         newfile.write(tb(3) + "string test_path;\n") 
   118                         newfile.write(tb(3) + "// Objects declared here can be used by all tests in the " + modline + " suite.\n\n")
   119                         newfile.write(tb(1) + "};\n\n")
   120                         newfile.write("}  // namespace\n\n\n")
   121 
   122                         fixture_in = True
   123             else:
   124                 #void *Tests::check*() {
   125                 # -> TEST_F(*Test, check*) {
   126                 modline = re.sub(r'void\s*(.*)Tests::check(.*)\(\)\s*{', r'TEST_F(\1Test, check\2) {', line)
   127                 if (line != modline):
   128                     newfile.write(modline + "\n")
   129                     continue
   130                     
   131                 #TEST_ASSERT(true)
   132                 # -> <nothing>                
   133                 if (line.find("TEST_ASSERT(true)") >= 0):
   134                     continue
   135                     
   136                 #TEST_ASSERT_MSG(strcmp(blah,blah) == 0, *)
   137                 #TEST_ASSERT(strcmp(blah,blah == 0))
   138                 # -> ASSERT_STREQ(blah,blah)
   139                 modline = re.sub(r'TEST_ASSERT_MSG\(\s*strcmp\(\s*(.*),\s*(.*)\)\s*==\s*0\s*,.*\);', r'ASSERT_STREQ(\1, \2);', line)
   140                 if (line != modline):
   141                     newfile.write(modline + "\n")
   142                     continue
   143                 modline = re.sub(r'TEST_ASSERT\(\s*strcmp\(\s*(.*),\s*(.*)\)\s*==\s*0\);', r'ASSERT_STREQ(\1, \2);', line)
   144                 if (line != modline):
   145                     newfile.write(modline + "\n")
   146                     continue
   147 
   148                 #TEST_ASSERT_MSG(strcmp(blah,blah) != 0, *)
   149                 #TEST_ASSERT(strcmp(blah,blah != 0))
   150                 # -> ASSERT_STREQ(blah,blah)
   151                 modline = re.sub(r'TEST_ASSERT_MSG\(\s*strcmp\(\s*(.*),\s*(.*)\)\s*!=\s*0\s*,.*\);', r'ASSERT_STRNE(\1, \2);', line)
   152                 if (line != modline):
   153                     newfile.write(modline + "\n")
   154                     continue
   155                 modline = re.sub(r'TEST_ASSERT\(\s*strcmp\(\s*(.*),\s*(.*)\)\s*!=\s*0\);', r'ASSERT_STRNE(\1, \2);', line)
   156                 if (line != modline):
   157                     newfile.write(modline + "\n")
   158                     continue
   159                     
   160                 #TEST_ASSERT_MSG(<x> == NULL, *);
   161                 #TEST_ASSERT(<x> == NULL);
   162                 # -> ASSERT_EQ(<x>, nullptr);
   163                 modline = re.sub(r'TEST_ASSERT_MSG\((.*)\s*==\s*NULL,.*\);',r'ASSERT_EQ(\1, nullptr);', line)
   164                 if (line != modline):
   165                     newfile.write(modline + "\n")
   166                     continue
   167                 modline = re.sub(r'TEST_ASSERT\((.*)\s*==\s*NULL\);', r'ASSERT_EQ(\1, nullptr);',line)
   168                 if (line != modline):
   169                     newfile.write(modline + "\n")
   170                     continue
   171                 
   172                 #TEST_ASSERT_MSG(<x> != NULL, *);
   173                 #TEST_ASSERT(<x> != NULL);
   174                 # -> ASSERT_NE(<x>, nullptr);
   175                 modline = re.sub(r'TEST_ASSERT_MSG\((.*)\s*!=\s*NULL,.*\);',r'ASSERT_NE(\1, nullptr);', line)
   176                 if (line != modline):
   177                     newfile.write(modline + "\n")
   178                     continue
   179                 modline = re.sub(r'TEST_ASSERT\((.*)\s*!=\s*NULL\);', r'ASSERT_NE(\1, nullptr);',line)
   180                 if (line != modline):
   181                     newfile.write(modline + "\n")
   182                     continue
   183                 
   184                 #TEST_ASSERT_MSG(<x> == <y>, *);
   185                 #TEST_ASSERT(<x> == <y>);
   186                 # -> ASSERT_EQ(<x>, <y>);
   187                 modline = re.sub(r'TEST_ASSERT_MSG\((.*)\s*==\s*(.*),.*\);', r'ASSERT_EQ(\1, \2);',line)
   188                 if (line != modline):
   189                     newfile.write(modline + "\n")
   190                     continue
   191                 modline = re.sub(r'TEST_ASSERT\((.*)\s*==\s*(.*)\);', r'ASSERT_EQ(\1, \2);',line)
   192                 if (line != modline):
   193                     newfile.write(modline + "\n")
   194                     continue
   195                 
   196                 #TEST_ASSERT_MSG(<x> != <y>, *);
   197                 #TEST_ASSERT(<x> != <y>);
   198                 # -> ASSERT_NE(<x>, <y>);
   199                 modline = re.sub(r'TEST_ASSERT_MSG\((.*)\s*!=\s*(.*),.*\);', r'ASSERT_NE(\1, \2);',line)
   200                 if (line != modline):
   201                     newfile.write(modline + "\n")
   202                     continue
   203                 modline = re.sub(r'TEST_ASSERT\((.*)\s*!=\s*(.*)\);', r'ASSERT_NE(\1, \2);',line)
   204                 if (line != modline):
   205                     newfile.write(modline + "\n")
   206                     continue
   207                 
   208                 #TEST_ASSERT_MSG(<x> >= <y>, *);
   209                 #TEST_ASSERT(<x> >= <y>);
   210                 # -> ASSERT_GE(<x>, <y>);
   211                 modline = re.sub(r'TEST_ASSERT_MSG\((.*)\s*[^-]>=\s*(.*),.*\);', r'ASSERT_GE(\1, \2);',line)
   212                 if (line != modline):
   213                     newfile.write(modline + "\n")
   214                     continue
   215                 modline = re.sub(r'TEST_ASSERT\((.*)\s*[^-]>=\s*(.*)\);', r'ASSERT_GE(\1, \2);',line)
   216                 if (line != modline):
   217                     newfile.write(modline + "\n")
   218                     continue
   219                 
   220                 #TEST_ASSERT_MSG(<x> > <y>, *);
   221                 #TEST_ASSERT(<x> > <y>);
   222                 # -> ASSERT_GT(<x>, <y>);
   223                 modline = re.sub(r'TEST_ASSERT_MSG\((.*)\s*[^-]>\s*(.*),.*\);', r'ASSERT_GT(\1, \2);',line)
   224                 if (line != modline):
   225                     newfile.write(modline + "\n")
   226                     continue
   227                 modline = re.sub(r'TEST_ASSERT\((.*)\s*[^-]>\s*(.*)\);', r'ASSERT_GT(\1, \2);',line)
   228                 if (line != modline):
   229                     newfile.write(modline + "\n")
   230                     continue
   231                 
   232                 #TEST_ASSERT_MSG(<x> <= <y>, *);
   233                 #TEST_ASSERT(<x> <= <y>);
   234                 # -> ASSERT_LE(<x>, <y>);
   235                 modline = re.sub(r'TEST_ASSERT_MSG\((.*)\s*<=\s*(.*),.*\);', r'ASSERT_LE(\1, \2);',line)
   236                 if (line != modline):
   237                     newfile.write(modline + "\n")
   238                     continue
   239                 modline = re.sub(r'TEST_ASSERT\((.*)\s*<=\s*(.*)\);', r'ASSERT_LE(\1, \2);',line)
   240                 if (line != modline):
   241                     newfile.write(modline + "\n")
   242                     continue
   243                 
   244                 #TEST_ASSERT_MSG(<x> < <y>, *);
   245                 #TEST_ASSERT(<x> < <y>);
   246                 # -> ASSERT_LT(<x>, <y>);
   247                 modline = re.sub(r'TEST_ASSERT_MSG\((.*)\s*<\s*(.*),.*\);', r'ASSERT_LT(\1, \2);',line)
   248                 if (line != modline):
   249                     newfile.write(modline + "\n")
   250                     continue
   251                 modline = re.sub(r'TEST_ASSERT\((.*)\s*<\s*(.*)\);', r'ASSERT_LT(\1, \2);',line)
   252                 if (line != modline):
   253                     newfile.write(modline + "\n")
   254                     continue
   255 
   256                 #TEST_ASSERT_MSG(slurp_and_import_key(
   257                 #TEST_ASSERT(slurp_and_import_key(
   258                 # -> ASSERT_TRUE(slurp_and_import_key(
   259                 modline = re.sub(r'TEST_ASSERT_MSG\(slurp_and_import_key', r'ASSERT_TRUE(slurp_and_import_key',line)
   260                 if (line != modline):
   261                     if not line.endswith(";"):
   262                         eat_next_line = True
   263                         modline = re.sub(r'\),', r'));', modline);
   264                     newfile.write(modline + "\n")
   265                     continue
   266                 modline = re.sub(r'TEST_ASSERT\(slurp_and_import_key', r'ASSERT_TRUE(slurp_and_import_key',line)
   267                 if (line != modline):
   268                     newfile.write(modline + "\n")
   269                     continue
   270                 
   271                 #TEST_ASSERT_MSG(!<x>, *);
   272                 #TEST_ASSERT(!<x>);
   273                 # -> ASSERT_FALSE(<x>);
   274                 mgroup = re.match(r'TEST_ASSERT_MSG\(!(.*),.*\);', line.lstrip());
   275                 if (mgroup == None):
   276                     mgroup = re.match(r'TEST_ASSERT\(!(.*)\);', line.lstrip());
   277                 
   278                 if (mgroup != None):
   279                     matchphrase = mgroup.group(0)
   280                     is_pointer = False
   281                     while True:
   282                         answer = input("ATTENTION: INPUT REQUIRED: In line " + line.lstrip() + ", is " + matchphrase + " a pointer? (y/n)")
   283                         if (answer == "y" or answer == "Y"):
   284                             is_pointer = True
   285                             break
   286                         elif (answer != "n" and answer != 'N'):
   287                             print("\'" + answer + "\' is not a valid answer. Please answer with 'y' or 'n'.") 
   288                             continue
   289                         break;       
   290                     
   291                     if (is_pointer):
   292                         modline = re.sub(r'TEST_ASSERT_MSG\(!(.*),.*\);',r'ASSERT_NE(\1, nullptr);', line)
   293                         if (line != modline):
   294                             newfile.write(modline + "\n")
   295                             continue
   296                         modline = re.sub(r'TEST_ASSERT\(!(.*)\);', r'ASSERT_NE(\1, nullptr);',line)
   297                         if (line != modline):
   298                             newfile.write(modline + "\n")
   299                             continue
   300                     else:        
   301                         modline = re.sub(r'TEST_ASSERT_MSG\(!(.*),.*\);',r'ASSERT_FALSE(\1);', line)
   302                         if (line != modline):
   303                             newfile.write(modline + "\n")
   304                             continue
   305                         modline = re.sub(r'TEST_ASSERT\(!(.*)\);', r'ASSERT_FALSE(\1);',line)
   306                         if (line != modline):
   307                             newfile.write(modline + "\n")
   308                             continue
   309                                                 
   310                 #TEST_ASSERT_MSG(<x>, *);
   311                 #TEST_ASSERT(<x>);
   312                 # -> ASSERT_TRUE(<x>);
   313                 mgroup = re.match(r'TEST_ASSERT_MSG\((.*),.*\);', line.lstrip());
   314                 if (mgroup == None):
   315                     mgroup = re.match(r'TEST_ASSERT\((.*)\);', line.lstrip());
   316                 
   317                 if (mgroup != None):
   318                     matchphrase = mgroup.group(1)
   319                     is_pointer = False
   320                     while True:
   321                         answer = input("ATTENTION: INPUT REQUIRED: In line " + line.lstrip() + ", is " + matchphrase + " a pointer? (y/n)")
   322                         if (answer == "y" or answer == "Y"):
   323                             is_pointer = True
   324                             break
   325                         elif (answer != "n" and answer != 'N'):
   326                             print("\'" + answer + "\' is not a valid answer. Please answer with 'y' or 'n'.") 
   327                             continue
   328                         break;       
   329                     
   330                     if (is_pointer):
   331                         modline = re.sub(r'TEST_ASSERT_MSG\((.*),.*\);',r'ASSERT_NE(\1, nullptr);', line)
   332                         if (line != modline):
   333                             newfile.write(modline + "\n")
   334                             continue
   335                         modline = re.sub(r'TEST_ASSERT\((.*)\);', r'ASSERT_NE(\1, nullptr);',line)
   336                         if (line != modline):
   337                             newfile.write(modline + "\n")
   338                             continue
   339                     else:        
   340                         modline = re.sub(r'TEST_ASSERT_MSG\((.*),.*\);',r'ASSERT_TRUE(\1);', line)
   341                         if (line != modline):
   342                             newfile.write(modline + "\n")
   343                             continue
   344                         modline = re.sub(r'TEST_ASSERT\((.*)\);', r'ASSERT_TRUE(\1);',line)
   345                         if (line != modline):
   346                             newfile.write(modline + "\n")
   347                             continue
   348                 
   349                 #Ok, it's something else. Print line and go.
   350                 newfile.write(line + "\n")