| 1 |
"""Tests uti.nodes functions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from textwrap import dedent |
| 6 |
from typing import TYPE_CHECKING, Any |
| 7 |
|
| 8 |
import pytest |
| 9 |
from docutils import frontend, nodes |
| 10 |
from docutils.parsers import rst |
| 11 |
from docutils.utils import new_document |
| 12 |
|
| 13 |
from sphinx.transforms import ApplySourceWorkaround |
| 14 |
from sphinx.util.nodes import ( |
| 15 |
NodeMatcher, |
| 16 |
apply_source_workaround, |
| 17 |
clean_astext, |
| 18 |
extract_messages, |
| 19 |
make_id, |
| 20 |
split_explicit_title, |
| 21 |
) |
| 22 |
|
| 23 |
if TYPE_CHECKING: |
| 24 |
from collections.abc import Iterable |
| 25 |
|
| 26 |
from docutils.nodes import document |
| 27 |
|
| 28 |
|
| 29 |
def _transform(doctree: nodes.document) -> None: |
| 30 |
ApplySourceWorkaround(doctree).apply() |
| 31 |
|
| 32 |
|
| 33 |
def create_new_document() -> document: |
| 34 |
# TODO: TYPING: Upstream docutils should accept both instances and subclasses |
| 35 |
# of SettingsSpec here. |
| 36 |
settings = frontend.get_default_settings(rst.Parser) # type: ignore[arg-type] |
| 37 |
settings.id_prefix = 'id' |
| 38 |
document = new_document('dummy.txt', settings) |
| 39 |
return document |
| 40 |
|
| 41 |
|
| 42 |
def _get_doctree(text): |
| 43 |
document = create_new_document() |
| 44 |
rst.Parser().parse(text, document) |
| 45 |
_transform(document) |
| 46 |
return document |
| 47 |
|
| 48 |
|
| 49 |
def assert_node_count( |
| 50 |
messages: Iterable[tuple[nodes.Element, str]], |
| 51 |
node_type: type[nodes.Node], |
| 52 |
expect_count: int, |
| 53 |
) -> None: |
| 54 |
count = 0 |
| 55 |
node_list = [node for node, msg in messages] |
| 56 |
for node in node_list: |
| 57 |
if isinstance(node, node_type): |
| 58 |
count += 1 |
| 59 |
|
| 60 |
assert count == expect_count, ( |
| 61 |
f'Count of {node_type!r} in the {node_list!r} ' |
| 62 |
f'is {count} instead of {expect_count}' |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
def test_NodeMatcher(): |
| 67 |
doctree = nodes.document(None, None) |
| 68 |
doctree += nodes.paragraph('', 'Hello') |
| 69 |
doctree += nodes.paragraph('', 'Sphinx', block=1) |
| 70 |
doctree += nodes.paragraph('', 'World', block=2) |
| 71 |
doctree += nodes.literal_block('', 'blah blah blah', block=3) |
| 72 |
|
| 73 |
# search by node class |
| 74 |
matcher = NodeMatcher(nodes.paragraph) |
| 75 |
assert len(list(doctree.findall(matcher))) == 3 |
| 76 |
|
| 77 |
# search by multiple node classes |
| 78 |
matcher = NodeMatcher(nodes.paragraph, nodes.literal_block) |
| 79 |
assert len(list(doctree.findall(matcher))) == 4 |
| 80 |
|
| 81 |
# search by node attribute |
| 82 |
matcher = NodeMatcher(block=1) |
| 83 |
assert len(list(doctree.findall(matcher))) == 1 |
| 84 |
|
| 85 |
# search by node attribute (Any) |
| 86 |
matcher = NodeMatcher(block=Any) |
| 87 |
assert len(list(doctree.findall(matcher))) == 3 |
| 88 |
|
| 89 |
# search by both class and attribute |
| 90 |
matcher = NodeMatcher(nodes.paragraph, block=Any) |
| 91 |
assert len(list(doctree.findall(matcher))) == 2 |
| 92 |
|
| 93 |
# mismatched |
| 94 |
matcher = NodeMatcher(nodes.title) |
| 95 |
assert len(list(doctree.findall(matcher))) == 0 |
| 96 |
|
| 97 |
# search with Any does not match to Text node |
| 98 |
matcher = NodeMatcher(blah=Any) |
| 99 |
assert len(list(doctree.findall(matcher))) == 0 |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.parametrize( |
| 103 |
('rst', 'node_cls', 'count'), |
| 104 |
[ |
| 105 |
( |
| 106 |
""" |
| 107 |
.. admonition:: admonition title |
| 108 |
|
| 109 |
admonition body |
| 110 |
""", |
| 111 |
nodes.title, |
| 112 |
1, |
| 113 |
), |
| 114 |
( |
| 115 |
""" |
| 116 |
.. figure:: foo.jpg |
| 117 |
|
| 118 |
this is title |
| 119 |
""", |
| 120 |
nodes.caption, |
| 121 |
1, |
| 122 |
), |
| 123 |
( |
| 124 |
""" |
| 125 |
.. rubric:: spam |
| 126 |
""", |
| 127 |
nodes.rubric, |
| 128 |
1, |
| 129 |
), |
| 130 |
( |
| 131 |
""" |
| 132 |
| spam |
| 133 |
| egg |
| 134 |
""", |
| 135 |
nodes.line, |
| 136 |
2, |
| 137 |
), |
| 138 |
( |
| 139 |
""" |
| 140 |
section |
| 141 |
======= |
| 142 |
|
| 143 |
+----------------+ |
| 144 |
| | **Title 1** | |
| 145 |
| | Message 1 | |
| 146 |
+----------------+ |
| 147 |
""", |
| 148 |
nodes.line, |
| 149 |
2, |
| 150 |
), |
| 151 |
( |
| 152 |
""" |
| 153 |
* | **Title 1** |
| 154 |
| Message 1 |
| 155 |
""", |
| 156 |
nodes.line, |
| 157 |
2, |
| 158 |
), |
| 159 |
], |
| 160 |
) |
| 161 |
def test_extract_messages(rst: str, node_cls: type[nodes.Element], count: int) -> None: |
| 162 |
msg = extract_messages(_get_doctree(dedent(rst))) |
| 163 |
assert_node_count(msg, node_cls, count) |
| 164 |
|
| 165 |
|
| 166 |
def test_extract_messages_without_rawsource() -> None: |
| 167 |
"""Check node.rawsource is fall-backed by using node.astext() value. |
| 168 |
|
| 169 |
`extract_message` which is used from Sphinx i18n feature drop ``not node.rawsource`` |
| 170 |
nodes. So, all nodes which want to translate must have ``rawsource`` value. |
| 171 |
However, sometimes node.rawsource is not set. |
| 172 |
|
| 173 |
For example: recommonmark-0.2.0 doesn't set rawsource to `paragraph` node. |
| 174 |
|
| 175 |
See https://github.com/sphinx-doc/sphinx/pull/1994 |
| 176 |
""" |
| 177 |
p = nodes.paragraph() |
| 178 |
p.append(nodes.Text('test')) |
| 179 |
p.append(nodes.Text('sentence')) |
| 180 |
assert not p.rawsource # target node must not have rawsource value |
| 181 |
document = create_new_document() |
| 182 |
document.append(p) |
| 183 |
_transform(document) |
| 184 |
assert_node_count(extract_messages(document), nodes.TextElement, 1) |
| 185 |
assert next(m for n, m in extract_messages(document)), 'text sentence' |
| 186 |
|
| 187 |
|
| 188 |
def test_clean_astext() -> None: |
| 189 |
node: nodes.Element |
| 190 |
node = nodes.paragraph(text='hello world') |
| 191 |
assert clean_astext(node) == 'hello world' |
| 192 |
|
| 193 |
node = nodes.image(alt='hello world') |
| 194 |
assert clean_astext(node) == '' |
| 195 |
|
| 196 |
node = nodes.paragraph(text='hello world') |
| 197 |
node += nodes.raw('', 'raw text', format='html') |
| 198 |
assert clean_astext(node) == 'hello world' |
| 199 |
|
| 200 |
|
| 201 |
@pytest.mark.parametrize( |
| 202 |
('prefix', 'term', 'expected'), |
| 203 |
[ |
| 204 |
('', '', 'id0'), |
| 205 |
('term', '', 'term-0'), |
| 206 |
('term', 'Sphinx', 'term-Sphinx'), |
| 207 |
('', 'io.StringIO', 'io.StringIO'), # contains a dot |
| 208 |
( |
| 209 |
# contains a dot & underscore |
| 210 |
'', |
| 211 |
'sphinx.setup_command', |
| 212 |
'sphinx.setup_command', |
| 213 |
), |
| 214 |
('', '_io.StringIO', 'io.StringIO'), # starts with underscore |
| 215 |
('', 'sphinx', 'sphinx'), # alphabets in unicode fullwidth characters |
| 216 |
('', '悠好', 'id0'), # multibytes text (in Chinese) |
| 217 |
('', 'Hello=悠好=こんにちは', 'Hello'), # alphabets and multibytes text |
| 218 |
('', 'fünf', 'funf'), # latin1 (umlaut) |
| 219 |
('', '0sphinx', 'sphinx'), # starts with number |
| 220 |
('', 'sphinx-', 'sphinx'), # ends with hyphen |
| 221 |
], |
| 222 |
) |
| 223 |
@pytest.mark.sphinx('html', testroot='root') |
| 224 |
def test_make_id(app, prefix, term, expected): |
| 225 |
document = create_new_document() |
| 226 |
assert make_id(app.env, document, prefix, term) == expected |
| 227 |
|
| 228 |
|
| 229 |
@pytest.mark.sphinx('html', testroot='root') |
| 230 |
def test_make_id_already_registered(app): |
| 231 |
document = create_new_document() |
| 232 |
document.ids['term-Sphinx'] = True # register "term-Sphinx" manually |
| 233 |
assert make_id(app.env, document, 'term', 'Sphinx') == 'term-0' |
| 234 |
|
| 235 |
|
| 236 |
@pytest.mark.sphinx('html', testroot='root') |
| 237 |
def test_make_id_sequential(app): |
| 238 |
document = create_new_document() |
| 239 |
document.ids['term-0'] = True |
| 240 |
assert make_id(app.env, document, 'term') == 'term-1' |
| 241 |
|
| 242 |
|
| 243 |
@pytest.mark.parametrize( |
| 244 |
('title', 'expected'), |
| 245 |
[ |
| 246 |
# implicit |
| 247 |
('hello', (False, 'hello', 'hello')), |
| 248 |
# explicit |
| 249 |
('hello <world>', (True, 'hello', 'world')), |
| 250 |
# explicit (title having angle brackets) |
| 251 |
('hello <world> <sphinx>', (True, 'hello <world>', 'sphinx')), |
| 252 |
], |
| 253 |
) |
| 254 |
def test_split_explicit_target(title: str, expected: tuple[bool, str, str]) -> None: |
| 255 |
assert split_explicit_title(title) == expected |
| 256 |
|
| 257 |
|
| 258 |
def test_apply_source_workaround_literal_block_no_source() -> None: |
| 259 |
"""Regression test for https://github.com/sphinx-doc/sphinx/issues/11091. |
| 260 |
|
| 261 |
Test that apply_source_workaround doesn't raise. |
| 262 |
""" |
| 263 |
literal_block = nodes.literal_block('', '') |
| 264 |
list_item = nodes.list_item('', literal_block) |
| 265 |
bullet_list = nodes.bullet_list('', list_item) |
| 266 |
|
| 267 |
assert literal_block.source is None |
| 268 |
assert list_item.source is None |
| 269 |
assert bullet_list.source is None |
| 270 |
|
| 271 |
apply_source_workaround(literal_block) |
| 272 |
|
| 273 |
assert literal_block.source is None |
| 274 |
assert list_item.source is None |
| 275 |
assert bullet_list.source is None |