Debian Codegraph @

tests/test_util/test_util_docutils_sphinx_directive.py

raw
1 from __future__ import annotations
2
3 from types import SimpleNamespace
4
5 from docutils import nodes
6 from docutils.parsers.rst.languages import en as english
7 from docutils.parsers.rst.states import (
8 Inliner,
9 RSTState,
10 RSTStateMachine,
11 state_classes,
12 )
13 from docutils.statemachine import StringList
14 from docutils.utils import Reporter
15
16 from sphinx.util.docutils import SphinxDirective, new_document
17
18
19 def make_directive(
20 *, env: SimpleNamespace, input_lines: StringList | None = None
21 ) -> SphinxDirective:
22 _, directive = make_directive_and_state(env=env, input_lines=input_lines)
23 return directive
24
25
26 def make_directive_and_state(
27 *, env: SimpleNamespace, input_lines: StringList | None = None
28 ) -> tuple[RSTState, SphinxDirective]:
29 sm = RSTStateMachine(state_classes, initial_state='Body')
30 sm.reporter = Reporter(source='<tests>', report_level=0, halt_level=0)
31 if input_lines is not None:
32 sm.input_lines = input_lines
33 state = RSTState(sm)
34 document = state.document = new_document('<tests>')
35 document.settings.env = env
36 document.settings.tab_width = 4
37 document.settings.pep_references = None
38 document.settings.rfc_references = None
39 inliner = Inliner()
40 inliner.init_customizations(document.settings)
41 state.inliner = inliner
42 state.parent = document
43 state.memo = SimpleNamespace(
44 document=document,
45 reporter=document.reporter,
46 language=english,
47 title_styles=[],
48 section_level=0,
49 section_bubble_up_kludge=False,
50 inliner=inliner,
51 )
52 directive = SphinxDirective(
53 name='test_directive',
54 arguments=[],
55 options={},
56 content=StringList(),
57 lineno=0,
58 content_offset=0,
59 block_text='',
60 state=state,
61 state_machine=sm,
62 )
63 return state, directive
64
65
66 def test_sphinx_directive_env() -> None:
67 state, directive = make_directive_and_state(env=SimpleNamespace())
68
69 assert hasattr(directive, 'env')
70 assert directive.env is state.document.settings.env
71
72
73 def test_sphinx_directive_config() -> None:
74 env = SimpleNamespace(config=object())
75 state, directive = make_directive_and_state(env=env)
76
77 assert hasattr(directive, 'config')
78 assert directive.config is directive.env.config
79 assert directive.config is state.document.settings.env.config
80
81
82 def test_sphinx_directive_get_source_info() -> None:
83 env = SimpleNamespace()
84 input_lines = StringList(['spam'], source='<source>')
85 directive = make_directive(env=env, input_lines=input_lines)
86
87 assert directive.get_source_info() == ('<source>', 1)
88
89
90 def test_sphinx_directive_set_source_info() -> None:
91 env = SimpleNamespace()
92 input_lines = StringList(['spam'], source='<source>')
93 directive = make_directive(env=env, input_lines=input_lines)
94
95 node = nodes.Element()
96 directive.set_source_info(node)
97 assert node.source == '<source>'
98 assert node.line == 1
99
100
101 def test_sphinx_directive_get_location() -> None:
102 env = SimpleNamespace()
103 input_lines = StringList(['spam'], source='<source>')
104 directive = make_directive(env=env, input_lines=input_lines)
105
106 assert directive.get_location() == '<source>:1'
107
108
109 def test_sphinx_directive_parse_content_to_nodes() -> None:
110 directive = make_directive(env=SimpleNamespace())
111 content = 'spam\n====\n\nEggs! *Lobster thermidor.*'
112 directive.content = StringList(content.split('\n'), source='<source>')
113
114 parsed = directive.parse_content_to_nodes(allow_section_headings=True)
115 assert len(parsed) == 1
116 node = parsed[0]
117 assert isinstance(node, nodes.section)
118 assert len(node.children) == 2
119 assert isinstance(node.children[0], nodes.title)
120 assert node.children[0].astext() == 'spam'
121 assert isinstance(node.children[1], nodes.paragraph)
122 assert node.children[1].astext() == 'Eggs! Lobster thermidor.'
123
124
125 def test_sphinx_directive_parse_text_to_nodes() -> None:
126 directive = make_directive(env=SimpleNamespace())
127 content = 'spam\n====\n\nEggs! *Lobster thermidor.*'
128
129 parsed = directive.parse_text_to_nodes(content, allow_section_headings=True)
130 assert len(parsed) == 1
131 node = parsed[0]
132 assert isinstance(node, nodes.section)
133 assert len(node.children) == 2
134 assert isinstance(node.children[0], nodes.title)
135 assert node.children[0].astext() == 'spam'
136 assert isinstance(node.children[1], nodes.paragraph)
137 assert node.children[1].astext() == 'Eggs! Lobster thermidor.'
138
139
140 def test_sphinx_directive_parse_inline() -> None:
141 directive = make_directive(env=SimpleNamespace())
142 content = 'Eggs! *Lobster thermidor.*'
143
144 parsed, messages = directive.parse_inline(content)
145 assert len(parsed) == 2
146 assert messages == []
147 assert parsed[0] == nodes.Text('Eggs! ')
148 assert isinstance(parsed[1], nodes.emphasis)
149 assert parsed[1].rawsource == '*Lobster thermidor.*'
150 assert parsed[1][0] == nodes.Text('Lobster thermidor.')