Debian Codegraph @

test/test_parsers/test_rst/test_citations.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_citations.py 9781 2024-07-28 10:50:23Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for states.py.
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[3]))
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 def test_parser(self):
27 parser = Parser()
28 settings = get_default_settings(Parser)
29 settings.warning_stream = ''
30 for name, cases in totest.items():
31 for casenum, (case_input, case_expected) in enumerate(cases):
32 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
33 document = new_document('test data', settings.copy())
34 parser.parse(case_input, document)
35 output = document.pformat()
36 self.assertEqual(case_expected, output)
37
38
39 totest = {}
40
41 totest['citations'] = [
42 ["""\
43 .. [citation] This is a citation.
44 """,
45 """\
46 <document source="test data">
47 <citation ids="citation" names="citation">
48 <label>
49 citation
50 <paragraph>
51 This is a citation.
52 """],
53 ["""\
54 .. [citation1234] This is a citation with year.
55 """,
56 """\
57 <document source="test data">
58 <citation ids="citation1234" names="citation1234">
59 <label>
60 citation1234
61 <paragraph>
62 This is a citation with year.
63 """],
64 ["""\
65 .. [citation] This is a citation
66 on multiple lines.
67 """,
68 """\
69 <document source="test data">
70 <citation ids="citation" names="citation">
71 <label>
72 citation
73 <paragraph>
74 This is a citation
75 on multiple lines.
76 """],
77 ["""\
78 .. [citation1] This is a citation
79 on multiple lines with more space.
80
81 .. [citation2] This is a citation
82 on multiple lines with less space.
83 """,
84 """\
85 <document source="test data">
86 <citation ids="citation1" names="citation1">
87 <label>
88 citation1
89 <paragraph>
90 This is a citation
91 on multiple lines with more space.
92 <citation ids="citation2" names="citation2">
93 <label>
94 citation2
95 <paragraph>
96 This is a citation
97 on multiple lines with less space.
98 """],
99 ["""\
100 .. [citation]
101 This is a citation on multiple lines
102 whose block starts on line 2.
103 """,
104 """\
105 <document source="test data">
106 <citation ids="citation" names="citation">
107 <label>
108 citation
109 <paragraph>
110 This is a citation on multiple lines
111 whose block starts on line 2.
112 """],
113 ["""\
114 .. [citation]
115
116 That was an empty citation.
117 """,
118 """\
119 <document source="test data">
120 <citation ids="citation" names="citation">
121 <label>
122 citation
123 <system_message level="2" line="2" source="test data" type="WARNING">
124 <paragraph>
125 Citation content expected.
126 <paragraph>
127 That was an empty citation.
128 """],
129 ["""\
130 .. [citation] The Source
131 No blank line.
132 """,
133 """\
134 <document source="test data">
135 <citation ids="citation" names="citation">
136 <label>
137 citation
138 <paragraph>
139 The Source
140 <system_message level="2" line="2" source="test data" type="WARNING">
141 <paragraph>
142 Explicit markup ends without a blank line; unexpected unindent.
143 <paragraph>
144 No blank line.
145 """],
146 ["""\
147 .. [citation label with spaces] this isn't a citation
148
149 .. [*citationlabelwithmarkup*] this isn't a citation
150 """,
151 """\
152 <document source="test data">
153 <comment xml:space="preserve">
154 [citation label with spaces] this isn't a citation
155 <comment xml:space="preserve">
156 [*citationlabelwithmarkup*] this isn't a citation
157 """],
158 ["""
159 isolated internals : ``.-_``.
160
161 .. [citation.withdot] one dot
162
163 .. [citation-withdot] one hyphen
164
165 .. [citation_withunderscore] one underscore
166
167 .. [citation:with:colons] two colons
168
169 .. [citation+withplus] one plus
170 """,
171 """<document source="test data">
172 <paragraph>
173 isolated internals : \n\
174 <literal>
175 .-_
176 .
177 <citation ids="citation-withdot" names="citation.withdot">
178 <label>
179 citation.withdot
180 <paragraph>
181 one dot
182 <citation ids="citation-withdot-1" names="citation-withdot">
183 <label>
184 citation-withdot
185 <paragraph>
186 one hyphen
187 <citation ids="citation-withunderscore" names="citation_withunderscore">
188 <label>
189 citation_withunderscore
190 <paragraph>
191 one underscore
192 <citation ids="citation-with-colons" names="citation:with:colons">
193 <label>
194 citation:with:colons
195 <paragraph>
196 two colons
197 <citation ids="citation-withplus" names="citation+withplus">
198 <label>
199 citation+withplus
200 <paragraph>
201 one plus
202 """],
203 ]
204
205
206 if __name__ == '__main__':
207 unittest.main()