| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import unittest | 6 import unittest |
| 7 | 7 |
| 8 from document_parser import ParseDocument, RemoveTitle | 8 from document_parser import ParseDocument, RemoveTitle |
| 9 | 9 |
| 10 | 10 |
| 11 _WHOLE_DOCUMENT = ''' | 11 _WHOLE_DOCUMENT = ''' |
| 12 Preamble before heading. | 12 Preamble before heading. |
| 13 | 13 |
| 14 <h1 id='main' class='header'>Main header</h1> | 14 <h1 id='main' class='header'>Main header</h1> |
| 15 Some intro to the content. | 15 Some intro to the content. |
| 16 | 16 |
| 17 <h2 id='banana' class='header'>Bananas</h2> | 17 <h2 id='banana' class='header' title=''>Bananas</h2> |
| 18 Something about bananas. | 18 Something about bananas. |
| 19 | 19 |
| 20 <h2 id='orange'>Oranges</h2> | 20 <h2 id='orange' title='hello'>Oranges</h2> |
| 21 Something about oranges. | 21 Something about oranges. |
| 22 | 22 |
| 23 <h3 id='valencia'>Valencia Oranges</h3> | 23 <h3 id='valencia'>Valencia Oranges</h3> |
| 24 A description of valencia oranges. | 24 A description of valencia oranges. |
| 25 | 25 |
| 26 <h3 id='seville'>Seville Oranges</h3> | 26 <h3 id='seville'>Seville Oranges</h3> |
| 27 A description of seville oranges. | 27 A description of seville oranges. |
| 28 | 28 |
| 29 <h2>Grapefruit</h3> | 29 <h2>Grapefruit</h3> |
| 30 Grapefruit closed a h2 with a h3. This should be a warning. | 30 Grapefruit closed a h2 with a h3. This should be a warning. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 48 Header 5s are not parsed. | 48 Header 5s are not parsed. |
| 49 ''' | 49 ''' |
| 50 | 50 |
| 51 | 51 |
| 52 _WHOLE_DOCUMENT_WITHOUT_TITLE = ''' | 52 _WHOLE_DOCUMENT_WITHOUT_TITLE = ''' |
| 53 Preamble before heading. | 53 Preamble before heading. |
| 54 | 54 |
| 55 | 55 |
| 56 Some intro to the content. | 56 Some intro to the content. |
| 57 | 57 |
| 58 <h2 id='banana' class='header'>Bananas</h2> | 58 <h2 id='banana' class='header' title=''>Bananas</h2> |
| 59 Something about bananas. | 59 Something about bananas. |
| 60 | 60 |
| 61 <h2 id='orange'>Oranges</h2> | 61 <h2 id='orange' title='hello'>Oranges</h2> |
| 62 Something about oranges. | 62 Something about oranges. |
| 63 | 63 |
| 64 <h3 id='valencia'>Valencia Oranges</h3> | 64 <h3 id='valencia'>Valencia Oranges</h3> |
| 65 A description of valencia oranges. | 65 A description of valencia oranges. |
| 66 | 66 |
| 67 <h3 id='seville'>Seville Oranges</h3> | 67 <h3 id='seville'>Seville Oranges</h3> |
| 68 A description of seville oranges. | 68 A description of seville oranges. |
| 69 | 69 |
| 70 <h2>Grapefruit</h3> | 70 <h2>Grapefruit</h3> |
| 71 Grapefruit closed a h2 with a h3. This should be a warning. | 71 Grapefruit closed a h2 with a h3. This should be a warning. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 'Found <h3> in the middle of processing a <h2> (line 25, column 9)', | 158 'Found <h3> in the middle of processing a <h2> (line 25, column 9)', |
| 159 # TODO(kalman): Re-enable this warning once the reference pages have | 159 # TODO(kalman): Re-enable this warning once the reference pages have |
| 160 # their references fixed. | 160 # their references fixed. |
| 161 #'Found <h4> without any preceding <h3> (line 28, column 1)', | 161 #'Found <h4> without any preceding <h3> (line 28, column 1)', |
| 162 ], result.warnings) | 162 ], result.warnings) |
| 163 | 163 |
| 164 # The non-trivial table of contents assertions... | 164 # The non-trivial table of contents assertions... |
| 165 self.assertEqual(1, len(result.sections)) | 165 self.assertEqual(1, len(result.sections)) |
| 166 entries = result.sections[0].structure | 166 entries = result.sections[0].structure |
| 167 | 167 |
| 168 self.assertEqual(5, len(entries), entries) | 168 self.assertEqual(4, len(entries), entries) |
| 169 entry0, entry1, entry2, entry3, entry4 = entries | 169 entry0, entry1, entry2, entry3 = entries |
| 170 | 170 |
| 171 self.assertEqual('Bananas', entry0.name) | 171 self.assertEqual('hello', entry0.name) |
| 172 self.assertEqual({'id': 'banana', 'class': 'header'}, entry0.attributes) | 172 self.assertEqual({'id': 'orange'}, entry0.attributes) |
| 173 self.assertEqual([], entry0.entries) | 173 self.assertEqual(2, len(entry0.entries)) |
| 174 entry0_0, entry0_1 = entry0.entries |
| 174 | 175 |
| 175 self.assertEqual('Oranges', entry1.name) | 176 self.assertEqual('Valencia Oranges', entry0_0.name) |
| 176 self.assertEqual({'id': 'orange'}, entry1.attributes) | 177 self.assertEqual({'id': 'valencia'}, entry0_0.attributes) |
| 177 self.assertEqual(2, len(entry1.entries)) | 178 self.assertEqual([], entry0_0.entries) |
| 178 entry1_0, entry1_1 = entry1.entries | 179 self.assertEqual('Seville Oranges', entry0_1.name) |
| 180 self.assertEqual({'id': 'seville'}, entry0_1.attributes) |
| 181 self.assertEqual([], entry0_1.entries) |
| 179 | 182 |
| 180 self.assertEqual('Valencia Oranges', entry1_0.name) | 183 self.assertEqual('Grapefruit', entry1.name) |
| 181 self.assertEqual({'id': 'valencia'}, entry1_0.attributes) | 184 self.assertEqual({}, entry1.attributes) |
| 182 self.assertEqual([], entry1_0.entries) | 185 self.assertEqual([], entry1.entries) |
| 183 self.assertEqual('Seville Oranges', entry1_1.name) | |
| 184 self.assertEqual({'id': 'seville'}, entry1_1.attributes) | |
| 185 self.assertEqual([], entry1_1.entries) | |
| 186 | 186 |
| 187 self.assertEqual('Grapefruit', entry2.name) | 187 self.assertEqual('Not the main header', entry2.name) |
| 188 self.assertEqual({}, entry2.attributes) | 188 self.assertEqual({'id': 'not-main'}, entry2.attributes) |
| 189 self.assertEqual([], entry2.entries) | 189 self.assertEqual([], entry2.entries) |
| 190 | 190 |
| 191 self.assertEqual('Not the main header', entry3.name) | 191 self.assertEqual('Not a banana', entry3.name) |
| 192 self.assertEqual({'id': 'not-main'}, entry3.attributes) | 192 self.assertEqual({}, entry3.attributes) |
| 193 self.assertEqual([], entry3.entries) | 193 self.assertEqual(2, len(entry3.entries)) |
| 194 entry3_1, entry3_2 = entry3.entries |
| 194 | 195 |
| 195 self.assertEqual('Not a banana', entry4.name) | 196 self.assertEqual('It\'s a h4', entry3_1.name) |
| 196 self.assertEqual({}, entry4.attributes) | 197 self.assertEqual({}, entry3_1.attributes) |
| 197 self.assertEqual(2, len(entry4.entries)) | 198 self.assertEqual([], entry3_1.entries) |
| 198 entry4_1, entry4_2 = entry4.entries | |
| 199 | 199 |
| 200 self.assertEqual('It\'s a h4', entry4_1.name) | 200 self.assertEqual('Plantains', entry3_2.name) |
| 201 self.assertEqual({}, entry4_1.attributes) | 201 self.assertEqual({}, entry3_2.attributes) |
| 202 self.assertEqual([], entry4_1.entries) | 202 self.assertEqual(1, len(entry3_2.entries)) |
| 203 entry3_2_1, = entry3_2.entries |
| 203 | 204 |
| 204 self.assertEqual('Plantains', entry4_2.name) | 205 self.assertEqual('Another h4', entry3_2_1.name) |
| 205 self.assertEqual({}, entry4_2.attributes) | 206 self.assertEqual({}, entry3_2_1.attributes) |
| 206 self.assertEqual(1, len(entry4_2.entries)) | 207 self.assertEqual([], entry3_2_1.entries) |
| 207 entry4_2_1, = entry4_2.entries | |
| 208 | |
| 209 self.assertEqual('Another h4', entry4_2_1.name) | |
| 210 self.assertEqual({}, entry4_2_1.attributes) | |
| 211 self.assertEqual([], entry4_2_1.entries) | |
| 212 | 208 |
| 213 def testSingleExplicitSection(self): | 209 def testSingleExplicitSection(self): |
| 214 def test(document): | 210 def test(document): |
| 215 result = ParseDocument(document, expect_title=True) | 211 result = ParseDocument(document, expect_title=True) |
| 216 self.assertEqual([], result.warnings) | 212 self.assertEqual([], result.warnings) |
| 217 self.assertEqual('Header', result.title) | 213 self.assertEqual('Header', result.title) |
| 218 self.assertEqual(1, len(result.sections)) | 214 self.assertEqual(1, len(result.sections)) |
| 219 section0, = result.sections | 215 section0, = result.sections |
| 220 entry0, = section0.structure | 216 entry0, = section0.structure |
| 221 self.assertEqual('An inner header', entry0.name) | 217 self.assertEqual('An inner header', entry0.name) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 252 def assert_single_header(section, name): | 248 def assert_single_header(section, name): |
| 253 self.assertEqual(1, len(section.structure)) | 249 self.assertEqual(1, len(section.structure)) |
| 254 self.assertEqual(name, section.structure[0].name) | 250 self.assertEqual(name, section.structure[0].name) |
| 255 assert_single_header(section0, 'First header') | 251 assert_single_header(section0, 'First header') |
| 256 assert_single_header(section1, 'Second header') | 252 assert_single_header(section1, 'Second header') |
| 257 assert_single_header(section2, 'Third header') | 253 assert_single_header(section2, 'Third header') |
| 258 | 254 |
| 259 | 255 |
| 260 if __name__ == '__main__': | 256 if __name__ == '__main__': |
| 261 unittest.main() | 257 unittest.main() |
| OLD | NEW |