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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
#!/usr/bin/env python
import unittest
import difflib
import xml2rfc
import xml2rfc.utils
import lxml
import tempfile
import re
def arrstrip(arr):
""" Strip beginning and end blanklines of an array """
if not arr[0]: arr.pop(0)
if not arr[-1]: arr.pop()
return arr
def diff_test(case, valid, test, failpath):
""" Compare two strings. If not equal, fail with a useful diff and save
second string to a file.
"""
# The following 2 lines does the right thing for both python 2.x and 3.x
test = str(test)
valid = str(valid)
# replace the current version with someting static, if present
test = test.replace(xml2rfc.__version__, 'N.N.N')
validarr = [line.rstrip() for line in valid.splitlines()]
testarr = [line.rstrip() for line in test.splitlines()]
if testarr != validarr:
diff = difflib.ndiff(validarr, testarr)
fh = open(failpath, 'w')
fh.write(test.encode('ascii', 'xmlcharrefreplace'))
fh.close()
case.fail("Output doesn't match, file saved to %s.\nDIFF:\n%s" % (failpath, '\n'.join(diff)))
def strip_formatting_whitespace(text):
""" We use non-breaking space, non-breaking hyphen
internally for formattin purposes - they need to be cleaned out
"""
return text.replace(u'\u00A0', ' ').replace(u'\u2011', '-')
class XmlRfcDummy():
""" Dummy rfc used for test fixtures """
def __init__(self):
self.pis = {
"artworkdelimiter": None,
"artworklines": 0 ,
"authorship": "yes",
"autobreaks": "yes",
"background": "" ,
"colonspace": "no" ,
"comments": "no" ,
"docmapping": "no",
"editing": "no",
"emoticonic": "no",
#"footer": Unset
"figurecount": "no",
#"header": Unset
"inline": "no",
"iprnotified": "no",
"linkmailto": "yes",
#"linefile": Unset
#"needLines": Unset
"multiple-initials":"no",
"notedraftinprogress": "yes",
"private": "",
"refparent": "References",
"rfcedstyle": "no",
"rfcprocack": "no",
"sectionorphan": 5,
"slides": "no",
"sortrefs": "yes", # different from default
"strict": "no",
"symrefs": "yes",
"tablecount": "no",
"text-list-symbols": "o*+-",
"toc": "no",
"tocappendix": "yes",
"tocdepth": 3,
"tocindent": "yes",
"tocnarrow": "yes",
"tocompact": "yes",
"topblock": "yes",
#"typeout": Unset
"useobject": "no" ,
}
self.pis["compact"] = self.pis["rfcedstyle"]
self.pis["subcompact"] = self.pis["compact"]
def getroot(self):
return lxml.etree.Element('rfc')
def getpis(self):
return self.pis
def parse_pi(self, pi):
if pi.text:
# Split text in the format 'key="val"'
chunks = re.split(r'=[\'"]([^\'"]*)[\'"]', pi.text)
# Create pairs from this flat list, discard last element if odd
tmp_dict = dict(zip(chunks[::2], chunks[1::2]))
for key, val in tmp_dict.items():
# Update main PI state
self.pis[key] = val
# Return the new values added
output_format = [
{
"ext": "txt",
"spacefix": xml2rfc.utils.formatXmlWhitespace,
"unicodefix": xml2rfc.utils.safeReplaceUnicode,
"writer": xml2rfc.PaginatedTextRfcWriter,
"postprocesslines": "post_process_lines"
},
{
"ext": "raw.txt",
"spacefix": xml2rfc.utils.formatXmlWhitespace,
"unicodefix": xml2rfc.utils.safeReplaceUnicode,
"writer": xml2rfc.RawTextRfcWriter,
"postprocesslines": "post_process_lines"
},
{
"ext": "nroff",
"spacefix": xml2rfc.utils.formatXmlWhitespace,
"unicodefix": xml2rfc.utils.safeReplaceUnicode,
"writer": xml2rfc.NroffRfcWriter,
"postprocesslines": "post_process_lines"
},
{
"ext": "html",
"spacefix": lambda x: x,
"unicodefix": lambda x: x,
"writer": xml2rfc.HtmlRfcWriter,
"postprocesslines": "post_process_lines"
},
{
"ext": "exp.xml",
"spacefix": lambda x: x,
"unicodefix": lambda x: x,
"writer": xml2rfc.HtmlRfcWriter,
"postprocesslines": "post_process_lines"
},
]
class WriterElementTest(unittest.TestCase):
""" Performs tests of isolated XML elements against text writer functions """
def setUp(self):
xml2rfc.log.quiet = True
def function_test(self, name, func_name):
""" Skeleton method for testing an XML element with a writer function """
xmlpath = "tests/input/%s.xml" % name
element = lxml.etree.parse(xmlpath).getroot()
for format in output_format:
ext = format["ext"]
spacefix = format["spacefix"]
unicodefix = format["unicodefix"]
writer = format["writer"](XmlRfcDummy(), quiet=True)
testfunc = getattr(writer, func_name)
postprocessing = getattr(writer, format["postprocesslines"])
#
validpath = "tests/valid/%s.%s" % (name, ext)
try:
fh = open(validpath)
except IOError:
fh = open(validpath, "w+")
valid = fh.read()
fh.close()
#
spacefix(element)
unicodefix(element)
testfunc(element)
output = '\n'.join(arrstrip(postprocessing(writer.buf))) # Don't care about initial blank
diff_test(self, valid, output, validpath.replace('valid', 'failed'))
def test_references(self):
return self.function_test('references', 'write_reference_list')
def test_list_format(self):
return self.function_test("list_format", "write_t_rec")
def test_list_hanging(self):
return self.function_test("list_hanging", "write_t_rec")
def test_list_letters(self):
return self.function_test("list_letters", "write_t_rec")
def test_texttable_small(self):
return self.function_test("texttable_small", "draw_table")
def test_texttable_small_full(self):
return self.function_test("texttable_small_full", "draw_table")
def test_texttable_small_all(self):
return self.function_test("texttable_small_all", "draw_table")
def test_texttable_small_headers(self):
return self.function_test("texttable_small_headers", "draw_table")
def test_texttable_small_none(self):
return self.function_test("texttable_small_none", "draw_table")
def test_texttable_full(self):
return self.function_test("texttable_full", "write_section_rec")
def test_figure_title(self):
return self.function_test("figure_title", "write_t_rec")
def test_texttable_title(self):
return self.function_test("texttable_title", "write_section_rec")
def test_section(self):
return self.function_test("section", "write_section_rec")
def test_textwrap(self):
return self.function_test("textwrap", "write_section_rec")
class WriterRootTest(unittest.TestCase):
""" Performs tests of full <rfc> + <front> trees against text writer functions """
def parse(self, path):
""" Parse a minimal RFC tree and instantiate a writer """
self.parser = xml2rfc.XmlRfcParser(path, quiet=True)
self.xmlrfc = self.parser.parse()
self.writer = xml2rfc.PaginatedTextRfcWriter(self.xmlrfc, quiet=True)
self.writer._format_date()
self.writer.pre_rendering()
def set_root_attrs(self, submissionType, category, consensus):
""" Modify basic attributes on root element for testing """
self.xmlrfc.getroot().attrib['submissionType'] = submissionType
self.xmlrfc.getroot().attrib['category'] = category
self.xmlrfc.getroot().attrib['consensus'] = consensus
def set_valid(self, validpath):
""" Set the output to validate against and the path to fail to """
assert('valid' in validpath)
fh = open(validpath)
self.valid = fh.read()
self.failpath = validpath.replace('valid', 'failed')
fh.close()
def header_footer_test(self):
output = '\n'.join(self.writer._make_footer_and_header(1))
output = strip_formatting_whitespace(output)
diff_test(self, self.valid, output, self.failpath)
def top_test(self):
self.writer.write_top(self.writer._prepare_top_left(),
self.writer._prepare_top_right())
output = '\n'.join(self.writer.buf) # Care about initial blank
output = strip_formatting_whitespace(output)
diff_test(self, self.valid, output, self.failpath)
def status_test(self):
self.writer.write_status_section()
output = '\n'.join(arrstrip(self.writer.buf)) # Don't care about initial blank
output = strip_formatting_whitespace(output)
diff_test(self, self.valid, output, self.failpath)
class WriterDraftTest(WriterRootTest):
""" Test Internet-Draft boilerplate"""
def setUp(self):
self.parse('tests/input/draft_root.xml')
def test_header_footer(self):
self.set_valid('tests/valid/header_footer_draft.txt')
return self.header_footer_test()
def test_top(self):
self.set_valid('tests/valid/top_draft.txt')
return self.top_test()
class WriterRfcTest(WriterRootTest):
""" Test RFC boilerplate """
def setUp(self):
self.parse('tests/input/rfc_root.xml')
def test_header_footer(self):
self.set_valid('tests/valid/header_footer_rfc.txt')
return self.header_footer_test()
def test_top(self):
self.set_valid('tests/valid/top_rfc.txt')
return self.top_test()
def test_status_ietf_std_yes(self):
self.set_root_attrs('IETF', 'std', 'yes')
self.set_valid('tests/valid/status_ietf_std_yes.txt')
return self.status_test()
def test_status_ietf_bcp_yes(self):
self.set_root_attrs('IETF', 'bcp', 'yes')
self.set_valid('tests/valid/status_ietf_bcp_yes.txt')
return self.status_test()
def test_status_ietf_exp_yes(self):
self.set_root_attrs('IETF', 'exp', 'yes')
self.set_valid('tests/valid/status_ietf_exp_yes.txt')
return self.status_test()
def test_status_ietf_exp_yes(self):
self.set_root_attrs('IETF', 'exp', 'no')
self.set_valid('tests/valid/status_ietf_exp_no.txt')
return self.status_test()
def test_status_ietf_historic_no(self):
self.set_root_attrs('IETF', 'historic', 'no')
self.set_valid('tests/valid/status_ietf_historic_no.txt')
return self.status_test()
def test_status_ietf_historic_yes(self):
self.set_root_attrs('IETF', 'historic', 'yes')
self.set_valid('tests/valid/status_ietf_historic_yes.txt')
return self.status_test()
def test_status_ietf_info_yes(self):
self.set_root_attrs('IETF', 'info', 'yes')
self.set_valid('tests/valid/status_ietf_info_yes.txt')
return self.status_test()
def test_status_ietf_info_no(self):
self.set_root_attrs('IETF', 'info', 'no')
self.set_valid('tests/valid/status_ietf_info_no.txt')
return self.status_test()
def test_status_iab_info(self):
self.set_root_attrs('IAB', 'info', 'no')
self.set_valid('tests/valid/status_iab_info.txt')
return self.status_test()
def test_status_iab_historic(self):
self.set_root_attrs('IAB', 'historic', 'no')
self.set_valid('tests/valid/status_iab_historic.txt')
return self.status_test()
def test_status_iab_exp(self):
self.set_root_attrs('IAB', 'exp', 'no')
self.set_valid('tests/valid/status_iab_exp.txt')
return self.status_test()
def test_status_irtf_exp_yes(self):
self.set_root_attrs('IRTF', 'exp', 'yes')
self.set_valid('tests/valid/status_irtf_exp_yes.txt')
return self.status_test()
def test_status_irtf_exp_no(self):
self.set_root_attrs('IRTF', 'exp', 'no')
self.set_valid('tests/valid/status_irtf_exp_no.txt')
return self.status_test()
def test_status_irtf_exp_nowg(self):
self.xmlrfc.getroot().find('front/workgroup').text = ''
self.set_root_attrs('IRTF', 'exp', 'no')
self.set_valid('tests/valid/status_irtf_exp_nowg.txt')
return self.status_test()
def test_status_irtf_historic_yes(self):
self.set_root_attrs('IRTF', 'historic', 'yes')
self.set_valid('tests/valid/status_irtf_historic_yes.txt')
return self.status_test()
def test_status_irtf_historic_no(self):
self.set_root_attrs('IRTF', 'historic', 'no')
self.set_valid('tests/valid/status_irtf_historic_no.txt')
return self.status_test()
def test_status_irtf_historic_nowg(self):
self.xmlrfc.getroot().find('front/workgroup').text = ''
self.set_root_attrs('IRTF', 'historic', 'no')
self.set_valid('tests/valid/status_irtf_historic_nowg.txt')
return self.status_test()
def test_status_irtf_info_yes(self):
self.set_root_attrs('IRTF', 'info', 'yes')
self.set_valid('tests/valid/status_irtf_info_yes.txt')
return self.status_test()
def test_status_irtf_info_no(self):
self.set_root_attrs('IRTF', 'info', 'no')
self.set_valid('tests/valid/status_irtf_info_no.txt')
return self.status_test()
def test_status_irtf_info_nowg(self):
self.xmlrfc.getroot().find('front/workgroup').text = ''
self.set_root_attrs('IRTF', 'info', 'no')
self.set_valid('tests/valid/status_irtf_info_nowg.txt')
return self.status_test()
def test_status_independent_info(self):
self.set_root_attrs('independent', 'info', 'no')
self.set_valid('tests/valid/status_independent_info.txt')
return self.status_test()
def test_status_independent_historic(self):
self.set_root_attrs('independent', 'historic', 'no')
self.set_valid('tests/valid/status_independent_historic.txt')
return self.status_test()
def test_status_independent_exp(self):
self.set_root_attrs('independent', 'exp', 'no')
self.set_valid('tests/valid/status_independent_exp.txt')
return self.status_test()
if __name__ == '__main__':
unittest.main()
|