-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pelican_cjk.py
295 lines (223 loc) · 9.14 KB
/
test_pelican_cjk.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
import re
import unittest
from unittest import mock
import pelican_cjk
class RangesAsRegexTestCase(unittest.TestCase):
def test_ranges_with_start_and_end(self):
data = (
('a', 'z'),
('\u00A0', '\u00AF'),
)
re_range = pelican_cjk.ranges_as_regex(data)
self.assertEqual(re_range, '[a-z\u00A0-\u00AF]')
def test_ranges_without_end(self):
data = (
('A', ''),
('\u00A0\u00A1', ''),
('!@#$%', ''),
)
re_range = pelican_cjk.ranges_as_regex(data)
self.assertEqual(re_range, '[A\u00A0\u00A1!@#$%]')
def test_ranges_mixed(self):
data = (
('A', 'Z'),
('\u00A0', ''),
('!@#', ''),
('a', 'z'),
)
re_range = pelican_cjk.ranges_as_regex(data)
self.assertEqual(re_range, '[A-Z\u00A0!@#a-z]')
class CjkRangeTestCase(unittest.TestCase):
def is_cjk(self, c):
return re.match(pelican_cjk.ranges_as_regex(pelican_cjk.CJK_RANGES), c) is not None
def test_pattern_matched(self):
self.assertTrue(self.is_cjk('我'))
self.assertTrue(self.is_cjk('あ'))
self.assertTrue(self.is_cjk('マ'))
self.assertTrue(self.is_cjk('한'))
self.assertTrue(self.is_cjk('목'))
self.assertTrue(self.is_cjk('ㄱ'))
def test_pattern_not_matched(self):
self.assertFalse(self.is_cjk('a'))
self.assertFalse(self.is_cjk('Ā'))
self.assertFalse(self.is_cjk('ŝ'))
self.assertFalse(self.is_cjk('>'))
self.assertFalse(self.is_cjk('!'))
self.assertFalse(self.is_cjk('\n'))
class RemoveNewlineTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_newline_should_be_removed_cases(self):
test_cases = (
('你好\n好笑', '你好好笑'),
('逗號,\n後面', '逗號,後面'),
('(全形括號)\n後面', '(全形括號)後面'),
('句號。\nabc', '句號。abc'),
('括號)\nabc', '括號)abc'),
('abc\n,中文', 'abc,中文'),
('。\na\n,\nb', '。a,b'),
)
for data, answer in test_cases:
with self.subTest(data=data, answer=answer):
result = pelican_cjk.remove_paragraph_newline(data)
self.assertEqual(result, answer)
def test_newline_should_be_kept_cases(self):
test_cases = (
'英文abcd\n後面',
'<em>中文</em>\n後面',
'``literal``\n下一行',
'`link`_\n下一行',
'**emph**\n下一行',
'半形逗號,\n下一行',
'半形句號.\n下一行',
)
for data in test_cases:
with self.subTest(data=data):
result = pelican_cjk.remove_paragraph_newline(data)
self.assertEqual(result, data)
class AutoSpacingTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_should_add_space_cases(self):
test_cases = (
('哈哈ABC', '哈哈 ABC'),
('ABC中文', 'ABC 中文'),
('哈哈##@@嘻嘻', '哈哈 ##@@ 嘻嘻'),
('一2三4', '一 2 三 4'),
('1二3四', '1 二 3 四'),
('這是α', '這是 α'),
('這是£', '這是 £'),
('這是=', '這是 ='),
)
for data, answer in test_cases:
with self.subTest(data=data, answer=answer):
result = pelican_cjk.auto_spacing(data)
self.assertEqual(result, answer)
def test_with_tag_should_add_space_cases(self):
test_cases = (
('A<em>啥</em>B', 'A <em>啥</em> B'),
('中<em>A</em>文', '中 <em>A</em> 文'),
('哈哈<em>ABC</em>', '哈哈 <em>ABC</em>'),
('ABC<em>哈哈</em>', 'ABC <em>哈哈</em>'),
('<strong>ABC</strong>中文', '<strong>ABC</strong> 中文'),
('<strong>中文</strong>ABC', '<strong>中文</strong> ABC'),
('一<em>2</em>三<em>4</em>', '一 <em>2</em> 三 <em>4</em>'),
('<em>1</em>二<em>3</em>四', '<em>1</em> 二 <em>3</em> 四'),
('ABC<a href=http://a.b.c>連結</a>CBA', 'ABC <a href=http://a.b.c>連結</a> CBA'),
('<em>A</em>NotCJK<em>中文</em>', '<em>A</em>NotCJK <em>中文</em>'),
('<em>中</em>是中文<strong>A</strong>', '<em>中</em>是中文 <strong>A</strong>'),
)
for data, answer in test_cases:
with self.subTest(data=data):
result = pelican_cjk.auto_spacing(data)
self.assertEqual(result, answer)
def test_should_not_change_cases(self):
test_cases = (
'abcd α£ 1234',
'哈<some_tag/>啥',
'中文<p>啥</p>中文',
'中文 <p>啥</p> 中文',
'abc <em>def</em> ghi',
'五<六>七',
'這&還在',
'abc。123',
'123,abc',
)
for data in test_cases:
with self.subTest(data=data):
result = pelican_cjk.auto_spacing(data)
self.assertEqual(result, data)
def test_with_tag_should_not_change_cases(self):
test_cases = (
'這是 <strong>中文</strong> 好嗎',
'ABC <em>ABC</em> ABC',
'Nested<em><strong>行內</strong></em>Markup',
'<em>行</em><strong>inside</strong',
)
for data in test_cases:
with self.subTest(data=data):
result = pelican_cjk.auto_spacing(data)
self.assertEqual(result, data)
class RemoveMarkupSpacingTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_should_remove_space_cases(self):
test_cases = (
('中文 <em>中文</em>', '中文<em>中文</em>'),
('<em>中文</em> 中文', '<em>中文</em>中文'),
('你 <em>好</em> 嗎 <strong>嗨</strong> 嗨',
'你<em>好</em>嗎<strong>嗨</strong>嗨'),
)
for data, answer in test_cases:
with self.subTest(data=data):
result = pelican_cjk.remove_markup_spacing(data)
self.assertEqual(result, answer)
def test_should_not_change_cases(self):
test_cases = (
'中文 中文 中文',
'ABC <em>中文</em>',
'<em>中文</em> ABC',
'中文 <em>ABC</em>',
'<em>中文</em> ABC',
'<strong><em>中文</strong></em> 中文'
'中文 <strong><em>中文</strong></em>'
)
for data in test_cases:
with self.subTest(data=data):
result = pelican_cjk.remove_markup_spacing(data)
self.assertEqual(result, data)
@mock.patch('pelican_cjk.remove_markup_spacing')
@mock.patch('pelican_cjk.remove_paragraph_newline')
@mock.patch('pelican_cjk.auto_spacing')
class MainTestCase(unittest.TestCase):
def setUp(self):
self.mock_content = mock.MagicMock()
self.mock_content._content = 'SomeTextThatWillRemainIntact'
self.mock_content.settings = {'someKey': 'someValue'}
def tearDown(self):
pass
def test_with_default_settings(
self,
m_auto_spacing,
m_remove_newline,
m_remove_markup_spacing):
pelican_cjk.main(self.mock_content)
m_remove_newline.assert_called_with('SomeTextThatWillRemainIntact')
m_auto_spacing.assert_called_with(m_remove_newline.return_value)
m_remove_markup_spacing.assert_called_with(m_auto_spacing.return_value)
def test_with_CJK_REMOVE_PARAGRAPH_NEWLINE_disabled(
self,
m_auto_spacing,
m_remove_newline,
m_remove_markup_spacing):
self.mock_content.settings.update({'CJK_REMOVE_PARAGRAPH_NEWLINE': False})
pelican_cjk.main(self.mock_content)
m_remove_newline.assert_not_called()
m_auto_spacing.assert_called_with('SomeTextThatWillRemainIntact')
m_remove_markup_spacing.assert_called_with(m_auto_spacing.return_value)
def test_with_CJK_AUTO_SPACING_disabled(
self,
m_auto_spacing,
m_remove_newline,
m_remove_markup_spacing):
self.mock_content.settings.update({'CJK_AUTO_SPACING': False})
pelican_cjk.main(self.mock_content)
m_remove_newline.assert_called_with('SomeTextThatWillRemainIntact')
m_auto_spacing.assert_not_called()
m_remove_markup_spacing.assert_called_with(m_remove_newline.return_value)
def test_with_REMOVE_CJK_MAKRUP_SPACING_disabled(
self,
m_auto_spacing,
m_remove_newline,
m_remove_markup_spacing):
self.mock_content.settings.update({'CJK_REMOVE_MARKUP_SPACING': False})
pelican_cjk.main(self.mock_content)
m_remove_newline.assert_called_with('SomeTextThatWillRemainIntact')
m_auto_spacing.assert_called_with(m_remove_newline.return_value)
m_remove_markup_spacing.assert_not_called()