Skip to content

Commit

Permalink
Handle special case where given hex code is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
joowani committed Feb 4, 2021
1 parent e7423c1 commit c5212c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
9 changes: 6 additions & 3 deletions colorpedia/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
15 changes: 13 additions & 2 deletions tests/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c5212c7

Please sign in to comment.