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
14 Various tests for the `pygments` code highlighter.
17 from __future__
import absolute_import
20 if __name__
== '__main__':
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".
33 autoload("abc_mode", "abc");
37 Unknown language "S-Lang".
43 autoload("abc_mode", "abc");
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__':