#! /usr/bin/env python3 # $Id: test_field_lists.py 9425 2023-06-30 14:56:47Z milde $ # Author: David Goodger # Copyright: This module has been placed in the public domain. """ Tests for states.py. """ from pathlib import Path import sys import unittest if __name__ == '__main__': # prepend the "docutils root" to the Python library path # so we import the local `docutils` package. sys.path.insert(0, str(Path(__file__).resolve().parents[3])) from docutils.frontend import get_default_settings from docutils.parsers.rst import Parser from docutils.utils import new_document class ParserTestCase(unittest.TestCase): def test_parser(self): parser = Parser() settings = get_default_settings(Parser) settings.warning_stream = '' for name, cases in totest.items(): for casenum, (case_input, case_expected) in enumerate(cases): with self.subTest(id=f'totest[{name!r}][{casenum}]'): document = new_document('test data', settings.copy()) parser.parse(case_input, document) output = document.pformat() self.assertEqual(case_expected, output) totest = {} totest['field_lists'] = [ ["""\ One-liners: :Author: Me :Version: 1 :Date: 2001-08-11 :Parameter i: integer """, """\ One-liners: Author Me Version 1 Date 2001-08-11 Parameter i integer """], ["""\ One-liners, no blank lines: :Author: Me :Version: 1 :Date: 2001-08-11 :Parameter i: integer """, """\ One-liners, no blank lines: Author Me Version 1 Date 2001-08-11 Parameter i integer """], ["""\ :field: empty item above, no blank line """, """\ field Field list ends without a blank line; unexpected unindent. empty item above, no blank line """], ["""\ Field bodies starting on the next line: :Author: Me :Version: 1 :Date: 2001-08-11 :Parameter i: integer """, """\ Field bodies starting on the next line: Author Me Version 1 Date 2001-08-11 Parameter i integer """], ["""\ One-paragraph, multi-liners: :Authors: Me, Myself, and I :Version: 1 or so :Date: 2001-08-11 (Saturday) :Parameter i: counter (integer) """, """\ One-paragraph, multi-liners: Authors Me, Myself, and I Version 1 or so Date 2001-08-11 (Saturday) Parameter i counter (integer) """], ["""\ One-paragraph, multi-liners, not lined up: :Authors: Me, Myself, and I :Version: 1 or so :Date: 2001-08-11 (Saturday) :Parameter i: counter (integer) """, """\ One-paragraph, multi-liners, not lined up: Authors Me, Myself, and I Version 1 or so Date 2001-08-11 (Saturday) Parameter i counter (integer) """], ["""\ Multiple body elements: :Authors: - Me - Myself - I :Abstract: This is a field list item's body, containing multiple elements. Here's a literal block:: def f(x): return x**2 + x Even nested field lists are possible: :Date: 2001-08-11 :Day: Saturday :Time: 15:07 """, """\ Multiple body elements: Authors Me Myself I Abstract This is a field list item's body, containing multiple elements. Here's a literal block: def f(x): return x**2 + x Even nested field lists are possible: Date 2001-08-11 Day Saturday Time 15:07 """], ["""\ Nested field lists on one line: :field1: :field2: :field3: body :field4: :field5: :field6: body :field7: body :field8: body :field9: body line 1 body line 2 """, """\ Nested field lists on one line: field1 field2 field3 body field4 field5 field6 body field7 body field8 body field9 body line 1 body line 2 """], ["""\ :Parameter i j k: multiple arguments """, """\ Parameter i j k multiple arguments """], ["""\ :Field *name* `with` **inline** ``markup``: inline markup in field name is parsed. """, """\ Field \n\ name \n\ with \n\ inline \n\ markup inline markup in field name is parsed. """], ["""\ :Field name with *bad inline markup: should generate warning. """, """\ Field name with \n\ * bad inline markup Inline emphasis start-string without end-string. should generate warning. """], [r"""Some edge cases: :Empty: :Author: Me No blank line before this paragraph. : Field: marker must not begin with whitespace. :Field : marker must not end with whitespace. Field: marker is missing its open-colon. :Field marker is missing its close-colon. :Field\: names\: with\: colons\:: are possible. :\\Field\ names with backslashes\\: are possible, too. :\\: A backslash. :Not a\\\: field list. :Not a \: field list either. :\: Not a field list either. :\: A definition list, not a field list. """, """\ Some edge cases: Empty Author Me Field list ends without a blank line; unexpected unindent. No blank line before this paragraph. : Field: marker must not begin with whitespace. :Field : marker must not end with whitespace. Field: marker is missing its open-colon. :Field marker is missing its close-colon. Field: names: with: colons: are possible. \\Field names with backslashes\\ are possible, too. \\ A backslash. :Not a\\: field list. :Not a : field list either. :: Not a field list either. :: A definition list, not a field list. """], [r""" :first: field :field:name:with:embedded:colons: unambiguous, no need for escapes .. :embedded:colons: in first field name :field:\`:name: not interpreted text :field:\`name: not interpreted text """, """\ first field field:name:with:embedded:colons unambiguous, no need for escapes embedded:colons in first field name field:`:name not interpreted text field:`name not interpreted text """], [r""" Edge cases involving embedded colons and interpreted text. Recognized as field list items: :field\:`name`: interpreted text (standard role) requires escaping a leading colon in a field name :field:name: unambiguous, no need for escapes :field::name: double colons are OK, too :field:\`name`: not interpreted text :`field name`:code:: interpreted text with role in the field name works only when the role follows the text :a `complex`:code:\ field name: field body Not recognized as field list items: ::code:`not a field name`: paragraph with interpreted text :code:`not a field name`: paragraph with interpreted text """, """\ Edge cases involving embedded colons and interpreted text. Recognized as field list items: field: name interpreted text (standard role) requires escaping a leading colon in a field name field:name unambiguous, no need for escapes field::name double colons are OK, too field:`name` not interpreted text field name interpreted text with role in the field name works only when the role follows the text a \n\ complex field name field body Not recognized as field list items: : not a field name : paragraph with interpreted text not a field name : paragraph with interpreted text """], ] if __name__ == '__main__': unittest.main()