tests/subfiles.py

changeset 62
f0a6bf48b05e
parent 55
388df1fa18a2
child 64
1c0884f5506e
--- a/tests/subfiles.py	Wed Jun 05 00:33:50 2019 +0300
+++ b/tests/subfiles.py	Sat Jun 08 01:32:25 2019 +0300
@@ -1,4 +1,4 @@
-from testsuite import warning, error
+from testsuite import problem_type, report_problem
 import testsuite
 from geometry import *
 from os.path import dirname
@@ -11,13 +11,17 @@
 with ini_path.open() as file:
     library_standards.read_file(file)
 
+@problem_type('zero-determinant',
+    severity = 'error',
+    message = 'matrix row or column all zero'
+)
 def determinant_test(model):
     '''
         Checks all subfile references for matrices with rows or columns all
         zero.
     '''
     yield from (
-        error(subfile_reference, 'zero-determinant')
+        report_problem('zero-determinant', bad_object = subfile_reference)
         for subfile_reference in model.subfile_references
         if abs(subfile_reference.matrix.determinant() - 0) < 1e-15
     )
@@ -28,10 +32,13 @@
         controls what axes are printed and can be used to filter away
         uninteresting values.
     '''
-    return ', '.join(
-        str.format('{} = {}', letter, getattr(scaling, letter))
-        for letter in axes
-    )
+    if isinstance(scaling, str):
+        return scaling
+    else:
+        return ', '.join(
+            str.format('{} = {}', letter, getattr(scaling, letter))
+            for letter in axes
+        )
 
 def check_scaling(scaling, axes):
     ''' Returns whether all given axes on the given scaling vector are 1. '''
@@ -51,6 +58,14 @@
     ]),
 }
 
+@problem_type('illegal-scaling',
+    severity = 'error',
+    message = lambda primitive, scaling, axes:
+        str.format('scaling of unscalable primitive {} ({})',
+            primitive,
+            scaling_description(scaling, axes),
+        ),
+)
 def scaling_legality_test(model):
     '''
         Checks the part against primitive references with bad scaling. Some
@@ -80,11 +95,43 @@
                 for axis in 'xyz'
                 if abs(getattr(scaling, axis) - 1) > 1e-5
             )
-            yield warning(subfile_reference, 'illegal-scaling',
+            yield report_problem('illegal-scaling',
+                bad_object = subfile_reference,
                 primitive = primitive,
                 axes = interesting_axes,
-                scaling = scaling)
+                scaling = scaling,
+            )
 
+@problem_type('cyclical-reference',
+    severity = 'error',
+    message = lambda chain:
+        str.format('cyclical subfile dependency: {chain}',
+            **locals(),
+        ),
+)
+@problem_type('bad-subfile',
+    severity = 'error',
+    message = lambda path, problem_text:
+        str.format('cannot process subfile "{path}": {problem_text}',
+            **locals(),
+        ),
+)
+@problem_type('moved-file-used',
+    severity = 'error',
+    message = lambda moved_file, new_file:
+        str.format('subfile "{moved_file}" has been moved to "{new_file}"',
+            **locals(),
+        ),
+)
+@problem_type('unnecessary-scaling',
+    severity = 'notice',
+    message = lambda scaled_flat_dimensions, scaling_vector:
+        str.format(
+            'subfile unnecessarily scaled in the {dims} ({scaling})',
+            dims = dimensions_description(scaled_flat_dimensions),
+            scaling = scaling_description(scaling_vector),
+        ),
+)
 def dependent_subfile_tests(model):
     '''
         Tests subfile references for such qualities that are dependent on the
@@ -104,11 +151,15 @@
                 subfile = cache.prepare_file(path)
             except filecache.CyclicalReferenceError as e:
                 failed_subfiles.add(path)
-                yield error(subfile_reference, 'cyclical-reference',
+                yield report_problem(
+                    'cyclical-reference',
+                    bad_object = subfile_reference,
                     chain = str(e),
                 )
             if not subfile.valid:
-                yield error(subfile_reference, 'bad-subfile',
+                yield report_problem(
+                    'bad-subfile',
+                    bad_object = subfile_reference,
                     path = path,
                     problem_text = subfile.problem,
                 )
@@ -117,9 +168,12 @@
                 import re
                 match = re.search(r'^\~Moved(?: to (\w+))?$', subfile.description)
                 if match:
-                    yield error(subfile_reference, 'moved-file-used',
+                    yield report_problem(
+                        'moved-file-used',
+                        bad_object = subfile_reference,
                         moved_file = path,
-                        new_file = match.group(1))
+                        new_file = match.group(1),
+                    )
                 scaling_vector = subfile_reference.matrix.scaling_vector()
                 scaled_dimensions = {
                     dimension
@@ -132,52 +186,30 @@
                 }
                 scaled_flat_dimensions = subfile.flatness & scaled_dimensions
                 if scaled_flat_dimensions:
-                    yield testsuite.notice(subfile_reference, 'unnecessary-scaling',
+                    yield report_problem(
+                        'unnecessary-scaling',
+                        bad_object = subfile_reference,
                         scaled_flat_dimensions = scaled_flat_dimensions,
                         scaling_vector = scaling_vector,
                     )
 
 def dimensions_description(dimensions):
-    sorted_dims = sorted(dimensions)
-    if len(sorted_dims) == 1:
-        return sorted_dims[0] + ' dimension'
+    if isinstance(dimensions, str):
+        return dimensions
     else:
-        return str.format('{} and {} dimensions',
-            ', '.join(sorted_dims[:-1]),
-            sorted_dims[-1],
-        )
+        sorted_dims = sorted(dimensions)
+        if len(sorted_dims) == 1:
+            return sorted_dims[0] + ' dimension'
+        else:
+            return str.format('{} and {} dimensions',
+                ', '.join(sorted_dims[:-1]),
+                sorted_dims[-1],
+            )
 
 manifest = {
-    'tests': {
-        'determinant': determinant_test,
-        'scaling-legality': scaling_legality_test,
-        'dependent-subfiles': dependent_subfile_tests,
-    },
-    'messages': {
-        'zero-determinant': 'matrix determinant is zero '
-            '(row or column all zero)',
-        'illegal-scaling': lambda primitive, scaling, axes:
-            str.format('scaling of unscalable primitive {} ({})',
-                primitive,
-                scaling_description(scaling, axes),
-            ),
-        'cyclical-reference': lambda chain:
-            str.format('cyclical subfile dependency: {chain}',
-                **locals(),
-            ),
-        'bad-subfile': lambda path, problem_text:
-            str.format('cannot process subfile "{path}": {problem_text}',
-                **locals(),
-            ),
-        'moved-file-used': lambda moved_file, new_file:
-            str.format('subfile "{moved_file}" has been moved to "{new_file}"',
-                **locals(),
-            ),
-        'unnecessary-scaling': lambda scaled_flat_dimensions, scaling_vector:
-            str.format(
-                'subfile unnecessarily scaled in the {dims} ({scaling})',
-                dims = dimensions_description(scaled_flat_dimensions),
-                scaling = scaling_description(scaling_vector),
-            )
-    },
+    'tests': [
+        determinant_test,
+        scaling_legality_test,
+        dependent_subfile_tests,
+    ],
 }

mercurial