##// END OF EJS Templates
adding tests from jupyter/nbviewer#420
Nicholas Bollweg -
Show More
@@ -1,152 +1,176 b''
1 # coding: utf-8
1 # coding: utf-8
2 """Tests for conversions from markdown to other formats"""
2 """Tests for conversions from markdown to other formats"""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 import re
7 import re
8 from copy import copy
8 from copy import copy
9
9
10 from IPython.utils.py3compat import string_types
10 from IPython.utils.py3compat import string_types
11 from IPython.testing import decorators as dec
11 from IPython.testing import decorators as dec
12
12
13 from ...tests.base import TestsBase
13 from ...tests.base import TestsBase
14 from ..markdown import markdown2latex, markdown2html, markdown2rst
14 from ..markdown import markdown2latex, markdown2html, markdown2rst
15
15
16 from jinja2 import Environment
16 from jinja2 import Environment
17
17
18 class TestMarkdown(TestsBase):
18 class TestMarkdown(TestsBase):
19
19
20 tests = [
20 tests = [
21 '*test',
21 '*test',
22 '**test',
22 '**test',
23 '*test*',
23 '*test*',
24 '_test_',
24 '_test_',
25 '__test__',
25 '__test__',
26 '__*test*__',
26 '__*test*__',
27 '**test**',
27 '**test**',
28 '#test',
28 '#test',
29 '##test',
29 '##test',
30 'test\n----',
30 'test\n----',
31 'test [link](https://google.com/)',
31 'test [link](https://google.com/)',
32 ]
32 ]
33
33
34 tokens = [
34 tokens = [
35 '*test',
35 '*test',
36 '**test',
36 '**test',
37 'test',
37 'test',
38 'test',
38 'test',
39 'test',
39 'test',
40 'test',
40 'test',
41 'test',
41 'test',
42 'test',
42 'test',
43 'test',
43 'test',
44 'test',
44 'test',
45 ('test', 'https://google.com/'),
45 ('test', 'https://google.com/'),
46 ]
46 ]
47
47
48
48
49 @dec.onlyif_cmds_exist('pandoc')
49 @dec.onlyif_cmds_exist('pandoc')
50 def test_markdown2latex(self):
50 def test_markdown2latex(self):
51 """markdown2latex test"""
51 """markdown2latex test"""
52 for index, test in enumerate(self.tests):
52 for index, test in enumerate(self.tests):
53 self._try_markdown(markdown2latex, test, self.tokens[index])
53 self._try_markdown(markdown2latex, test, self.tokens[index])
54
54
55 @dec.onlyif_cmds_exist('pandoc')
55 @dec.onlyif_cmds_exist('pandoc')
56 def test_markdown2latex_markup(self):
56 def test_markdown2latex_markup(self):
57 """markdown2latex with markup kwarg test"""
57 """markdown2latex with markup kwarg test"""
58 # This string should be passed through unaltered with pandoc's
58 # This string should be passed through unaltered with pandoc's
59 # markdown_strict reader
59 # markdown_strict reader
60 s = '1) arabic number with parenthesis'
60 s = '1) arabic number with parenthesis'
61 self.assertEqual(markdown2latex(s, markup='markdown_strict'), s)
61 self.assertEqual(markdown2latex(s, markup='markdown_strict'), s)
62 # This string should be passed through unaltered with pandoc's
62 # This string should be passed through unaltered with pandoc's
63 # markdown_strict+tex_math_dollars reader
63 # markdown_strict+tex_math_dollars reader
64 s = r'$\alpha$ latex math'
64 s = r'$\alpha$ latex math'
65 # sometimes pandoc uses $math$, sometimes it uses \(math\)
65 # sometimes pandoc uses $math$, sometimes it uses \(math\)
66 expected = re.compile(r'(\$|\\\()\\alpha(\$|\\\)) latex math')
66 expected = re.compile(r'(\$|\\\()\\alpha(\$|\\\)) latex math')
67 try:
67 try:
68 # py3
68 # py3
69 assertRegex = self.assertRegex
69 assertRegex = self.assertRegex
70 except AttributeError:
70 except AttributeError:
71 # py2
71 # py2
72 assertRegex = self.assertRegexpMatches
72 assertRegex = self.assertRegexpMatches
73 assertRegex(
73 assertRegex(
74 markdown2latex(s, markup='markdown_strict+tex_math_dollars'),
74 markdown2latex(s, markup='markdown_strict+tex_math_dollars'),
75 expected)
75 expected)
76
76
77 @dec.onlyif_cmds_exist('pandoc')
77 @dec.onlyif_cmds_exist('pandoc')
78 def test_pandoc_extra_args(self):
78 def test_pandoc_extra_args(self):
79 # pass --no-wrap
79 # pass --no-wrap
80 s = '\n'.join([
80 s = '\n'.join([
81 "#latex {{long_line | md2l('markdown', ['--no-wrap'])}}",
81 "#latex {{long_line | md2l('markdown', ['--no-wrap'])}}",
82 "#rst {{long_line | md2r(['--columns', '5'])}}",
82 "#rst {{long_line | md2r(['--columns', '5'])}}",
83 ])
83 ])
84 long_line = ' '.join(['long'] * 30)
84 long_line = ' '.join(['long'] * 30)
85 env = Environment()
85 env = Environment()
86 env.filters.update({
86 env.filters.update({
87 'md2l': markdown2latex,
87 'md2l': markdown2latex,
88 'md2r': markdown2rst,
88 'md2r': markdown2rst,
89 })
89 })
90 tpl = env.from_string(s)
90 tpl = env.from_string(s)
91 rendered = tpl.render(long_line=long_line)
91 rendered = tpl.render(long_line=long_line)
92 _, latex, rst = rendered.split('#')
92 _, latex, rst = rendered.split('#')
93
93
94 self.assertEqual(latex.strip(), 'latex %s' % long_line)
94 self.assertEqual(latex.strip(), 'latex %s' % long_line)
95 self.assertEqual(rst.strip(), 'rst %s' % long_line.replace(' ', '\n'))
95 self.assertEqual(rst.strip(), 'rst %s' % long_line.replace(' ', '\n'))
96
96
97 def test_markdown2html(self):
97 def test_markdown2html(self):
98 """markdown2html test"""
98 """markdown2html test"""
99 for index, test in enumerate(self.tests):
99 for index, test in enumerate(self.tests):
100 self._try_markdown(markdown2html, test, self.tokens[index])
100 self._try_markdown(markdown2html, test, self.tokens[index])
101
101
102 def test_markdown2html_heading_anchors(self):
102 def test_markdown2html_heading_anchors(self):
103 for md, tokens in [
103 for md, tokens in [
104 ('# test',
104 ('# test',
105 ('<h1', '>test', 'id="test"', u'&#182;</a>', "anchor-link")
105 ('<h1', '>test', 'id="test"', u'&#182;</a>', "anchor-link")
106 ),
106 ),
107 ('###test head space',
107 ('###test head space',
108 ('<h3', '>test head space', 'id="test-head-space"', u'&#182;</a>', "anchor-link")
108 ('<h3', '>test head space', 'id="test-head-space"', u'&#182;</a>', "anchor-link")
109 )
109 )
110 ]:
110 ]:
111 self._try_markdown(markdown2html, md, tokens)
111 self._try_markdown(markdown2html, md, tokens)
112
112
113 def test_markdown2html_math(self):
113 def test_markdown2html_math(self):
114 # Mathematical expressions should be passed through unaltered
114 # Mathematical expressions should be passed through unaltered
115 cases = [("\\begin{equation*}\n"
115 cases = [("\\begin{equation*}\n"
116 "\\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"
116 "\\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"
117 "\\end{equation*}"),
117 "\\end{equation*}"),
118 ("$$\n"
118 ("$$\n"
119 "a = 1 *3* 5\n"
119 "a = 1 *3* 5\n"
120 "$$"),
120 "$$"),
121 "$ a = 1 *3* 5 $",
121 "$ a = 1 *3* 5 $",
122 ]
122 ]
123 for case in cases:
123 for case in cases:
124 self.assertIn(case, markdown2html(case))
124 self.assertIn(case, markdown2html(case))
125
125
126 def test_markdown2html_math_paragraph(self):
126 def test_markdown2html_math_paragraph(self):
127 # https://github.com/ipython/ipython/issues/6724
127 """these should all parse without modification"""
128 a = """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage,
128 patterns = [
129 # https://github.com/ipython/ipython/issues/6724
130 """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage,
129 $s_{t-1}$, plus a stochastic inflow, $I_t$, minus what is being released in $t$, $r_t$.
131 $s_{t-1}$, plus a stochastic inflow, $I_t$, minus what is being released in $t$, $r_t$.
130 With $s_0$ defined as the initial storage content in $t=1$, we have"""
132 With $s_0$ defined as the initial storage content in $t=1$, we have""",
131 self.assertIn(a, markdown2html(a))
133 # https://github.com/jupyter/nbviewer/issues/420
134 """$C_{ik}$
135 $$
136 C_{ik} = \sum_{j=1}
137 $$
138 $C_{ik}$""",
139 """$m$
140 $$
141 C = \begin{pmatrix}
142 0 & 0 & 0 & \cdots & 0 & 0 & -c_0 \\
143 0 & 0 & 0 & \cdots & 0 & 1 & -c_{m-1}
144 \end{pmatrix}
145 $$
146 $x^m$""",
147 """$r=\overline{1,n}$
148 $$ {\bf
149 b}_{i}^{r}(t)=(1-t)\,{\bf b}_{i}^{r-1}(t)+t\,{\bf b}_{i+1}^{r-1}(t),\:
150 i=\overline{0,n-r}, $$
151 i.e. the $i^{th}$"""
152 ]
153
154 for pattern in patterns:
155 self.assertIn(pattern, markdown2html(pattern))
132
156
133 @dec.onlyif_cmds_exist('pandoc')
157 @dec.onlyif_cmds_exist('pandoc')
134 def test_markdown2rst(self):
158 def test_markdown2rst(self):
135 """markdown2rst test"""
159 """markdown2rst test"""
136
160
137 #Modify token array for rst, escape asterik
161 #Modify token array for rst, escape asterik
138 tokens = copy(self.tokens)
162 tokens = copy(self.tokens)
139 tokens[0] = r'\*test'
163 tokens[0] = r'\*test'
140 tokens[1] = r'\*\*test'
164 tokens[1] = r'\*\*test'
141
165
142 for index, test in enumerate(self.tests):
166 for index, test in enumerate(self.tests):
143 self._try_markdown(markdown2rst, test, tokens[index])
167 self._try_markdown(markdown2rst, test, tokens[index])
144
168
145
169
146 def _try_markdown(self, method, test, tokens):
170 def _try_markdown(self, method, test, tokens):
147 results = method(test)
171 results = method(test)
148 if isinstance(tokens, string_types):
172 if isinstance(tokens, string_types):
149 assert tokens in results
173 assert tokens in results
150 else:
174 else:
151 for token in tokens:
175 for token in tokens:
152 assert token in results
176 assert token in results
General Comments 0
You need to be logged in to leave comments. Login now