forked from ingwinlu/pelican-toc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_toc.py
142 lines (118 loc) · 5.57 KB
/
test_toc.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
from io import open
import unittest
import re
import toc
from pelican.readers import MarkdownReader
from pelican.contents import Article
from pelican.tests.support import get_settings
class TestToCGeneration(unittest.TestCase):
@classmethod
def setUpClass(cls):
toc.init_default_config(None)
cls.settings = get_settings()
cls.md_reader = MarkdownReader(cls.settings)
def setUp(self):
# have to reset the default, because shallow copies
self.settings['TOC']['TOC_HEADERS'] = '^h[1-6]'
self.settings['TOC']['TOC_RUN'] = 'true'
self.maxDiff = 9999
def _handle_article_generation(self, path):
content, metadata = self.md_reader.read(path)
return Article(content=content, metadata=metadata)
def _generate_toc(self, article_path, expected_path):
result = self._handle_article_generation(article_path)
toc.generate_toc(result)
expected = ""
with open(expected_path, 'r') as f:
expected = f.read()
return result, expected
def test_toc_generation(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'true'
result, expected = self._generate_toc(
"test_data/article_with_headers.md",
"test_data/article_with_headers_toc.html"
)
self.assertEqual(result.toc, expected)
def test_toc_generation_nonascii(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'true'
result, expected = self._generate_toc(
"test_data/article_with_headers_nonascii.md",
"test_data/article_with_headers_toc_nonascii.html"
)
self.assertEqual(result.toc, expected)
def test_toc_generation_exclude_small_headers(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'true'
self.settings['TOC']['TOC_HEADERS'] = '^h[1-3]'
result, expected = self._generate_toc(
"test_data/article_with_headers_exclude_small_headers.md",
"test_data/article_with_headers_toc_exclude_small_headers.html"
)
self.assertEqual(result.toc, expected)
def test_toc_generation_exclude_small_headers_metadata(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'true'
result, expected = self._generate_toc(
"test_data/article_with_headers_exclude_small_headers_metadata.md",
"test_data/article_with_headers_toc_exclude_small_headers.html"
)
self.assertEqual(result.toc, expected)
def test_bad_TOC_HEADERS(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'true'
self.settings['TOC']['TOC_HEADERS'] = '^[1-'
with self.assertRaises(re.error):
self._generate_toc(
"test_data/article_with_headers_exclude_small_headers.md",
"test_data/article_with_headers_toc_exclude_small_headers.html"
)
def test_toc_generation_no_title(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'false'
result, expected = self._generate_toc(
"test_data/article_with_headers.md",
"test_data/article_with_headers_toc_no_title.html"
)
self.assertEqual(result.toc, expected)
def test_toc_generation_nonascii_no_title(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'false'
result, expected = self._generate_toc(
"test_data/article_with_headers_nonascii.md",
"test_data/article_with_headers_toc_nonascii_no_title.html"
)
self.assertEqual(result.toc, expected)
def test_toc_generation_exclude_small_headers_no_title(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'false'
self.settings['TOC']['TOC_HEADERS'] = '^h[1-3]'
result, expected = self._generate_toc(
"test_data/article_with_headers_exclude_small_headers.md",
"test_data/article_with_headers_toc_exclude_small_headers_no_title.html"
)
self.assertEqual(result.toc, expected)
def test_toc_generation_exclude_small_headers_metadata_no_title(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'false'
result, expected = self._generate_toc(
"test_data/article_with_headers_exclude_small_headers_metadata.md",
"test_data/article_with_headers_toc_exclude_small_headers_no_title.html"
)
self.assertEqual(result.toc, expected)
def test_bad_TOC_HEADERS(self):
self.settings['TOC']['TOC_INCLUDE_TITLE'] = 'false'
self.settings['TOC']['TOC_HEADERS'] = '^[1-'
with self.assertRaises(re.error):
self._generate_toc(
"test_data/article_with_headers_exclude_small_headers.md",
"test_data/article_with_headers_toc_exclude_small_headers_no_title.html"
)
def test_no_toc_generation(self):
article_without_headers_path = "test_data/article_without_headers.md"
article_without_headers = self._handle_article_generation(
article_without_headers_path)
toc.generate_toc(article_without_headers)
with self.assertRaises(AttributeError):
self.assertIsNone(article_without_headers.toc)
def test_no_toc_generation_metadata(self):
article_without_headers_path = "test_data/article_with_headers_metadata.md"
article_without_headers = self._handle_article_generation(
article_without_headers_path)
toc.generate_toc(article_without_headers)
with self.assertRaises(AttributeError):
self.assertIsNone(article_without_headers.toc)
if __name__ == "__main__":
unittest.main()