Skip to content

Commit 10c0dcc

Browse files
committed
more tests!
1 parent 209c1bb commit 10c0dcc

File tree

1 file changed

+120
-20
lines changed

1 file changed

+120
-20
lines changed

tests/test_colormap_parse.py

Lines changed: 120 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,128 @@
11
import pytest
22

3-
from branca.colormap import _parse_color_as_numerical_sequence
4-
5-
6-
@pytest.mark.parametrize("input_data, expected", [
7-
((0, 0, 0), (0.0, 0.0, 0.0, 1.0)),
8-
((255, 255, 255), (1.0, 1.0, 1.0, 1.0)),
9-
((255, 0, 0), (1.0, 0.0, 0.0, 1.0)),
10-
((0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0)),
11-
((0.5, 0.5, 0.5), (0.5, 0.5, 0.5, 1.0)),
12-
((0.1, 0.2, 0.3, 0.4), (0.1, 0.2, 0.3, 0.4)),
13-
((0.0, 1.0, 0.0, 0.5), (0.0, 1.0, 0.0, 0.5)),
14-
((0, 0, 0, 255.0), (0.0, 0.0, 0.0, 1.0)),
15-
((0, 0, 255.0, 0.0), (0.0, 0.0, 1.0, 0.0)),
16-
])
3+
from branca.colormap import (
4+
_parse_color_as_numerical_sequence,
5+
_color_normalized_float_to_byte_int,
6+
_color_byte_to_normalized_float,
7+
_parse_hex,
8+
_is_hex,
9+
_parse_color,
10+
)
11+
12+
13+
@pytest.mark.parametrize(
14+
"input_data, expected",
15+
[
16+
((255, 0, 0), (1.0, 0.0, 0.0, 1.0)),
17+
((255, 0, 0, 127), (1.0, 0.0, 0.0, 0.4980392156862745)),
18+
("#FF0000", (1.0, 0.0, 0.0, 1.0)),
19+
("red", (1.0, 0.0, 0.0, 1.0)),
20+
((0.5, 0.5, 0.5), (0.5, 0.5, 0.5, 1.0)),
21+
((0.25, 0.5, 0.75), (0.25, 0.5, 0.75, 1.0)),
22+
((0.1, 0.2, 0.3, 0.4), (0.1, 0.2, 0.3, 0.4)),
23+
("#0000FF", (0.0, 0.0, 1.0, 1.0)),
24+
("#00FF00", (0.0, 1.0, 0.0, 1.0)),
25+
("#FFFFFF", (1.0, 1.0, 1.0, 1.0)),
26+
("#000000", (0.0, 0.0, 0.0, 1.0)),
27+
("#808080", (0.5019607843137255, 0.5019607843137255, 0.5019607843137255, 1.0)),
28+
(
29+
"#1A2B3C",
30+
(0.10196078431372549, 0.16862745098039217, 0.23529411764705882, 1.0),
31+
),
32+
("green", (0.0, 0.5019607843137255, 0.0, 1.0)),
33+
],
34+
)
35+
def test_parse_color(input_data, expected):
36+
assert _parse_color(input_data) == expected
37+
38+
39+
@pytest.mark.parametrize(
40+
"input_data, expected",
41+
[
42+
((0, 0, 0), (0.0, 0.0, 0.0, 1.0)),
43+
((255, 255, 255), (1.0, 1.0, 1.0, 1.0)),
44+
((255, 0, 0), (1.0, 0.0, 0.0, 1.0)),
45+
((0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0)),
46+
((0.5, 0.5, 0.5), (0.5, 0.5, 0.5, 1.0)),
47+
((0.1, 0.2, 0.3, 0.4), (0.1, 0.2, 0.3, 0.4)),
48+
((0.0, 1.0, 0.0, 0.5), (0.0, 1.0, 0.0, 0.5)),
49+
((0, 0, 0, 255.0), (0.0, 0.0, 0.0, 1.0)),
50+
((0, 0, 255.0, 0.0), (0.0, 0.0, 1.0, 0.0)),
51+
],
52+
)
1753
def test_parse_color_as_numerical_sequence(input_data, expected):
1854
assert _parse_color_as_numerical_sequence(input_data) == expected
1955

20-
@pytest.mark.parametrize("input_data, raises", [
21-
((256, 0, 0), ValueError),
22-
((0, 0, -1), ValueError),
23-
((0, 1, 2, 3, 4), ValueError),
24-
((0.5, 0.5, 0.5, "string"), TypeError),
25-
])
56+
57+
@pytest.mark.parametrize(
58+
"input_data, raises",
59+
[
60+
((256, 0, 0), ValueError),
61+
((0, 0, -1), ValueError),
62+
((0, 1, 2, 3, 4), ValueError),
63+
((0.5, 0.5, 0.5, "string"), TypeError),
64+
],
65+
)
2666
def test_parse_color_as_numerical_sequence_invalid(input_data, raises):
2767
with pytest.raises(raises):
2868
_parse_color_as_numerical_sequence(input_data)
69+
70+
71+
@pytest.mark.parametrize(
72+
"input_data, expected",
73+
[
74+
("#123456", True),
75+
("#abcdef", True),
76+
("#ABCDEF", True),
77+
("#1A2B3C", True),
78+
("#123", False),
79+
("123456", False),
80+
("#1234567", False),
81+
],
82+
)
83+
def test_is_hex(input_data, expected):
84+
assert _is_hex(input_data) == expected
85+
86+
87+
@pytest.mark.parametrize(
88+
"input_data, expected",
89+
[
90+
("#000000", (0.0, 0.0, 0.0, 1.0)),
91+
("#FFFFFF", (1.0, 1.0, 1.0, 1.0)),
92+
("#FF0000", (1.0, 0.0, 0.0, 1.0)),
93+
("#00FF00", (0.0, 1.0, 0.0, 1.0)),
94+
("#0000FF", (0.0, 0.0, 1.0, 1.0)),
95+
("#808080", (0.5019607843137255, 0.5019607843137255, 0.5019607843137255, 1.0)),
96+
],
97+
)
98+
def test_parse_hex(input_data, expected):
99+
assert _parse_hex(input_data) == expected
100+
101+
102+
@pytest.mark.parametrize(
103+
"input_data, expected",
104+
[
105+
(0, 0.0),
106+
(255, 1.0),
107+
(128, 0.5019607843137255),
108+
(64, 0.25098039215686274),
109+
(192, 0.7529411764705882),
110+
],
111+
)
112+
def test_color_byte_to_normalized_float(input_data, expected):
113+
assert _color_byte_to_normalized_float(input_data) == expected
114+
115+
116+
@pytest.mark.parametrize(
117+
"input_data, expected",
118+
[
119+
(0.0, 0),
120+
(0.5, 127),
121+
(1.0, 255),
122+
(0.9999, 255),
123+
(0.1, 25),
124+
(0.75, 191),
125+
],
126+
)
127+
def test_color_normalized_float_to_byte_int(input_data, expected):
128+
assert _color_normalized_float_to_byte_int(input_data) == expected

0 commit comments

Comments
 (0)