-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_hyperlink.py
More file actions
149 lines (124 loc) · 5.07 KB
/
test_hyperlink.py
File metadata and controls
149 lines (124 loc) · 5.07 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
"""Test suite for the docx.text.hyperlink module."""
from typing import cast
import pytest
from docx import types as t
from docx.opc.rel import _Relationship # pyright: ignore[reportPrivateUsage]
from docx.oxml.text.hyperlink import CT_Hyperlink
from docx.parts.story import StoryPart
from docx.text.hyperlink import Hyperlink
from ..unitutil.cxml import element
from ..unitutil.mock import FixtureRequest, Mock, instance_mock
class DescribeHyperlink:
"""Unit-test suite for the docx.text.hyperlink.Hyperlink object."""
@pytest.mark.parametrize(
("hlink_cxml", "expected_value"),
[
('w:hyperlink{r:id=rId6}/w:r/w:t"post"', "https://google.com/"),
("w:hyperlink{w:anchor=_Toc147925734}", ""),
("w:hyperlink", ""),
],
)
def it_knows_the_hyperlink_address(
self, hlink_cxml: str, expected_value: str, fake_parent: t.ProvidesStoryPart
):
hlink = cast(CT_Hyperlink, element(hlink_cxml))
hyperlink = Hyperlink(hlink, fake_parent)
assert hyperlink.address == expected_value
@pytest.mark.parametrize(
("hlink_cxml", "expected_value"),
[
("w:hyperlink", False),
("w:hyperlink/w:r", False),
('w:hyperlink/w:r/(w:t"abc",w:lastRenderedPageBreak,w:t"def")', True),
('w:hyperlink/w:r/(w:lastRenderedPageBreak,w:t"abc",w:t"def")', True),
('w:hyperlink/w:r/(w:t"abc",w:t"def",w:lastRenderedPageBreak)', True),
],
)
def it_knows_whether_it_contains_a_page_break(
self, hlink_cxml: str, expected_value: bool, fake_parent: t.ProvidesStoryPart
):
hlink = cast(CT_Hyperlink, element(hlink_cxml))
hyperlink = Hyperlink(hlink, fake_parent)
assert hyperlink.contains_page_break is expected_value
@pytest.mark.parametrize(
("hlink_cxml", "expected_value"),
[
("w:hyperlink{r:id=rId6}", ""),
("w:hyperlink{w:anchor=intro}", "intro"),
],
)
def it_knows_the_link_fragment_when_there_is_one(
self, hlink_cxml: str, expected_value: str, fake_parent: t.ProvidesStoryPart
):
hlink = cast(CT_Hyperlink, element(hlink_cxml))
hyperlink = Hyperlink(hlink, fake_parent)
assert hyperlink.fragment == expected_value
@pytest.mark.parametrize(
("hlink_cxml", "count"),
[
("w:hyperlink", 0),
("w:hyperlink/w:r", 1),
("w:hyperlink/(w:r,w:r)", 2),
("w:hyperlink/(w:r,w:lastRenderedPageBreak)", 1),
("w:hyperlink/(w:lastRenderedPageBreak,w:r)", 1),
("w:hyperlink/(w:r,w:lastRenderedPageBreak,w:r)", 2),
],
)
def it_provides_access_to_the_runs_it_contains(
self, hlink_cxml: str, count: int, fake_parent: t.ProvidesStoryPart
):
hlink = cast(CT_Hyperlink, element(hlink_cxml))
hyperlink = Hyperlink(hlink, fake_parent)
runs = hyperlink.runs
actual = [type(item).__name__ for item in runs]
expected = ["Run" for _ in range(count)]
assert actual == expected
@pytest.mark.parametrize(
("hlink_cxml", "expected_text"),
[
("w:hyperlink", ""),
("w:hyperlink/w:r", ""),
('w:hyperlink/w:r/w:t"foobar"', "foobar"),
('w:hyperlink/w:r/(w:t"foo",w:lastRenderedPageBreak,w:t"bar")', "foobar"),
('w:hyperlink/w:r/(w:t"abc",w:tab,w:t"def",w:noBreakHyphen)', "abc\tdef-"),
],
)
def it_knows_the_visible_text_of_the_link(
self, hlink_cxml: str, expected_text: str, fake_parent: t.ProvidesStoryPart
):
hlink = cast(CT_Hyperlink, element(hlink_cxml))
hyperlink = Hyperlink(hlink, fake_parent)
text = hyperlink.text
assert text == expected_text
@pytest.mark.parametrize(
("hlink_cxml", "expected_value"),
[
("w:hyperlink", ""),
("w:hyperlink{w:anchor=_Toc147925734}", ""),
('w:hyperlink{r:id=rId6}/w:r/w:t"post"', "https://google.com/"),
(
'w:hyperlink{r:id=rId6,w:anchor=foo}/w:r/w:t"post"',
"https://google.com/#foo",
),
],
)
def it_knows_the_full_url_for_web_addresses(
self, hlink_cxml: str, expected_value: str, fake_parent: t.ProvidesStoryPart
):
hlink = cast(CT_Hyperlink, element(hlink_cxml))
hyperlink = Hyperlink(hlink, fake_parent)
assert hyperlink.url == expected_value
# -- fixtures --------------------------------------------------------------------
@pytest.fixture
def fake_parent(self, story_part: Mock, rel: Mock) -> t.ProvidesStoryPart:
class StoryChild:
@property
def part(self) -> StoryPart:
return story_part
return StoryChild()
@pytest.fixture
def rel(self, request: FixtureRequest):
return instance_mock(request, _Relationship, target_ref="https://google.com/")
@pytest.fixture
def story_part(self, request: FixtureRequest, rel: Mock):
return instance_mock(request, StoryPart, rels={"rId6": rel})