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_renderer import DocumentRenderer | 8 from document_renderer import DocumentRenderer |
9 from server_instance import ServerInstance | 9 from server_instance import ServerInstance |
10 from test_file_system import TestFileSystem | 10 from test_file_system import TestFileSystem |
11 from test_data.canned_data import CANNED_TEST_FILE_SYSTEM_DATA | 11 from test_data.canned_data import CANNED_TEST_FILE_SYSTEM_DATA |
12 | 12 |
13 | 13 |
14 class DocumentRendererUnittest(unittest.TestCase): | 14 class DocumentRendererUnittest(unittest.TestCase): |
15 def setUp(self): | 15 def setUp(self): |
16 self._renderer = ServerInstance.ForTest( | 16 self._renderer = ServerInstance.ForTest( |
17 TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA)).document_renderer | 17 TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA)).document_renderer |
| 18 self._path = 'apps/some/path/to/document.html' |
| 19 |
| 20 def _Render(self, document, render_title=False): |
| 21 return self._renderer.Render(document, |
| 22 self._path, |
| 23 render_title=render_title) |
18 | 24 |
19 def testNothingToSubstitute(self): | 25 def testNothingToSubstitute(self): |
20 document = 'hello world' | 26 document = 'hello world' |
21 path = 'apps/some/path/to/document.html' | |
22 | 27 |
23 text, warnings = self._renderer.Render(document, path) | 28 text, warnings = self._Render(document) |
24 self.assertEqual(document, text) | 29 self.assertEqual(document, text) |
25 self.assertEqual([], warnings) | 30 self.assertEqual([], warnings) |
26 | 31 |
27 text, warnings = self._renderer.Render(document, path, render_title=True) | 32 text, warnings = self._Render(document, render_title=True) |
28 self.assertEqual(document, text) | 33 self.assertEqual(document, text) |
29 self.assertEqual(['Expected a title'], warnings) | 34 self.assertEqual(['Expected a title'], warnings) |
30 | 35 |
31 def testTitles(self): | 36 def testTitles(self): |
32 document = '<h1>title</h1> then $(title) then another $(title)' | 37 document = '<h1>title</h1> then $(title) then another $(title)' |
33 path = 'apps/some/path/to/document.html' | |
34 | 38 |
35 text, warnings = self._renderer.Render(document, path) | 39 text, warnings = self._Render(document) |
36 self.assertEqual(document, text) | 40 self.assertEqual(document, text) |
37 self.assertEqual(['Found unexpected title "title"'], warnings) | 41 self.assertEqual(['Found unexpected title "title"'], warnings) |
38 | 42 |
39 text, warnings = self._renderer.Render(document, path, render_title=True) | 43 text, warnings = self._Render(document, render_title=True) |
40 self.assertEqual('<h1>title</h1> then title then another $(title)', text) | 44 self.assertEqual('<h1>title</h1> then title then another $(title)', text) |
41 self.assertEqual([], warnings) | 45 self.assertEqual([], warnings) |
42 | 46 |
43 def testTocs(self): | 47 def testTocs(self): |
44 document = ('here is a toc $(table_of_contents) ' | 48 document = ('here is a toc $(table_of_contents) ' |
45 'and another $(table_of_contents)') | 49 'and another $(table_of_contents)') |
46 expected_document = ('here is a toc <table-of-contents> and another ' | 50 expected_document = ('here is a toc <table-of-contents> and another ' |
47 '$(table_of_contents)') | 51 '$(table_of_contents)') |
48 path = 'apps/some/path/to/document.html' | |
49 | 52 |
50 text, warnings = self._renderer.Render(document, path) | 53 text, warnings = self._Render(document) |
51 self.assertEqual(expected_document, text) | 54 self.assertEqual(expected_document, text) |
52 self.assertEqual([], warnings) | 55 self.assertEqual([], warnings) |
53 | 56 |
54 text, warnings = self._renderer.Render(document, path, render_title=True) | 57 text, warnings = self._Render(document, render_title=True) |
55 self.assertEqual(expected_document, text) | 58 self.assertEqual(expected_document, text) |
56 self.assertEqual(['Expected a title'], warnings) | 59 self.assertEqual(['Expected a title'], warnings) |
57 | 60 |
58 def testRefs(self): | 61 def testRefs(self): |
59 # The references in this and subsequent tests won't actually be resolved | 62 # The references in this and subsequent tests won't actually be resolved |
60 document = 'A ref $(ref:baz.baz_e1) here, $(ref:foo.foo_t3 ref title) there' | 63 document = 'A ref $(ref:baz.baz_e1) here, $(ref:foo.foo_t3 ref title) there' |
61 expected_document = ('A ref <a href=#type-baz_e1>baz.baz_e1</a> ' | 64 expected_document = ''.join([ |
62 'here, <a href=#type-foo_t3>ref title</a> ' | 65 'A ref <a href=/apps/#type-baz_e1>baz.baz_e1</a> here, ', |
63 'there') | 66 '<a href=/apps/#type-foo_t3>ref title</a> there' |
64 path = 'apps/some/path/to/document.html' | 67 ]) |
65 | 68 |
66 text, warnings = self._renderer.Render(document, path) | 69 text, warnings = self._Render(document) |
67 self.assertEqual(expected_document, text) | 70 self.assertEqual(expected_document, text) |
68 self.assertEqual([], warnings) | 71 self.assertEqual([], warnings) |
69 | 72 |
70 text, warnings = self._renderer.Render(document, path, render_title=True) | 73 text, warnings = self._Render(document, render_title=True) |
71 self.assertEqual(expected_document, text) | 74 self.assertEqual(expected_document, text) |
72 self.assertEqual(['Expected a title'], warnings) | 75 self.assertEqual(['Expected a title'], warnings) |
73 | 76 |
74 def testTitleAndToc(self): | 77 def testTitleAndToc(self): |
75 document = '<h1>title</h1> $(title) and $(table_of_contents)' | 78 document = '<h1>title</h1> $(title) and $(table_of_contents)' |
76 path = 'apps/some/path/to/document.html' | |
77 | 79 |
78 text, warnings = self._renderer.Render(document, path) | 80 text, warnings = self._Render(document) |
79 self.assertEqual('<h1>title</h1> $(title) and <table-of-contents>', text) | 81 self.assertEqual('<h1>title</h1> $(title) and <table-of-contents>', text) |
80 self.assertEqual(['Found unexpected title "title"'], warnings) | 82 self.assertEqual(['Found unexpected title "title"'], warnings) |
81 | 83 |
82 text, warnings = self._renderer.Render(document, path, render_title=True) | 84 text, warnings = self._Render(document, render_title=True) |
83 self.assertEqual('<h1>title</h1> title and <table-of-contents>', text) | 85 self.assertEqual('<h1>title</h1> title and <table-of-contents>', text) |
84 self.assertEqual([], warnings) | 86 self.assertEqual([], warnings) |
85 | 87 |
86 def testRefInTitle(self): | 88 def testRefInTitle(self): |
87 document = '<h1>$(ref:baz.baz_e1 title)</h1> A $(title) was here' | 89 document = '<h1>$(ref:baz.baz_e1 title)</h1> A $(title) was here' |
88 expected_document_no_title = ('<h1><a href=#type-baz_e1>' | 90 href = '/apps/#type-baz_e1' |
89 'title</a></h1> A $(title) was here') | 91 expected_document_no_title = ''.join([ |
| 92 '<h1><a href=%s>title</a></h1> A $(title) was here' % href |
| 93 ]) |
90 | 94 |
91 expected_document = ('<h1><a href=#type-baz_e1>title</a></h1>' | 95 expected_document = ''.join([ |
92 ' A title was here') | 96 '<h1><a href=%s>title</a></h1> A title was here' % href |
93 path = 'apps/some/path/to/document.html' | 97 ]) |
94 | 98 |
95 text, warnings = self._renderer.Render(document, path) | 99 text, warnings = self._Render(document) |
96 self.assertEqual(expected_document_no_title, text) | 100 self.assertEqual(expected_document_no_title, text) |
97 self.assertEqual([('Found unexpected title "title"')], warnings) | 101 self.assertEqual([('Found unexpected title "title"')], warnings) |
98 | 102 |
99 text, warnings = self._renderer.Render(document, path, render_title=True) | 103 text, warnings = self._Render(document, render_title=True) |
100 self.assertEqual(expected_document, text) | 104 self.assertEqual(expected_document, text) |
101 self.assertEqual([], warnings) | 105 self.assertEqual([], warnings) |
102 | 106 |
103 def testRefSplitAcrossLines(self): | 107 def testRefSplitAcrossLines(self): |
104 document = 'Hello, $(ref:baz.baz_e1 world). A $(ref:foo.foo_t3\n link)' | 108 document = 'Hello, $(ref:baz.baz_e1 world). A $(ref:foo.foo_t3\n link)' |
105 expected_document = ('Hello, <a href=#type-baz_e1>world</a>. A <a href=' | 109 expected_document = ''.join([ |
106 '#type-foo_t3>link</a>') | 110 'Hello, <a href=/apps/#type-baz_e1>world</a>. ', |
| 111 'A <a href=/apps/#type-foo_t3>link</a>' |
| 112 ]) |
107 | 113 |
108 path = 'apps/some/path/to/document.html' | |
109 | 114 |
110 text, warnings = self._renderer.Render(document, path) | 115 text, warnings = self._Render(document) |
111 self.assertEqual(expected_document, text) | 116 self.assertEqual(expected_document, text) |
112 self.assertEqual([], warnings) | 117 self.assertEqual([], warnings) |
113 | 118 |
114 text, warnings = self._renderer.Render(document, path, render_title=True) | 119 text, warnings = self._Render(document, render_title=True) |
115 self.assertEqual(expected_document, text) | 120 self.assertEqual(expected_document, text) |
116 self.assertEqual(['Expected a title'], warnings) | 121 self.assertEqual(['Expected a title'], warnings) |
117 | 122 |
118 def testInvalidRef(self): | 123 def testInvalidRef(self): |
119 # DocumentRenderer attempts to detect unclosed $(ref:...) tags by limiting | 124 # DocumentRenderer attempts to detect unclosed $(ref:...) tags by limiting |
120 # how far it looks ahead. Lorem Ipsum should be long enough to trigger that. | 125 # how far it looks ahead. Lorem Ipsum should be long enough to trigger that. |
121 _LOREM_IPSUM = ( | 126 _LOREM_IPSUM = ( |
122 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ' | 127 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ' |
123 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ' | 128 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ' |
124 'ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut ' | 129 'ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut ' |
125 'aliquip ex ea commodo consequat. Duis aute irure dolor in ' | 130 'aliquip ex ea commodo consequat. Duis aute irure dolor in ' |
126 'reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla ' | 131 'reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla ' |
127 'pariatur. Excepteur sint occaecat cupidatat non proident, sunt in ' | 132 'pariatur. Excepteur sint occaecat cupidatat non proident, sunt in ' |
128 'culpa qui officia deserunt mollit anim id est laborum.') | 133 'culpa qui officia deserunt mollit anim id est laborum.') |
129 document = ('An invalid $(ref:foo.foo_t3 a title ' + _LOREM_IPSUM + | 134 document = ''.join([ |
130 '$(ref:baz.baz_e1) here') | 135 'An invalid $(ref:foo.foo_t3 a title ', |
131 expected_document = ('An invalid $(ref:foo.foo_t3 a title ' + _LOREM_IPSUM + | 136 _LOREM_IPSUM, |
132 '<a href=#type-baz_e1>baz.baz_e1</a> here') | 137 '$(ref:baz.baz_e1) here' |
133 path = 'apps/some/path/to/document_api.html' | 138 ]) |
| 139 expected_document = ''.join([ |
| 140 'An invalid $(ref:foo.foo_t3 a title ', |
| 141 _LOREM_IPSUM, |
| 142 '<a href=/apps/#type-baz_e1>baz.baz_e1</a> here' |
| 143 ]) |
134 | 144 |
135 text, warnings = self._renderer.Render(document, path) | 145 text, warnings = self._Render(document) |
136 self.assertEqual(expected_document, text) | 146 self.assertEqual(expected_document, text) |
137 self.assertEqual([], warnings) | 147 self.assertEqual([], warnings) |
138 | 148 |
139 text, warnings = self._renderer.Render(document, path, render_title=True) | 149 text, warnings = self._Render(document, render_title=True) |
140 self.assertEqual(expected_document, text) | 150 self.assertEqual(expected_document, text) |
141 self.assertEqual(['Expected a title'], warnings) | 151 self.assertEqual(['Expected a title'], warnings) |
142 | 152 |
143 | 153 |
144 if __name__ == '__main__': | 154 if __name__ == '__main__': |
145 unittest.main() | 155 unittest.main() |
OLD | NEW |