Fix tests for code directive warnings.
[docutils.git] / docutils / test / test_parsers / test_rst / test_directives / test_code_parsing.py
blob646403320c2d991b1a6118cde1929872040a0bf8
1 #!/usr/bin/env python3
2 # -*- coding: utf8 -*-
3 # :Copyright: © 2020 Günter Milde.
4 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
6 # Copying and distribution of this file, with or without modification,
7 # are permitted in any medium without royalty provided the copyright
8 # notice and this notice are preserved.
9 # This file is offered as-is, without any warranty.
11 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
13 """
14 Various tests for the `pygments` code highlighter.
15 """
17 from __future__ import absolute_import
18 import unittest
20 if __name__ == '__main__':
21 import __init__
22 from test_parsers import DocutilsTestSupport # must be imported before docutils
23 from docutils import core, utils
24 from docutils.core import publish_string
25 from docutils.utils.code_analyzer import with_pygments
27 unknown_language = """\
28 Unknown language "S-Lang".
30 .. code:: s-lang
32 % abc.sl
33 autoload("abc_mode", "abc");
34 """
36 class_arg = """\
37 Unknown language "S-Lang".
39 .. code::
40 :class: s-lang
42 % abc.sl
43 autoload("abc_mode", "abc");
44 """
46 skip_msg = 'optional module "pygments" not found'
47 settings = {'warning_stream': ''}
49 class CodeParsingTests(unittest.TestCase):
51 @unittest.skipUnless(with_pygments, skip_msg)
52 def test_lexer_error(self):
53 output = publish_string(unknown_language, settings_overrides=settings)
54 self.assertIn(b'<system_message level="2"', output)
55 self.assertIn(b'Cannot analyze code. '
56 b'No Pygments lexer found for "s-lang".', output)
57 self.assertIn(b'<literal_block xml:space="preserve">', output)
59 @unittest.skipUnless(with_pygments, skip_msg)
60 def test_lexer_error_workaround(self):
61 output = publish_string(class_arg, settings_overrides=settings)
62 self.assertNotIn(b'<system_message', output)
63 self.assertIn(b'<literal_block classes="code s-lang"', output)
64 self.assertIn(b'autoload("abc_mode", "abc");', output)
67 if __name__ == '__main__':
68 unittest.main()