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
|
# -*- coding: utf-8 -*-
#
# Copyright © 2017 markdown-link-style contributors.
#
# This file is part of markdown-link-style.
#
# markdown-link-style is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# markdown-link-style is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with markdown-link-style (see COPYING). If not, see
# <http://www.gnu.org/licenses/>.
import os
import os.path
import sys
from io import StringIO
from random import randint
from unittest import mock
from mistune import Renderer, Markdown
from nose.tools import *
from pkg_resources import resource_string, resource_filename
from markdown_link_style._version import __version__
from mdl_style import *
from mdl_style import _get_args, _mdl_stylize
def _get_data(f):
rs = resource_string(__name__, "/".join(["data", f]))
return rs.decode()
def _get_data_path(f):
return resource_filename(__name__, "/".join(["data", f]))
class TestLSRendererIL(object):
"""Test class for mdl_style.LSRenderer inline link style.
"""
def setup(self):
self.md = LSMarkdown(link_style="inline")
def test_autolink_00(self):
d = _get_data("autolink_00.md")
d_expected = _get_data("autolink_00-expected.md")
assert_equals(self.md(d), d_expected)
def test_link_footnote_to_inline_style_conversion_00(self):
d = _get_data("inline_link_style_00.md")
expected_result = _get_data("inline_link_style_00-expected.md")
assert_equal(self.md(d), expected_result)
def test_link_footnote_to_inline_style_conversion_01(self):
d = _get_data("inline_link_style_01.md")
expected_result = _get_data("inline_link_style_01-expected.md")
assert_equal(self.md(d), expected_result)
def test_renderer_parses_images_00(self):
d = _get_data("inline_parses_images_00.md")
expected_result = _get_data("inline_parses_images_00-expected.md")
assert_equal(self.md(d), expected_result)
def test_renderer_does_not_parse_link_breaks_00(self):
d = _get_data("does_not_parse_link_breaks_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_headers_00(self):
d = _get_data("does_not_parse_headers_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_blockquotes_00(self):
d = _get_data("does_not_parse_blockquotes_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_lists_00(self):
d = _get_data("does_not_parse_lists_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_codeblocks_00(self):
d = _get_data("does_not_parse_codeblocks_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_hrules_00(self):
d = _get_data("does_not_parse_hrules_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_emphasis_00(self):
d = _get_data("does_not_parse_emphasis_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_code_00(self):
d = _get_data("does_not_parse_code_00.md")
assert_equal(self.md(d), d)
def teardown(self):
pass
class TestLSRendererFN(object):
"""Test class for mdl_style.LSRenderer footnote link style.
"""
def setup(self):
self.md = LSMarkdown(link_style="footnote")
def test_autolink_00(self):
d = _get_data("autolink_00.md")
d_expected = _get_data("autolink_00-expected.md")
assert_equals(self.md(d), d_expected)
def test_link_inline_to_footnote_style_conversion_00(self):
d = _get_data("footnote_link_style_00.md")
expected_result = _get_data("footnote_link_style_00-expected.md")
assert_equal(self.md(d), expected_result)
def test_link_inline_to_footnote_style_conversion_01(self):
d = _get_data("footnote_link_style_01.md")
expected_result = _get_data("footnote_link_style_01-expected.md")
assert_equal(self.md(d), expected_result)
def test_renderer_parses_images_00(self):
d = _get_data("footnote_parses_images_00.md")
expected_result = _get_data("footnote_parses_images_00-expected.md")
assert_equal(self.md(d), expected_result)
def test_renderer_does_not_parse_link_breaks_00(self):
d = _get_data("does_not_parse_link_breaks_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_headers_00(self):
d = _get_data("does_not_parse_headers_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_blockquotes_00(self):
d = _get_data("does_not_parse_blockquotes_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_lists_00(self):
d = _get_data("does_not_parse_lists_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_codeblocks_00(self):
d = _get_data("does_not_parse_codeblocks_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_hrules_00(self):
d = _get_data("does_not_parse_hrules_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_emphasis_00(self):
d = _get_data("does_not_parse_emphasis_00.md")
assert_equal(self.md(d), d)
def test_renderer_does_not_parse_code_00(self):
d = _get_data("does_not_parse_code_00.md")
assert_equal(self.md(d), d)
def teardown(self):
pass
class TestMdlStyleMdlStylize(object):
"""Testing mdl_style._mdl_stylize.
"""
@classmethod
def setup_class(self):
self.test_data = {}
self.test_data["inline"] = {}
self.test_data["inline"]["in"] = _get_data("inline_link_style_00.md")
self.test_data["inline"]["out"] = _get_data("inline_link_style_00-expected.md")
self.test_data["footnote"] = {}
self.test_data["footnote"]["in"] = _get_data("footnote_link_style_00.md")
self.test_data["footnote"]["out"] = _get_data(
"footnote_link_style_00-expected.md"
)
self.in_file = "in_file.md"
self.out_file = "out_file.md"
def setup(self):
self.link_style = ["inline", "footnote"][randint(0, 1)]
with open(self.in_file, "w") as f:
f.write(self.test_data[self.link_style]["in"])
def test_mdl_stylize_infile(self):
args = _get_args([self.link_style, self.in_file])
_mdl_stylize(args) # the test.
with open(self.in_file, "r") as f:
assert_equal(f.read(), self.test_data[self.link_style]["out"])
def test_mdl_stylize_infile_outfile(self):
args = _get_args([self.link_style, self.in_file, self.out_file])
_mdl_stylize(args)
with open(self.out_file, "r") as f:
assert_equal(f.read(), self.test_data[self.link_style]["out"])
def teardown(self):
if os.path.isfile(self.in_file):
os.remove(self.in_file)
if os.path.isfile(self.out_file):
os.remove(self.out_file)
class TestMdlStyleGetArgs(object):
"""Testing mdl_style._get_args.
"""
def setup(self):
self.out = None
@raises(SystemExit)
def test_get_args_version(self):
real_exit = sys.exit
def mock_exit(args):
assert_equal(sys.stdout.getvalue(), __version__ + "\n")
real_exit(args)
with mock.patch("sys.stdout", new_callable=StringIO) as out, mock.patch(
"sys.exit", new=mock_exit
):
raw_args = ["--version"]
_get_args(raw_args)
def test_get_args_inline_infile(self):
lstyle = "inline"
md_file = "autolink_00.md"
raw_args = [lstyle, _get_data_path(md_file)]
args = _get_args(raw_args)
assert_equal(args.link_style, lstyle)
assert_equal(args.in_file.read(), _get_data(md_file))
def test_get_args_inline_infile_outfile(self):
lstyle = "inline"
md_file = "autolink_00.md"
raw_args = [lstyle, _get_data_path(md_file), "outfile.md"]
args = _get_args(raw_args)
assert_equal(args.link_style, lstyle)
assert_equal(args.in_file.read(), _get_data(md_file))
assert_equal(type(args.out_file), str)
def test_get_args_footnote_infile(self):
lstyle = "footnote"
md_file = "autolink_00.md"
raw_args = [lstyle, _get_data_path(md_file)]
args = _get_args(raw_args)
assert_equal(args.link_style, lstyle)
assert_equal(args.in_file.read(), _get_data(md_file))
def test_get_args_footnote_infile_outfile(self):
lstyle = "footnote"
md_file = "autolink_00.md"
raw_args = [lstyle, _get_data_path(md_file), "outfile.md"]
args = _get_args(raw_args)
assert_equal(args.link_style, lstyle)
assert_equal(args.in_file.read(), _get_data(md_file))
assert_equal(type(args.out_file), str)
def teardown(self):
pass
|