-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_parser_errors.py
631 lines (509 loc) · 25.2 KB
/
test_parser_errors.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import pytest
from names import Names
from network import Network
from devices import Devices
from monitors import Monitors
from parse import Parser
from scanner import Scanner, Symbol
# Workaround to stop Python stealing _ for translations
# Necessary for tests to work
import sys
import wx
import builtins
def _hook(obj):
if obj is not None:
print(repr(obj))
builtins.__dict__['_'] = wx.GetTranslation
sys.displayhook = _hook
def new_parser(path):
"""Return a Parser class instance for given path."""
new_names = Names()
new_devices = Devices(new_names)
new_network = Network(new_names, new_devices)
new_monitors = Monitors(new_names, new_devices, new_network)
new_scanner = Scanner(path, new_names)
new_parser = Parser(
new_names,
new_devices,
new_network,
new_monitors,
new_scanner
)
return new_parser
def get_symbol_generator():
dummy_parser = new_parser("test_files/blank.txt")
return dummy_parser
dummy_parser = get_symbol_generator()
class TestParserDevices:
@pytest.mark.parametrize("symbol_list, success, expected_error_count, "
"parse_device_call_number",
[
([dummy_parser.scanner.OPEN_SQUARE,
dummy_parser.scanner.OPEN_CURLY,
dummy_parser.scanner.SEMICOLON,
dummy_parser.scanner.CONNECTIONS_ID],
True,
0, 1), # device_list_is_perfect
([dummy_parser.scanner.OPEN_SQUARE,
dummy_parser.scanner.OPEN_CURLY,
dummy_parser.scanner.DEVICES_ID,
dummy_parser.scanner.CONNECTIONS_ID],
False,
1, 1), # one error will be reported (no semi
# colon at end of device list)
([dummy_parser.scanner.CLOSE_SQUARE,
dummy_parser.scanner.OPEN_CURLY,
dummy_parser.scanner.DEVICES_ID,
dummy_parser.scanner.CONNECTIONS_ID],
False,
1, 0), # as soon as one error is reported we
# break and skip to connections (no calls
# to parse devices are made)
])
def test_parse_devices_list(
self,
mocker,
symbol_list,
success,
expected_error_count,
parse_device_call_number):
"""Test Parser._parse_devices_list error handling in outer
'wrapping' for a DEVICES list.
The use of patching lets one assume that Parser._parse_device
reaches the end of all the devices and that Parser._set_next
always returns the correct symbol.
"""
parser_obj = new_parser("test_files/blank.txt")
parser_obj.symbol = Symbol()
def symbol_generator(sym_list):
return (x for x in sym_list)
gen = symbol_generator(symbol_list)
def mock_error(self, msg, expt_list):
parser_obj.error_count += 1
print(f"SYNTAX ERROR FOUND: {msg}, recevied"
f" {parser_obj._get_symbol_string()}")
# Parser._error is patched so that we do not test scanner.show_error
mocker.patch('parse.Parser._error', mock_error)
def mock_set_next(self):
parser_obj.symbol.id = next(gen)
return
# Parser._set_next is patched so that we do not test scanner
# functionality
mocker.patch('parse.Parser._set_next', mock_set_next)
def mock_parse_device(self, err):
parser_obj.symbol.id = parser_obj.scanner.CLOSE_SQUARE
return False
# Parser._parse_device is patched so that we are only testing the
# actual _parse_device_list function
mocker.patch('parse.Parser._parse_device', mock_parse_device)
spy = mocker.spy(parser_obj, "_parse_device")
assert parser_obj._parse_devices_list() == success
assert parser_obj.error_count == expected_error_count
assert spy.call_count == parse_device_call_number
def test_parse_device_semantic(self, mocker):
"""Test if a semantic error will be detected and handled
correctly in Parser._parse_device
"""
parser_obj = new_parser("test_files/parse_device_semantic_error.txt")
parser_obj._set_next()
def mock_error(self, msg, expt_list):
parser_obj.error_count += 1
print(f"SYNTAX ERROR FOUND: {msg}, recevied"
f" {parser_obj._get_symbol_string()}")
mocker.patch('parse.Parser._error', mock_error)
spy_semantic = mocker.spy(parser_obj, "_semantic_error")
spy_syntactic = mocker.spy(parser_obj, "_error")
assert not parser_obj._parse_device(0) # no missing semicolon
assert spy_semantic.call_count == 1 # semantic error is detected
assert spy_syntactic.call_count == 0 # no syntax error detected
def test_device_already_present(self, mocker):
"""Test if specific device semantic errors will be handled
appropriately"""
# if time, add more tests to parametise and figure out how to access
# error_type / use stdout capture to assert?
parser_obj = \
new_parser("test_files/device_semantic_testing/device_present.txt")
spy_semantic = mocker.spy(parser_obj, "_semantic_error")
parser_obj.parse_network()
spy_semantic.assert_called_once()
def test_parse_device_missing_semicolon_handling(self, mocker):
"""Test if one of
_parse_device_id/_parse_device_kind/_parse_device_qual
return a missing semicolon, the next device is skipped
"""
# e.g. if _parse_device_id encounters a misisng semicolon, no calls to
# device kind or device qual are made
parser_obj = new_parser(
"test_files/parse_device_missing_semicolon_handling.txt")
parser_obj._set_next()
mocker.patch('parse.Parser._parse_device_id', return_value=(True,
None,
None))
spy_id = mocker.spy(parser_obj, "_parse_device_id")
spy_kind = mocker.spy(parser_obj, "_parse_device_kind")
spy_qual = mocker.spy(parser_obj, "_parse_device_qual")
parser_obj._parse_device(0)
assert spy_id.call_count == 1 # called but missing semicolon
assert spy_kind.call_count == 0 # not called
assert spy_qual.call_count == 0 # not called
@pytest.mark.parametrize("text_file, syntax_errors, semantic_errors",
[
("parse_device_optional_qual.txt", 0, 0),
("parse_device_should_have_qual.txt", 0, 1)
])
def test_parse_device_optional_qual(self, mocker, text_file,
semantic_errors, syntax_errors):
"""Test that if qualifier is not given, the parsing of the device can
continue regardless if qualifier is semantically necessary
"""
parser_obj = new_parser(f"test_files/{text_file}")
parser_obj._set_next()
spy_qual = mocker.spy(parser_obj, "_parse_device_qual")
spy_syntactic = mocker.spy(parser_obj, "_error")
spy_semantic = mocker.spy(parser_obj, "_semantic_error")
parser_obj._parse_device(0)
assert spy_qual.call_count == 0 # correctly skipped
assert spy_semantic.call_count == semantic_errors
assert spy_syntactic.call_count == syntax_errors
@pytest.mark.parametrize("text_file, missing_semicolon, device_name, "
"error_calls",
[
("device_id_correct.txt", False, "A", 0),
("device_id_name_syntax.txt", False, None, 1),
("device_id_missing.txt", False, None, 1),
("device_id_missing_semicolon.txt", True,
"A", 1),
])
def test_parse_device_id(self, mocker, text_file, missing_semicolon,
device_name, error_calls):
"""Test that if something is wrong with a 'id:name;' block in
definition file, the appropriate error will be thrown
"""
parser_obj = new_parser(f"test_files/{text_file}")
parser_obj._set_next()
def mock_error(self, msg, expt_list):
parser_obj.error_count += 1
print(f"SYNTAX ERROR FOUND: {msg}, recevied"
f" {parser_obj._get_symbol_string()}")
mocker.patch('parse.Parser._error', mock_error)
spy_parse_device_id = mocker.spy(parser_obj, "_parse_device_id")
spy_error = mocker.spy(parser_obj, "_error")
parser_obj._parse_device_id()
assert spy_parse_device_id.spy_return[0] == missing_semicolon
assert spy_parse_device_id.spy_return[1] == device_name
assert spy_error.call_count == error_calls
@pytest.mark.parametrize("text_file, missing_semicolon, "
"device_kind_string, "
"error_calls",
[
("device_kind_correct.txt", False, "NOR", 0),
("device_kind_simple_syntax.txt", False, None,
1),
("device_kind_missing.txt", False, None, 1),
("device_kind_missing_semicolon.txt", True,
"NOR", 1),
])
def test_parse_device_kind(self, mocker, text_file, missing_semicolon,
device_kind_string, error_calls):
"""Test that if something is wrong with a 'kind:kind;' block in
definition file, the appropriate error will be thrown
"""
parser_obj = new_parser(f"test_files/{text_file}")
parser_obj._set_next()
def mock_error(self, msg, expt_list):
parser_obj.error_count += 1
print(f"SYNTAX ERROR FOUND: {msg}, recevied"
f" {parser_obj._get_symbol_string()}")
mocker.patch('parse.Parser._error', mock_error)
spy_parse_device_kind = mocker.spy(parser_obj, "_parse_device_kind")
spy_error = mocker.spy(parser_obj, "_error")
parser_obj._parse_device_kind()
assert spy_parse_device_kind.spy_return[0] == missing_semicolon
assert spy_parse_device_kind.spy_return[1] == device_kind_string
assert spy_error.call_count == error_calls
@pytest.mark.parametrize("text_file, missing_semicolon, "
"syntax_errors",
[("device_qual_correct.txt",
False,
0),
("device_qual_not_number.txt",
False,
1),
("device_qual_missing.txt",
False,
1),
("device_qual_missing_semicolon.txt",
True,
1),
])
def test_parse_device_qual(self, mocker, text_file, missing_semicolon,
syntax_errors):
"""Test that if something is wrong with a 'qual:num;' block in
definition file, the appropriate error will be thrown
"""
parser_obj = new_parser(f"test_files/{text_file}")
parser_obj._set_next()
def mock_error(self, msg, expt_list):
parser_obj.error_count += 1
print(f"SYNTAX ERROR FOUND: {msg}, recevied"
f" {parser_obj._get_symbol_string()}")
mocker.patch('parse.Parser._error', mock_error)
spy_parse_device_qual = mocker.spy(parser_obj, "_parse_device_qual")
spy_error = mocker.spy(parser_obj, "_error")
parser_obj._parse_device_qual()
assert spy_parse_device_qual.spy_return[0] == missing_semicolon
assert spy_error.call_count == syntax_errors
class TestParserConnections:
@pytest.mark.parametrize("symbol_list, success, set_next_count, "
"error_count",
[
([dummy_parser.scanner.OPEN_SQUARE,
None, # could be anything
dummy_parser.scanner.SEMICOLON,
None], # could be anything
True,
4, 0),
([dummy_parser.scanner.OPEN_SQUARE,
None, # could be anything
dummy_parser.scanner.INVALID_CHAR,
None], # could be anything
False,
4, 1),
([dummy_parser.scanner.CLOSE_CURLY,
None, # could be anything
dummy_parser.scanner.INVALID_CHAR,
None], # could be anything
False,
2, 1),
])
def test_parse_connections_list_wrapper(self, mocker, symbol_list, success,
set_next_count, error_count):
"""Test Parser._parse_connections_list error handling in outer
'wrapping' for a CONNECTIONS list.
The use of patching lets one assume that Parser._parse_connection
reaches the end of all the connections and that Parser.set_next
always returns the correct symbol.
"""
parser_obj = new_parser(f"test_files/blank.txt")
parser_obj.symbol = Symbol()
def symbol_generator(sym_list):
return (x for x in sym_list)
gen = symbol_generator(symbol_list)
def mock_set_next(self):
parser_obj.symbol.id = next(gen)
return
# Parser._set_next is patched so that we do not test scanner
# functionality
mocker.patch('parse.Parser._set_next', mock_set_next)
def mock_error(self, msg, expt_list):
parser_obj.error_count += 1
print(f"SYNTAX ERROR FOUND: {msg}, recevied"
f" {parser_obj._get_symbol_string()}")
mocker.patch('parse.Parser._error', mock_error)
def mock_parse_connection(self, err):
parser_obj.symbol.id = parser_obj.scanner.CLOSE_SQUARE
return False
mocker.patch('parse.Parser._parse_connection', mock_parse_connection)
spy_set_next = mocker.spy(parser_obj, "_set_next")
spy_error = mocker.spy(parser_obj, "_error")
assert parser_obj._parse_connections_list(0) == success
assert spy_set_next.call_count == set_next_count
assert spy_error.call_count == error_count
def test_parse_connection_semantic(self, mocker):
"""Test if a semantic error will be detected and handled
correctly in Parser._parse_connection.
"""
parser_obj = new_parser(
"test_files/parse_connection_semantic_error.txt")
parser_obj._set_next()
def mock_error(self, msg, expt_list):
parser_obj.error_count += 1
print(f"SYNTAX ERROR FOUND: {msg}, recevied"
f" {parser_obj._get_symbol_string()}")
mocker.patch('parse.Parser._error', mock_error)
spy_semantic = mocker.spy(parser_obj, "_semantic_error")
spy_syntactic = mocker.spy(parser_obj, "_error")
assert not parser_obj._parse_connection(0) # no missing semicolon
assert spy_semantic.call_count == 1 # absent device syntax errors
assert spy_syntactic.call_count == 0 # no syntax error detected
@pytest.mark.parametrize("text_file, end_marker_missing, signal_name, "
"port_name, errors",
[("A_signal.txt",
False,
"A",
None,
0),
("G1I1_signal.txt",
False,
"G1",
"I1",
0),
("missing_end_marker_signal.txt",
True,
"G1",
"I1",
1),
("missing_port_name_signal.txt",
False,
"G1",
"I1",
1),
])
def test_parse_signal(self, mocker, text_file, end_marker_missing,
signal_name, port_name, errors):
"""Test that if something is wrong in Parser._parse_signal
then the correct error handling will occur by checking the calls
Parser._error().
"""
parser_obj = new_parser(f"test_files/{text_file}")
parser_obj._set_next()
def mock_error(self, msg, expt_list):
parser_obj.error_count += 1
print(f"SYNTAX ERROR FOUND: {msg}, recevied"
f" {parser_obj._get_symbol_string()}")
mocker.patch('parse.Parser._error', mock_error)
spy_syntactic = mocker.spy(parser_obj, "_error")
result = parser_obj._parse_signal()
assert result[0] == end_marker_missing
assert result[1] == parser_obj.names.query(signal_name)
assert result[2] == parser_obj.names.query(port_name)
assert spy_syntactic.call_count == errors
class TestParserMonitors:
@pytest.mark.parametrize("symbol_list, success, set_next_count, "
"error_count",
[
([dummy_parser.scanner.OPEN_SQUARE,
None, # could be anything
dummy_parser.scanner.SEMICOLON,
None], # could be anything
True,
4, 0),
([dummy_parser.scanner.OPEN_SQUARE,
None, # could be anything
dummy_parser.scanner.INVALID_CHAR,
None], # could be anything
False,
4, 1),
([dummy_parser.scanner.CLOSE_CURLY,
None, # could be anything
dummy_parser.scanner.INVALID_CHAR,
None], # could be anything
False,
2, 1),
])
def test_parse_monitors_list(self, mocker, symbol_list, success,
set_next_count, error_count):
"""
Test Parser._parse_monitors_list error handling in outer
'wrapping' for a MONITORS list.
The use of patching lets one assume that Parser._parse_monitor
reaches the end of all the monitors and that Parser._set_next
always returns the correct symbol.
"""
parser_obj = new_parser(f"test_files/blank.txt")
parser_obj.symbol = Symbol()
def symbol_generator(sym_list):
return (x for x in sym_list)
gen = symbol_generator(symbol_list)
def mock_set_next(self):
parser_obj.symbol.id = next(gen)
return
# Parser.set_next is patched so that we do not test scanner
# functionality
mocker.patch('parse.Parser._set_next', mock_set_next)
def mock_error(self, msg, expt_list):
parser_obj.error_count += 1
print(f"SYNTAX ERROR FOUND: {msg}, recevied"
f" {parser_obj._get_symbol_string()}")
mocker.patch('parse.Parser._error', mock_error)
def mock_parse_monitor(self, err):
parser_obj.symbol.id = parser_obj.scanner.CLOSE_SQUARE
return False
mocker.patch('parse.Parser._parse_monitor', mock_parse_monitor)
spy_set_next = mocker.spy(parser_obj, "_set_next")
spy_error = mocker.spy(parser_obj, "_error")
assert parser_obj._parse_monitors_list(0) == success
assert spy_set_next.call_count == set_next_count
assert spy_error.call_count == error_count
@pytest.mark.parametrize("text_file, syntax_errors, semantic_errors",
[
("parse_monitor_semantic.txt", 0, 1),
("parse_monitor_syntax_semantic.txt", 1, 1),
])
def test_parse_monitor_semantic(self, mocker, text_file, semantic_errors,
syntax_errors):
"""Test if a semantic error will be detected and handled
correctly in Parser._parse_monitor.
"""
parser_obj = new_parser(f"test_files/{text_file}")
parser_obj.symbol = Symbol()
spy_syntactic = mocker.spy(parser_obj, "_error")
spy_semantic = mocker.spy(parser_obj, "_semantic_error")
parser_obj.parse_network()
assert spy_semantic.call_count == semantic_errors
assert spy_syntactic.call_count == syntax_errors
class TestParserErrorRecovery:
@pytest.mark.parametrize("text_file, expected_symbol_string",
[("er_device_list_missing_end_semicolon.txt",
"MONITORS"),
("er_device_list_missing_open_square.txt",
"CONNECTIONS"),
])
def test_error_recovery_parse_devices_list(
self, mocker, text_file, expected_symbol_string):
"""Test Error Recovery in Parser._parse_devices_list
is correct by checking the expected symbol is
skipped to after an error is reported.
"""
parser_obj = new_parser(f"test_files/{text_file}")
parser_obj.symbol = Symbol()
def mock_scanner_error(self, symbol):
return "caret message", "ln", "cn"
mocker.patch('scanner.Scanner.show_error', mock_scanner_error)
spy_symbol_id = mocker.spy(parser_obj, "_get_symbol_string")
parser_obj._parse_devices_list()
assert spy_symbol_id.spy_return == expected_symbol_string
@pytest.mark.parametrize("text_file, expected_symbol_string",
[("er_parse_device_bad_id.txt",
"kind"),
("er_parse_device_id_missing_semicolon.txt",
"{"),
])
def test_error_recovery_parse_device_id(
self, mocker, text_file, expected_symbol_string):
"""Test Error Recovery in Parser.parse_devices_id
is correct by checking the expected symbol is
skipped to after an error is reported.
"""
parser_obj = new_parser(f"test_files/{text_file}")
parser_obj.symbol = Symbol()
parser_obj._set_next()
def mock_scanner_error(self, symbol):
return "caret message", "ln", "cn"
mocker.patch('scanner.Scanner.show_error', mock_scanner_error)
spy_symbol_id = mocker.spy(parser_obj, "_get_symbol_string")
parser_obj._parse_device_id()
assert spy_symbol_id.spy_return == expected_symbol_string
def test_empty_file():
"""Test an empty file will throw an error"""
parser_obj = new_parser(f"test_files/empty_file_error_test.txt")
result = parser_obj.parse_network()
# test it is not just a file with an unclosed comment in it
unclosed_comment = parser_obj.unclosed_comment
assert not unclosed_comment
# test an error is thrown
assert not result
@pytest.mark.parametrize("text_file, expected_number_errors",
[
("within_devices.txt", 2),
("within_connections.txt", 3),
("within_monitors.txt", 3),
])
def test_unclosed_comment_handling(text_file, expected_number_errors):
"""Test unclosed comment handling gives correct error count"""
# want to test that the total error count will stay constant after an
# unclosed comment is found
parser_obj = new_parser(f"test_files/unclosed_comment_testing/{text_file}")
parser_obj.parse_network()
final_error_count = parser_obj.error_count
assert final_error_count == expected_number_errors