-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathtest_util_rst.py
More file actions
187 lines (155 loc) · 5.88 KB
/
test_util_rst.py
File metadata and controls
187 lines (155 loc) · 5.88 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""Tests sphinx.util.rst functions."""
from __future__ import annotations
from docutils.statemachine import StringList
from jinja2 import Environment
from sphinx.util.rst import (
_append_epilogue,
_prepend_prologue,
escape,
heading,
textwidth,
)
def test_escape() -> None:
assert escape(':ref:`id`') == r'\:ref\:\`id\`'
assert escape('footnote [#]_') == r'footnote \[\#\]\_'
assert escape('sphinx.application') == r'sphinx.application'
assert escape('.. toctree::') == r'\.. toctree\:\:'
def test_append_epilogue() -> None:
epilog = 'this is rst_epilog\ngood-bye reST!'
content = StringList(
['hello Sphinx world', 'Sphinx is a document generator'],
'dummy.rst',
)
_append_epilogue(content, epilog)
assert list(content.xitems()) == [
('dummy.rst', 0, 'hello Sphinx world'),
('dummy.rst', 1, 'Sphinx is a document generator'),
('dummy.rst', 2, ''),
('<rst_epilogue>', 0, 'this is rst_epilog'),
('<rst_epilogue>', 1, 'good-bye reST!'),
]
def test_prepend_prologue() -> None:
prologue = 'this is rst_prolog\nhello reST!'
content = StringList(
[
':title: test of SphinxFileInput',
':author: Sphinx team',
'',
'hello Sphinx world',
'Sphinx is a document generator',
],
'dummy.rst',
)
_prepend_prologue(content, prologue)
assert list(content.xitems()) == [
('dummy.rst', 0, ':title: test of SphinxFileInput'),
('dummy.rst', 1, ':author: Sphinx team'),
('<generated>', 0, ''),
('<rst_prologue>', 0, 'this is rst_prolog'),
('<rst_prologue>', 1, 'hello reST!'),
('<generated>', 0, ''),
('dummy.rst', 2, ''),
('dummy.rst', 3, 'hello Sphinx world'),
('dummy.rst', 4, 'Sphinx is a document generator'),
]
def test_prepend_prolog_with_CR() -> None:
# prologue having CR at tail
prologue = 'this is rst_prolog\nhello reST!\n'
content = StringList(
['hello Sphinx world', 'Sphinx is a document generator'],
'dummy.rst',
)
_prepend_prologue(content, prologue)
assert list(content.xitems()) == [
('<rst_prologue>', 0, 'this is rst_prolog'),
('<rst_prologue>', 1, 'hello reST!'),
('<generated>', 0, ''),
('dummy.rst', 0, 'hello Sphinx world'),
('dummy.rst', 1, 'Sphinx is a document generator'),
]
def test_prepend_prolog_without_CR() -> None:
# prologue not having CR at tail
prologue = 'this is rst_prolog\nhello reST!'
content = StringList(
['hello Sphinx world', 'Sphinx is a document generator'],
'dummy.rst',
)
_prepend_prologue(content, prologue)
assert list(content.xitems()) == [
('<rst_prologue>', 0, 'this is rst_prolog'),
('<rst_prologue>', 1, 'hello reST!'),
('<generated>', 0, ''),
('dummy.rst', 0, 'hello Sphinx world'),
('dummy.rst', 1, 'Sphinx is a document generator'),
]
def test_prepend_prolog_with_roles_in_sections() -> None:
prologue = 'this is rst_prolog\nhello reST!'
content = StringList(
[
':title: test of SphinxFileInput',
':author: Sphinx team',
'', # this newline is required
':mod:`foo`',
'----------',
'',
'hello',
],
'dummy.rst',
)
_prepend_prologue(content, prologue)
assert list(content.xitems()) == [
('dummy.rst', 0, ':title: test of SphinxFileInput'),
('dummy.rst', 1, ':author: Sphinx team'),
('<generated>', 0, ''),
('<rst_prologue>', 0, 'this is rst_prolog'),
('<rst_prologue>', 1, 'hello reST!'),
('<generated>', 0, ''),
('dummy.rst', 2, ''),
('dummy.rst', 3, ':mod:`foo`'),
('dummy.rst', 4, '----------'),
('dummy.rst', 5, ''),
('dummy.rst', 6, 'hello'),
]
def test_prepend_prolog_with_roles_in_sections_with_newline() -> None:
# prologue with trailing line break
prologue = 'this is rst_prolog\nhello reST!\n'
content = StringList([':mod:`foo`', '-' * 10, '', 'hello'], 'dummy.rst')
_prepend_prologue(content, prologue)
assert list(content.xitems()) == [
('<rst_prologue>', 0, 'this is rst_prolog'),
('<rst_prologue>', 1, 'hello reST!'),
('<generated>', 0, ''),
('dummy.rst', 0, ':mod:`foo`'),
('dummy.rst', 1, '----------'),
('dummy.rst', 2, ''),
('dummy.rst', 3, 'hello'),
]
def test_prepend_prolog_with_roles_in_sections_without_newline() -> None:
# prologue with no trailing line break
prologue = 'this is rst_prolog\nhello reST!'
content = StringList([':mod:`foo`', '-' * 10, '', 'hello'], 'dummy.rst')
_prepend_prologue(content, prologue)
assert list(content.xitems()) == [
('<rst_prologue>', 0, 'this is rst_prolog'),
('<rst_prologue>', 1, 'hello reST!'),
('<generated>', 0, ''),
('dummy.rst', 0, ':mod:`foo`'),
('dummy.rst', 1, '----------'),
('dummy.rst', 2, ''),
('dummy.rst', 3, 'hello'),
]
def test_textwidth() -> None:
assert textwidth('Hello') == 5
assert textwidth('русский язык') == 12
assert textwidth('русский язык', 'WFA') == 23 # Cyrillic are ambiguous chars
def test_heading() -> None:
env = Environment(autoescape=True)
env.extend(language=None)
assert heading(env, 'Hello') == 'Hello\n====='
assert heading(env, 'Hello', 1) == 'Hello\n====='
assert heading(env, 'Hello', 2) == 'Hello\n-----'
assert heading(env, 'Hello', 3) == 'Hello\n~~~~~'
assert heading(env, 'русский язык', 1) == 'русский язык\n============'
# language=ja: ambiguous
env.language = 'ja' # type: ignore[attr-defined]
assert heading(env, 'русский язык', 1) == 'русский язык\n======================='