test_markdown.py
109 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
|
r17259 | """Tests for conversions from markdown to other formats""" | ||
|
r11902 | |||
|
r17259 | # Copyright (c) IPython Development Team. | ||
|
r11902 | # Distributed under the terms of the Modified BSD License. | ||
|
r11920 | from copy import copy | ||
|
r11902 | |||
|
r11917 | from IPython.utils.py3compat import string_types | ||
|
r11935 | from IPython.testing import decorators as dec | ||
|
r11902 | |||
from ...tests.base import TestsBase | ||||
|
r11928 | from ..markdown import markdown2latex, markdown2html, markdown2rst | ||
|
r11902 | |||
|
r17885 | from jinja2 import Environment | ||
|
r11902 | |||
class TestMarkdown(TestsBase): | ||||
tests = [ | ||||
'*test', | ||||
'**test', | ||||
'*test*', | ||||
'_test_', | ||||
'__test__', | ||||
'__*test*__', | ||||
'**test**', | ||||
'#test', | ||||
'##test', | ||||
'test\n----', | ||||
|
r11916 | 'test [link](https://google.com/)'] | ||
|
r11902 | |||
|
r11917 | tokens = [ | ||
'*test', | ||||
'**test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
('test', 'https://google.com/')] | ||||
|
r11902 | |||
|
r11935 | @dec.onlyif_cmds_exist('pandoc') | ||
|
r11902 | def test_markdown2latex(self): | ||
|
r11934 | """markdown2latex test""" | ||
|
r11902 | for index, test in enumerate(self.tests): | ||
|
r12373 | self._try_markdown(markdown2latex, test, self.tokens[index]) | ||
|
r11902 | |||
|
r17885 | @dec.onlyif_cmds_exist('pandoc') | ||
def test_pandoc_extra_args(self): | ||||
# pass --no-wrap | ||||
s = '\n'.join([ | ||||
"#latex {{long_line | md2l(['--no-wrap'])}}", | ||||
"#rst {{long_line | md2r(['--columns', '5'])}}", | ||||
]) | ||||
long_line = ' '.join(['long'] * 30) | ||||
env = Environment() | ||||
env.filters.update({ | ||||
'md2l': markdown2latex, | ||||
'md2r': markdown2rst, | ||||
}) | ||||
tpl = env.from_string(s) | ||||
rendered = tpl.render(long_line=long_line) | ||||
_, latex, rst = rendered.split('#') | ||||
self.assertEqual(latex.strip(), 'latex %s' % long_line) | ||||
self.assertEqual(rst.strip(), 'rst %s' % long_line.replace(' ', '\n')) | ||||
|
r11902 | def test_markdown2html(self): | ||
|
r11934 | """markdown2html test""" | ||
|
r11902 | for index, test in enumerate(self.tests): | ||
|
r12373 | self._try_markdown(markdown2html, test, self.tokens[index]) | ||
|
r11902 | |||
|
r17259 | def test_markdown2html_math(self): | ||
# Mathematical expressions should be passed through unaltered | ||||
cases = [("\\begin{equation*}\n" | ||||
"\\left( \\sum_{k=1}^n a_k b_k \\right)^2 \\leq \\left( \\sum_{k=1}^n a_k^2 \\right) \\left( \\sum_{k=1}^n b_k^2 \\right)\n" | ||||
"\\end{equation*}"), | ||||
("$$\n" | ||||
"a = 1 *3* 5\n" | ||||
"$$"), | ||||
"$ a = 1 *3* 5 $", | ||||
] | ||||
for case in cases: | ||||
self.assertIn(case, markdown2html(case)) | ||||
|
r11902 | |||
|
r11935 | @dec.onlyif_cmds_exist('pandoc') | ||
|
r11902 | def test_markdown2rst(self): | ||
|
r11934 | """markdown2rst test""" | ||
|
r11920 | |||
#Modify token array for rst, escape asterik | ||||
tokens = copy(self.tokens) | ||||
tokens[0] = r'\*test' | ||||
tokens[1] = r'\*\*test' | ||||
|
r11902 | for index, test in enumerate(self.tests): | ||
|
r12373 | self._try_markdown(markdown2rst, test, tokens[index]) | ||
|
r11902 | |||
|
r11917 | def _try_markdown(self, method, test, tokens): | ||
results = method(test) | ||||
if isinstance(tokens, string_types): | ||||
assert tokens in results | ||||
else: | ||||
for token in tokens: | ||||
assert token in results | ||||