#!/usr/bin/env python3 # :Copyright: © 2020 Günter Milde. # :License: Released under the terms of the `2-Clause BSD license`_, in short: # # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. # This file is offered as-is, without any warranty. # # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause """ Test for enumerated lists in CommonMark parsers Cf. the `CommonMark Specification `__ """ 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.recommonmark_wrapper import Parser from docutils.utils import new_document class RecommonmarkParserTestCase(unittest.TestCase): def test_parser(self): parser = Parser() settings = get_default_settings(Parser) 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['enumerated_lists'] = [ ["""\ 1. Item one. 2. Item two. 3. Item three. """, """\ Item one. Item two. Item three. """], ["""\ No blank lines betwen items: 1. Item one. 2. Item two. 3. Item three. """, """\ No blank lines betwen items: Item one. Item two. Item three. """], ["""\ 1. Content may start at the next line if it's indented at least 3 spaces. """, """\ Content may start at the next line if it's indented at least 3 spaces. """], ["""\ 1. empty item above, no blank line, no indent """, """\ empty item above, no blank line, no indent """], ["""\ Items are auto-numbered. No check for consistency: Skipping item 3 1. Item 1. 2. Item 2. 4. Item 4. """, """\ Items are auto-numbered. No check for consistency: Skipping item 3 Item 1. Item 2. Item 4. """], ["""\ No warning when starting with non-ordinal-1: 0. Item zero. 1. Item one. 2. Item two. 3. Item three. And again: 2. Item two. 3. Item three. """, """\ No warning when starting with non-ordinal-1: Item zero. Item one. Item two. Item three. And again: Item two. Item three. """], ["""\ 1. Item one: line 1, line 2. 2. Item two: line 1, line 2. 3. Item three: paragraph 1, line 1, line 2. Paragraph 2. """, """\ Item one: line 1, line 2. Item two: line 1, line 2. Item three: paragraph 1, line 1, line 2. Paragraph 2. """], ["""\ Supported enumeration sequences: 1. Item 1. 2. Item 2. 1) Item 1) 2) Item 2) """, """\ Supported enumeration sequences: Item 1. Item 2. Item 1) Item 2) """], ["""\ Nested enumerated lists: 1. Item 1 1) Item 1.1 2) Item 1.2 3) Item 1.3 2. Item 2 1. Item 2.1 1) Item 2.1.1 2) Item 2.1.2 3) Item 2.1.3 2. Item 2.2 3. Item 2.3 3. Item 3. """, """\ Nested enumerated lists: Item 1 Item 1.1 Item 1.2 Item 1.3 Item 2 Item 2.1 Item 2.1.1 Item 2.1.2 Item 2.1.3 Item 2.2 Item 2.3 Item 3. """], ["""\ 1. Item one: line 1, line 2. 2. Item two: line 1, line 2. 3. Item three: paragraph 1, line 1, line 2. Item three: paragraph 2. """, """\ Item one: line 1, line 2. Item two: line 1, line 2. Item three: paragraph 1, line 1, line 2. Item three: paragraph 2. """], ["""\ 3-space indent, with a trailing space: 1. \n\ list item 1 3-space indent, no trailing space: 1. list item 1 2-space indent, empty list item: 1. foo 1-space indent, empty list item: 1. foo 0-space indent, empty list item: 1. foo No item content: 1. """, """\ 3-space indent, with a trailing space: list item 1 3-space indent, no trailing space: list item 1 2-space indent, empty list item: foo 1-space indent, empty list item: foo 0-space indent, empty list item: foo No item content: """], ] if __name__ == '__main__': unittest.main()