-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_plugin.py
434 lines (365 loc) · 12.8 KB
/
test_plugin.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
"""mkdocs-alias-plugin tests
Various unit tests for the non-mkdocs functions in plugin.py.
"""
import logging
import re
from mkdocs.structure.files import File
from alias.plugin import get_alias_names, get_page_title, replace_tag, ALIAS_TAG_REGEX
PAGE_FILE = File("/folder1/folder4/folder5/test.md", "/src/", "/dest/", False)
def test_get_page_title_1():
"""Test title from H1 tag extraction"""
source = "# The actual title\n\nSome body text"
meta = {}
assert get_page_title(source, meta) == 'The actual title'
def test_get_page_title_2():
"""Test title from meta block extraction"""
source = "# NOT The actual title\n\nSome body text"
meta = {'title': 'The actual title'}
assert get_page_title(source, meta) == 'The actual title'
def test_get_page_title_3():
"""Test page without a title"""
source = "Some body text"
meta = {}
assert get_page_title(source, meta) is None
def test_get_page_title_4():
"""Test page with both meta and H1 titles"""
source = "# Not the title\n\nsome text\n"
meta = {'title': 'The actual title'}
assert get_page_title(source, meta) == 'The actual title'
def test_get_page_title_5():
"""Test page with both meta title and icon enabled"""
source = "# Not the title\n\nsome text\n"
meta = {'title': 'The actual title', 'icon': 'path/to/my-icon'}
assert get_page_title(source, meta, include_icon=True) == ':path-to-my-icon: The actual title'
def test_get_page_title_6():
"""Test page with both meta title and icon but disabled"""
source = "# Not the title\n\nsome text\n"
meta = {'title': 'The actual title', 'icon': 'path/to/my-icon'}
assert get_page_title(source, meta, include_icon=False) == 'The actual title'
def test_get_alias_name_1():
"""Test alias from meta extraction"""
meta = {'alias': 'my-alias'}
assert get_alias_names(meta) == ['my-alias']
def test_get_alias_name_2():
"""Test alias from empty meta extraction"""
meta = {}
assert get_alias_names(meta) is None
def test_get_alias_name_3():
"""Test alias from name/title pair"""
meta = {'alias': {'name': 'my-alias', 'title': 'My Title'}}
assert get_alias_names(meta) == ['my-alias']
def test_get_alias_name_4():
"""Test aliases from array"""
meta = {'alias': ['my-alias', 'my-other-alias', 'foobar']}
assert get_alias_names(meta) == ['my-alias', 'my-other-alias', 'foobar']
def test_get_alias_name_5():
"""Test aliases from array with non-string entries"""
meta = {'alias': ['my-alias', 1, False, None, 4.821]}
assert get_alias_names(meta) == ['my-alias']
def test_get_multiple_aliases():
"""Allow for the use of multiple aliases"""
meta = {'alias': ['my-alias', 'ma', 'myalias']}
assert get_alias_names(meta) == ['my-alias', 'ma', 'myalias']
def test_replace_tag_1():
"""Should match a simple alias tag"""
logger = logging.getLogger()
aliases = {'my-alias': {
'text': 'link text',
'alias': 'my-alias',
'url': 'my-alias.md'
}}
markdown = 'Test: [[my-alias]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == 'Test: [link text](../../../my-alias.md)'
def test_replace_tag_2():
"""Should replace an alias with a specified title"""
logger = logging.getLogger()
aliases = {'my-alias': {
'text': 'link text',
'alias': 'my-alias',
'url': 'my-alias.md'
}}
markdown = 'Test: [[my-alias|Alternate Text]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == 'Test: [Alternate Text](../../../my-alias.md)'
def test_replace_tag_3():
"""Should not replace an escaped alias"""
logger = logging.getLogger()
aliases = {'my-alias': {
'text': 'link text',
'alias': 'my-alias',
'url': 'my-alias.md'
}}
markdown = 'Test: \\[[my-alias|Alternate Text]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == 'Test: [[my-alias|Alternate Text]]'
def test_replace_tag_4():
"""Should not parse an escaped alias"""
logger = logging.getLogger()
aliases = {'my-alias': {
'text': 'link text',
'alias': 'my-alias',
'url': 'my-alias.md'
}}
markdown = '\\[[ ! -d $HOME/myfolder ]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == '[[ ! -d $HOME/myfolder ]]'
def test_replace_tag_5():
"""Should return the original string if the alias wasn't found"""
logger = logging.getLogger()
aliases = {'my-alias': {
'text': 'link text',
'alias': 'my-alias',
'url': 'my-alias.md'
}}
markdown = 'Test: [[unknown]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == markdown
def test_replace_tag_6():
"""Should use the URL as text if the text wasn't set"""
logger = logging.getLogger()
aliases = {'my-alias': {
'text': None,
'alias': 'my-alias',
'url': 'my-alias.md'
}}
markdown = 'Test: [[my-alias]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == 'Test: [my-alias.md](../../../my-alias.md)'
def test_replace_tag_7():
"""Should handle aliases with spaces"""
logger = logging.getLogger()
aliases = {' my spacey alias ': {
'text': 'The Text',
'alias': ' my spacey alias ',
'url': 'my-alias.md'
}}
markdown = 'Test: [[ my spacey alias ]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == 'Test: [The Text](../../../my-alias.md)'
def test_replace_tag_with_anchor():
"""Should handle aliases with URL anchors"""
logger = logging.getLogger()
aliases = {'my alias': {
'text': 'The Text',
'alias': 'my alias',
'url': 'my-alias.md'
}}
markdown = 'Test: [[my alias#anchor]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == 'Test: [The Text](../../../my-alias.md#anchor)'
def test_replace_tag_with_anchor2():
"""Should handle aliases with multiple URL anchors"""
logger = logging.getLogger()
aliases = {'my alias': {
'text': 'The Text',
'alias': 'my alias',
'url': 'my-alias.md'
}}
markdown = 'Test: [[my alias#anchor#another ignored anchor]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == 'Test: [The Text](../../../my-alias.md#anchor)'
def test_replace_tag_with_anchor3():
"""Should handle aliases with an anchor and a title"""
logger = logging.getLogger()
aliases = {'my alias': {
'text': 'The Text',
'alias': 'my alias',
'url': 'my-alias.md'
}}
markdown = 'Test: [[my alias#my anchor|The Title]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == 'Test: [The Title](../../../my-alias.md#my anchor)'
def test_plugin_shouldnt_break_with_text():
"""An alias with the word 'text' in it shouldn't break the plugin"""
logger = logging.getLogger()
aliases = {'text': {
'text': 'The Text',
'alias': 'text',
'url': 'my-alias.md'
}}
markdown = 'Test: [[text]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE),
markdown
)
assert result == 'Test: [The Text](../../../my-alias.md)'
def test_plugin_should_replace_anchors_with_titles():
"""An alias with an anchor should be replaced with the title if the
config option is set"""
logger = logging.getLogger()
aliases = {'my alias': {
'text': 'The Text',
'alias': 'my alias',
'url': 'my-alias.md',
'anchors': [{
'id': 'anchor',
'name': 'The Anchor'
}]
}}
markdown = 'Test: [[my alias#anchor]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE, True),
markdown
)
assert result == 'Test: [The Anchor](../../../my-alias.md#anchor)'
def test_plugin_should_note_replace_anchors_with_titles():
"""An alias with an anchor should be NOT replaced with the title if the
config option is not set"""
logger = logging.getLogger()
aliases = {'my alias': {
'text': 'The Text',
'alias': 'my alias',
'url': 'my-alias.md',
'anchors': [{
'id': 'anchor',
'name': 'The Anchor'
}]
}}
markdown = 'Test: [[my alias#anchor]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE, False),
markdown
)
assert result == 'Test: [The Text](../../../my-alias.md#anchor)'
def test_plugin_should_replace_anchors_with_nesting():
"""An alias with an anchor should be replaced with the title if the
config option is set with the appropriate nested child anchor"""
logger = logging.getLogger()
aliases = {'my alias': {
'text': 'The Text',
'alias': 'my alias',
'url': 'my-alias.md',
'anchors': [{
'id': 'anchor',
'name': 'The Anchor',
'children': [{
'id': 'nested',
'name': 'The Nested Anchor'
}]
}]
}}
markdown = 'Test: [[my alias#nested]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE, True),
markdown
)
assert result == 'Test: [The Nested Anchor](../../../my-alias.md#nested)'
def test_plugin_should_function_as_usual_without_anchors():
"""An alias with an anchor, with the config option set to true, but no
anchors should still function as usual"""
logger = logging.getLogger()
aliases = {'my alias': {
'text': 'The Text',
'alias': 'my alias',
'url': 'my-alias.md',
'anchors': []
}}
markdown = 'Test: [[my alias#anchor]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE, True),
markdown
)
assert result == 'Test: [The Text](../../../my-alias.md#anchor)'
def test_plugin_should_use_custom_title_with_anchor_titles():
"""An alias with an anchor, with the config option set to true, with
anchors, but with a custom title should use the custom title"""
logger = logging.getLogger()
aliases = {'my alias': {
'text': 'The Text',
'alias': 'my alias',
'url': 'my-alias.md',
'anchors': [{
'id': 'anchor',
'name': 'The Anchor',
'children': [{
'id': 'nested',
'name': 'The Nested Anchor'
}]
}]
}}
markdown = 'Test: [[my alias#nested|Custom Title]]'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, aliases, logger, PAGE_FILE, True),
markdown
)
assert result == 'Test: [Custom Title](../../../my-alias.md#nested)'
def test_plugin_should_link_to_anchor_on_current_page():
"""An alias with an anchor on the current page should link to it
without the page path"""
logger = logging.getLogger()
page_file = File(
'foo/bar.md',
src_dir=None,
dest_dir='/path/to/site',
use_directory_urls=False,
)
page_file.content_string = 'Test: [[#anchor]]\n\n## Anchor\n\nSome text'
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, {}, logger, page_file),
page_file.content_string
)
assert result == 'Test: [Anchor](#anchor)\n\n## Anchor\n\nSome text'
def test_get_alias_names_1():
"""Test alias from meta extraction"""
meta = {'alias': 'my-alias'}
assert get_alias_names(meta) == ['my-alias']
def test_get_alias_names_2():
"""Test alias from empty meta extraction"""
meta = {}
assert get_alias_names(meta) is None
def test_get_alias_names_3():
"""Test alias from meta extraction using a custom key"""
meta = {'custom-key': 'my-alias'}
assert get_alias_names(meta, 'custom-key') == ['my-alias']
def test_get_alias_names_4():
"""Test alias from name/title pair"""
meta = {'alias': {'name': 'my-alias', 'title': 'My Title'}}
assert get_alias_names(meta, 'alias') == ['my-alias']