Debian Codegraph @

tests/test_directives/test_directive_patch.py

raw
1 """Test the patched directives."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 import pytest
8 from docutils import nodes
9
10 from sphinx.testing import restructuredtext
11 from sphinx.testing.util import assert_node
12
13 if TYPE_CHECKING:
14 from sphinx.testing.util import SphinxTestApp
15
16
17 @pytest.mark.sphinx('html', testroot='_blank')
18 def test_code_directive(app: SphinxTestApp) -> None:
19 # normal case
20 text = '.. code::\n\n print("hello world")\n'
21
22 doctree = restructuredtext.parse(app, text)
23 assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
24 assert_node(doctree[0], language='default', highlight_args={})
25
26 # with language
27 text = '.. code:: python\n\n print("hello world")\n'
28
29 doctree = restructuredtext.parse(app, text)
30 assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
31 assert_node(doctree[0], language='python', highlight_args={})
32
33 # :number-lines: option
34 text = '.. code:: python\n :number-lines:\n\n print("hello world")\n'
35
36 doctree = restructuredtext.parse(app, text)
37 assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
38 assert_node(doctree[0], language='python', linenos=True, highlight_args={})
39
40 # :number-lines: option
41 text = '.. code:: python\n :number-lines: 5\n\n print("hello world")\n'
42
43 doctree = restructuredtext.parse(app, text)
44 assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
45 assert_node(
46 doctree[0], language='python', linenos=True, highlight_args={'linenostart': 5}
47 )
48
49
50 def _as_element(node: nodes.Node) -> nodes.Element:
51 assert isinstance(node, nodes.Element)
52 return node
53
54
55 @pytest.mark.sphinx('html', testroot='directive-csv-table')
56 def test_csv_table_directive(app: SphinxTestApp) -> None:
57 # relative path from current document
58 text = '.. csv-table::\n :file: example.csv\n'
59 doctree = restructuredtext.parse(app, text, docname='subdir/index')
60 assert_node(
61 doctree,
62 (
63 [
64 nodes.table,
65 nodes.tgroup,
66 (nodes.colspec, nodes.colspec, nodes.colspec, [nodes.tbody, nodes.row]),
67 ],
68 ),
69 )
70 table = _as_element(doctree[0])
71 tgroup = _as_element(table[0])
72 tbody = _as_element(tgroup[3])
73 first_row = _as_element(tbody[0])
74 assert_node(
75 first_row,
76 (
77 [nodes.entry, nodes.paragraph, 'FOO'],
78 [nodes.entry, nodes.paragraph, 'BAR'],
79 [nodes.entry, nodes.paragraph, 'BAZ'],
80 ),
81 )
82
83 # absolute path from source directory
84 text = '.. csv-table::\n :file: /example.csv\n'
85 doctree = restructuredtext.parse(app, text, docname='subdir/index')
86 assert_node(
87 doctree,
88 (
89 [
90 nodes.table,
91 nodes.tgroup,
92 (nodes.colspec, nodes.colspec, nodes.colspec, [nodes.tbody, nodes.row]),
93 ],
94 ),
95 )
96 table = _as_element(doctree[0])
97 tgroup = _as_element(table[0])
98 tbody = _as_element(tgroup[3])
99 first_row = _as_element(tbody[0])
100 assert_node(
101 first_row,
102 (
103 [nodes.entry, nodes.paragraph, 'foo'],
104 [nodes.entry, nodes.paragraph, 'bar'],
105 [nodes.entry, nodes.paragraph, 'baz'],
106 ),
107 )
108
109
110 @pytest.mark.sphinx('html', testroot='_blank')
111 def test_math_directive(app: SphinxTestApp) -> None:
112 # normal case
113 text = '.. math:: E = mc^2'
114 doctree = restructuredtext.parse(app, text)
115 assert_node(doctree, [nodes.document, nodes.math_block, 'E = mc^2\n\n'])
116
117 # :name: option
118 text = '.. math:: E = mc^2\n :name: eq1\n'
119 doctree = restructuredtext.parse(app, text)
120 assert_node(
121 doctree, [nodes.document, (nodes.target, [nodes.math_block, 'E = mc^2\n\n'])]
122 )
123 assert_node(doctree[1], nodes.math_block, docname='index', label='eq1', number=1)
124
125 # :label: option
126 text = '.. math:: E = mc^2\n :label: eq2\n'
127 doctree = restructuredtext.parse(app, text)
128 assert_node(
129 doctree, [nodes.document, (nodes.target, [nodes.math_block, 'E = mc^2\n\n'])]
130 )
131 assert_node(doctree[1], nodes.math_block, docname='index', label='eq2', number=2)
132
133 # :label: option without value
134 text = '.. math:: E = mc^2\n :label:\n'
135 doctree = restructuredtext.parse(app, text)
136 assert_node(
137 doctree, [nodes.document, (nodes.target, [nodes.math_block, 'E = mc^2\n\n'])]
138 )
139 assert_node(
140 doctree[1],
141 nodes.math_block,
142 ids=['equation-index-0'],
143 docname='index',
144 label='index:0',
145 number=3,
146 )