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