File: test_normalization.py

package info (click to toggle)
webcolors 25.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 336 kB
  • sloc: python: 1,486; makefile: 124
file content (132 lines) | stat: -rw-r--r-- 3,473 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
Test the color-value normalization functions.

"""

# SPDX-License-Identifier: BSD-3-Clause

import pytest

import webcolors


@pytest.mark.parametrize(
    ["raw", "normalized"],
    [
        ("#0099cc", "#0099cc"),
        ("#0099CC", "#0099cc"),
        ("#09c", "#0099cc"),
        ("#09C", "#0099cc"),
    ],
    ids=["lowercase", "uppercase", "three-digit-lowercase", "three-digit-uppercase"],
)
def test_normalize_hex(raw: str, normalized: str):
    """
    Hexadecimal normalization normalizes valid hex color codes to 6 digits,
    lowercase.

    """
    assert normalized == webcolors.normalize_hex(raw)


@pytest.mark.parametrize(
    "hex_value",
    ["0099cc", "#0000gg", "#0000", "#00000000"],
    ids=["no-hash", "not-hex", "too-short", "too-long"],
)
def test_normalize_hex_format(hex_value: str):
    """
    Hex normalization raises ValueError on invalid hex color code.

    """
    with pytest.raises(ValueError, match="not a valid hexadecimal color value"):
        webcolors.normalize_hex(hex_value)


@pytest.mark.parametrize(
    ["raw", "normalized"],
    [(255, 255), (0, 0), (128, 128), (-20, 0), (270, 255), (-0, 0)],
    ids=["max", "min", "middle", "clipped-to-min", "clipped-to-max", "negative-zero"],
)
def test_normalize_integer_rgb(raw: int, normalized: int):
    """
    Integer normalization clips to 0-255.

    """
    # pylint: disable=protected-access
    assert normalized == webcolors._normalization._normalize_integer_rgb(raw)


@pytest.mark.parametrize(
    ["triplet", "normalized"],
    [
        ((128, 128, 128), (128, 128, 128)),
        ((0, 0, 0), (0, 0, 0)),
        ((255, 255, 255), (255, 255, 255)),
        ((270, -20, 128), (255, 0, 128)),
        ((-0, -0, -0), (0, 0, 0)),
    ],
    ids=["navy", "black", "white", "clipped", "negative-zero"],
)
def test_normalize_integer_triplet(
    triplet: webcolors.IntTuple, normalized: webcolors.IntTuple
):
    """
    Integer triplet normalization clips all values to 0-255.

    """
    result = webcolors.normalize_integer_triplet(triplet)
    assert isinstance(result, webcolors.IntegerRGB)
    assert normalized == result


@pytest.mark.parametrize(
    ["raw", "normalized"],
    [
        ("0%", "0%"),
        ("100%", "100%"),
        ("62%", "62%"),
        ("-5%", "0%"),
        ("250%", "100%"),
        ("85.49%", "85.49%"),
        ("-0%", "0%"),
    ],
    ids=[
        "min",
        "max",
        "not-special",
        "clipped-to-min",
        "clipped-to-max",
        "floating-point",
        "negative-zero",
    ],
)
def test_normalize_percent_rgb(raw: str, normalized: str):
    """
    Percent normalization clips to 0%-100%.

    """
    # pylint: disable=protected-access
    assert normalized == webcolors._normalization._normalize_percent_rgb(raw)


@pytest.mark.parametrize(
    ["triplet", "normalized"],
    [
        (("50%", "50%", "50%"), ("50%", "50%", "50%")),
        (("0%", "100%", "0%"), ("0%", "100%", "0%")),
        (("-10%", "250%", "500%"), ("0%", "100%", "100%")),
        (("-0%", "-0%", "-0%"), ("0%", "0%", "0%")),
    ],
    ids=["gray", "green", "clipped", "negative-zero"],
)
def test_normalize_percent_triplet(
    triplet: webcolors.PercentTuple, normalized: webcolors.PercentTuple
):
    """
    Percent triplet normalization clips all values to 0%-100%.

    """
    result = webcolors.normalize_percent_triplet(triplet)
    assert isinstance(result, webcolors.PercentRGB)
    assert normalized == result