From e863fd965dd6253243c3342bd6f0adc4fc8aec4d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 13:31:33 +0200 Subject: Adding upstream version 5.3.0. Signed-off-by: Daniel Baumann --- tests/test_highlighting.py | 103 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 tests/test_highlighting.py (limited to 'tests/test_highlighting.py') diff --git a/tests/test_highlighting.py b/tests/test_highlighting.py new file mode 100644 index 0000000..d7e3625 --- /dev/null +++ b/tests/test_highlighting.py @@ -0,0 +1,103 @@ +"""Test the Pygments highlighting bridge.""" + +from unittest import mock + +from pygments.formatters.html import HtmlFormatter +from pygments.lexer import RegexLexer +from pygments.token import Name, Text + +from sphinx.highlighting import PygmentsBridge + + +class MyLexer(RegexLexer): + name = 'testlexer' + + tokens = { + 'root': [ + ('a', Name), + ('b', Text), + ], + } + + +class MyFormatter(HtmlFormatter): + def format(self, tokensource, outfile): + for tok in tokensource: + outfile.write(tok[1]) + + +class ComplainOnUnhighlighted(PygmentsBridge): + def unhighlighted(self, source): + raise AssertionError("should highlight %r" % source) + + +def test_add_lexer(app, status, warning): + app.add_lexer('test', MyLexer) + + bridge = PygmentsBridge('html') + ret = bridge.highlight_block('ab', 'test') + assert 'ab' in ret + + +def test_detect_interactive(): + bridge = ComplainOnUnhighlighted('html') + blocks = [ + """ + >>> testing() + True + """, + ] + for block in blocks: + ret = bridge.highlight_block(block.lstrip(), 'python') + assert ret.startswith("
") + + +def test_lexer_options(): + bridge = PygmentsBridge('html') + ret = bridge.highlight_block('//comment', 'php', opts={'startinline': True}) + assert '//comment' in ret + + +def test_set_formatter(): + PygmentsBridge.html_formatter = MyFormatter + try: + bridge = PygmentsBridge('html') + ret = bridge.highlight_block('foo\n', 'python') + assert ret == 'foo\n' + finally: + PygmentsBridge.html_formatter = HtmlFormatter + + +@mock.patch('sphinx.highlighting.logger') +def test_default_highlight(logger): + bridge = PygmentsBridge('html') + + # default: highlights as python3 + ret = bridge.highlight_block('print "Hello sphinx world"', 'default') + assert ret == ('
print '
+                   '"Hello sphinx world"\n
\n') + + # default: fallbacks to none if highlighting failed + ret = bridge.highlight_block('reST ``like`` text', 'default') + assert ret == '
reST ``like`` text\n
\n' + + # python: highlights as python3 + ret = bridge.highlight_block('print("Hello sphinx world")', 'python') + assert ret == ('
print'
+                   '('
+                   '"Hello sphinx world"'
+                   ')\n
\n') + + # python3: highlights as python3 + ret = bridge.highlight_block('print("Hello sphinx world")', 'python3') + assert ret == ('
print'
+                   '('
+                   '"Hello sphinx world"'
+                   ')\n
\n') + + # python: raises error if highlighting failed + ret = bridge.highlight_block('reST ``like`` text', 'python') + logger.warning.assert_called_with('Could not lex literal_block as "%s". ' + 'Highlighting skipped.', 'python', + type='misc', subtype='highlighting_failure', + location=None) -- cgit v1.2.3