File: test_directive_patch.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 (146 lines) | stat: -rw-r--r-- 4,724 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
"""Test the patched directives."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from docutils import nodes

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_code_directive(app: SphinxTestApp) -> None:
    # normal case
    text = '.. code::\n\n   print("hello world")\n'

    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
    assert_node(doctree[0], language='default', highlight_args={})

    # with language
    text = '.. code:: python\n\n   print("hello world")\n'

    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
    assert_node(doctree[0], language='python', highlight_args={})

    # :number-lines: option
    text = '.. code:: python\n   :number-lines:\n\n   print("hello world")\n'

    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
    assert_node(doctree[0], language='python', linenos=True, highlight_args={})

    # :number-lines: option
    text = '.. code:: python\n   :number-lines: 5\n\n   print("hello world")\n'

    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
    assert_node(
        doctree[0], language='python', linenos=True, highlight_args={'linenostart': 5}
    )


def _as_element(node: nodes.Node) -> nodes.Element:
    assert isinstance(node, nodes.Element)
    return node


@pytest.mark.sphinx('html', testroot='directive-csv-table')
def test_csv_table_directive(app: SphinxTestApp) -> None:
    # relative path from current document
    text = '.. csv-table::\n   :file: example.csv\n'
    doctree = restructuredtext.parse(app, text, docname='subdir/index')
    assert_node(
        doctree,
        (
            [
                nodes.table,
                nodes.tgroup,
                (nodes.colspec, nodes.colspec, nodes.colspec, [nodes.tbody, nodes.row]),
            ],
        ),
    )
    table = _as_element(doctree[0])
    tgroup = _as_element(table[0])
    tbody = _as_element(tgroup[3])
    first_row = _as_element(tbody[0])
    assert_node(
        first_row,
        (
            [nodes.entry, nodes.paragraph, 'FOO'],
            [nodes.entry, nodes.paragraph, 'BAR'],
            [nodes.entry, nodes.paragraph, 'BAZ'],
        ),
    )

    # absolute path from source directory
    text = '.. csv-table::\n   :file: /example.csv\n'
    doctree = restructuredtext.parse(app, text, docname='subdir/index')
    assert_node(
        doctree,
        (
            [
                nodes.table,
                nodes.tgroup,
                (nodes.colspec, nodes.colspec, nodes.colspec, [nodes.tbody, nodes.row]),
            ],
        ),
    )
    table = _as_element(doctree[0])
    tgroup = _as_element(table[0])
    tbody = _as_element(tgroup[3])
    first_row = _as_element(tbody[0])
    assert_node(
        first_row,
        (
            [nodes.entry, nodes.paragraph, 'foo'],
            [nodes.entry, nodes.paragraph, 'bar'],
            [nodes.entry, nodes.paragraph, 'baz'],
        ),
    )


@pytest.mark.sphinx('html', testroot='_blank')
def test_math_directive(app: SphinxTestApp) -> None:
    # normal case
    text = '.. math:: E = mc^2'
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, [nodes.document, nodes.math_block, 'E = mc^2\n\n'])

    # :name: option
    text = '.. math:: E = mc^2\n   :name: eq1\n'
    doctree = restructuredtext.parse(app, text)
    assert_node(
        doctree, [nodes.document, (nodes.target, [nodes.math_block, 'E = mc^2\n\n'])]
    )
    assert_node(doctree[1], nodes.math_block, docname='index', label='eq1', number=1)

    # :label: option
    text = '.. math:: E = mc^2\n   :label: eq2\n'
    doctree = restructuredtext.parse(app, text)
    assert_node(
        doctree, [nodes.document, (nodes.target, [nodes.math_block, 'E = mc^2\n\n'])]
    )
    assert_node(doctree[1], nodes.math_block, docname='index', label='eq2', number=2)

    # :label: option without value
    text = '.. math:: E = mc^2\n   :label:\n'
    doctree = restructuredtext.parse(app, text)
    assert_node(
        doctree, [nodes.document, (nodes.target, [nodes.math_block, 'E = mc^2\n\n'])]
    )
    assert_node(
        doctree[1],
        nodes.math_block,
        ids=['equation-index-0'],
        docname='index',
        label='index:0',
        number=3,
    )