Debian Codegraph @

test/test_parsers/test_get_parser_class.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_get_parser_class.py 9966 2024-10-22 21:41:26Z milde $
4 # Author: grubert abadger1999
5 # Maintainer: [email protected]
6 # Copyright: This module has been placed in the public domain.
7
8 """test `docutils.parsers.get_parser_class()`"""
9
10 from pathlib import Path
11 import sys
12 import unittest
13
14 # Prepend the "docutils root" to the Python library path
15 # so we import the local `docutils` and `test` packages.
16 # ensure `test` package can be loaded also if not running as __main__
17 # (required by ``python -m unittest``
18 DOCUTILS_ROOT = Path(__file__).resolve().parents[2]
19 if str(DOCUTILS_ROOT) not in sys.path:
20 sys.path.insert(0, str(DOCUTILS_ROOT))
21
22 from docutils.core import publish_string # noqa: E402
23 from docutils.parsers import get_parser_class # noqa: E402
24 try:
25 md_parser_class = get_parser_class('recommonmark')
26 except ImportError:
27 md_parser_class = None
28
29
30 class GetParserClassTestCase(unittest.TestCase):
31
32 def test_registered_parsers(self):
33 get_parser_class('null')
34 get_parser_class('rst')
35 get_parser_class('docutils_xml')
36 # raises ImportError on failure
37
38 def test_registered_parsers_case_folding(self):
39 get_parser_class('reStructuredText')
40 get_parser_class('XML')
41 # raises ImportError on failure
42
43 def test_bogus_parser(self):
44 with self.assertRaises(ImportError):
45 get_parser_class('nope')
46
47 def test_local_parser(self):
48 # requires local-parser.py in "test root" directory
49 get_parser_class('test.local-parser')
50 # raises ImportError on failure
51
52
53 @unittest.skipUnless(md_parser_class is None, '"recommonmark" module found.')
54 class RecommonmarkMissingTests(unittest.TestCase):
55
56 def test_missing_parser_message(self):
57 # match multiline message (?s) = re.DOTALL "." also matches newline
58 with self.assertRaisesRegex(
59 ImportError, '(?s)requires the.*package .*recommonmark'):
60 publish_string('test data', parser='recommonmark')
61
62
63 if __name__ == '__main__':
64 unittest.main()