forked from philipritchey/autograder-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_parsing.py
575 lines (473 loc) · 17.8 KB
/
test_parsing.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
'''
Methods for parsing test specifications.
'''
from typing import List, Tuple, Dict, Any
from dataclasses import dataclass
from attributes import Attributes
from config import BEGIN_MULTILINE_COMMENT_DELIMITER, BEGIN_TEST_DELIMITER, DEFAULT_NUMBER,\
DEFAULT_POINTS, DEFAULT_SHOW_OUTPUT, DEFAULT_TARGET, DEFAULT_TIMEOUT, DEFAULT_VISIBILITY,\
EMPTY_TEST_BLOCK, END_MULTILINE_COMMENT_DELIMITER, END_TEST_DELIMITER, VISIBILITY_OPTIONS
@dataclass
class FilePosition:
'''
Stores file contents with position
'''
index: int
lines: List[str]
filename: str
def unexpected_end_of_input(file_pos: FilePosition) -> Exception:
'''
construct a syntax error reaching an unexpected end of input.
'''
# filename lineno offset text
return SyntaxError(
'unexpected end of input',
(file_pos.filename, file_pos.index, file_pos.lines[-1]))
def goto_next_line(file_pos: FilePosition) -> str:
'''
go to the next non-blank line.
'''
file_pos.index += 1
if file_pos.index >= len(file_pos.lines):
raise unexpected_end_of_input(file_pos)
# skip blank lines
skip_blank_lines(file_pos)
line = file_pos.lines[file_pos.index].strip()
return line
def skip_blank_lines(file_pos: FilePosition) -> None:
'''
skip blank lines.
'''
while file_pos.index < len(file_pos.lines) and not file_pos.lines[file_pos.index].strip():
file_pos.index += 1
def eat_block_of_test(file_pos: FilePosition) -> str:
'''
read the test body.
'''
# go to next line
line = goto_next_line(file_pos)
# expect start of test block
if line == EMPTY_TEST_BLOCK:
return line
if line != BEGIN_TEST_DELIMITER:
# filename lineno offset text
raise SyntaxError(
f'missing expected start of test block: "{BEGIN_TEST_DELIMITER}"',
(file_pos.filename, file_pos.index+1, 1, line))
# eat until end of test block
while line != END_TEST_DELIMITER:
# go to next line
line = goto_next_line(file_pos)
return line
def eat_empty_test_block(file_pos: FilePosition) -> str:
'''
read an empty test block or throw a syntax error if block is not empty.
'''
# go to next line
line = goto_next_line(file_pos)
# expect empty test block
if line == BEGIN_TEST_DELIMITER:
line = goto_next_line(file_pos)
if line != END_TEST_DELIMITER:
raise SyntaxError(
f'expected end of test: "{END_TEST_DELIMITER}"',
(file_pos.filename, file_pos.index+1, 1, line))
return line
if line != EMPTY_TEST_BLOCK:
# filename lineno offset text
raise SyntaxError(
f'expected empty test block: "{EMPTY_TEST_BLOCK}"',
(file_pos.filename, file_pos.index+1, 1, line))
return line
def current_line(file_pos: FilePosition) -> str:
'''
get the current line from the file position structure.
'''
return file_pos.lines[file_pos.index].strip()
def expect_start_of_multiline_comment(file_pos: FilePosition) -> None:
'''
throw a syntax error if the next line is not the beginning of multiline comment delimiter.
'''
# expect start of multiline comment
line = current_line(file_pos)
if line != BEGIN_MULTILINE_COMMENT_DELIMITER:
# filename lineno offset text
raise SyntaxError(
f'missing expected start of multiline comment: "{BEGIN_MULTILINE_COMMENT_DELIMITER}"',
(file_pos.filename, file_pos.index+1, 1, line))
def expect_end_of_multiline_comment(file_pos: FilePosition) -> None:
'''
throw a syntax error if the next line is not the end of multiline comment delimiter.
'''
# expect end of multiline comment
line = current_line(file_pos)
if line != END_MULTILINE_COMMENT_DELIMITER:
# filename lineno offset text
raise SyntaxError(
f'missing expected end of multiline comment: "{END_MULTILINE_COMMENT_DELIMITER}"',
(file_pos.filename, file_pos.index+1, 1, line))
def read_annotations(file_pos: FilePosition) -> Dict[str, Any]:
'''
read test annotations.
'''
expect_start_of_multiline_comment(file_pos)
# go to next line
line = goto_next_line(file_pos)
attr_dict: Dict[str, Any] = dict()
while line.startswith('@'):
try:
tag, value = line.split(sep=':', maxsplit=1)
except ValueError:
raise SyntaxError(
'missing attribute value? (attributes look like "@name: value")',
(file_pos.filename, file_pos.index+1, 1, line))
tag = tag.strip()[1:]
value = value.strip()
if tag in attr_dict:
old_value = attr_dict[tag]
print((
f'[WARNING] ({file_pos.filename}:{file_pos.index+1})'
f' tag "{tag}" already exists,'
f'old value will be overwritten: {old_value} --> {value}'))
if tag == "points":
points = DEFAULT_POINTS
try:
points = float(value)
except ValueError:
print((
f'[WARNING] ({file_pos.filename}:{file_pos.index+1})'
f' points attribute has invalid value ({value}),'
f' using default value ({DEFAULT_POINTS})'))
attr_dict['points'] = points
elif tag == 'timeout':
timeout = DEFAULT_TIMEOUT
try:
timeout = float(value)
if timeout <= 0:
raise ValueError('timeout must be positive')
except ValueError:
print((
f'[WARNING] ({file_pos.filename}:{file_pos.index+1})'
f' timeout attribute has invalid value ({value}),'
f' using default value ({DEFAULT_TIMEOUT})'))
attr_dict['timeout'] = timeout
elif tag == 'visibility':
visibility = DEFAULT_VISIBILITY
if value not in VISIBILITY_OPTIONS:
print((
f'[WARNING] ({file_pos.filename}:{file_pos.index+1})'
f' visibility attribute has invalid value ({value}),'
f' using default value ({DEFAULT_VISIBILITY})'))
else:
visibility = value
attr_dict['visibility'] = visibility
else:
attr_dict[tag] = value
# go to next line
line = goto_next_line(file_pos)
expect_end_of_multiline_comment(file_pos)
return attr_dict
def verify_required_annotations(annotations: Dict[str, Any], file_pos: FilePosition) -> None:
'''
verify that all required attributes are present.
'''
# verify all required attributes are present
# 'target' not required for script or style or compile or memory errors tests
exempt_types = ('script', 'style', 'compile', 'memory_errors')
required_attributes = ('name', 'points', 'type', 'target')
additonal_details = str()
for attr in annotations:
additonal_details += f' {attr}: {annotations[attr]}\n'
for attribute in required_attributes:
if attribute != 'target' or annotations['type'] not in exempt_types:
if attribute not in annotations:
raise KeyError((
f'({file_pos.filename}:{file_pos.index+1})'
f' missing required attribute: {attribute}'
f'\n{additonal_details}'))
if annotations[attribute] == '':
raise ValueError((
f'({file_pos.filename}:{file_pos.index+1})'
f' required attribute missing value: {attribute}'
f'\n{additonal_details}'))
def apply_default_annotations(annotations: Dict[str, Any]) -> None:
'''
apply default values to missing attributes.
'''
if 'number' not in annotations:
annotations['number'] = DEFAULT_NUMBER
if 'target' not in annotations:
annotations['target'] = DEFAULT_TARGET
# set timeouts to default if not specified
if 'timeout' not in annotations:
annotations['timeout'] = DEFAULT_TIMEOUT
# set show_output to default if not specified
if 'show_output' not in annotations:
if (annotations['type'] == 'approved_includes' or
annotations['type'] == 'coverage' or
annotations['type'] == 'compile'):
annotations['show_output'] = 'True'
else:
annotations['show_output'] = DEFAULT_SHOW_OUTPUT
if 'include' not in annotations:
annotations['include'] = ''
if 'skip' in annotations and annotations['skip'].lower() == 'true':
annotations['skip'] = True
else:
annotations['skip'] = False
if 'visibility' not in annotations:
annotations['visibility'] = DEFAULT_VISIBILITY
def read_attributes(file_pos: FilePosition) -> Attributes:
'''
read, verify, andapply default values to test annotations.
'''
# skip blank lines
skip_blank_lines(file_pos)
attributes: Attributes = {
'number': '',
'name': '',
'points': 0.0,
'type': '',
'target': '',
'show_output': '',
'timeout': 0.0,
'include': '',
'code': '',
'expected_input':'',
'expected_output': '',
'script_content': '',
'approved_includes': [],
'skip': False,
'script_args': '',
'visibility': ''
}
if file_pos.index >= len(file_pos.lines):
# at end of file
return attributes
annotations = read_annotations(file_pos)
verify_required_annotations(annotations, file_pos)
apply_default_annotations(annotations)
attributes['number'] = annotations['number']
attributes['name'] = annotations['name']
attributes['points'] = annotations['points']
attributes['type'] = annotations['type']
attributes['target'] = annotations['target']
attributes['show_output'] = annotations['show_output']
attributes['timeout'] = annotations['timeout']
attributes['include'] = annotations['include']
attributes['skip'] = annotations['skip']
attributes['visibility'] = annotations['visibility']
return attributes
def read_block_of_test(file_pos: FilePosition) -> str:
'''
read lines until and end test body delimited is read
'''
code = str()
got_code = True
while got_code:
line = goto_next_line(file_pos)
if line != END_TEST_DELIMITER:
code += line + '\n'
else:
got_code = False
return code
def expect_start_of_test_block(file_pos: FilePosition) -> None:
'''
throw a syntax error if the next line is not the begin test body delimiter
'''
# expect start of test block
line = goto_next_line(file_pos)
if line != BEGIN_TEST_DELIMITER:
# filename lineno offset text
raise SyntaxError(
f'missing expected start of test block: "{BEGIN_TEST_DELIMITER}"',
(file_pos.filename, file_pos.index+1, 1, line))
def expect_end_of_test_block(file_pos: FilePosition) -> None:
'''
throw a syntax error if the next line is not the end test body delimiter
'''
line = goto_next_line(file_pos)
if line != END_TEST_DELIMITER:
# filename lineno offset text
raise SyntaxError(
f'missing expected end of test block: "{END_TEST_DELIMITER}"',
(file_pos.filename, file_pos.index+1, 1, line))
def read_unit_test(file_pos: FilePosition) -> str:
'''
read a unit test.
'''
expect_start_of_test_block(file_pos)
# expect next lines to be unit test code
code = read_block_of_test(file_pos)
return code
def read_io_test(file_pos: FilePosition) -> Tuple[str, str]:
'''
read an i/o test.
'''
expect_start_of_test_block(file_pos)
input_filename = None
output_filename = None
for _ in range(2):
# go to next line
line = goto_next_line(file_pos)
try:
tag, value = line.split(':')
except ValueError:
# filename lineno offset text
raise SyntaxError(
'expected "tag: value" pair',
(file_pos.filename, file_pos.index+1, 1, line))
tag = tag.strip()
value = value.strip()
if tag == 'input':
input_filename = value
elif tag == 'output':
output_filename = value
else:
# filename lineno offset text
raise SyntaxError(
f'unexpected tag ({tag}) in i/o test',
(file_pos.filename, file_pos.index+1, 1, line))
expect_end_of_test_block(file_pos)
if not input_filename:
raise SyntaxError(
'missing output filename in i/o test',
(file_pos.filename, file_pos.index+1, 1, line))
if not output_filename:
raise SyntaxError(
'missing input filename in i/o test',
(file_pos.filename, file_pos.index+1, 1, line))
expected_input = None
expected_output = None
try:
with open(input_filename, 'r') as file:
expected_input = file.read()
except FileNotFoundError:
raise SyntaxError(
f'input file not found: {input_filename}',
(file_pos.filename, file_pos.index+1, 1, line))
try:
with open(output_filename, 'r') as file:
expected_output = file.read()
except FileNotFoundError:
raise SyntaxError(
f'output file not found: {output_filename}',
(file_pos.filename, file_pos.index+1, 1, line))
return expected_input, expected_output
def read_script_test(file_pos: FilePosition) -> Tuple[str, str]:
'''
read a script/custom test.
'''
expect_start_of_test_block(file_pos)
# go to next line
line = goto_next_line(file_pos)
values = line.split(None, 1)
script_args = str()
script_content = str()
if len(values) == 0:
raise SyntaxError(
'missing expected name of script, e.g. scripts/example.sh',
(file_pos.filename, file_pos.index+1, 1, line))
if len(values) == 1:
# does not have args (only script path)
script_filename_string = line
else:
# has args
script_filename_string = values[0]
script_args = values[1]
# print("Script filename: " + script_filename_string)
expect_end_of_test_block(file_pos)
try:
with open(script_filename_string, 'r') as file:
script_content = file.read()
except FileNotFoundError:
print(f'No such file or directory: \'{script_filename_string}\'')
return script_args, script_content
def read_approved_includes(file_pos: FilePosition) -> List[str]:
'''
read an approved include test.
'''
expect_start_of_test_block(file_pos)
# go to next line
line = goto_next_line(file_pos)
approved_includes = list()
while line != END_TEST_DELIMITER:
approved_includes.append(line)
line = goto_next_line(file_pos)
return approved_includes
def read_coverage_test(file_pos: FilePosition) -> Tuple[str, List[str]]:
'''
read a coverage test.
'''
expect_start_of_test_block(file_pos)
source = list()
main = str()
# go to next line
line = goto_next_line(file_pos)
while line != END_TEST_DELIMITER:
try:
tag, values = line.split(':')
except ValueError:
# filename lineno offset text
raise SyntaxError(
'expected "tag: value" pair',
(file_pos.filename, file_pos.index+1, 1, line))
tag = tag.strip()
if tag == 'source':
source = values.split()
elif tag == 'main':
main = values.strip()
else:
# filename lineno offset text
raise SyntaxError(
f'unexpected tag ({tag}) in coverage test',
(file_pos.filename, file_pos.index+1, 1, line))
# go to next line
line = goto_next_line(file_pos)
if not main:
# filename lineno offset text
raise SyntaxError(
'missing expected main and/or target in coverage test',
(file_pos.filename, file_pos.index+1, 1, line))
return main, source
def read_tests(filename: str) -> List[Attributes]:
'''
read tests from file into a list.
'''
with open(filename) as file:
lines = file.readlines()
# trim empty lines at end of file
while len(lines) > 0 and lines[-1].strip() == '':
del lines[-1]
file_pos = FilePosition(0, lines, filename)
tests = list()
while file_pos.index < len(file_pos.lines):
# expect next lines to be only attributes and values
attributes = read_attributes(file_pos)
if file_pos.index < 0:
# at end of file
break
test_type = attributes['type']
if test_type in ('unit', 'performance'):
attributes['code'] = read_unit_test(file_pos)
tests.append(attributes)
elif test_type == 'i/o':
attributes['expected_input'], attributes['expected_output'] = read_io_test(file_pos)
tests.append(attributes)
elif test_type == 'script':
attributes['script_args'], attributes['script_content'] = read_script_test(file_pos)
tests.append(attributes)
elif test_type in ('approved_includes', 'compile', 'memory_errors', 'style'):
attributes['approved_includes'] = read_approved_includes(file_pos)
tests.append(attributes)
elif test_type == 'coverage':
attributes['include'], attributes['approved_includes'] = read_coverage_test(file_pos)
tests.append(attributes)
else:
print((
f'WARNING: unsupported test type: {test_type}.'
f' this one will be ignored: {attributes["number"]} {attributes["name"]}'
))
_ = eat_block_of_test(file_pos)
file_pos.index += 1
return tests