Mon, 24 Jun 2019 00:51:04 +0300
added a test for mirrored studs
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
1 | from testsuite import problem_type, report_problem |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
2 | import testsuite |
24 | 3 | from geometry import * |
4 | from os.path import dirname | |
5 | from pathlib import Path | |
6 | from configparser import ConfigParser | |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
7 | import math |
24 | 8 | ini_path = Path(dirname(__file__)) / 'library-standards.ini' |
9 | library_standards = ConfigParser() | |
10 | ||
37
e46fa477007b
Fix operation under Python 3.4
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
11 | with ini_path.open() as file: |
24 | 12 | library_standards.read_file(file) |
23 | 13 | |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
14 | @problem_type('zero-determinant', |
64
1c0884f5506e
changed severity names to be better understood
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
15 | severity = 'hold', |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
16 | message = 'matrix row or column all zero' |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
17 | ) |
23 | 18 | def determinant_test(model): |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
19 | ''' |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
20 | Checks all subfile references for matrices with rows or columns all |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
21 | zero. |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
22 | ''' |
24 | 23 | yield from ( |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
24 | report_problem('zero-determinant', bad_object = subfile_reference) |
23 | 25 | for subfile_reference in model.subfile_references |
26 | if abs(subfile_reference.matrix.determinant() - 0) < 1e-15 | |
24 | 27 | ) |
28 | ||
29 | def scaling_description(scaling, axes = 'xyz'): | |
30 | ''' | |
31 | Returns a pretty description of a scaling vector. The axes parameter | |
32 | controls what axes are printed and can be used to filter away | |
33 | uninteresting values. | |
34 | ''' | |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
35 | if isinstance(scaling, str): |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
36 | return scaling |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
37 | else: |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
38 | return ', '.join( |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
39 | str.format('{} = {}', letter, getattr(scaling, letter)) |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
40 | for letter in axes |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
41 | ) |
24 | 42 | |
43 | def check_scaling(scaling, axes): | |
44 | ''' Returns whether all given axes on the given scaling vector are 1. ''' | |
45 | return all( | |
46 | abs(getattr(scaling, axis) - 1) < 1e-5 | |
47 | for axis in axes | |
48 | ) | |
49 | ||
50 | # Restriction to checking function mapping. | |
51 | restriction_tests = { | |
52 | 'no scaling': lambda scaling: check_scaling(scaling, 'xyz'), | |
53 | 'y-scaling only': lambda scaling: check_scaling(scaling, 'xz'), | |
54 | 'stud3-like scaling': lambda scaling: all([ | |
55 | check_scaling(scaling, 'xz'), | |
56 | # check if y-scaling is 1 or -1 | |
57 | abs(abs(scaling.y) - 1) < 1e-5, | |
58 | ]), | |
59 | } | |
60 | ||
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
61 | @problem_type('illegal-scaling', |
64
1c0884f5506e
changed severity names to be better understood
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
62 | severity = 'hold', |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
63 | message = lambda primitive, scaling, axes: |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
64 | str.format('scaling of unscalable primitive {} ({})', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
65 | primitive, |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
66 | scaling_description(scaling, axes), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
67 | ), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
68 | ) |
24 | 69 | def scaling_legality_test(model): |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
70 | ''' |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
71 | Checks the part against primitive references with bad scaling. Some |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
72 | primitives (e.g. pegs) are not allowed to be scaled in the |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
73 | X or Z directions. Some (e.g. most studs) are not allowed to be scaled |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
74 | in the Y direction either. |
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
24
diff
changeset
|
75 | ''' |
24 | 76 | from fnmatch import fnmatch |
77 | scaling_restrictions = library_standards['scaling restrictions'] | |
78 | for subfile_reference in model.subfile_references: | |
79 | primitive = subfile_reference.subfile_path.lower() | |
80 | scaling = subfile_reference.matrix.scaling_vector() | |
81 | # Find all restrictions that apply to this subfile reference. | |
82 | restrictions = { | |
83 | restriction | |
84 | for pattern, restriction in scaling_restrictions.items() | |
85 | if fnmatch(primitive, pattern) | |
86 | } | |
87 | # Check restrictions against the subfile. If any restrictions were | |
88 | # found then the scaling vector must pass at least one of them. | |
89 | if restrictions and not any( | |
90 | restriction_tests[restriction](scaling) | |
91 | for restriction in restrictions | |
92 | ): | |
93 | interesting_axes = ''.join( | |
94 | axis | |
95 | for axis in 'xyz' | |
96 | if abs(getattr(scaling, axis) - 1) > 1e-5 | |
97 | ) | |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
98 | yield report_problem('illegal-scaling', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
99 | bad_object = subfile_reference, |
24 | 100 | primitive = primitive, |
101 | axes = interesting_axes, | |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
102 | scaling = scaling, |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
103 | ) |
23 | 104 | |
80
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
105 | def subfile_references_with_invertnext(model): |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
106 | import linetypes |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
107 | has_invertnext = False |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
108 | for element in model.body: |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
109 | if isinstance(element, linetypes.MetaCommand) \ |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
110 | and element.text == 'BFC INVERTNEXT': |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
111 | has_invertnext = True |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
112 | else: |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
113 | if isinstance(element, linetypes.SubfileReference): |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
114 | yield element, has_invertnext |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
115 | has_invertnext = False |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
116 | |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
117 | @problem_type('cyclical-reference', |
64
1c0884f5506e
changed severity names to be better understood
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
118 | severity = 'hold', |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
119 | message = lambda chain: |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
120 | str.format('cyclical subfile dependency: {chain}', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
121 | **locals(), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
122 | ), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
123 | ) |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
124 | @problem_type('bad-subfile', |
64
1c0884f5506e
changed severity names to be better understood
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
125 | severity = 'hold', |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
126 | message = lambda path, problem_text: |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
127 | str.format('cannot process subfile "{path}": {problem_text}', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
128 | **locals(), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
129 | ), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
130 | ) |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
131 | @problem_type('moved-file-used', |
64
1c0884f5506e
changed severity names to be better understood
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
132 | severity = 'hold', |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
133 | message = lambda moved_file, new_file: |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
134 | str.format('subfile "{moved_file}" has been moved to "{new_file}"', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
135 | **locals(), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
136 | ), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
137 | ) |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
138 | @problem_type('unnecessary-scaling', |
64
1c0884f5506e
changed severity names to be better understood
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
139 | severity = 'warning', |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
140 | message = lambda scaled_flat_dimensions, scaling_vector: |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
141 | str.format( |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
142 | 'subfile unnecessarily scaled in the {dims} ({scaling})', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
143 | dims = dimensions_description(scaled_flat_dimensions), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
144 | scaling = scaling_description(scaling_vector), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
145 | ), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
146 | ) |
80
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
147 | @problem_type('unnecessary-invertnext', |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
148 | severity = 'warning', |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
149 | message = 'flat subfile unnecessarily inverted using BFC INVERTNEXT' |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
150 | ) |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
151 | def dependent_subfile_tests(model): |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
152 | ''' |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
153 | Tests subfile references for such qualities that are dependent on the |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
154 | actual contents of the subfiles. Checks whether moved-to files are used. |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
155 | Checks whether flat subfiles are scaled in the flat direction. |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
156 | ''' |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
157 | import filecache |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
158 | cache = filecache.SubfileCache(model.ldraw_directories) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
159 | failed_subfiles = set() |
80
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
160 | for subfile_reference, has_invertnext in subfile_references_with_invertnext(model): |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
161 | path = subfile_reference.subfile_path.lower() |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
162 | if path in failed_subfiles: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
163 | # Already proven to be a bad apple, don't complain twice |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
164 | pass |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
165 | else: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
166 | try: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
167 | subfile = cache.prepare_file(path) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
168 | except filecache.CyclicalReferenceError as e: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
169 | failed_subfiles.add(path) |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
170 | yield report_problem( |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
171 | 'cyclical-reference', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
172 | bad_object = subfile_reference, |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
173 | chain = str(e), |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
174 | ) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
175 | if not subfile.valid: |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
176 | yield report_problem( |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
177 | 'bad-subfile', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
178 | bad_object = subfile_reference, |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
179 | path = path, |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
180 | problem_text = subfile.problem, |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
181 | ) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
182 | failed_subfiles.add(path) |
55
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
183 | else: |
80
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
184 | # Test for use of moved-to files |
55
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
185 | import re |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
186 | match = re.search(r'^\~Moved(?: to (\w+))?$', subfile.description) |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
187 | if match: |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
188 | yield report_problem( |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
189 | 'moved-file-used', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
190 | bad_object = subfile_reference, |
55
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
191 | moved_file = path, |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
192 | new_file = match.group(1), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
193 | ) |
80
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
194 | # Test for scaling in flat direction |
55
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
195 | scaling_vector = subfile_reference.matrix.scaling_vector() |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
196 | scaled_dimensions = { |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
197 | dimension |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
198 | for dimension in subfile.flatness |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
199 | if not math.isclose( |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
200 | getattr(scaling_vector, dimension), |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
201 | 1, |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
202 | abs_tol = 1.0e-05 |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
203 | ) |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
204 | } |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
205 | scaled_flat_dimensions = subfile.flatness & scaled_dimensions |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
206 | if scaled_flat_dimensions: |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
207 | yield report_problem( |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
208 | 'unnecessary-scaling', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
209 | bad_object = subfile_reference, |
55
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
210 | scaled_flat_dimensions = scaled_flat_dimensions, |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
211 | scaling_vector = scaling_vector, |
388df1fa18a2
fixed handling of invalid subfiles
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
212 | ) |
80
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
213 | # Test whether a flat subfile is inverted using invertnext |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
214 | if has_invertnext and subfile.flatness: |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
215 | yield report_problem( |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
216 | 'unnecessary-invertnext', |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
217 | bad_object = subfile_reference, |
2f81758d1537
added a test for unnecessary invertnext use
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
218 | ) |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
219 | |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
220 | def dimensions_description(dimensions): |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
221 | if isinstance(dimensions, str): |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
222 | return dimensions |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
223 | else: |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
224 | sorted_dims = sorted(dimensions) |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
225 | if len(sorted_dims) == 1: |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
226 | return sorted_dims[0] + ' dimension' |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
227 | else: |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
228 | return str.format('{} and {} dimensions', |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
229 | ', '.join(sorted_dims[:-1]), |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
230 | sorted_dims[-1], |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
231 | ) |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
232 | |
79
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
233 | @problem_type('bad-category', |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
234 | severity = 'hold', |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
235 | message = lambda category: str.format( |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
236 | '"{category}" is not an official category', |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
237 | category = category, |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
238 | ) |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
239 | ) |
82
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
240 | @problem_type('bad-category-in-description', |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
241 | severity = 'hold', |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
242 | message = lambda category: str.format( |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
243 | 'the category "{category}" must be set using !CATEGORY ' |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
244 | 'and not by description', |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
245 | category = category, |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
246 | ) |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
247 | ) |
79
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
248 | def category_test(model): |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
249 | if model.header.valid: |
82
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
250 | categories = library_standards['categories'] |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
251 | illegal_categories_in_description = [ |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
252 | category_name.lower() |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
253 | for category_name in categories.keys() |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
254 | if ' ' in category_name |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
255 | ] |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
256 | has_bad_category = False |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
257 | if model.header.effective_category not in categories.keys(): |
79
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
258 | try: |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
259 | bad_object = model.find_first_header_object('category') |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
260 | except KeyError: |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
261 | # category was not specified using !CATEGORY, blame |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
262 | # the description instead |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
263 | bad_object = model.body[0] |
82
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
264 | has_bad_category = True |
79
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
265 | yield report_problem( |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
266 | 'bad-category', |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
267 | bad_object = bad_object, |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
268 | category = model.header.effective_category, |
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
269 | ) |
82
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
270 | # Check if the description sets a multi-word category |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
271 | if not has_bad_category and model.header.category is None: |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
272 | for category_name in illegal_categories_in_description: |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
273 | if model.header.description.lower().startswith(category_name): |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
274 | yield report_problem( |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
275 | 'bad-category-in-description', |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
276 | bad_object = model.body[0], |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
277 | category = category_name.title(), |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
278 | ) |
75b5241a35ec
added a test for the description setting a multi-word category
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
279 | break |
79
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
280 | |
83
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
281 | @problem_type('mirrored-studs', |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
282 | severity = 'warning', |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
283 | message = lambda primitive: str.format( |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
284 | '"{primitive}" should not be mirrored', |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
285 | primitive = primitive, |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
286 | ) |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
287 | ) |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
288 | def mirrored_studs_test(model): |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
289 | for subfile_reference in model.subfile_references: |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
290 | # Test whether any stud subfile is mirrored. |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
291 | # A subfile is mirrored if its determinant is negative. |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
292 | if subfile_reference.subfile_path.startswith('stu') \ |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
293 | and subfile_reference.subfile_path != 'stud4.dat' \ |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
294 | and subfile_reference.matrix.determinant() < 0: |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
295 | yield report_problem( |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
296 | 'mirrored-studs', |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
297 | bad_object = subfile_reference, |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
298 | primitive = subfile_reference.subfile_path, |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
299 | ) |
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
300 | |
23 | 301 | manifest = { |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
302 | 'tests': [ |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
303 | determinant_test, |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
304 | scaling_legality_test, |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
305 | dependent_subfile_tests, |
79
eb93feb6d3a3
added a test for valid categories
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
306 | category_test, |
83
bd840d5dc8d8
added a test for mirrored studs
Teemu Piippo <teemu@hecknology.net>
parents:
82
diff
changeset
|
307 | mirrored_studs_test, |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
308 | ], |
23 | 309 | } |