|
| 1 | +"""Base test classes for line shapes.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from numbers import Number |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +class LinesModuleTestBase: |
| 12 | + """Base for testing line-based shapes.""" |
| 13 | + |
| 14 | + shape_name: str |
| 15 | + distance_test_cases: tuple[tuple[tuple[Number], float]] |
| 16 | + expected_line_count: int |
| 17 | + expected_slopes: tuple[Number] | Number |
| 18 | + |
| 19 | + @pytest.fixture(scope='class') |
| 20 | + def shape(self, shape_factory): |
| 21 | + """Fixture to get the shape for testing.""" |
| 22 | + return shape_factory.generate_shape(self.shape_name) |
| 23 | + |
| 24 | + @pytest.fixture(scope='class') |
| 25 | + def slopes(self, shape): |
| 26 | + """Fixture to get the slopes of the lines.""" |
| 27 | + xs, ys = np.array(shape.lines).T |
| 28 | + runs = np.diff(xs, axis=0) |
| 29 | + rises = np.diff(ys, axis=0) |
| 30 | + slopes = rises / np.ma.masked_array(runs, mask=runs == 0) |
| 31 | + return slopes.filled(np.inf) |
| 32 | + |
| 33 | + def test_init(self, shape): |
| 34 | + """Test that the shape consists of the correct number of distinct lines.""" |
| 35 | + num_unique_lines, *_ = np.unique(shape.lines, axis=0).shape |
| 36 | + assert num_unique_lines == self.expected_line_count |
| 37 | + |
| 38 | + def test_distance(self, shape, test_point, expected_distance): |
| 39 | + """ |
| 40 | + Test the distance() method parametrized by distance_test_cases |
| 41 | + (see conftest.py). |
| 42 | + """ |
| 43 | + assert pytest.approx(shape.distance(*test_point)) == expected_distance |
| 44 | + |
| 45 | + def test_slopes(self, slopes): |
| 46 | + """Test that the slopes are as expected.""" |
| 47 | + expected = ( |
| 48 | + [self.expected_slopes] |
| 49 | + if isinstance(self.expected_slopes, Number) |
| 50 | + else self.expected_slopes |
| 51 | + ) |
| 52 | + assert np.array_equal(np.unique(slopes), expected) |
| 53 | + |
| 54 | + |
| 55 | +class ParallelLinesModuleTestBase(LinesModuleTestBase): |
| 56 | + """Base for testing parallel line-based shapes.""" |
| 57 | + |
| 58 | + def test_lines_are_parallel(self, slopes): |
| 59 | + """Test that the lines are parallel (slopes are equal).""" |
| 60 | + assert np.unique(slopes).size == 1 |
| 61 | + |
| 62 | + |
| 63 | +class PolygonsLineModuleTestBase: |
| 64 | + """Base for testing polygon shapes.""" |
| 65 | + |
| 66 | + shape_name: str |
| 67 | + distance_test_cases: tuple[tuple[tuple[Number], float]] |
| 68 | + expected_line_count: int |
| 69 | + |
| 70 | + @pytest.fixture(scope='class') |
| 71 | + def shape(self, shape_factory): |
| 72 | + """Fixture to get the shape for testing.""" |
| 73 | + return shape_factory.generate_shape(self.shape_name) |
| 74 | + |
| 75 | + @pytest.fixture(scope='class') |
| 76 | + def slopes(self, shape): |
| 77 | + """Fixture to get the slopes of the lines.""" |
| 78 | + xs, ys = np.array(shape.lines).T |
| 79 | + runs = np.diff(xs, axis=0) |
| 80 | + rises = np.diff(ys, axis=0) |
| 81 | + slopes = rises / np.ma.masked_array(runs, mask=runs == 0) |
| 82 | + return slopes.filled(np.inf) |
| 83 | + |
| 84 | + def test_init(self, shape): |
| 85 | + """Test that the shape consists of the correct number of distinct lines.""" |
| 86 | + num_unique_lines, *_ = np.unique(shape.lines, axis=0).shape |
| 87 | + assert num_unique_lines == self.expected_line_count |
| 88 | + |
| 89 | + def test_distance(self, shape, test_point, expected_distance): |
| 90 | + """ |
| 91 | + Test the distance() method parametrized by distance_test_cases |
| 92 | + (see conftest.py). |
| 93 | + """ |
| 94 | + assert pytest.approx(shape.distance(*test_point)) == expected_distance |
| 95 | + |
| 96 | + def test_lines_form_polygon(self, shape): |
| 97 | + """Test that the lines form a polygon.""" |
| 98 | + endpoints = np.array(shape.lines).reshape(-1, 2) |
| 99 | + assert np.unique(endpoints, axis=0).shape[0] == self.expected_line_count |
0 commit comments