|
12 | 12 | from matplotlib.testing.decorators import image_comparison, check_figures_equal
|
13 | 13 |
|
14 | 14 |
|
| 15 | +class TestTriangulationParams: |
| 16 | + x = [-1, 0, 1, 0] |
| 17 | + y = [0, -1, 0, 1] |
| 18 | + triangles = [[0, 1, 2], [0, 2, 3]] |
| 19 | + mask = [False, True] |
| 20 | + |
| 21 | + @pytest.mark.parametrize('args, kwargs, expected', [ |
| 22 | + ([x, y], {}, [x, y, None, None]), |
| 23 | + ([x, y, triangles], {}, [x, y, triangles, None]), |
| 24 | + ([x, y], dict(triangles=triangles), [x, y, triangles, None]), |
| 25 | + ([x, y], dict(mask=mask), [x, y, None, mask]), |
| 26 | + ([x, y, triangles], dict(mask=mask), [x, y, triangles, mask]), |
| 27 | + ([x, y], dict(triangles=triangles, mask=mask), [x, y, triangles, mask]) |
| 28 | + ]) |
| 29 | + def test_extract_triangulation_params(self, args, kwargs, expected): |
| 30 | + other_args = [1, 2] |
| 31 | + other_kwargs = {'a': 3, 'b': '4'} |
| 32 | + x_, y_, triangles_, mask_, args_, kwargs_ = \ |
| 33 | + mtri.Triangulation._extract_triangulation_params( |
| 34 | + args + other_args, {**kwargs, **other_kwargs}) |
| 35 | + x, y, triangles, mask = expected |
| 36 | + assert x_ is x |
| 37 | + assert y_ is y |
| 38 | + assert_array_equal(triangles_, triangles) |
| 39 | + assert mask_ is mask |
| 40 | + assert args_ == other_args |
| 41 | + assert kwargs_ == other_kwargs |
| 42 | + |
| 43 | + |
| 44 | +def test_extract_triangulation_positional_mask(): |
| 45 | + # mask cannot be passed positionally |
| 46 | + mask = [True] |
| 47 | + args = [[0, 2, 1], [0, 0, 1], [[0, 1, 2]], mask] |
| 48 | + x_, y_, triangles_, mask_, args_, kwargs_ = \ |
| 49 | + mtri.Triangulation._extract_triangulation_params(args, {}) |
| 50 | + assert mask_ is None |
| 51 | + assert args_ == [mask] |
| 52 | + # the positional mask must be caught downstream because this must pass |
| 53 | + # unknown args through |
| 54 | + |
| 55 | + |
| 56 | +def test_triangulation_init(): |
| 57 | + x = [-1, 0, 1, 0] |
| 58 | + y = [0, -1, 0, 1] |
| 59 | + with pytest.raises(ValueError, match="x and y must be equal-length"): |
| 60 | + mtri.Triangulation(x, [1, 2]) |
| 61 | + with pytest.raises( |
| 62 | + ValueError, |
| 63 | + match=r"triangles must be a \(N, 3\) int array, but found shape " |
| 64 | + r"\(3,\)"): |
| 65 | + mtri.Triangulation(x, y, [0, 1, 2]) |
| 66 | + with pytest.raises( |
| 67 | + ValueError, |
| 68 | + match=r"triangles must be a \(N, 3\) int array, not 'other'"): |
| 69 | + mtri.Triangulation(x, y, 'other') |
| 70 | + with pytest.raises(ValueError, match="found value 99"): |
| 71 | + mtri.Triangulation(x, y, [[0, 1, 99]]) |
| 72 | + with pytest.raises(ValueError, match="found value -1"): |
| 73 | + mtri.Triangulation(x, y, [[0, 1, -1]]) |
| 74 | + |
| 75 | + |
15 | 76 | def test_delaunay():
|
16 | 77 | # No duplicate points, regular grid.
|
17 | 78 | nx = 5
|
@@ -177,6 +238,49 @@ def test_tripcolor():
|
177 | 238 | plt.title('facecolors')
|
178 | 239 |
|
179 | 240 |
|
| 241 | +def test_tripcolor_color(): |
| 242 | + x = [-1, 0, 1, 0] |
| 243 | + y = [0, -1, 0, 1] |
| 244 | + fig, ax = plt.subplots() |
| 245 | + with pytest.raises(ValueError, match="Missing color parameter"): |
| 246 | + ax.tripcolor(x, y) |
| 247 | + with pytest.raises(ValueError, match="The length of C must match either"): |
| 248 | + ax.tripcolor(x, y, [1, 2, 3]) |
| 249 | + with pytest.raises(ValueError, |
| 250 | + match="length of facecolors must match .* triangles"): |
| 251 | + ax.tripcolor(x, y, facecolors=[1, 2, 3, 4]) |
| 252 | + with pytest.raises(ValueError, |
| 253 | + match="'gouraud' .* at the points.* not at the faces"): |
| 254 | + ax.tripcolor(x, y, facecolors=[1, 2], shading='gouraud') |
| 255 | + with pytest.raises(ValueError, |
| 256 | + match="'gouraud' .* at the points.* not at the faces"): |
| 257 | + ax.tripcolor(x, y, [1, 2], shading='gouraud') # faces |
| 258 | + with pytest.raises(ValueError, |
| 259 | + match=r"pass C positionally or facecolors via keyword"): |
| 260 | + ax.tripcolor(x, y, C=[1, 2, 3, 4]) |
| 261 | + |
| 262 | + # smoke test for valid color specifications (via C or facecolors) |
| 263 | + ax.tripcolor(x, y, [1, 2, 3, 4]) # edges |
| 264 | + ax.tripcolor(x, y, [1, 2, 3, 4], shading='gouraud') # edges |
| 265 | + ax.tripcolor(x, y, [1, 2]) # faces |
| 266 | + ax.tripcolor(x, y, facecolors=[1, 2]) # faces |
| 267 | + |
| 268 | + |
| 269 | +def test_tripcolor_warnings(): |
| 270 | + x = [-1, 0, 1, 0] |
| 271 | + y = [0, -1, 0, 1] |
| 272 | + C = [0.4, 0.5] |
| 273 | + fig, ax = plt.subplots() |
| 274 | + # additional parameters |
| 275 | + with pytest.warns(UserWarning, match="Additional positional parameters"): |
| 276 | + ax.tripcolor(x, y, C, 'unused_positional') |
| 277 | + # facecolors takes precednced over C |
| 278 | + with pytest.warns(UserWarning, match="Positional parameter C .*no effect"): |
| 279 | + ax.tripcolor(x, y, C, facecolors=C) |
| 280 | + with pytest.warns(UserWarning, match="Positional parameter C .*no effect"): |
| 281 | + ax.tripcolor(x, y, 'interpreted as C', facecolors=C) |
| 282 | + |
| 283 | + |
180 | 284 | def test_no_modify():
|
181 | 285 | # Test that Triangulation does not modify triangles array passed to it.
|
182 | 286 | triangles = np.array([[3, 2, 0], [3, 1, 0]], dtype=np.int32)
|
|
0 commit comments