-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_shared.py
More file actions
169 lines (133 loc) · 5.12 KB
/
Copy pathtest_shared.py
File metadata and controls
169 lines (133 loc) · 5.12 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""Test suite for the docx.shared module."""
from __future__ import annotations
import pytest
from docx.opc.part import XmlPart
from docx.shared import (
Cm,
ElementProxy,
Emu,
Inches,
Length,
Mm,
Pt,
RGBColor,
Twips,
lazyproperty,
)
from .unitutil.cxml import element
from .unitutil.mock import FixtureRequest, Mock, instance_mock
class DescribeElementProxy:
"""Unit-test suite for `docx.shared.ElementProxy` objects."""
def it_knows_when_its_equal_to_another_proxy_object(self):
p, q = element("w:p"), element("w:p")
proxy = ElementProxy(p)
proxy_2 = ElementProxy(p)
proxy_3 = ElementProxy(q)
not_a_proxy = "Foobar"
assert (proxy == proxy_2) is True
assert (proxy == proxy_3) is False
assert (proxy == not_a_proxy) is False
assert (proxy != proxy_2) is False
assert (proxy != proxy_3) is True
assert (proxy != not_a_proxy) is True
def it_knows_its_element(self):
p = element("w:p")
proxy = ElementProxy(p)
assert proxy.element is p
def it_knows_its_part(self, other_proxy_: Mock, part_: Mock):
other_proxy_.part = part_
proxy = ElementProxy(element("w:p"), other_proxy_)
assert proxy.part is part_
# -- fixture ---------------------------------------------------------------------------------
@pytest.fixture
def other_proxy_(self, request: FixtureRequest):
return instance_mock(request, ElementProxy)
@pytest.fixture
def part_(self, request: FixtureRequest):
return instance_mock(request, XmlPart)
class DescribeLength:
"""Unit-test suite for `docx.shared.Length` objects."""
@pytest.mark.parametrize(
("UnitCls", "units_val", "emu"),
[
(Length, 914400, 914400),
(Inches, 1.1, 1005840),
(Cm, 2.53, 910799),
(Emu, 9144.9, 9144),
(Mm, 13.8, 496800),
(Pt, 24.5, 311150),
(Twips, 360, 228600),
],
)
def it_can_construct_from_convenient_units(self, UnitCls: type, units_val: float, emu: int):
length = UnitCls(units_val)
assert isinstance(length, Length)
assert length == emu
@pytest.mark.parametrize(
("prop_name", "expected_value", "expected_type"),
[
("inches", 1.0, float),
("cm", 2.54, float),
("emu", 914400, int),
("mm", 25.4, float),
("pt", 72.0, float),
("twips", 1440, int),
],
)
def it_can_self_convert_to_convenient_units(
self, prop_name: str, expected_value: float, expected_type: type
):
# -- use an inch for the initial value --
length = Length(914400)
length_in_units = getattr(length, prop_name)
assert length_in_units == expected_value
assert isinstance(length_in_units, expected_type)
class DescribeRGBColor:
"""Unit-test suite for `docx.shared.RGBColor` objects."""
def it_is_natively_constructed_using_three_ints_0_to_255(self):
rgb_color = RGBColor(0x12, 0x34, 0x56)
assert isinstance(rgb_color, RGBColor)
# -- it is comparable to a tuple[int, int, int] --
assert rgb_color == (18, 52, 86)
def it_raises_with_helpful_error_message_on_wrong_types(self):
with pytest.raises(TypeError, match=r"RGBColor\(\) takes three integer valu"):
RGBColor("12", "34", "56") # pyright: ignore
with pytest.raises(ValueError, match=r"\(\) takes three integer values 0-255"):
RGBColor(-1, 34, 56)
with pytest.raises(ValueError, match=r"RGBColor\(\) takes three integer valu"):
RGBColor(12, 256, 56)
def it_can_construct_from_a_hex_string_rgb_value(self):
rgb = RGBColor.from_string("123456")
assert rgb == RGBColor(0x12, 0x34, 0x56)
def it_can_provide_a_hex_string_rgb_value(self):
assert str(RGBColor(0xF3, 0x8A, 0x56)) == "F38A56"
def it_has_a_custom_repr(self):
rgb_color = RGBColor(0x42, 0xF0, 0xBA)
assert repr(rgb_color) == "RGBColor(0x42, 0xf0, 0xba)"
class DescribeLazyproperty:
"""Unit-test suite for `docx.shared.lazyproperty` objects."""
def it_evaluates_the_getter_only_once_when_it_returns_None(self):
class Widget:
def __init__(self):
self.call_count = 0
@lazyproperty
def maybe_missing_child(self):
self.call_count += 1
return None
widget = Widget()
assert widget.maybe_missing_child is None
assert widget.maybe_missing_child is None
assert widget.maybe_missing_child is None
assert widget.call_count == 1
def it_still_evaluates_the_getter_only_once_for_a_non_None_value(self):
class Widget:
def __init__(self):
self.call_count = 0
@lazyproperty
def value(self):
self.call_count += 1
return "some result"
widget = Widget()
assert widget.value == "some result"
assert widget.value == "some result"
assert widget.call_count == 1