|
| 1 | +from collections import namedtuple |
| 2 | +import numpy.testing as nptest |
1 | 3 | import pytest
|
2 | 4 | from matplotlib.testing.decorators import image_comparison
|
3 | 5 | import matplotlib.pyplot as plt
|
@@ -123,4 +125,58 @@ def test_get_packed_offsets(wd_list, total, sep, mode):
|
123 | 125 | # issue tickets (at least #10476 and #10784) related to corner cases
|
124 | 126 | # triggered inside this function when calling higher-level functions
|
125 | 127 | # (e.g. `Axes.legend`).
|
| 128 | + # These are just some additional smoke tests. The output is untested. |
126 | 129 | _get_packed_offsets(wd_list, total, sep, mode=mode)
|
| 130 | + |
| 131 | + |
| 132 | +_Params = namedtuple('_params', 'wd_list, total, sep, expected') |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.parametrize('wd_list, total, sep, expected', [ |
| 136 | + _Params( # total=None |
| 137 | + [(3, 0), (1, 0), (2, 0)], total=None, sep=1, expected=(8, [0, 4, 6])), |
| 138 | + _Params( # total larger than required |
| 139 | + [(3, 0), (1, 0), (2, 0)], total=10, sep=1, expected=(10, [0, 4, 6])), |
| 140 | + _Params( # total smaller than required |
| 141 | + [(3, 0), (1, 0), (2, 0)], total=5, sep=1, expected=(5, [0, 4, 6])), |
| 142 | +]) |
| 143 | +def test_get_packed_offsets_fixed(wd_list, total, sep, expected): |
| 144 | + result = _get_packed_offsets(wd_list, total, sep, mode='fixed') |
| 145 | + assert result[0] == expected[0] |
| 146 | + nptest.assert_allclose(result[1], expected[1]) |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.parametrize('wd_list, total, sep, expected', [ |
| 150 | + _Params( # total=None (implicit 1) |
| 151 | + [(.1, 0)] * 3, total=None, sep=None, expected=(1, [0, .45, .9])), |
| 152 | + _Params( # total larger than sum of widths |
| 153 | + [(3, 0), (1, 0), (2, 0)], total=10, sep=1, expected=(10, [0, 5, 8])), |
| 154 | + _Params( # total smaller sum of widths: overlapping boxes |
| 155 | + [(3, 0), (1, 0), (2, 0)], total=5, sep=1, expected=(5, [0, 2.5, 3])), |
| 156 | +]) |
| 157 | +def test_get_packed_offsets_expand(wd_list, total, sep, expected): |
| 158 | + result = _get_packed_offsets(wd_list, total, sep, mode='expand') |
| 159 | + assert result[0] == expected[0] |
| 160 | + nptest.assert_allclose(result[1], expected[1]) |
| 161 | + |
| 162 | + |
| 163 | +@pytest.mark.parametrize('wd_list, total, sep, expected', [ |
| 164 | + _Params( # total larger than required |
| 165 | + [(3, 0), (2, 0), (1, 0)], total=6, sep=None, expected=(6, [0, 2, 4])), |
| 166 | + _Params( # total smaller sum of widths: overlapping boxes |
| 167 | + [(3, 0), (2, 0), (1, 0), (.5, 0)], total=2, sep=None, |
| 168 | + expected=(2, [0, 0.5, 1, 1.5])), |
| 169 | + _Params( # total larger than required |
| 170 | + [(.5, 0), (1, 0), (.2, 0)], total=None, sep=1, |
| 171 | + expected=(6, [0, 2, 4])), |
| 172 | + # the case total=None, sep=None is tested separately below |
| 173 | +]) |
| 174 | +def test_get_packed_offsets_equal(wd_list, total, sep, expected): |
| 175 | + result = _get_packed_offsets(wd_list, total, sep, mode='equal') |
| 176 | + assert result[0] == expected[0] |
| 177 | + nptest.assert_allclose(result[1], expected[1]) |
| 178 | + |
| 179 | + |
| 180 | +def test_get_packed_offsets_equal_total_none_sep_none(): |
| 181 | + with pytest.raises(ValueError): |
| 182 | + _get_packed_offsets([(1, 0)] * 3, total=None, sep=None, mode='equal') |
0 commit comments