| 1 |
"""Tests the reStructuredText domain.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from sphinx import addnodes |
| 10 |
from sphinx.addnodes import ( |
| 11 |
desc, |
| 12 |
desc_addname, |
| 13 |
desc_annotation, |
| 14 |
desc_content, |
| 15 |
desc_name, |
| 16 |
desc_signature, |
| 17 |
) |
| 18 |
from sphinx.domains.rst import parse_directive |
| 19 |
from sphinx.testing import restructuredtext |
| 20 |
from sphinx.testing.util import assert_node |
| 21 |
|
| 22 |
from tests.utils import extract_node |
| 23 |
|
| 24 |
if TYPE_CHECKING: |
| 25 |
from sphinx.testing.util import SphinxTestApp |
| 26 |
|
| 27 |
|
| 28 |
def test_parse_directive() -> None: |
| 29 |
s = parse_directive(' foö ') |
| 30 |
assert s == ('foö', '') |
| 31 |
|
| 32 |
s = parse_directive(' .. foö :: ') |
| 33 |
assert s == ('foö', '') |
| 34 |
|
| 35 |
s = parse_directive('.. foö:: args1 args2') |
| 36 |
assert s == ('foö', ' args1 args2') |
| 37 |
|
| 38 |
s = parse_directive('.. :: bar') |
| 39 |
assert s == ('.. :: bar', '') |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 43 |
def test_rst_directive(app: SphinxTestApp) -> None: |
| 44 |
# bare |
| 45 |
text = '.. rst:directive:: toctree' |
| 46 |
doctree = restructuredtext.parse(app, text) |
| 47 |
assert_node( |
| 48 |
doctree, |
| 49 |
( |
| 50 |
addnodes.index, |
| 51 |
[desc, ([desc_signature, desc_name, '.. toctree::'], [desc_content, ()])], |
| 52 |
), |
| 53 |
) |
| 54 |
assert_node( |
| 55 |
doctree[0], |
| 56 |
entries=[('single', 'toctree (directive)', 'directive-toctree', '', None)], |
| 57 |
) |
| 58 |
assert_node( |
| 59 |
doctree[1], |
| 60 |
addnodes.desc, |
| 61 |
desctype='directive', |
| 62 |
domain='rst', |
| 63 |
objtype='directive', |
| 64 |
no_index=False, |
| 65 |
) |
| 66 |
|
| 67 |
# decorated |
| 68 |
text = '.. rst:directive:: .. toctree::' |
| 69 |
doctree = restructuredtext.parse(app, text) |
| 70 |
assert_node( |
| 71 |
doctree, |
| 72 |
( |
| 73 |
addnodes.index, |
| 74 |
[desc, ([desc_signature, desc_name, '.. toctree::'], [desc_content, ()])], |
| 75 |
), |
| 76 |
) |
| 77 |
assert_node( |
| 78 |
doctree[0], |
| 79 |
entries=[('single', 'toctree (directive)', 'directive-toctree', '', None)], |
| 80 |
) |
| 81 |
assert_node( |
| 82 |
doctree[1], |
| 83 |
addnodes.desc, |
| 84 |
desctype='directive', |
| 85 |
domain='rst', |
| 86 |
objtype='directive', |
| 87 |
no_index=False, |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 92 |
def test_rst_directive_with_argument(app: SphinxTestApp) -> None: |
| 93 |
text = '.. rst:directive:: .. toctree:: foo bar baz' |
| 94 |
doctree = restructuredtext.parse(app, text) |
| 95 |
assert_node( |
| 96 |
doctree, |
| 97 |
( |
| 98 |
addnodes.index, |
| 99 |
[ |
| 100 |
desc, |
| 101 |
( |
| 102 |
[ |
| 103 |
desc_signature, |
| 104 |
([desc_name, '.. toctree::'], [desc_addname, ' foo bar baz']), |
| 105 |
], |
| 106 |
[desc_content, ()], |
| 107 |
), |
| 108 |
], |
| 109 |
), |
| 110 |
) |
| 111 |
assert_node( |
| 112 |
doctree[0], |
| 113 |
entries=[('single', 'toctree (directive)', 'directive-toctree', '', None)], |
| 114 |
) |
| 115 |
assert_node( |
| 116 |
doctree[1], |
| 117 |
addnodes.desc, |
| 118 |
desctype='directive', |
| 119 |
domain='rst', |
| 120 |
objtype='directive', |
| 121 |
no_index=False, |
| 122 |
) |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 126 |
def test_rst_directive_option(app: SphinxTestApp) -> None: |
| 127 |
text = '.. rst:directive:option:: foo' |
| 128 |
doctree = restructuredtext.parse(app, text) |
| 129 |
assert_node( |
| 130 |
doctree, |
| 131 |
( |
| 132 |
addnodes.index, |
| 133 |
[desc, ([desc_signature, desc_name, ':foo:'], [desc_content, ()])], |
| 134 |
), |
| 135 |
) |
| 136 |
assert_node( |
| 137 |
doctree[0], |
| 138 |
entries=[ |
| 139 |
('single', ':foo: (directive option)', 'directive-option-foo', '', 'F') |
| 140 |
], |
| 141 |
) |
| 142 |
assert_node( |
| 143 |
doctree[1], |
| 144 |
addnodes.desc, |
| 145 |
desctype='directive:option', |
| 146 |
domain='rst', |
| 147 |
objtype='directive:option', |
| 148 |
no_index=False, |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 153 |
def test_rst_directive_option_with_argument(app: SphinxTestApp) -> None: |
| 154 |
text = '.. rst:directive:option:: foo: bar baz' |
| 155 |
doctree = restructuredtext.parse(app, text) |
| 156 |
assert_node( |
| 157 |
doctree, |
| 158 |
( |
| 159 |
addnodes.index, |
| 160 |
[ |
| 161 |
desc, |
| 162 |
( |
| 163 |
[ |
| 164 |
desc_signature, |
| 165 |
([desc_name, ':foo:'], [desc_annotation, ' bar baz']), |
| 166 |
], |
| 167 |
[desc_content, ()], |
| 168 |
), |
| 169 |
], |
| 170 |
), |
| 171 |
) |
| 172 |
assert_node( |
| 173 |
doctree[0], |
| 174 |
entries=[ |
| 175 |
('single', ':foo: (directive option)', 'directive-option-foo', '', 'F') |
| 176 |
], |
| 177 |
) |
| 178 |
assert_node( |
| 179 |
doctree[1], |
| 180 |
addnodes.desc, |
| 181 |
desctype='directive:option', |
| 182 |
domain='rst', |
| 183 |
objtype='directive:option', |
| 184 |
no_index=False, |
| 185 |
) |
| 186 |
|
| 187 |
|
| 188 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 189 |
def test_rst_directive_option_type(app: SphinxTestApp) -> None: |
| 190 |
text = '.. rst:directive:option:: foo\n :type: directives.flags\n' |
| 191 |
doctree = restructuredtext.parse(app, text) |
| 192 |
assert_node( |
| 193 |
doctree, |
| 194 |
( |
| 195 |
addnodes.index, |
| 196 |
[ |
| 197 |
desc, |
| 198 |
( |
| 199 |
[ |
| 200 |
desc_signature, |
| 201 |
( |
| 202 |
[desc_name, ':foo:'], |
| 203 |
[desc_annotation, ' (directives.flags)'], |
| 204 |
), |
| 205 |
], |
| 206 |
[desc_content, ()], |
| 207 |
), |
| 208 |
], |
| 209 |
), |
| 210 |
) |
| 211 |
assert_node( |
| 212 |
doctree[0], |
| 213 |
entries=[ |
| 214 |
('single', ':foo: (directive option)', 'directive-option-foo', '', 'F') |
| 215 |
], |
| 216 |
) |
| 217 |
assert_node( |
| 218 |
doctree[1], |
| 219 |
addnodes.desc, |
| 220 |
desctype='directive:option', |
| 221 |
domain='rst', |
| 222 |
objtype='directive:option', |
| 223 |
no_index=False, |
| 224 |
) |
| 225 |
|
| 226 |
|
| 227 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 228 |
def test_rst_directive_and_directive_option(app: SphinxTestApp) -> None: |
| 229 |
text = '.. rst:directive:: foo\n\n .. rst:directive:option:: bar\n' |
| 230 |
doctree = restructuredtext.parse(app, text) |
| 231 |
assert_node( |
| 232 |
doctree, |
| 233 |
( |
| 234 |
addnodes.index, |
| 235 |
[ |
| 236 |
desc, |
| 237 |
( |
| 238 |
[desc_signature, desc_name, '.. foo::'], |
| 239 |
[desc_content, (addnodes.index, desc)], |
| 240 |
), |
| 241 |
], |
| 242 |
), |
| 243 |
) |
| 244 |
assert_node( |
| 245 |
extract_node(doctree, 1, 1, 0), |
| 246 |
entries=[ |
| 247 |
( |
| 248 |
'pair', |
| 249 |
'foo (directive); :bar: (directive option)', |
| 250 |
'directive-option-foo-bar', |
| 251 |
'', |
| 252 |
'B', |
| 253 |
) |
| 254 |
], |
| 255 |
) |
| 256 |
assert_node( |
| 257 |
extract_node(doctree, 1, 1, 1), |
| 258 |
([desc_signature, desc_name, ':bar:'], [desc_content, ()]), |
| 259 |
) |
| 260 |
assert_node( |
| 261 |
extract_node(doctree, 1, 1, 1), |
| 262 |
addnodes.desc, |
| 263 |
desctype='directive:option', |
| 264 |
domain='rst', |
| 265 |
objtype='directive:option', |
| 266 |
no_index=False, |
| 267 |
) |
| 268 |
|
| 269 |
|
| 270 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 271 |
def test_rst_role(app: SphinxTestApp) -> None: |
| 272 |
text = '.. rst:role:: ref' |
| 273 |
doctree = restructuredtext.parse(app, text) |
| 274 |
assert_node( |
| 275 |
doctree, |
| 276 |
( |
| 277 |
addnodes.index, |
| 278 |
[desc, ([desc_signature, desc_name, ':ref:'], [desc_content, ()])], |
| 279 |
), |
| 280 |
) |
| 281 |
assert_node(doctree[0], entries=[('single', 'ref (role)', 'role-ref', '', None)]) |
| 282 |
assert_node( |
| 283 |
doctree[1], |
| 284 |
addnodes.desc, |
| 285 |
desctype='role', |
| 286 |
domain='rst', |
| 287 |
objtype='role', |
| 288 |
no_index=False, |
| 289 |
) |