Debian Codegraph @

test/test_parsers/test_docutils_xml/test_misc.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # :Copyright: © 2024 Günter Milde.
3 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
4 #
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved.
8 # This file is offered as-is, without any warranty.
9 #
10 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
11
12 """Various tests for the XML parser.
13
14 Test parsing + transformations with `publish_string()`.
15 """
16
17 from pathlib import Path
18 import sys
19 import unittest
20
21 if __name__ == '__main__':
22 # prepend the "docutils root" to the Python library path
23 # so we import the local `docutils` package.
24 sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
25
26 from docutils.core import publish_string
27 from docutils.parsers import docutils_xml
28
29
30 class XMLParserTests(unittest.TestCase):
31 maxDiff = None
32
33 mysettings = {'_disable_config': True,
34 'output_encoding': 'unicode',
35 'warning_stream': '',
36 }
37
38 def test_publish(self):
39 for name, (settings, cases) in totest.items():
40 settings = self.mysettings | settings
41 for casenum, (case_input, case_expected) in enumerate(cases):
42 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
43 output = publish_string(case_input,
44 parser=docutils_xml.Parser(),
45 settings_overrides=settings)
46 self.assertEqual(case_expected, output)
47
48
49 totest = {}
50
51 totest['decoration'] = ({'datestamp': 'pi-day'}, # generate footer
52 [
53 ["""\
54 <decoration>
55 <footer>
56 <paragraph>myfooter</paragraph>
57 </footer>
58 </decoration>
59 """,
60 """\
61 <document source="<string>">
62 <decoration>
63 <footer>
64 <paragraph>
65 myfooter
66 <paragraph>
67 Generated on: pi-day.
68 """],
69 ])
70
71 totest['hyperlinks'] = ({}, # resolve hyperlinks
72 [
73 ["""\
74 <document source="test data">
75 <paragraph>A <reference anonymous="1" name="link">link</reference> to Docutils.</paragraph>
76 <target anonymous="1" ids="target-1" refuri="http://docutils.sourceforge.io"/>
77 </document>
78 """,
79 """\
80 <document source="test data">
81 <paragraph>
82 A \n\
83 <reference anonymous="1" name="link" refuri="http://docutils.sourceforge.io">
84 link
85 to Docutils.
86 <target anonymous="1" ids="target-1" refuri="http://docutils.sourceforge.io">
87 """],
88 # duplicate ids are an error
89 ["""\
90 <tip ids="i1 i2">
91 <paragraph><strong ids="i2 i3"></strong></paragraph>
92 </tip>
93 """,
94 """\
95 <document source="<string>">
96 <tip ids="i1 i2">
97 <paragraph>
98 <strong ids="i2 i3">
99 <section classes="system-messages">
100 <title>
101 Docutils System Messages
102 <system_message level="3" line="2" source="<string>" type="ERROR">
103 <paragraph>
104 Duplicate ID: "i2" used by <tip ids="i1 i2"> and <strong ids="i2 i3">
105 """],
106 ])
107
108 totest['substitutions'] = ({}, # resolve substitutions
109 [
110 ["""\
111 <document source="test data">
112 <substitution_definition ltrim="0" names="holla">Mara</substitution_definition>
113 <paragraph>Das ist
114 <substitution_reference refname="Holla">Holla</substitution_reference>
115 die Waldfee</paragraph>
116 </document>
117 """,
118 """\
119 <document source="test data">
120 <substitution_definition ltrim="0" names="holla">
121 Mara
122 <paragraph>
123 Das ist
124 Mara
125 \n\
126 die Waldfee
127 """],
128 ])
129
130 if __name__ == '__main__':
131 unittest.main()