| 1 | #!/usr/bin/env python3
|
| 2 | import unittest
|
| 3 |
|
| 4 | from doctools.search_index import FindHeadings
|
| 5 |
|
| 6 |
|
| 7 | class FindHeadingsTest(unittest.TestCase):
|
| 8 | def GetSymbols(self, input_str, filename="index.html"):
|
| 9 | find_headings = FindHeadings()
|
| 10 | find_headings.feed(input_str)
|
| 11 | return find_headings.GetSymbols(filename)
|
| 12 |
|
| 13 | def testSimple(self):
|
| 14 | symbols = self.GetSymbols("""
|
| 15 | <title>My Title</title>
|
| 16 | <a name="anchor1"></a>
|
| 17 | <h1>Heading 1</h1>
|
| 18 |
|
| 19 | <a name="anchor1-1"></a>
|
| 20 | <h2>Heading 1.1</h2>
|
| 21 |
|
| 22 | <a name="anchor1-1-1"></a>
|
| 23 | <h3>Heading 1.1.1</h3>
|
| 24 |
|
| 25 | <a name="anchor1-1-2"></a>
|
| 26 | <h3>Heading 1.1.2</h3>
|
| 27 |
|
| 28 | <a name="anchor2"></a>
|
| 29 | <h1>Heading 2</h1>
|
| 30 |
|
| 31 | <a name="anchor2-1"></a>
|
| 32 | <h2>Heading 2.1</h2>
|
| 33 | """)
|
| 34 |
|
| 35 | self.assertEqual(
|
| 36 | symbols,
|
| 37 | [
|
| 38 | {
|
| 39 | "symbol": "My Title",
|
| 40 | "children": [
|
| 41 | {
|
| 42 | "symbol": "Heading 1",
|
| 43 | "anchor": "index.html#anchor1",
|
| 44 | "children": [
|
| 45 | {
|
| 46 | "symbol": "Heading 1.1",
|
| 47 | "anchor": "index.html#anchor1-1",
|
| 48 | "children": [
|
| 49 | {
|
| 50 | "symbol": "Heading 1.1.1",
|
| 51 | "anchor": "index.html#anchor1-1-1",
|
| 52 | },
|
| 53 | {
|
| 54 | "symbol": "Heading 1.1.2",
|
| 55 | "anchor": "index.html#anchor1-1-2",
|
| 56 | },
|
| 57 | ],
|
| 58 | }
|
| 59 | ],
|
| 60 | },
|
| 61 | {
|
| 62 | "symbol": "Heading 2",
|
| 63 | "anchor": "index.html#anchor2",
|
| 64 | "children": [
|
| 65 | {
|
| 66 | "symbol": "Heading 2.1",
|
| 67 | "anchor": "index.html#anchor2-1",
|
| 68 | }
|
| 69 | ],
|
| 70 | },
|
| 71 | ],
|
| 72 | "anchor": "index.html",
|
| 73 | }
|
| 74 | ],
|
| 75 | )
|
| 76 |
|
| 77 | def testNoTitle(self):
|
| 78 | # Pages without a title should be skipped from indexing
|
| 79 | # Redirect pages like doc/oil-help.html don't have a title (or any
|
| 80 | # content)
|
| 81 |
|
| 82 | symbols = self.GetSymbols("""
|
| 83 | <a name="anchor1"></a>
|
| 84 | <h1>Heading 1</h1>
|
| 85 | """)
|
| 86 |
|
| 87 | self.assertEqual(symbols, [])
|
| 88 |
|
| 89 | def testEmptyTitle(self):
|
| 90 | # However, empty titles are fine!
|
| 91 |
|
| 92 | symbols = self.GetSymbols("""
|
| 93 | <title></title>
|
| 94 |
|
| 95 | <a name="anchor1"></a>
|
| 96 | <h1>Heading 1</h1>
|
| 97 | """)
|
| 98 |
|
| 99 | self.assertEqual(
|
| 100 | symbols,
|
| 101 | [
|
| 102 | {
|
| 103 | "symbol": "",
|
| 104 | "children": [
|
| 105 | {"symbol": "Heading 1", "anchor": "index.html#anchor1"}
|
| 106 | ],
|
| 107 | "anchor": "index.html",
|
| 108 | }
|
| 109 | ],
|
| 110 | )
|
| 111 |
|
| 112 | def testBadNesting(self):
|
| 113 | # We don't want to drop any headers, so we should gracefully handle
|
| 114 | # headers no wrapped in a higher heading level
|
| 115 |
|
| 116 | for h in ["h2", "h3"]:
|
| 117 | symbols = self.GetSymbols(f"""
|
| 118 | <title>Hello!</title>
|
| 119 |
|
| 120 | <a name="anchor"></a>
|
| 121 | <{h}>Sub-heading</{h}>
|
| 122 | """)
|
| 123 |
|
| 124 | self.assertEqual(
|
| 125 | symbols,
|
| 126 | [
|
| 127 | {
|
| 128 | "symbol": "Hello!",
|
| 129 | "children": [
|
| 130 | {"symbol": "Sub-heading", "anchor": "index.html#anchor"}
|
| 131 | ],
|
| 132 | "anchor": "index.html",
|
| 133 | }
|
| 134 | ],
|
| 135 | )
|
| 136 |
|
| 137 | def testComplexHeading(self):
|
| 138 | # Some headings have formatting. This is ignored, but we still extract all the text.
|
| 139 |
|
| 140 | symbols = self.GetSymbols(f"""
|
| 141 | <title>Title</title>
|
| 142 |
|
| 143 | <a name="anchor"></a>
|
| 144 | <h2><b>Some</b> Heading</h2>
|
| 145 | """)
|
| 146 |
|
| 147 | self.assertEqual(
|
| 148 | symbols,
|
| 149 | [
|
| 150 | {
|
| 151 | "symbol": "Title",
|
| 152 | "children": [
|
| 153 | {"symbol": "Some Heading", "anchor": "index.html#anchor"}
|
| 154 | ],
|
| 155 | "anchor": "index.html",
|
| 156 | }
|
| 157 | ],
|
| 158 | )
|
| 159 |
|
| 160 |
|
| 161 | if __name__ == "__main__":
|
| 162 | unittest.main()
|