Skip to content

Commit

Permalink
TST: avoid using high level (and thread-unsafe) pyplot API in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Nov 3, 2024
1 parent 8b5ef73 commit fa6858d
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pytest
from matplotlib.colors import ListedColormap as LC
from matplotlib.figure import Figure
from matplotlib.legend import Legend

import cmasher as cmr
Expand Down Expand Up @@ -62,7 +62,7 @@ def _MPL38_colormap_eq(cmap, other) -> bool:
]

# Obtain list of all colormaps registered in MPL
mpl_cmaps = plt.colormaps()
mpl_cmaps = mpl.colormaps()
mpl_cmaps_as_str: list[str] = list(mpl_cmaps)


Expand Down Expand Up @@ -222,7 +222,7 @@ def test_standalone_copy(self, name, tmp_path):

if name == "infinity":
# Check that the shifted version of infinity also exists
assert "cmr.infinity_s" in plt.colormaps()
assert "cmr.infinity_s" in mpl.colormaps()

# Test if providing an invalid colormap name fails
def test_invalid_cmap(self, tmp_path):
Expand Down Expand Up @@ -401,7 +401,7 @@ def test_cyclic_cmap(self):
name = "cyclic"
import_cmaps(TEST_DIR / "data" / f"cm_{name}.txt")
for cmap in [name, name + "_r", name + "_s", name + "_s_r"]:
assert "cmr." + cmap in plt.colormaps()
assert "cmr." + cmap in mpl.colormaps()
assert getattr(cmrcm, cmap, None) is not None
assert cmap in cmrcm.cmap_d
assert cmap in cmrcm.cmap_cd["cyclic"]
Expand Down Expand Up @@ -432,7 +432,9 @@ class Test_set_cmap_legend_entry:
# Test if providing a PathCollection object works
def test_path_collection(self):
# Create a small scatter plot
plot = plt.scatter([1, 2, 3], [2, 3, 4], c=[3, 4, 5], cmap=cmr.rainforest)
fig = Figure()
ax = fig.add_subplot()
plot = ax.scatter([1, 2, 3], [2, 3, 4], c=[3, 4, 5], cmap=cmr.rainforest)

# Add a cmap legend entry
set_cmap_legend_entry(plot, "Test")
Expand All @@ -441,23 +443,19 @@ def test_path_collection(self):
assert plot in Legend.get_default_handler_map()

# Create a legend
plt.legend()

# Close the plot
plt.close()
ax.legend()

# Test if providing a Line2D object does not work
def test_line2d(self):
# Create a small line plot
plot = plt.plot([1, 2, 3], [2, 3, 4])
fig = Figure()
ax = fig.add_subplot()
plot = ax.plot([1, 2, 3], [2, 3, 4])

# Attempt to add the cmap legend entry
with pytest.raises(ValueError):
set_cmap_legend_entry(plot, "Test")

# Close the plot
plt.close()


# Pytest class for take_cmap_colors()-function
class Test_take_cmap_colors:
Expand Down

0 comments on commit fa6858d

Please sign in to comment.