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