Browse Source

PolygonalCircuit.__init__(): validate type of vertex indices argument

Fabian Peter Hammerle 5 years ago
parent
commit
6f8cbe09c7
2 changed files with 11 additions and 1 deletions
  1. 2 1
      freesurfer_surface/__init__.py
  2. 9 0
      tests/test_polygonal_circuit.py

+ 2 - 1
freesurfer_surface/__init__.py

@@ -113,6 +113,7 @@ class PolygonalCircuit:
 
     def __init__(self, vertex_indices: _VERTEX_INDICES_TYPE):
         self._vertex_indices = tuple(vertex_indices)
+        assert all(isinstance(idx, int) for idx in self._vertex_indices)
 
     @property
     def vertex_indices(self):
@@ -501,5 +502,5 @@ class Surface:
         vertex_index_conversion = numpy.cumsum(vertex_index_conversion)
         for triangle_index in range(len(self.triangles)):
             self.triangles[triangle_index] \
-                = Triangle(map(lambda i: i + vertex_index_conversion[i],
+                = Triangle(map(lambda i: i + int(vertex_index_conversion[i]),
                                self.triangles[triangle_index].vertex_indices))

+ 9 - 0
tests/test_polygonal_circuit.py

@@ -18,6 +18,15 @@ def test_init_iterator():
     assert circuit.vertex_indices == (1, 2, 3, 5)
 
 
+def test_init_invalid_type():
+    with pytest.raises(Exception):
+        PolygonalCircuit((0, 1, 2.0))
+    with pytest.raises(Exception):
+        PolygonalCircuit((Vertex(2, 3, 4),))
+    with pytest.raises(Exception):
+        PolygonalCircuit((0, 1, Vertex(2, 3, 4)))
+
+
 @pytest.mark.parametrize(('source_vertex_indices', 'expected_vertex_indices'), [
     ((1,), (1,)),
     ((1, 2), (1, 2)),