Debian Codegraph @

test/test_parsers/test_rst/test_directives/test_sidebars.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_sidebars.py 10256 2025-11-02 17:51:21Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for the "sidebar" directive.
9 """
10
11 from pathlib import Path
12 import sys
13 import unittest
14
15 if __name__ == '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys.path.insert(0, str(Path(__file__).resolve().parents[4]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.utils import new_document
23
24
25 class ParserTestCase(unittest.TestCase):
26 maxDiff = None
27
28 def test_parser(self):
29 parser = Parser()
30 settings = get_default_settings(Parser)
31 settings.warning_stream = ''
32 for name, cases in totest.items():
33 for casenum, (case_input, case_expected) in enumerate(cases):
34 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
35 document = new_document('test data', settings.copy())
36 parser.parse(case_input, document)
37 output = document.pformat()
38 self.assertEqual(case_expected, output)
39
40
41 totest = {}
42
43 totest['sidebars'] = [
44 ["""\
45 .. sidebar:: Outer
46
47 .. sidebar:: Nested
48
49 Body.
50 """,
51 """\
52 <document source="test data">
53 <sidebar>
54 <title>
55 Outer
56 <system_message level="3" line="3" source="test data" type="ERROR">
57 <paragraph>
58 The "sidebar" directive may not be used within a sidebar element.
59 <literal_block xml:space="preserve">
60 .. sidebar:: Nested
61 \n\
62 Body.
63 """],
64 ["""\
65 .. sidebar:: Margin Notes
66 :subtitle: with options
67 :class: margin
68 :name: note:Options
69
70 Body.
71 """,
72 """\
73 <document source="test data">
74 <sidebar classes="margin" ids="note-options" names="note:options">
75 <title>
76 Margin Notes
77 <subtitle>
78 with options
79 <paragraph>
80 Body.
81 """],
82 ["""\
83 .. sidebar::
84
85 The title is optional.
86 """,
87 """\
88 <document source="test data">
89 <sidebar>
90 <paragraph>
91 The title is optional.
92 """],
93 ["""\
94 .. sidebar:: Outer
95
96 .. topic:: Topic
97
98 .. sidebar:: Inner
99
100 text
101 """,
102 """\
103 <document source="test data">
104 <sidebar>
105 <title>
106 Outer
107 <topic>
108 <title>
109 Topic
110 <system_message level="3" line="5" source="test data" type="ERROR">
111 <paragraph>
112 The "sidebar" directive may not be used within topics or body elements.
113 <literal_block xml:space="preserve">
114 .. sidebar:: Inner
115 \n\
116 text
117 """],
118 ]
119
120
121 if __name__ == '__main__':
122 unittest.main()