Debian Codegraph @

test/test_parsers/test_rst/test_directives/test_code_parsing.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # :Copyright: © 2020 Günter Milde.
3 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
4 #
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved.
8 # This file is offered as-is, without any warranty.
9 #
10 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
11
12 """
13 Various tests for the `pygments` code highlighter.
14 """
15
16 from pathlib import Path
17 import sys
18 import unittest
19
20 if __name__ == '__main__':
21 # prepend the "docutils root" to the Python library path
22 # so we import the local `docutils` package.
23 sys.path.insert(0, str(Path(__file__).resolve().parents[4]))
24
25 from docutils.core import publish_string
26 from docutils.utils.code_analyzer import with_pygments
27
28 unknown_language = """\
29 Unknown language "S-Lang".
30
31 .. code:: s-lang
32
33 % abc.sl
34 autoload("abc_mode", "abc");
35 """
36
37 workaround = """\
38 Workaround to silence warning: disable code parsing with
39 "syntax highlight" setting or put code language in class argument:
40
41 .. code::
42 :class: s-lang
43
44 % abc.sl
45 autoload("abc_mode", "abc");
46 """
47
48 settings = {'warning_stream': ''}
49
50
51 @unittest.skipUnless(with_pygments, 'optional module "pygments" not found')
52 class CodeParsingTests(unittest.TestCase):
53
54 def test_lexer_error(self):
55 output = publish_string(unknown_language, settings_overrides=settings)
56 self.assertIn(b'<system_message level="2"', output)
57 self.assertIn(b'Cannot analyze code. '
58 b'No Pygments lexer found for "s-lang".', output)
59 self.assertIn(b'<literal_block xml:space="preserve">', output)
60
61 def test_lexer_error_workaround(self):
62 output = publish_string(workaround, settings_overrides=settings)
63 self.assertNotIn(b'<system_message', output)
64 self.assertIn(b'<literal_block classes="code s-lang"', output)
65 self.assertIn(b'autoload("abc_mode", "abc");', output)
66
67
68 if __name__ == '__main__':
69 unittest.main()