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