File: test_color.py

package info (click to toggle)
python-docx 1.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,216 kB
  • sloc: xml: 25,323; python: 23,414; makefile: 175
file content (132 lines) | stat: -rw-r--r-- 4,897 bytes parent folder | download
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
# pyright: reportPrivateUsage=false

"""Unit-test suite for the `docx.dml.color` module."""

from __future__ import annotations

from typing import cast

import pytest

from docx.dml.color import ColorFormat
from docx.enum.dml import MSO_COLOR_TYPE, MSO_THEME_COLOR
from docx.oxml.text.run import CT_R
from docx.shared import RGBColor

from ..unitutil.cxml import element, xml


class DescribeColorFormat:
    """Unit-test suite for `docx.dml.color.ColorFormat` objects."""

    @pytest.mark.parametrize(
        ("r_cxml", "expected_value"),
        [
            ("w:r", None),
            ("w:r/w:rPr", None),
            ("w:r/w:rPr/w:color{w:val=auto}", MSO_COLOR_TYPE.AUTO),
            ("w:r/w:rPr/w:color{w:val=4224FF}", MSO_COLOR_TYPE.RGB),
            ("w:r/w:rPr/w:color{w:themeColor=dark1}", MSO_COLOR_TYPE.THEME),
            (
                "w:r/w:rPr/w:color{w:val=F00BA9,w:themeColor=accent1}",
                MSO_COLOR_TYPE.THEME,
            ),
        ],
    )
    def it_knows_its_color_type(self, r_cxml: str, expected_value: MSO_COLOR_TYPE | None):
        assert ColorFormat(cast(CT_R, element(r_cxml))).type == expected_value

    @pytest.mark.parametrize(
        ("r_cxml", "rgb"),
        [
            ("w:r", None),
            ("w:r/w:rPr", None),
            ("w:r/w:rPr/w:color{w:val=auto}", None),
            ("w:r/w:rPr/w:color{w:val=4224FF}", "4224ff"),
            ("w:r/w:rPr/w:color{w:val=auto,w:themeColor=accent1}", None),
            ("w:r/w:rPr/w:color{w:val=F00BA9,w:themeColor=accent1}", "f00ba9"),
        ],
    )
    def it_knows_its_RGB_value(self, r_cxml: str, rgb: str | None):
        expected_value = RGBColor.from_string(rgb) if rgb else None
        assert ColorFormat(cast(CT_R, element(r_cxml))).rgb == expected_value

    @pytest.mark.parametrize(
        ("r_cxml", "new_value", "expected_cxml"),
        [
            ("w:r", RGBColor(10, 20, 30), "w:r/w:rPr/w:color{w:val=0A141E}"),
            ("w:r/w:rPr", RGBColor(1, 2, 3), "w:r/w:rPr/w:color{w:val=010203}"),
            (
                "w:r/w:rPr/w:color{w:val=123abc}",
                RGBColor(42, 24, 99),
                "w:r/w:rPr/w:color{w:val=2A1863}",
            ),
            (
                "w:r/w:rPr/w:color{w:val=auto}",
                RGBColor(16, 17, 18),
                "w:r/w:rPr/w:color{w:val=101112}",
            ),
            (
                "w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=dark1}",
                RGBColor(24, 42, 99),
                "w:r/w:rPr/w:color{w:val=182A63}",
            ),
            ("w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=dark1}", None, "w:r/w:rPr"),
            ("w:r", None, "w:r"),
        ],
    )
    def it_can_change_its_RGB_value(
        self, r_cxml: str, new_value: RGBColor | None, expected_cxml: str
    ):
        color_format = ColorFormat(cast(CT_R, element(r_cxml)))
        color_format.rgb = new_value
        assert color_format._element.xml == xml(expected_cxml)

    @pytest.mark.parametrize(
        ("r_cxml", "expected_value"),
        [
            ("w:r", None),
            ("w:r/w:rPr", None),
            ("w:r/w:rPr/w:color{w:val=auto}", None),
            ("w:r/w:rPr/w:color{w:val=4224FF}", None),
            ("w:r/w:rPr/w:color{w:themeColor=accent1}", MSO_THEME_COLOR.ACCENT_1),
            ("w:r/w:rPr/w:color{w:val=F00BA9,w:themeColor=dark1}", MSO_THEME_COLOR.DARK_1),
        ],
    )
    def it_knows_its_theme_color(self, r_cxml: str, expected_value: MSO_THEME_COLOR | None):
        color_format = ColorFormat(cast(CT_R, element(r_cxml)))
        assert color_format.theme_color == expected_value

    @pytest.mark.parametrize(
        ("r_cxml", "new_value", "expected_cxml"),
        [
            (
                "w:r",
                MSO_THEME_COLOR.ACCENT_1,
                "w:r/w:rPr/w:color{w:val=000000,w:themeColor=accent1}",
            ),
            (
                "w:r/w:rPr",
                MSO_THEME_COLOR.ACCENT_2,
                "w:r/w:rPr/w:color{w:val=000000,w:themeColor=accent2}",
            ),
            (
                "w:r/w:rPr/w:color{w:val=101112}",
                MSO_THEME_COLOR.ACCENT_3,
                "w:r/w:rPr/w:color{w:val=101112,w:themeColor=accent3}",
            ),
            (
                "w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=dark1}",
                MSO_THEME_COLOR.LIGHT_2,
                "w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=light2}",
            ),
            ("w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=dark1}", None, "w:r/w:rPr"),
            ("w:r", None, "w:r"),
        ],
    )
    def it_can_change_its_theme_color(
        self, r_cxml: str, new_value: MSO_THEME_COLOR | None, expected_cxml: str
    ):
        color_format = ColorFormat(cast(CT_R, element(r_cxml)))
        color_format.theme_color = new_value
        assert color_format._element.xml == xml(expected_cxml)