From 222916faae554ef0eed40ea6cfa306bc8d0d37f0 Mon Sep 17 00:00:00 2001 From: Kesara Rathnayake Date: Mon, 19 Feb 2024 17:17:34 +1300 Subject: [PATCH] test: Add unit test for annotations Add unit tests for annotations in xml2rfc.TextWriter.render_reference method. --- test.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/test.py b/test.py index 2d1afc00..f486424a 100644 --- a/test.py +++ b/test.py @@ -11,6 +11,7 @@ from xml2rfc.boilerplate_rfc_7841 import boilerplate_rfc_status_of_memo from xml2rfc.walkpdf import xmldoc from xml2rfc.writers.base import default_options +from xml2rfc.writers.text import MAX_WIDTH try: from xml2rfc import debug @@ -635,6 +636,67 @@ def test_boilerplate_insert_status_of_memo_editorial(self): target = 'https://www.rfc-editor.org/info/rfc9280' self.assertEqual(target, rfc.xpath('./section/t/eref')[0].get('target')) +class TextWriterTest(unittest.TestCase): + '''TextWriter tests''' + + def setUp(self): + xml2rfc.log.quiet = True + path = 'tests/input/elements.xml' + self.parser = xml2rfc.XmlRfcParser(path, + quiet=True, + options=default_options, + **options_for_xmlrfcparser) + self.xmlrfc = self.parser.parse() + self.writer = xml2rfc.TextWriter(self.xmlrfc, quiet=True) + + def test_render_reference(self): + # test annotations + reference = ''' + + + + Reference Test + + ACMI Corp. + + + + {annotation} + +''' + self.writer.refname_mapping['REFTEST'] = 'REFTEST' + + # single line annotation + annotation = 'foobar' + references = lxml.etree.fromstring(reference.format(annotation=annotation)) + lines = self.writer.render_reference(references.getchildren()[0], width=60) + self.assertEqual(len(lines), 1) + self.assertIn(annotation, lines[0].text) + + # multi line annotation + annotation = '''Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. + Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.''' + references = lxml.etree.fromstring(reference.format(annotation=annotation)) + lines = self.writer.render_reference(references.getchildren()[0], width=60) + self.assertGreater(len(lines), 1) + self.assertIn(annotation[:5], lines[0].text) + + # single line annotation (larger than width and smaller than MAX_WIDTH) + annotation = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commo' + references = lxml.etree.fromstring(reference.format(annotation=annotation)) + lines = self.writer.render_reference(references.getchildren()[0], width=len(annotation)-5) + self.assertGreater(MAX_WIDTH, len(annotation)) + self.assertGreater(len(lines), 1) + self.assertIn(annotation[:5], lines[0].text) + + # annotation with URL + url = 'https://example.org' + annotation = f'Example: ' + references = lxml.etree.fromstring(reference.format(annotation=annotation)) + lines = self.writer.render_reference(references.getchildren()[0], width=60) + self.assertEqual(len(lines), 2) + self.assertIn(url, lines[1].text) + if __name__ == '__main__': unittest.main()