-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.py
322 lines (244 loc) · 11.1 KB
/
tests.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from __future__ import unicode_literals
from flask import Flask, render_template_string
from markupsafe import Markup
from unittest import TestCase
from textwrap import dedent
try:
from unittest import mock
except ImportError:
import mock
import misaka
from misaka import (EXT_AUTOLINK, EXT_FENCED_CODE, # pyflakes.ignore
EXT_NO_INTRA_EMPHASIS, EXT_SPACE_HEADERS, EXT_STRIKETHROUGH,
EXT_SUPERSCRIPT, EXT_TABLES, HTML_ESCAPE, HTML_HARD_WRAP, HTML_SKIP_HTML,
HTML_USE_XHTML, TABLE_ALIGNMASK, TABLE_HEADER, TABLE_ALIGN_CENTER, TABLE_ALIGN_LEFT,
TABLE_ALIGN_RIGHT, EXT_MATH, EXT_FOOTNOTES, EXT_UNDERLINE, EXT_MATH_EXPLICIT,
EXT_DISABLE_INDENTED_CODE, EXT_HIGHLIGHT, EXT_QUOTE)
from flask_misaka import Misaka, markdown
TEST_MD = "*This* ~~contains~~ ``some`` mark^(down) extensions: www.markdown.com foo_bar_baz it's"
app = Flask(__name__)
app.debug = True
Misaka(app)
# templating tests #
@app.route('/a')
def view_render_inline():
s = "This is ~~restructuredtext~~ *markdown*"
return render_template_string('{{s|markdown}}', s=s)
def test_render_inline():
client = app.test_client()
resp = client.open('/a')
assert resp.data == b'<p>This is ~~restructuredtext~~ <em>markdown</em></p>\n'
@app.route('/b')
def view_render_var_block():
s = "This is a *markdown* block"
tpl = '''{% filter markdown %}{{s}}{% endfilter %}'''
return render_template_string(tpl, s=s)
def test_render_var_block():
client = app.test_client()
resp = client.open('/b')
assert resp.data == b'<p>This is a <em>markdown</em> block</p>\n'
@app.route('/c')
def view_render_in_block():
tpl = '''{% filter markdown %}This is a *markdown* block{% endfilter %}'''
return render_template_string(tpl)
def test_render_in_block():
client = app.test_client()
resp = client.open('/c')
assert resp.data == b'<p>This is a <em>markdown</em> block</p>\n'
# markdown extensions in templates
extapp = Flask(__name__)
extapp.debug = True
Misaka(extapp, strikethrough=True)
@extapp.route('/d')
def view_render_inline_ext():
s = "This is ~~restructuredtext~~ *markdown*"
return render_template_string('{{s|markdown}}', s=s)
def test_render_inline_ext():
client = extapp.test_client()
resp = client.open('/d')
assert resp.data == b'<p>This is <del>restructuredtext</del> <em>markdown</em></p>\n'
# Note that the Markdown extension tests aren't actually testing that the
# Markdown is rendered correctly; that should be covered by the test suite of
# the misaka module. These tests should test that Flask-Misaka is calling
# the misaka module correctly, and returning the result unmodified
# (aside from being wrapped in a Markup class instance.)
@mock.patch("flask_misaka.misaka.html", side_effect=misaka.html)
class MarkdownExtensionTests(TestCase):
def test_defaults(self, html):
ext, flags = 0, 0
result = markdown(TEST_MD)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_one_ext(self, html):
ext, flags = EXT_AUTOLINK, 0
result = markdown(TEST_MD, autolink=True)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_two_ext(self, html):
ext, flags = EXT_FENCED_CODE | EXT_AUTOLINK, 0
result = markdown(TEST_MD, fenced_code=True, autolink=True)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_one_render(self, html):
ext, flags = 0, HTML_ESCAPE
result = markdown(TEST_MD, escape=True)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_two_render(self, html):
ext, flags = 0, HTML_HARD_WRAP | HTML_ESCAPE
result = markdown(TEST_MD, wrap=True, escape=True)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_one_ext_one_render(self, html):
ext, flags = EXT_NO_INTRA_EMPHASIS, HTML_SKIP_HTML
result = markdown(TEST_MD, no_intra_emphasis=True, no_html=True)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_two_ext_two_render(self, html):
ext = EXT_STRIKETHROUGH | EXT_SUPERSCRIPT
flags = HTML_HARD_WRAP | HTML_USE_XHTML
result = markdown(TEST_MD, strikethrough=True, superscript=True,
hard_wrap=True, use_xhtml=True)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_inverse_ext(self, html):
ext, flags = EXT_NO_INTRA_EMPHASIS, 0
result = markdown(TEST_MD, intra_emphasis=False)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_inverse_render(self, html):
ext, flags = 0, HTML_SKIP_HTML
result = markdown(TEST_MD, no_html=True)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_undefined_option(self, html):
ext, flags = 0, 0
result = markdown(TEST_MD, fireworks=True)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_defined_and_undefined_options(self, html):
ext, flags = 0, HTML_HARD_WRAP
result = markdown(TEST_MD, hard_wrap=True, stupid_hard_wrap=False)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_set_defaults(self, html):
ext, flags = EXT_TABLES, HTML_HARD_WRAP
md = Misaka(hard_wrap=True, tables=True)
result = md.render(TEST_MD)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_override_defaults(self, html):
ext, flags = 0, 0
md = Misaka(autolink=True)
result = md.render(TEST_MD, autolink=False)
html.assert_called_with(TEST_MD, extensions=ext, render_flags=flags)
self.assertIsInstance(result, Markup)
self.assertEqual(result, misaka.html(TEST_MD,
extensions=ext, render_flags=flags))
def test_custom_renderer(self, html):
class CustomRenderer(misaka.HtmlRenderer):
def image(self, link, title, alt_text):
return '<div><img src="{0}" alt="{2}" title="{1}"><div>{1}</div></div>'.format(
link, title, alt_text)
test_md = '![Alt text](/img.jpg "Title")'
expected_result = '<p><div><img src="/img.jpg" alt="Alt text" title="Title"><div>Title</div></div></p>\n'
md = Misaka(None, CustomRenderer())
result = md.render(test_md)
self.assertFalse(html.called)
self.assertEqual(str(result), expected_result)
def test_smartypants(self, html):
text = "Don't call me Shirley"
expected_result = "<p>Don’t call me Shirley</p>\n"
md = Misaka(smartypants=True)
result = md.render(text)
self.assertIsInstance(result, Markup)
self.assertEqual(result, expected_result)
def test_smartypants_table(self, html):
"smartypants should not interfere with processing tables"
text = dedent("""
| Left align | Right align | Center align |
|:-----------|------------:|:------------:|
| This | This | This |
| column | column | column |
| will | will | will |
| be | be | be |
| left | right | center |
| aligned | aligned | aligned |
""")
expected_result = dedent("""
<table>
<thead>
<tr>
<th style="text-align: left">Left align</th>
<th style="text-align: right">Right align</th>
<th style="text-align: center">Center align</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left">This</td>
<td style="text-align: right">This</td>
<td style="text-align: center">This</td>
</tr>
<tr>
<td style="text-align: left">column</td>
<td style="text-align: right">column</td>
<td style="text-align: center">column</td>
</tr>
<tr>
<td style="text-align: left">will</td>
<td style="text-align: right">will</td>
<td style="text-align: center">will</td>
</tr>
<tr>
<td style="text-align: left">be</td>
<td style="text-align: right">be</td>
<td style="text-align: center">be</td>
</tr>
<tr>
<td style="text-align: left">left</td>
<td style="text-align: right">right</td>
<td style="text-align: center">center</td>
</tr>
<tr>
<td style="text-align: left">aligned</td>
<td style="text-align: right">aligned</td>
<td style="text-align: center">aligned</td>
</tr>
</tbody>
</table>
""")
md = Misaka(tables=True, smartypants=True)
result = md.render(text).strip()
self.assertIsInstance(result, Markup)
self.assertEqual(result.strip(), expected_result.strip())
class FactoryPatternTests(TestCase):
def test_init(self):
md = Misaka()
app2 = Flask(__name__)
md.init_app(app2)
self.assertIn("markdown", app2.jinja_env.filters)