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