OILS / doctools / cmark_test.py View on Github | oils.pub

208 lines, 83 significant
1#!/usr/bin/env python2
2"""cmark_test.py: Tests for cmark.py."""
3from __future__ import print_function
4
5import cStringIO
6import unittest
7from pprint import pprint
8
9import cmark # module under test
10
11# No TOC!
12SIMPLE_DOC = cStringIO.StringIO("""
13hi
14""")
15
16TOC_DOC = cStringIO.StringIO("""
17Title
18-----
19
20<div id="toc">
21</div>
22
23### Intro
24
25This is an h3
26in the intro.
27
28### Part One: <code>bash</code>
29
30Another h3.
31
32#### Detail 1 with <a href="foo.html?a=1&b=2">link</a>
33
34An h4.
35
36<h4 id="detail2">Detail 2</h4>
37
38Another h4.
39
40### Conclusion
41
42Concluding h3.
43
44<!-- The blank lines here show a problem that is papered over by fill-blank-lines
45 in Snip -->
46<div class="highlight"><pre><span></span>
47def f():
48 if 0:
49 return False
50
51 if 0:
52 return True
53</pre></div>
54""")
55
56NEW_DOC = """
57Title
58=====
59
60<div id="toc">
61</div>
62
63## One
64
65hello h2.
66
67### subheading `backticks`
68
69hello H3.
70
71#### subsubheading
72
73This kind of heading gets an h4. It's not in the TOC, but it can be linked to.
74
75## Two &amp; Three
76
77"""
78
79DOC_WITH_METADATA = cStringIO.StringIO("""
80- repo-url: doc/README.md
81
82Title
83=====
84
85## One
86""")
87
88_HTML_1 = '''
89<p>dummy
90</p>
91
92<div id="toc">
93</div>
94
95<h2>One <a href="/">link</a></h2>
96
97hello one.
98
99<h3>subheading <code>backticks</code></h3>
100
101<h3>one &amp; two</h3>
102
103<h2 id="explicit">Two</h2>
104
105'''
106
107
108class RenderTest(unittest.TestCase):
109
110 def testRender(self):
111 # type: () -> None
112 opts, _ = cmark.Options().parse_args([])
113
114 out_file = cStringIO.StringIO()
115 cmark.Render(opts, {}, SIMPLE_DOC, out_file)
116 self.assertEqual('<p>hi</p>\n', out_file.getvalue())
117
118 out_file = cStringIO.StringIO()
119 cmark.Render(opts, {}, TOC_DOC, out_file)
120 print(out_file.getvalue())
121
122 def testNewRender(self):
123 # type: () -> None
124 # New style of doc
125
126 new_flags = ['--toc-tag', 'h2', '--toc-tag', 'h3']
127 opts, _ = cmark.Options().parse_args(new_flags)
128
129 in_file = cStringIO.StringIO(NEW_DOC)
130 out_file = cStringIO.StringIO()
131 cmark.Render(opts, {}, in_file, out_file)
132
133 h = out_file.getvalue()
134 self.assert_('<div class="toclevel1"><a href="#one">' in h, h)
135
136 def testNewPrettyHref(self):
137 # type: () -> None
138 # New style of doc
139
140 new_flags = ['--toc-tag', 'h2', '--toc-tag', 'h3', '--toc-pretty-href']
141 opts, _ = cmark.Options().parse_args(new_flags)
142
143 in_file = cStringIO.StringIO(NEW_DOC)
144 out_file = cStringIO.StringIO()
145 cmark.Render(opts, {}, in_file, out_file)
146 h = out_file.getvalue()
147 self.assert_('<a name="subsubheading">' in h, h)
148
149 self.assert_('<div class="toclevel1"><a href="#one">' in h, h)
150 print(h)
151
152 def testExtractor(self):
153 # type: () -> None
154 parser = cmark.TocExtractor()
155 parser.feed(_HTML_1)
156 self.assertEqual(5, parser.toc_begin_line)
157
158 for heading in parser.headings:
159 print(heading)
160
161 headings = parser.headings
162 self.assertEqual(4, len(headings))
163
164 line_num, tag, css_id, html, text = headings[0]
165 self.assertEqual(8, line_num)
166 self.assertEqual('h2', tag)
167 self.assertEqual(None, css_id)
168 # nested <a> tags are omitted!
169 self.assertEqual('One link', ''.join(html))
170 self.assertEqual('One link', ''.join(text))
171
172 line_num, tag, css_id, html, text = headings[1]
173 self.assertEqual(12, line_num)
174 self.assertEqual('h3', tag)
175 self.assertEqual(None, css_id)
176 self.assertEqual('subheading <code>backticks</code>', ''.join(html))
177 self.assertEqual('subheading backticks', ''.join(text))
178
179 line_num, tag, css_id, html, text = headings[2]
180 self.assertEqual(14, line_num)
181 self.assertEqual('h3', tag)
182 self.assertEqual(None, css_id)
183 self.assertEqual('one &amp; two', ''.join(html))
184 self.assertEqual('one two', ''.join(text))
185
186 line_num, tag, css_id, html, text = headings[3]
187 self.assertEqual(16, line_num)
188 self.assertEqual('h2', tag)
189 self.assertEqual('explicit', css_id)
190 self.assertEqual('Two', ''.join(html))
191 self.assertEqual('Two', ''.join(text))
192
193 def testExtractorDense(self):
194 # type: () -> None
195 parser = cmark.TocExtractor()
196 parser.feed(_HTML_1.replace('"toc"', '"dense-toc"'))
197
198 self.assertEqual(-1, parser.toc_begin_line)
199 self.assertEqual(5, parser.dense_toc_begin_line)
200
201 insertions = cmark._MakeTocInsertionsDense(parser.headings,
202 parser.dense_toc_begin_line,
203 True)
204 pprint(insertions)
205
206
207if __name__ == '__main__':
208 unittest.main()