| 1 |
|
| 2 |
e. |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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 |
|
| 22 |
|
| 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() |