forked from jupyter/nbconvert
/
test_highlight.py
82 lines (61 loc) · 2.45 KB
/
test_highlight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Module with tests for Highlight
"""
# -----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
import xml
from ...tests.base import TestsBase
from ..highlight import Highlight2HTML, Highlight2Latex
# -----------------------------------------------------------------------------
# Class
# -----------------------------------------------------------------------------
highlight2html = Highlight2HTML()
highlight2latex = Highlight2Latex()
highlight2html_ruby = Highlight2HTML(pygments_lexer="ruby")
class TestHighlight(TestsBase):
"""Contains test functions for highlight.py"""
# Hello world test, magics test, blank string test
tests = [
"""
#Hello World Example
import foo
def say(text):
foo.bar(text)
end
say('Hello World!')
""",
"""
%%pylab
plot(x,y, 'r')
""",
]
tokens = [["Hello World Example", "say", "text", "import", "def"], ["pylab", "plot"]]
def test_highlight2html(self):
"""highlight2html test"""
for index, test in enumerate(self.tests):
self._try_highlight(highlight2html, test, self.tokens[index])
def test_highlight2latex(self):
"""highlight2latex test"""
for index, test in enumerate(self.tests):
self._try_highlight(highlight2latex, test, self.tokens[index])
def test_parse_html_many_lang(self):
ht = highlight2html(self.tests[0])
rb = highlight2html_ruby(self.tests[0])
for lang, tkns in [(ht, ("def",)), (rb, ("def", "end"))]:
root = xml.etree.ElementTree.fromstring(lang)
self.assertEqual(self._extract_tokens(root, "k"), set(tkns))
def _extract_tokens(self, root, cls):
return set(map(lambda x: x.text, root.findall(".//*[@class='" + cls + "']")))
def _try_highlight(self, method, test, tokens):
"""Try highlighting source, look for key tokens"""
results = method(test)
for token in tokens:
assert token in results