tests/misc.py

Fri, 18 Sep 2020 21:41:38 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 18 Sep 2020 21:41:38 +0300
changeset 140
46cdbd4bbc32
parent 105
43c367c8895b
child 145
fde18c4d6784
permissions
-rw-r--r--

added moved to with extension unit tests

62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
1 from testsuite import problem_type, report_problem
38
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
2 import linetypes
25
8990ac138cc2 added check for invalid colours
Santeri Piippo
parents:
diff changeset
3
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
4 @problem_type('bad-colour',
64
1c0884f5506e changed severity names to be better understood
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
5 severity = 'hold',
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
6 message = lambda colour_index, count: str.format(
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
7 'invalid colour {} used {} time(s)',
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
8 colour_index,
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
9 count,
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
10 ),
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
11 )
25
8990ac138cc2 added check for invalid colours
Santeri Piippo
parents:
diff changeset
12 def colours_test(model):
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 25
diff changeset
13 ''' Checks that all colours used in the part model are valid. '''
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
14 from collections import defaultdict
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
15 bad_colours = defaultdict(lambda: {'count': 0, 'first-occurrence': None})
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
16 for element in model.body:
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
17 if hasattr(element, 'colour') and not element.colour.is_valid:
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
18 bad_colours[element.colour.index]['count'] += 1
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
19 if not bad_colours[element.colour.index]['first-occurrence']:
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
20 bad_colours[element.colour.index]['first-occurrence'] = element
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
21 yield from [
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
22 report_problem(
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
23 'bad-colour',
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
24 bad_object = bad_colour['first-occurrence'],
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
25 colour_index = colour_index,
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
26 count = bad_colour['count'],
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
27 )
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
28 for colour_index, bad_colour in bad_colours.items()
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
29 ]
25
8990ac138cc2 added check for invalid colours
Santeri Piippo
parents:
diff changeset
30
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
31 @problem_type('syntax-error',
64
1c0884f5506e changed severity names to be better understood
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
32 severity = 'hold',
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
33 message = lambda reason: str.format('syntax error: {}', reason),
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
34 )
38
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
35 def syntax_errors(model):
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
36 yield from (
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
37 report_problem('syntax-error',
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
38 bad_object = element,
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
39 reason = element.reason
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
40 )
38
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
41 for element in model.body
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
42 if isinstance(element, linetypes.Error)
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
43 )
66c9591b733d added proper handling of syntax errors
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
44
78
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
45 @problem_type('unknown-metacommand',
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
46 severity = 'hold',
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
47 message = lambda command_text: str.format(
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
48 'unknown or deprecated metacommand: {command_text}',
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
49 command_text = command_text,
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
50 )
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
51 )
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
52 def metacommands_test(model):
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
53 allowed_metacommand_patterns = [
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
54 r'^BFC (CLIP|NOCLIP|INVERTNEXT)$',
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
55 r'^\!TEXMAP (START|NEXT) .+',
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
56 r'^\!: .+',
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
57 r'^\!TEXMAP (FALLBACK|END)$',
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
58 ]
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
59 import re
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
60 for element in model.body[model.header_size:]:
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
61 if isinstance(element, linetypes.MetaCommand):
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
62 if element.text and not any(
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
63 re.match(pattern, element.text)
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
64 for pattern in allowed_metacommand_patterns
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
65 ):
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
66 yield report_problem('unknown-metacommand',
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
67 bad_object = element,
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
68 command_text = element.text,
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
69 )
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
70
94
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
71 @problem_type('bad-line-endings',
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
72 severity = 'hold',
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
73 message = lambda count: str.format(
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
74 'file contains non-DOS line endings ({count} total)',
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
75 count = count,
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
76 ),
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
77 )
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
78 def line_endings_test(model):
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
79 # Line endings are already checked during parse. This function
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
80 # only serves to report them.
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
81 if model.line_ending_errors != None:
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
82 yield report_problem(
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
83 'bad-line-endings',
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
84 bad_object = model.body[model.line_ending_errors['first-at']],
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
85 count = model.line_ending_errors['count'],
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
86 )
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
87
105
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
88 @problem_type('bfc-invertnext-not-on-subfile',
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
89 severity = 'hold',
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
90 message = '"BFC INVERTNEXT" not followed by a type-1 line',
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
91 )
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
92 def bfc_invertnext_not_on_subfile_test(model):
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
93 def get_invertnexts(model):
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
94 yield from [
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
95 (index, element)
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
96 for index, element in enumerate(model.body)
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
97 if isinstance(element, linetypes.MetaCommand) \
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
98 and element.text == 'BFC INVERTNEXT'
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
99 ]
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
100 def has_subfile_after_invertnext(index):
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
101 index_subfile = index + 1 # subfile reference should be on the next line
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
102 if index_subfile >= len(model.body):
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
103 return False # past the end...
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
104 else:
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
105 element = model.body[index_subfile]
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
106 return isinstance(element, linetypes.SubfileReference)
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
107 for index, invertnext in get_invertnexts(model):
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
108 if not has_subfile_after_invertnext(index):
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
109 yield report_problem('bfc-invertnext-not-on-subfile',
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
110 bad_object = model.body[index],
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
111 )
43c367c8895b split header tests into their own file
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
112
25
8990ac138cc2 added check for invalid colours
Santeri Piippo
parents:
diff changeset
113 manifest = {
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
114 'tests': [
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
115 colours_test,
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
116 syntax_errors,
75
3ec747dfbfb9 added a test for BFC INVERTNEXT not followed by a type-1 line
Teemu Piippo <teemu@hecknology.net>
parents: 74
diff changeset
117 bfc_invertnext_not_on_subfile_test,
78
1970afe52da4 added a test for unknown metacommands
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
118 metacommands_test,
94
109fb7cf658f added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
119 line_endings_test,
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
120 ],
25
8990ac138cc2 added check for invalid colours
Santeri Piippo
parents:
diff changeset
121 }

mercurial