Skip to content

Commit 6215af7

Browse files
authored
Merge pull request #67 from mfisher87/unit-test-refactor
Unit test refactor
2 parents af0f34e + 355688f commit 6215af7

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.PHONY: test
22
test:
33
python -m pytest --version
4-
python -m pytest test/
4+
python -m pytest -v test/
55

66

77
.PHONY: lint

test/test_editor_loads_native.py renamed to test/test_editor_load.py

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,74 @@ def approxeq(x, y, *, err=0.0001):
1818
"viscm/examples/sample_diverging_continuous.jscm",
1919
],
2020
)
21-
@pytest.mark.xfail(reason="Test very old; intent unclear")
22-
def test_editor_loads_native(colormap_file):
23-
with open(colormap_file) as f:
24-
data = json.loads(f.read())
25-
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
26-
cm.load(colormap_file)
27-
viscm = viscm_editor(
28-
uniform_space=cm.uniform_space,
29-
cmtype=cm.cmtype,
30-
method=cm.method,
31-
**cm.params,
32-
)
33-
assert viscm.name == data["name"]
34-
35-
extensions = data["extensions"]["https://matplotlib.org/viscm"]
36-
xp, yp, fixed = viscm.control_point_model.get_control_points()
37-
38-
assert extensions["fixed"] == fixed
39-
assert len(extensions["xp"]) == len(xp)
40-
assert len(extensions["yp"]) == len(yp)
41-
assert len(xp) == len(yp)
42-
for i in range(len(xp)):
43-
assert extensions["xp"][i] == xp[i]
44-
assert extensions["yp"][i] == yp[i]
45-
assert extensions["min_Jp"] == viscm.min_Jp
46-
assert extensions["max_Jp"] == viscm.max_Jp
47-
assert extensions["filter_k"] == viscm.cmap_model.filter_k
48-
assert extensions["cmtype"] == viscm.cmtype
49-
50-
# Decode hexadecimal-encoded colormap string (grouped in units of 3 pairs of
51-
# two-character (0-255) values) to 3-tuples of floats (0-1).
52-
colors_hex = data["colors"]
53-
colors_hex = [colors_hex[i : i + 6] for i in range(0, len(colors_hex), 6)]
54-
colors = [
55-
[int(c[i : i + 2], 16) / 255 for i in range(0, len(c), 2)] for c in colors_hex
56-
]
57-
58-
editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist()
59-
60-
for i in range(len(colors)):
61-
for z in range(3):
62-
# FIXME: The right-hand side of this comparison will always be 0.
63-
# https://github.com/matplotlib/viscm/pull/66#discussion_r1213818015
64-
assert colors[i][z] == np.rint(editor_colors[i][z] / 256)
65-
# Should the test look more like this?
66-
# assert approxeq(colors[i][z], editor_colors[i][z], err=0.005)
21+
class TestEditorLoad:
22+
def expected(self, colormap_file):
23+
with open(colormap_file) as f:
24+
exp = json.loads(f.read())
25+
return exp
26+
27+
def actual(self, colormap_file):
28+
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
29+
cm.load(colormap_file)
30+
act = viscm_editor(
31+
uniform_space=cm.uniform_space,
32+
cmtype=cm.cmtype,
33+
method=cm.method,
34+
**cm.params,
35+
)
36+
return act
37+
38+
def test_editor_loads_jscm_parameters_match(self, colormap_file):
39+
expected = self.expected(colormap_file)
40+
actual = self.actual(colormap_file)
41+
42+
assert actual.name == expected["name"]
43+
44+
extensions = expected["extensions"]["https://matplotlib.org/viscm"]
45+
xp, yp, fixed = actual.control_point_model.get_control_points()
46+
47+
assert extensions["fixed"] == fixed
48+
assert len(extensions["xp"]) == len(xp)
49+
assert len(extensions["yp"]) == len(yp)
50+
assert len(xp) == len(yp)
51+
for i in range(len(xp)):
52+
assert extensions["xp"][i] == xp[i]
53+
assert extensions["yp"][i] == yp[i]
54+
assert extensions["min_Jp"] == actual.min_Jp
55+
assert extensions["max_Jp"] == actual.max_Jp
56+
assert extensions["filter_k"] == actual.cmap_model.filter_k
57+
assert extensions["cmtype"] == actual.cmtype
58+
59+
@pytest.mark.xfail(reason="Test very old; intent unclear")
60+
def test_editor_loads_jscm_data_match(self, colormap_file):
61+
expected = self.expected(colormap_file)
62+
actual = self.actual(colormap_file)
63+
64+
# Decode hexadecimal-encoded colormap string (grouped in units of 3 pairs of
65+
# two-character [00-ff / 0-255] values) to 3-tuples of floats (0-1).
66+
expected_colors_hex = expected["colors"]
67+
expected_colors_hex = [
68+
expected_colors_hex[i : i + 6]
69+
for i in range(0, len(expected_colors_hex), 6)
70+
]
71+
expected_colors = [
72+
[int(c[i : i + 2], 16) / 255 for i in range(0, len(c), 2)]
73+
for c in expected_colors_hex
74+
]
75+
76+
actual_colors = actual.cmap_model.get_sRGB(num=256)[0].tolist()
77+
78+
for i in range(len(expected_colors)):
79+
for z in range(3):
80+
# FIXME: The right-hand side of this comparison will always be 0.
81+
# https://github.com/matplotlib/viscm/pull/66#discussion_r1213818015
82+
assert actual_colors[i][z] == np.rint(expected_colors[i][z] / 256)
83+
# Should the test look more like this?
84+
# assert approxeq(
85+
# expected_colors[i][z],
86+
# actual_colors[i][z],
87+
# err=0.005,
88+
# )
6789

6890

6991
# import matplotlib as mpl

0 commit comments

Comments
 (0)