Skip to content

Commit 7827552

Browse files
authored
Add unittests for AutoFormatting (#360)
* rm debug prints * add unittests for autoformatter
1 parent 5ab153e commit 7827552

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

ultraplot/tests/test_tickers.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,38 @@ def test_autocftime_locator_safe_helpers():
731731
assert locator_noleap._safe_create_datetime(2001, 2, 29) is None
732732

733733

734+
@pytest.mark.parametrize(
735+
"formatter_args, values, expected, ylim",
736+
[
737+
({"negpos": "NP"}, [10, -10, 0], ["10P", "10N", "0"], (-20, 20)),
738+
({"tickrange": (0, 10)}, [5, 11, -1], ["5", "", ""], (0, 10)),
739+
({"wraprange": (-180, 180)}, [200, -200], ["−160", "160"], (-200, 200)),
740+
],
741+
)
742+
def test_auto_formatter_options(formatter_args, values, expected, ylim):
743+
from ultraplot.ticker import AutoFormatter
744+
import matplotlib.pyplot as plt
745+
746+
fig, ax = plt.subplots()
747+
formatter = AutoFormatter(**formatter_args)
748+
ax.xaxis.set_major_formatter(formatter)
749+
ax.plot(values, [0] * len(values), "o")
750+
ax.set_xticks(values)
751+
if ylim is not None:
752+
ax.set_ylim(*ylim)
753+
fig.canvas.draw()
754+
# Use the tick locations set by matplotlib after drawing the canvas
755+
# formatter.locs is now set by matplotlib; do not assign manually
756+
for val, exp in zip(values, expected):
757+
try:
758+
result = formatter(val)
759+
except IndexError:
760+
result = ""
761+
except Exception as e:
762+
assert False, f"For value {val}: unexpected error {e}"
763+
assert result == exp, f"For value {val}: got {result}, expected {exp}"
764+
765+
734766
def test_autocftime_locator_safe_daily_locator():
735767
from ultraplot.ticker import AutoCFDatetimeLocator
736768

0 commit comments

Comments
 (0)