Debian Codegraph @

test/test_parsers/test_recommonmark/test_targets.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # :Copyright: © 2020 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 Test for targets in CommonMark parsers.
13 Cf. the `CommonMark Specification <https://spec.commonmark.org/>`__
14 """
15
16 from pathlib import Path
17 import sys
18 import unittest
19
20 if __name__ == '__main__':
21 # prepend the "docutils root" to the Python library path
22 # so we import the local `docutils` package.
23 sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
24
25
26 from docutils.frontend import get_default_settings
27 from docutils.parsers.recommonmark_wrapper import Parser
28 from docutils.utils import new_document
29
30
31 class RecommonmarkParserTestCase(unittest.TestCase):
32 def test_parser(self):
33 parser = Parser()
34 settings = get_default_settings(Parser)
35 for name, cases in totest.items():
36 for casenum, (case_input, case_expected) in enumerate(cases):
37 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
38 document = new_document('test data', settings.copy())
39 parser.parse(case_input, document)
40 output = document.pformat()
41 self.assertEqual(case_expected, output)
42
43
44 totest = {}
45
46 totest['targets'] = [
47 [r"""
48 External hyperlink [target]s:
49
50 [target]: http://www.python.org/
51 """,
52 """\
53 <document source="test data">
54 <paragraph>
55 External hyperlink \n\
56 <reference refuri="http://www.python.org/">
57 target
58 s:
59 """],
60 ["""\
61 Indirect hyperlink [target]s:
62
63 [target]: target2
64
65 [target2]: /url
66 """,
67 """\
68 <document source="test data">
69 <paragraph>
70 Indirect hyperlink \n\
71 <reference name="target" refuri="target2">
72 target
73 s:
74 """],
75 ["""\
76 Duplicate external [targets] (different URIs):
77
78 [targets]: <first wins>
79 [targets]: second
80 """,
81 """\
82 <document source="test data">
83 <paragraph>
84 Duplicate external \n\
85 <reference name="targets" refuri="first%20wins">
86 targets
87 (different URIs):
88 """],
89 ["""\
90 Duplicate external [targets] (same URIs):
91
92 [targets]: spam
93 [targets]: spam
94 """,
95 """\
96 <document source="test data">
97 <paragraph>
98 Duplicate external \n\
99 <reference name="targets" refuri="spam">
100 targets
101 (same URIs):
102 """],
103 ["""\
104 Duplicate implicit targets.
105
106 Title
107 =====
108
109 Paragraph.
110
111 Title
112 =====
113
114 Paragraph.
115 """,
116 """\
117 <document source="test data">
118 <paragraph>
119 Duplicate implicit targets.
120 <section dupnames="title" ids="title">
121 <title>
122 Title
123 <paragraph>
124 Paragraph.
125 <section dupnames="title" ids="title-1">
126 <title>
127 Title
128 <system_message backrefs="title-1" level="1" line="8" source="test data" type="INFO">
129 <paragraph>
130 Duplicate implicit target name: "title".
131 <paragraph>
132 Paragraph.
133 """],
134 ["""\
135 Duplicate implicit/explicit targets.
136
137 Title
138 =====
139
140 [title]: hoppla
141
142 Paragraph with link to [title].
143 """,
144 """\
145 <document source="test data">
146 <paragraph>
147 Duplicate implicit/explicit targets.
148 <section ids="title" names="title">
149 <title>
150 Title
151 <paragraph>
152 Paragraph with link to \n\
153 <reference name="title" refuri="hoppla">
154 title
155 .
156 """],
157 ]
158
159
160 if __name__ == '__main__':
161 unittest.main()