File: test_transforms_reorder_nodes.py

package info (click to toggle)
sphinx 9.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,732 kB
  • sloc: python: 109,394; javascript: 37,318; perl: 449; makefile: 183; sh: 37; xml: 19; ansic: 2
file content (147 lines) | stat: -rw-r--r-- 4,386 bytes parent folder | download
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
"""Tests the transformations"""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from docutils import nodes

from sphinx import addnodes
from sphinx.testing import restructuredtext
from sphinx.testing.util import assert_node

if TYPE_CHECKING:
    from sphinx.testing.util import SphinxTestApp


@pytest.mark.sphinx('html', testroot='_blank')
def test_transforms_reorder_consecutive_target_and_index_nodes_preserve_order(
    app: SphinxTestApp,
) -> None:
    text = '.. index:: abc\n.. index:: def\n.. index:: ghi\n.. index:: jkl\n\ntext\n'
    doctree = restructuredtext.parse(app, text)
    assert_node(
        doctree,
        (
            addnodes.index,
            addnodes.index,
            addnodes.index,
            addnodes.index,
            nodes.target,
            nodes.target,
            nodes.target,
            nodes.target,
            nodes.paragraph,
        ),
    )
    assert_node(
        doctree[0], addnodes.index, entries=[('single', 'abc', 'index-0', '', None)]
    )
    assert_node(
        doctree[1], addnodes.index, entries=[('single', 'def', 'index-1', '', None)]
    )
    assert_node(
        doctree[2], addnodes.index, entries=[('single', 'ghi', 'index-2', '', None)]
    )
    assert_node(
        doctree[3], addnodes.index, entries=[('single', 'jkl', 'index-3', '', None)]
    )
    assert_node(doctree[4], nodes.target, refid='index-0')
    assert_node(doctree[5], nodes.target, refid='index-1')
    assert_node(doctree[6], nodes.target, refid='index-2')
    assert_node(doctree[7], nodes.target, refid='index-3')
    # assert_node(doctree[8], nodes.paragraph)


@pytest.mark.sphinx('html', testroot='_blank')
def test_transforms_reorder_consecutive_target_and_index_nodes_no_merge_across_other_nodes(
    app: SphinxTestApp,
) -> None:
    text = (
        '.. index:: abc\n'
        '.. index:: def\n'
        '\n'
        'text\n'
        '\n'
        '.. index:: ghi\n'
        '.. index:: jkl\n'
        '\n'
        'text\n'
    )
    doctree = restructuredtext.parse(app, text)
    assert_node(
        doctree,
        (
            addnodes.index,
            addnodes.index,
            nodes.target,
            nodes.target,
            nodes.paragraph,
            addnodes.index,
            addnodes.index,
            nodes.target,
            nodes.target,
            nodes.paragraph,
        ),
    )
    assert_node(
        doctree[0], addnodes.index, entries=[('single', 'abc', 'index-0', '', None)]
    )
    assert_node(
        doctree[1], addnodes.index, entries=[('single', 'def', 'index-1', '', None)]
    )
    assert_node(doctree[2], nodes.target, refid='index-0')
    assert_node(doctree[3], nodes.target, refid='index-1')
    # assert_node(doctree[4], nodes.paragraph)
    assert_node(
        doctree[5], addnodes.index, entries=[('single', 'ghi', 'index-2', '', None)]
    )
    assert_node(
        doctree[6], addnodes.index, entries=[('single', 'jkl', 'index-3', '', None)]
    )
    assert_node(doctree[7], nodes.target, refid='index-2')
    assert_node(doctree[8], nodes.target, refid='index-3')
    # assert_node(doctree[9], nodes.paragraph)


@pytest.mark.sphinx('html', testroot='_blank')
def test_transforms_reorder_consecutive_target_and_index_nodes_merge_with_labels(
    app: SphinxTestApp,
) -> None:
    text = (
        '.. _abc:\n'
        '.. index:: def\n'
        '.. _ghi:\n'
        '.. index:: jkl\n'
        '.. _mno:\n'
        '\n'
        'Heading\n'
        '=======\n'
    )
    doctree = restructuredtext.parse(app, text)
    assert_node(
        doctree,
        (
            nodes.title,
            addnodes.index,
            addnodes.index,
            nodes.target,
            nodes.target,
            nodes.target,
            nodes.target,
            nodes.target,
        ),
    )
    # assert_node(doctree[8], nodes.title)
    assert_node(
        doctree[1], addnodes.index, entries=[('single', 'def', 'index-0', '', None)]
    )
    assert_node(
        doctree[2], addnodes.index, entries=[('single', 'jkl', 'index-1', '', None)]
    )
    assert_node(doctree[3], nodes.target, refid='abc')
    assert_node(doctree[4], nodes.target, refid='index-0')
    assert_node(doctree[5], nodes.target, refid='ghi')
    assert_node(doctree[6], nodes.target, refid='index-1')
    assert_node(doctree[7], nodes.target, refid='mno')