-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_3.py
310 lines (300 loc) · 14.1 KB
/
module_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
##
# @copyright: 2016
#
# @File Name: module_3.py
# @Author: Christopher Gibbs
# @Date: 10/8/16
# @Purpose: This module is responsible for creating a C++ file that contains
# the test case code for the commented functions in the original
# C++ file. The make_test_file function is defined here, which
# takes in a list of Test object from module_2 and a string
# representing the name of the original file and outputs
# nothing. It instead writes code into a new file.
##
# Lukas' module which defines the Test and Fun
import module_2
##
# @fun_file_name:
# The name of the original C++ file that the functions were
# written in.
# @test_list: The list of Test objects that were created from parsing the
# original file.
# @return: Nothing.
# @Purpose: Writes testing code to an output file corresponding to the given
# Tests.
##
def make_test_file(fun_file_name, test_list):
# Remove .cpp from end of fun_file_name if it exists
if fun_file_name.endswith('.cpp'):
stripped_fun_file_name = fun_file_name[0:len(fun_file_name) - 4]
else:
stripped_fun_file_name = fun_file_name
test_file_name = stripped_fun_file_name + '_tests.cpp'
# If file already exists, overwrite it. Else, create the file
test_file = open(test_file_name, 'w+')
# Add the include directive to the file
test_file.write('#include "' + fun_file_name + '"\n\n')
test_file.write('#include <string>\n#include <vector>\n' +
'#include <exception>\n')
# Overload the ostream operator for exceptions
# We have to do this so that we can print arbitrary exception types
# (e.g: std::string)
test_file.write("std::ostream& operator<<(std::ostream &out, const " +
"std::exception &rhs){return out << rhs.what();}")
# Write all code in a main function
test_file.write("int main(int argc, char *argv[]){")
# bool value that stores whether the test passed or failed
test_file.write('bool result, test_suite_result = true;\n')
test_file.write('std::vector<std::string> fail_vec;\n')
test_file.write('bool has_message;\nstd::string exn_mess;\n')
test_file.write('std::string output_str = "";\n\n')
# Counter for number of tests ran
test_num = 1
# For each test, wrap in a try-catch for exceptions, then make function
for test in test_list:
# Create a string of the input arguments with correct commas
input_string = ''
for inarg in test.inputs:
input_string += inarg + ", "
input_string = input_string[:-2]
# TEST
if test.Type == module_2.Test_Type['test']:
expect_out = test.output[0]
# Try block
test_file.write('try\n')
test_file.write('{\n')
count = 0
deferred_declarations = []
# Argument declarations
for Type in test.fun.arg_types:
if Type.strip()[len(Type.strip()) - 1] == '&':
# we've got a reference and we can't
# default initialize it
deferred_declarations.append\
((Type, test.fun.arg_names[count]))
else:
test_file.write(Type + " " + test.fun.arg_names[count] +
';\n')
count += 1
# Setup
for setup_val in test.setup:
setup_no_paren = setup_val[1:-1]
for declaration in deferred_declarations:
if setup_no_paren.strip().startswith(declaration[1]) and \
not setup_no_paren.strip().startswith(declaration[1] +
"."):
setup_no_paren = declaration[0] + " " + setup_no_paren
test_file.write('\t' + setup_no_paren + ';\n')
# Create variable of output type to store output
test_file.write('\t' + test.fun.output_type + ' output;\n')
# Function
test_file.write('\toutput =')
test_file.write(test.fun.name + '(' + input_string + ');\n')
test_file.write('\tif (output == ' + expect_out + ')\n')
test_file.write('\t\tresult = true;\n')
test_file.write('\telse\n')
test_file.write('\t\tresult = false;\n')
test_file.write('}\n')
# Catch block
test_file.write('catch (std::exception& e)\n')
test_file.write('{\n')
test_file.write('\tstd::cout << "Exception caught: "' +
' << e << std::endl;\n')
test_file.write('\tresult = false;\n')
test_file.write('}\n')
test_file.write('catch (...)\n')
test_file.write('{\n')
test_file.write('\tstd::cout << "Caught unexpected ' +
'exception" << std::endl;\n')
test_file.write('}\n\n')
test_file.write('if (!result)\n')
test_file.write('{\n')
test_file.write('\ttest_suite_result = false;\n')
test_file.write('\tfail_vec.push_back("' +
test.fun.name + '(' + input_string +
') == ' + expect_out + '");\n')
test_file.write('}\n\n')
# TEST-EXN
elif test.Type == module_2.Test_Type['test-exn']:
exn_type = test.output[0]
exn_message = '"Dummy value"'
output_str = ""
test_file.write('has_message = true;\n')
if (len(test.output) == 2):
exn_message = test.output[1]
test_file.write('has_message = true;\n')
else:
test_file.write('has_message = false;\n')
test_file.write('exn_mess = ' + exn_message + ';\n')
# Try block
test_file.write('output_str = "";\n')
test_file.write('try\n')
test_file.write('{\n')
count = 0
deferred_declarations = []
# Argument declarations
for Type in test.fun.arg_types:
if Type.strip()[len(Type.strip()) - 1] == '&':
# we've got a reference and we can't
# default initialize it
deferred_declarations.append\
((Type, test.fun.arg_names[count]))
else:
test_file.write(Type + " " + test.fun.arg_names[count] +
';\n')
count += 1
# Setup
for setup_val in test.setup:
setup_no_paren = setup_val[1:-1]
for declaration in deferred_declarations:
if setup_no_paren.strip().startswith(declaration[1]) and \
not setup_no_paren.strip().startswith(declaration[1] +
"."):
setup_no_paren = declaration[0] + " " + setup_no_paren
test_file.write('\t' + setup_no_paren + ';\n')
# Function
test_file.write(test.fun.name + '(' + input_string + ');\n')
test_file.write('\tresult = false;\n')
test_file.write('\toutput_str = " passed without exception";\n')
test_file.write('}\n')
# Catch block
# cstr == c string == const char*
if exn_type == 'cstr':
exn_type = 'const char*'
test_file.write('catch (' + exn_type + '& e)\n')
test_file.write('{\n')
test_file.write('\tif (has_message)\n')
test_file.write('\t\tif (exn_mess.compare(e) ' + '== 0)\n')
test_file.write('\t\t\tresult = true;\n')
test_file.write('\t\telse\n\t\t{\n')
test_file.write('\t\t\tstd::cout << ' + '"Exception caught: "' +
' << e << std::endl;\n')
test_file.write('\t\t\tresult = false;\n')
test_file.write('\toutput_str = "threw right exception but ' + \
'wrong message";\n')
test_file.write('\t\t}\n\telse\n')
test_file.write('\t\tresult = true;\n')
test_file.write('}\n')
# Catch block
test_file.write('catch (std::exception& e)\n')
test_file.write('{\n')
test_file.write('\tstd::cout << "Exception caught: "' +
' << e << std::endl;\n')
test_file.write('\tresult = false;\n')
test_file.write('\toutput_str = "threw wrong exception";\n')
test_file.write('}\n')
test_file.write('catch (...)\n')
test_file.write('{\n')
test_file.write('\tstd::cout << "Caught unexpected ' +
'exception" << std::endl;\n')
test_file.write('}\n\n')
test_file.write('if (!result)\n')
test_file.write('{\n')
test_file.write('\ttest_suite_result = false;\n')
test_file.write('\tfail_vec.push_back("' + test.fun.name + '(' +
input_string + ')" + output_str);\n')
test_file.write('}\n\n')
# # TEST-PRINT - keeping for future implementation
# elif test.Type == module_2.Test_Type['test-print']
# expect_out = test.output[0]
# # Try block
# test_file.write('try\n')
# test_file.write('{\n')
# # For test-print, the function call needs to be before
# # the check
# test_file.write(test.fun.name + '(' + input_string\
# + ');\n')
# # Function
# test_file.write('BOOST_CHECK(')
# test_file.write(out_test)
# test_file.write(' == ' expect_out + ');\n')
# test_file.write('}\n')
# # Catch block
# test_file.write('catch (std::exception& e)')
# test_file.write('{\n')
# test_file.write('std::cout << "Exception caught: "'\
# + ' << e << std::endl;\n')
# test_file.write('}\n\n')
# TEST-INPUT
else:
# Try block
test_file.write('result = true;\n')
test_file.write('try\n')
test_file.write('{\n')
count = 0
deferred_declarations = []
# Argument declarations
for Type in test.fun.arg_types:
if Type.strip()[len(Type.strip()) - 1] == '&':
# we've got a reference and we can't
# default initialize it
deferred_declarations.append\
((Type, test.fun.arg_names[count]))
else:
test_file.write(Type + " " + test.fun.arg_names[count] +
';\n')
count += 1
# Setup
for setup_val in test.setup:
setup_no_paren = setup_val[1:-1]
for declaration in deferred_declarations:
if setup_no_paren.strip().startswith(declaration[1]) and \
not setup_no_paren.strip().startswith(declaration[1] +
"."):
setup_no_paren = declaration[0] + " " + setup_no_paren
test_file.write('\t' + setup_no_paren + ';\n')
# Function
test_file.write('\t' + test.fun.name + '(')
arg_string = ""
for arg in test.fun.arg_names:
arg_string += arg + ', '
arg_string = arg_string[:-2]
test_file.write(arg_string)
test_file.write(');\n')
count = 0
for in_to_check in test.inputs:
test_file.write('\tif (' + in_to_check + ' != ' +
test.output[count] + ')\n')
test_file.write('\t\tresult = false;\n')
count += 1
test_file.write('}\n')
# Catch block
test_file.write('catch (std::exception& e)\n')
test_file.write('{\n')
test_file.write('\tstd::cout << "Exception caught: "' +
' << e << std::endl;\n')
test_file.write('\tresult = false;\n')
test_file.write('}\n')
test_file.write('catch (...)\n')
test_file.write('{\n')
test_file.write('\tstd::cout << "Caught unexpected ' +
'exception" << std::endl;\n')
test_file.write('}\n\n')
test_file.write('if (!result)\n')
test_file.write('{\n')
test_file.write('\ttest_suite_result = false;\n')
# Get lists of parameters, setups, expected mutations
test_file.write('\tfail_vec.push_back("' + test.fun.name + '(' +
arg_string + ')' + ' with values being checked=' +
str(test.inputs) + ' and setup=' + str(test.setup) +
' and expected output=' + str(test.output) +'");\n')
test_file.write('}\n\n')
test_num += 1;
# All cases done, write final result code
test_file.write('if(test_suite_result)\n')
test_file.write('\tstd::cout << "All ' + str(test_num) + \
' tests passed." << std::endl;\n')
test_file.write('else\n')
test_file.write('{\n')
test_file.write('\tstd::cout << fail_vec.size() << " ' + \
'tests failed out of ' + str(test_num) + '."' + \
'<< " The following tests did not pass:" ' + \
'<< std::endl;\n')
test_file.write('\tfor (int i = 0; i < fail_vec.size(); ++i)\n\t{\n')
test_file.write('\t\tstd::cout << fail_vec[i] << std::endl;\n\t}\n')
test_file.write('}\n\n')
test_file.write("return 0;}")