From c5212c75260d61a3e45090e85c15d27b301cb468 Mon Sep 17 00:00:00 2001 From: Joohwan Oh Date: Wed, 3 Feb 2021 22:19:52 -0800 Subject: [PATCH] Handle special case where given hex code is 0 --- colorpedia/inputs.py | 9 ++++++--- tests/test_inputs.py | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/colorpedia/inputs.py b/colorpedia/inputs.py index f93d65e..8a83b40 100644 --- a/colorpedia/inputs.py +++ b/colorpedia/inputs.py @@ -42,9 +42,12 @@ def normalize_degree_angle(value: Union[float, int]) -> float: def normalize_hex_code(value: str) -> str: - if isinstance(value, int): - value = str(value) - if isinstance(value, str) and re.search(HEX_REGEX, value): + if type(value) == int: + if value == 0: + return "000000" + else: + value = str(value) + if type(value) == str and re.search(HEX_REGEX, value): return value if len(value) == 6 else "".join(c * 2 for c in value) raise InputValueError("hex code", f"a string matching {HEX_REGEX}") diff --git a/tests/test_inputs.py b/tests/test_inputs.py index dddd4f7..b107750 100644 --- a/tests/test_inputs.py +++ b/tests/test_inputs.py @@ -122,14 +122,25 @@ def test_normalize_degree_angle_bad_arg(bad_arg): @pytest.mark.parametrize( ("arg", "expected"), - (("ABC", "AABBCC"), ("ABCDEF", "ABCDEF"), ("FFFFFF", "FFFFFF"), (212121, "212121")), + ( + ("ABC", "AABBCC"), + ("ABCDEF", "ABCDEF"), + ("FFFFFF", "FFFFFF"), + ("100", "110000"), + ("010", "001100"), + ("001", "000011"), + (212121, "212121"), + (100, "110000"), + (0, "000000"), + ), ) def test_normalize_hex_code(arg, expected): assert normalize_hex_code(arg) == expected @pytest.mark.parametrize( - "bad_arg", ("", "F", "FFFFFH", "#FFFFFF", 212121.0, True, False, max, None, []) + "bad_arg", + ("", "F", "FFFFFH", "#FFFFFF", 212121.0, True, False, max, None, [], 1, 11), ) def test_normalize_hex_code_bad_arg(bad_arg): with pytest.raises(InputValueError) as err: