|
| 1 | +import pytest |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from grid_strategy.strategies import SquareStrategy |
| 5 | + |
| 6 | + |
| 7 | +class SpecValue: |
| 8 | + def __init__(self, rows, cols, parent=None): |
| 9 | + self.rows = rows |
| 10 | + self.cols = cols |
| 11 | + self.parent = parent |
| 12 | + |
| 13 | + def __repr__(self): # pragma: nocover |
| 14 | + return f"{self.__class__.__name__}({self.rows}, {self.cols})" |
| 15 | + |
| 16 | + def __eq__(self, other): |
| 17 | + return self.rows == other.rows and self.cols == other.cols |
| 18 | + |
| 19 | + |
| 20 | +class GridSpecMock: |
| 21 | + def __init__(self, nrows, ncols, *args, **kwargs): |
| 22 | + self._nrows_ = nrows |
| 23 | + self._ncols_ = ncols |
| 24 | + |
| 25 | + self._args_ = args |
| 26 | + self._kwargs_ = kwargs |
| 27 | + |
| 28 | + def __getitem__(self, key_tup): |
| 29 | + return SpecValue(*key_tup, self) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def gridspec_mock(): |
| 34 | + class Figure: |
| 35 | + pass |
| 36 | + |
| 37 | + def figure(*args, **kwargs): |
| 38 | + return Figure() |
| 39 | + |
| 40 | + with mock.patch(f"grid_strategy._abc.gridspec.GridSpec", new=GridSpecMock) as g: |
| 41 | + with mock.patch(f"grid_strategy._abc.plt.figure", new=figure): |
| 42 | + yield g |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + "align, n, exp_specs", |
| 47 | + [ |
| 48 | + ("center", 1, [(0, slice(0, 1))]), |
| 49 | + ("center", 2, [(0, slice(0, 1)), (0, slice(1, 2))]), |
| 50 | + ("center", 3, [(0, slice(0, 2)), (0, slice(2, 4)), (1, slice(1, 3))]), |
| 51 | + ("left", 3, [(0, slice(0, 2)), (0, slice(2, 4)), (1, slice(0, 2))]), |
| 52 | + ("right", 3, [(0, slice(0, 2)), (0, slice(2, 4)), (1, slice(2, 4))]), |
| 53 | + ("justified", 3, [(0, slice(0, 1)), (0, slice(1, 2)), (1, slice(0, 2))]), |
| 54 | + ( |
| 55 | + "center", |
| 56 | + 8, |
| 57 | + [ |
| 58 | + (0, slice(0, 2)), |
| 59 | + (0, slice(2, 4)), |
| 60 | + (0, slice(4, 6)), |
| 61 | + (1, slice(1, 3)), |
| 62 | + (1, slice(3, 5)), |
| 63 | + (2, slice(0, 2)), |
| 64 | + (2, slice(2, 4)), |
| 65 | + (2, slice(4, 6)), |
| 66 | + ], |
| 67 | + ), |
| 68 | + ("left", 2, [(0, slice(0, 1)), (0, slice(1, 2))]), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_square_spec(gridspec_mock, align, n, exp_specs): |
| 72 | + ss = SquareStrategy(align) |
| 73 | + |
| 74 | + act = ss.get_grid(n) |
| 75 | + exp = [SpecValue(*spec) for spec in exp_specs] |
| 76 | + |
| 77 | + assert act == exp |
0 commit comments