diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 17:25:40 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 17:25:40 +0000 |
commit | cf7da1843c45a4c2df7a749f7886a2d2ba0ee92a (patch) | |
tree | 18dcde1a8d1f5570a77cd0c361de3b490d02c789 /tests/test_highlighting.py | |
parent | Initial commit. (diff) | |
download | sphinx-upstream/7.2.6.tar.xz sphinx-upstream/7.2.6.zip |
Adding upstream version 7.2.6.upstream/7.2.6
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | tests/test_highlighting.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/tests/test_highlighting.py b/tests/test_highlighting.py new file mode 100644 index 0000000..a33ebb3 --- /dev/null +++ b/tests/test_highlighting.py @@ -0,0 +1,104 @@ +"""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 '<span class="n">a</span>b' 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("<div class=\"highlight\">") + + +def test_lexer_options(): + bridge = PygmentsBridge('html') + ret = bridge.highlight_block('//comment', 'php', opts={'startinline': True}) + assert '<span class="c1">//comment</span>' 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 == ('<div class="highlight"><pre><span></span><span class="nb">print</span> ' + '<span class="s2">"Hello sphinx world"</span>\n</pre></div>\n') + + # default: fallbacks to none if highlighting failed + ret = bridge.highlight_block('reST ``like`` text', 'default') + assert ret == '<div class="highlight"><pre><span></span>reST ``like`` text\n</pre></div>\n' + + # python: highlights as python3 + ret = bridge.highlight_block('print("Hello sphinx world")', 'python') + assert ret == ('<div class="highlight"><pre><span></span><span class="nb">print</span>' + '<span class="p">(</span>' + '<span class="s2">"Hello sphinx world"</span>' + '<span class="p">)</span>\n</pre></div>\n') + + # python3: highlights as python3 + ret = bridge.highlight_block('print("Hello sphinx world")', 'python3') + assert ret == ('<div class="highlight"><pre><span></span><span class="nb">print</span>' + '<span class="p">(</span>' + '<span class="s2">"Hello sphinx world"</span>' + '<span class="p">)</span>\n</pre></div>\n') + + # python: raises error if highlighting failed + ret = bridge.highlight_block('reST ``like`` text', 'python') + logger.warning.assert_called_with('Lexing literal_block %r as "%s" resulted in an error at token: %r. ' + 'Retrying in relaxed mode.', + 'reST ``like`` text', 'python', '`', + type='misc', subtype='highlighting_failure', + location=None) |