Skip to content

Commit fa0c10c

Browse files
author
Niru Maheswaranathan
authored
Merge pull request #23 from nirum/rwyoxf-codex/add-unit-tests-for-untested-parts
Add tests for chart utils and images.cmat
2 parents 600c9ab + 358115b commit fa0c10c

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

tests/test_images.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,14 @@ def test_img_corr_mode():
1616
# Colorbar should have been added
1717
assert len(fig.axes) == 2
1818
plt.close(fig)
19+
20+
def test_cmat_labels_and_colorbar():
21+
data = np.array([[0.0, 1.0], [1.0, 0.0]])
22+
fig, ax = plt.subplots()
23+
cb, returned_ax = images.cmat(data, labels=["a", "b"], cbar=True, fig=fig, ax=ax)
24+
25+
assert returned_ax is ax
26+
assert [tick.get_text() for tick in ax.get_xticklabels()] == ["a", "b"]
27+
assert [tick.get_text() for tick in ax.get_yticklabels()] == ["a", "b"]
28+
assert len(fig.axes) == 2
29+
plt.close(fig)

tests/test_utils.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,58 @@ def test_noticks():
5151
assert len(ax.get_yticks()) == 0
5252

5353
plt.close(fig)
54+
55+
def test_get_bounds_spines():
56+
fig, ax = plt.subplots()
57+
ax.plot([0, 1], [0, 1])
58+
ax.spines["bottom"].set_bounds(0, 1)
59+
ax.spines["left"].set_bounds(-1, 2)
60+
61+
assert cu.get_bounds("x", ax=ax) == (0, 1)
62+
assert cu.get_bounds("y", ax=ax) == (-1, 2)
63+
plt.close(fig)
64+
65+
66+
def test_get_bounds_label_fallback():
67+
fig, ax = plt.subplots()
68+
ax.plot([0, 1, 2, 3], [0, 1, 2, 3])
69+
ax.set_xticks([0, 1, 2, 3])
70+
ax.set_xticklabels(["", "a", "", "b"])
71+
72+
assert cu.get_bounds("x", ax=ax) == (1, 3)
73+
plt.close(fig)
74+
75+
76+
def test_breathe_adds_padding_and_hides_spines():
77+
fig, ax = plt.subplots()
78+
ax.plot([0, 1], [0, 1])
79+
old_xlim = ax.get_xlim()
80+
old_ylim = ax.get_ylim()
81+
82+
cu.breathe(ax=ax)
83+
84+
new_xlim = ax.get_xlim()
85+
new_ylim = ax.get_ylim()
86+
87+
assert new_xlim[0] < old_xlim[0] and new_xlim[1] > old_xlim[1]
88+
assert new_ylim[0] < old_ylim[0] and new_ylim[1] > old_ylim[1]
89+
90+
assert not ax.spines["top"].get_visible()
91+
assert not ax.spines["right"].get_visible()
92+
assert ax.spines["bottom"].get_bounds() == old_xlim
93+
assert ax.spines["left"].get_bounds() == old_ylim
94+
plt.close(fig)
95+
96+
97+
def test_xclamp_yclamp():
98+
fig, ax = plt.subplots()
99+
ax.plot([0, 1, 2, 3, 4], [0, 1, 2, 3, 4])
100+
101+
cu.xclamp(x0=0.5, x1=3.4, dt=1.0, ax=ax)
102+
assert ax.get_xlim() == (0.0, 4.0)
103+
assert list(ax.get_xticks()) == [0.0, 1.0, 2.0, 3.0, 4.0]
104+
105+
cu.yclamp(y0=0.2, y1=3.5, dt=1.0, ax=ax)
106+
assert ax.get_ylim() == (0.0, 4.0)
107+
assert list(ax.get_yticks()) == [0.0, 1.0, 2.0, 3.0, 4.0]
108+
plt.close(fig)

0 commit comments

Comments
 (0)