Debian Codegraph @

python-scripts/getentityname/tests/test_script.py

raw
1 import re
2 import pytest
3
4 # "gen" is the abbreviated name for "getentityname.py"
5 import gen
6
7
8 def test_should_raise_valueerror_in_rm_xml_comment_with_missing_closecomment():
9 with pytest.raises(ValueError, match=".* Missing '-->' .*"):
10 gen.remove_xml_comments("<!-- Wrong XML comment")
11
12
13 def test_should_raise_valueerror_in_rm_xml_comment_with_doubledashes():
14 with pytest.raises(ValueError):
15 gen.remove_xml_comments("<!-- Not--allowed-->")
16
17
18 @pytest.mark.parametrize("space", [
19 # space
20 " ",
21 # tab
22 "\t",
23 # newline
24 "\n",
25 # return
26 "\r"
27 ])
28 def test_regex_space(space):
29 assert re.search(gen.SPACE, space)
30
31
32 @pytest.mark.parametrize("name", [
33 "a",
34 "A",
35 "abc",
36 "_",
37 "_ab",
38 ":",
39 ":foo",
40 "a:foo",
41 ])
42 def test_regex_name(name):
43 assert re.search(gen.NAME, name)
44
45
46 #@pytest.mark.parametrize("qstr", [
47 #"'a'",
48 #'"a"',
49 #"'abc.def'",
50 #"'http://example.org/foo'",
51 #"'http://example.org/foo%20bar#xyz'",
52 #"'http://example.org/foo%20\"bar#xyz'",
53 #])
54 #def test_regex_qstr(qstr):
55 #assert re.search(gen.QSTR, qstr)
56
57
58 @pytest.mark.parametrize("sysid", [
59 "'a'",
60 '"a"',
61 "'abc.def'",
62 "'http://example.org/foo'",
63 "'http://example.org/foo%20bar#xyz'",
64 "'http://example.org/foo%20\"bar#xyz'",
65 ])
66 def test_regex_systemliteral(sysid):
67 assert re.search(gen.SYSTEMLITERAL, sysid)
68
69
70 @pytest.mark.parametrize("pubid", [
71 "'-//OASIS//DTD DocBook XML V4.5//EN'",
72 '"-//OASIS//DTD DocBook XML V4.5//EN"',
73 "'ISO 8879:1986//ENTITIES Added Math Symbols: Arrow Relations//EN//XML'",
74 '"-//W3C//ENTITIES Extra for MathML 2.0//EN"',
75 '"urn:oasis:names:tc:entity:xmlns:xml:catalog"',
76 '"-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"',
77 '"http://www.w3.org/Graphics/SVG/1.1/rng/svg11.rng"',
78 ])
79 def test_regex_publicliteral(pubid):
80 assert re.search(gen.PUBLICLITERAL, pubid)
81
82
83 @pytest.mark.parametrize("extid", [
84 "SYSTEM 'foo.dtd'",
85 'SYSTEM "foo.dtd"',
86 'SYSTEM\n"foo.dtd"',
87 'SYSTEM\t"foo.dtd"',
88 '''PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"''',
89 '''PUBLIC\n"-//OASIS//DTD DocBook XML V4.5//EN"\n"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"''',
90 ])
91 def test_regex_externalid(extid):
92 assert re.search(gen.EXTERNALID, extid, re.DOTALL|re.VERBOSE|re.MULTILINE)
93
94
95 @pytest.mark.parametrize("entity, expected", [
96 # no. 1
97 ("""<!ENTITY % ents SYSTEM "entities.ent">""",
98 {'PEDecl': 'ents', 'pubid': None, 'sysid': '"entities.ent"'}
99 ),
100 # no. 2
101 ("""<!ENTITY\n% ents\nSYSTEM\n"entities.ent"\n >""",
102 {'PEDecl': 'ents', 'pubid': None, 'sysid': '"entities.ent"'}
103 ),
104 # no. 3
105 ("""<!ENTITY % ents PUBLIC "urn:x-test:tom" "http://www.example.org/ent">""",
106 {'PEDecl': 'ents',
107 'pubid': '"urn:x-test:tom"',
108 'sysid': '"http://www.example.org/ent"'}
109 ),
110 # no. 4
111 ("""<!ENTITY\n%\n\tents PUBLIC\t"urn:x-test:tom"\r "http://www.example.org/ent"\n >""",
112 {'PEDecl': 'ents',
113 'pubid': '"urn:x-test:tom"',
114 'sysid': '"http://www.example.org/ent"'}
115 ),
116 # no. 5
117 ("""<!ENTITY % phrases-entities SYSTEM "phrases-decl.ent">""",
118 {'PEDecl': 'phrases-entities',
119 'pubid': None,
120 'sysid': '"phrases-decl.ent"'}
121 ),
122 ])
123 def test_regex_entity(entity, expected):
124 match = re.search(gen.ENTITY, entity, re.DOTALL|re.VERBOSE|re.MULTILINE)
125 assert match
126 resultdict = match.groupdict()
127 resultdict.pop("quote", None)
128 assert resultdict == expected
129
130
131
132 DOCTYPE_TEST_DATA = (
133 # ID-Name, Data, Expected
134 # no. 1
135 ("normal",
136 "<!DOCTYPE chapter>",
137 dict(Name='chapter', pubid=None, sysid=None)
138 ),
139
140 # no. 2
141 ("normal-with-linebreaks",
142 "<!DOCTYPE\nchapter \n>",
143 dict(Name='chapter', pubid=None, sysid=None)
144 ),
145
146 # no. 3
147 ("with-empty-intsubset",
148 "<!DOCTYPE\nchapter []\n>",
149 dict(Name='chapter', pubid=None, sysid=None)
150 ),
151
152 # no. 4
153 ("with-intsubset-oneline",
154 """
155 <!DOCTYPE chapter [<!ENTITY % ents SYSTEM "entities.ent">]>""",
156 dict(Name='chapter', pubid=None, sysid=None)
157 ),
158
159 # no. 5
160 ("with-linebreaks",
161 """
162 <!DOCTYPE
163 chapter
164 [
165 <!ENTITY % ents SYSTEM "entities.ent">
166 ]
167
168 >""",
169 dict(Name='chapter', pubid=None, sysid=None)
170 ),
171
172 # no. 6
173 ("with-xml-declaration",
174 """<?xml version="1.0"?>
175 <!DOCTYPE chapter
176 [
177 <!ENTITY % ents SYSTEM "entities.ent">
178 ]>
179 """,
180 dict(Name='chapter', pubid=None, sysid=None)
181 ),
182
183 # no. 7
184 ("with-xml-declaration",
185 """<?xml version="1.0"?>
186 <!DOCTYPE chapter SYSTEM "foo.dtd"
187 [
188 <!ENTITY % ents SYSTEM "entities.ent">
189 ]>
190 """,
191 dict(Name='chapter',
192 pubid=None,
193 sysid='"foo.dtd"')
194 ),
195
196 # no. 8
197 ("with-public-identifier",
198 """<!DOCTYPE chapter PUBLIC
199 "-//OASIS//DTD DocBook XML V4.5//EN"
200 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
201 [
202 <!ENTITY % ents SYSTEM "entities.ent">
203 ]>
204 """,
205 dict(Name='chapter',
206 pubid='"-//OASIS//DTD DocBook XML V4.5//EN"',
207 sysid='"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"')
208 ),
209
210 # no. 9
211 ("with-public-identifier",
212 """<!DOCTYPE chapter
213 PUBLIC
214 "-//OASIS//DTD DocBook XML V4.5//EN"
215 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
216 [
217 <!ENTITY % ents SYSTEM "entities.ent">
218 ]
219 >""",
220 dict(Name='chapter',
221 pubid='"-//OASIS//DTD DocBook XML V4.5//EN"',
222 sysid='"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"')
223 ),
224
225 # no. 10
226 ("with-comment-before-doctype",
227 """<!--
228 A very
229
230 long
231
232 XML
233
234 comment
235
236 -->
237 <!DOCTYPE chapter
238 [
239 <!ENTITY % ents SYSTEM "entities.ent">
240 ]>
241 """,
242 dict(Name='chapter', pubid=None, sysid=None)
243 ),
244
245 # no. 11
246
247 )
248
249
250 @pytest.mark.parametrize("header, expected",
251 [(data, expected) for _, data, expected in DOCTYPE_TEST_DATA],
252 ids=[name for name, _, _ in DOCTYPE_TEST_DATA]
253 )
254 def test_regex_doctype(header, expected):
255 # match = re.search(gen.DOCTYPE, header, re.VERBOSE|re.DOTALL|re.MULTILINE)
256 match = gen.r_DOCTYPE.search(header)
257 assert match
258 resultdict = match.groupdict()
259 resultdict.pop('IntSubset', None)
260 resultdict.pop("quote", None)
261 assert resultdict == expected
262
263
264 ENTITIES_TEST_DATA = (
265 # ID-Name, Data, Expected
266 # no. 1
267 ("system",
268 """<!ENTITY % ent SYSTEM "foo.ent">""",
269 {'PEDecl': 'ent',
270 'sysid': '"foo.ent"',
271 'pubid': None}
272 ),
273 # no. 2
274 ("public",
275 """<!ENTITY % ent PUBLIC "-//TEST//ENTITIES V1.0//EN" "http://example.org/foo.ent">""",
276 {'PEDecl': 'ent',
277 'pubid': '"-//TEST//ENTITIES V1.0//EN"',
278 'sysid': '"http://example.org/foo.ent"',
279 }
280 ),
281 # no. 3
282 ("with-linebreaks",
283 """
284 <!ENTITY % here
285 SYSTEM
286 "here.ent">
287 """,
288 {'PEDecl': 'here',
289 'sysid': '"here.ent"',
290 'pubid': None
291 }
292 ),
293 )
294
295 @pytest.mark.parametrize("ent, expected",
296 [(data, expected) for _, data, expected in ENTITIES_TEST_DATA],
297 ids=[name for name, _, _ in ENTITIES_TEST_DATA]
298 )
299 def test_should_match_entities(ent, expected):
300 match = gen.r_ENTITY.search(ent)
301 assert match
302 # Remove quotes
303 # print(">>> match:", match.groupdict())
304 resultdict = match.groupdict()
305 resultdict.pop("quote", None)
306 assert resultdict == expected
307
308
309 def test_joinents():
310 assert gen.joinEnts(["a", "b"], "-") == "a-b"
311