2012-09-12 15:34:00 -05:00
|
|
|
"""Test the only directive with the test root."""
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
import pytest
|
2018-02-19 07:39:14 -06:00
|
|
|
from docutils import nodes
|
2012-09-12 15:34:00 -05:00
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('text', testroot='directive-only')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_sectioning(app):
|
2012-09-12 15:34:00 -05:00
|
|
|
def getsects(section):
|
|
|
|
if not isinstance(section, nodes.section):
|
|
|
|
return [getsects(n) for n in section.children]
|
|
|
|
title = section.next_node(nodes.title).astext().strip()
|
|
|
|
subsects = []
|
|
|
|
children = section.children[:]
|
|
|
|
while children:
|
|
|
|
node = children.pop(0)
|
|
|
|
if isinstance(node, nodes.section):
|
|
|
|
subsects.append(node)
|
|
|
|
continue
|
|
|
|
children = list(node.children) + children
|
|
|
|
return [title, [getsects(subsect) for subsect in subsects]]
|
|
|
|
|
|
|
|
def testsects(prefix, sects, indent=0):
|
|
|
|
title = sects[0]
|
|
|
|
parent_num = title.split()[0]
|
2024-08-11 08:58:56 -05:00
|
|
|
assert prefix == parent_num, f'Section out of place: {title!r}'
|
2012-09-12 15:34:00 -05:00
|
|
|
for i, subsect in enumerate(sects[1]):
|
|
|
|
num = subsect[0].split()[0]
|
2024-08-11 08:58:56 -05:00
|
|
|
assert re.match(
|
|
|
|
'[0-9]+[.0-9]*[.]', num
|
|
|
|
), f'Unnumbered section: {subsect[0]!r}'
|
2017-01-25 10:13:17 -06:00
|
|
|
testsects(prefix + str(i + 1) + '.', subsect, indent + 4)
|
2012-09-12 15:34:00 -05:00
|
|
|
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'only.rst'])
|
2012-09-12 15:34:00 -05:00
|
|
|
doctree = app.env.get_doctree('only')
|
2017-04-27 07:49:19 -05:00
|
|
|
app.env.apply_post_transforms(doctree, 'only')
|
2012-09-12 15:34:00 -05:00
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
parts = [
|
|
|
|
getsects(n)
|
|
|
|
for n in [_n for _n in doctree.children if isinstance(_n, nodes.section)]
|
|
|
|
]
|
2012-09-12 15:34:00 -05:00
|
|
|
for i, s in enumerate(parts):
|
2017-01-25 10:13:17 -06:00
|
|
|
testsects(str(i + 1) + '.', s, 4)
|
2024-08-11 08:58:56 -05:00
|
|
|
actual_headings = '\n'.join(p[0] for p in parts)
|
|
|
|
assert (
|
|
|
|
len(parts) == 4
|
|
|
|
), f'Expected 4 document level headings, got:\n{actual_headings}'
|