Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: chrome/common/extensions/docs/server2/document_renderer_test.py

Issue 441053004: Docserver: Modify DocumentRenderer to generate absolute links (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 def _ConstructString(*strs):
not at google - send to devlin 2014/08/06 23:00:48 do you need this? you can have multi-line string i
15 return ''.join(strs)
16
17
14 class DocumentRendererUnittest(unittest.TestCase): 18 class DocumentRendererUnittest(unittest.TestCase):
15 def setUp(self): 19 def setUp(self):
16 self._renderer = ServerInstance.ForTest( 20 self._renderer = ServerInstance.ForTest(
17 TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA)).document_renderer 21 TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA)).document_renderer
22 self._path = 'apps/some/path/to/document.html'
23 self._host = 'localhost:8000'
24
25 def _Render(self, document, render_title=False):
26 return self._renderer.Render(document,
27 self._host,
28 self._path,
29 render_title=render_title)
18 30
19 def testNothingToSubstitute(self): 31 def testNothingToSubstitute(self):
20 document = 'hello world' 32 document = 'hello world'
21 path = 'apps/some/path/to/document.html'
22 33
23 text, warnings = self._renderer.Render(document, path) 34 text, warnings = self._Render(document)
24 self.assertEqual(document, text) 35 self.assertEqual(document, text)
25 self.assertEqual([], warnings) 36 self.assertEqual([], warnings)
26 37
27 text, warnings = self._renderer.Render(document, path, render_title=True) 38 text, warnings = self._Render(document, render_title=True)
28 self.assertEqual(document, text) 39 self.assertEqual(document, text)
29 self.assertEqual(['Expected a title'], warnings) 40 self.assertEqual(['Expected a title'], warnings)
30 41
31 def testTitles(self): 42 def testTitles(self):
32 document = '<h1>title</h1> then $(title) then another $(title)' 43 document = '<h1>title</h1> then $(title) then another $(title)'
33 path = 'apps/some/path/to/document.html'
34 44
35 text, warnings = self._renderer.Render(document, path) 45 text, warnings = self._Render(document)
36 self.assertEqual(document, text) 46 self.assertEqual(document, text)
37 self.assertEqual(['Found unexpected title "title"'], warnings) 47 self.assertEqual(['Found unexpected title "title"'], warnings)
38 48
39 text, warnings = self._renderer.Render(document, path, render_title=True) 49 text, warnings = self._Render(document, render_title=True)
40 self.assertEqual('<h1>title</h1> then title then another $(title)', text) 50 self.assertEqual('<h1>title</h1> then title then another $(title)', text)
41 self.assertEqual([], warnings) 51 self.assertEqual([], warnings)
42 52
43 def testTocs(self): 53 def testTocs(self):
44 document = ('here is a toc $(table_of_contents) ' 54 document = ('here is a toc $(table_of_contents) '
45 'and another $(table_of_contents)') 55 'and another $(table_of_contents)')
46 expected_document = ('here is a toc <table-of-contents> and another ' 56 expected_document = ('here is a toc <table-of-contents> and another '
47 '$(table_of_contents)') 57 '$(table_of_contents)')
48 path = 'apps/some/path/to/document.html'
49 58
50 text, warnings = self._renderer.Render(document, path) 59 text, warnings = self._Render(document)
51 self.assertEqual(expected_document, text) 60 self.assertEqual(expected_document, text)
52 self.assertEqual([], warnings) 61 self.assertEqual([], warnings)
53 62
54 text, warnings = self._renderer.Render(document, path, render_title=True) 63 text, warnings = self._Render(document, render_title=True)
55 self.assertEqual(expected_document, text) 64 self.assertEqual(expected_document, text)
56 self.assertEqual(['Expected a title'], warnings) 65 self.assertEqual(['Expected a title'], warnings)
57 66
58 def testRefs(self): 67 def testRefs(self):
59 # The references in this and subsequent tests won't actually be resolved 68 # 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' 69 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> ' 70 expected_document = _ConstructString(
62 'here, <a href=#type-foo_t3>ref title</a> ' 71 'A ref <a href=localhost:8000/apps/#type-baz_e1>baz.baz_e1</a> here, ',
63 'there') 72 '<a href=localhost:8000/apps/#type-foo_t3>ref title</a> there')
64 path = 'apps/some/path/to/document.html'
65 73
66 text, warnings = self._renderer.Render(document, path) 74 text, warnings = self._Render(document)
67 self.assertEqual(expected_document, text) 75 self.assertEqual(expected_document, text)
68 self.assertEqual([], warnings) 76 self.assertEqual([], warnings)
69 77
70 text, warnings = self._renderer.Render(document, path, render_title=True) 78 text, warnings = self._Render(document, render_title=True)
71 self.assertEqual(expected_document, text) 79 self.assertEqual(expected_document, text)
72 self.assertEqual(['Expected a title'], warnings) 80 self.assertEqual(['Expected a title'], warnings)
73 81
74 def testTitleAndToc(self): 82 def testTitleAndToc(self):
75 document = '<h1>title</h1> $(title) and $(table_of_contents)' 83 document = '<h1>title</h1> $(title) and $(table_of_contents)'
76 path = 'apps/some/path/to/document.html'
77 84
78 text, warnings = self._renderer.Render(document, path) 85 text, warnings = self._Render(document)
79 self.assertEqual('<h1>title</h1> $(title) and <table-of-contents>', text) 86 self.assertEqual('<h1>title</h1> $(title) and <table-of-contents>', text)
80 self.assertEqual(['Found unexpected title "title"'], warnings) 87 self.assertEqual(['Found unexpected title "title"'], warnings)
81 88
82 text, warnings = self._renderer.Render(document, path, render_title=True) 89 text, warnings = self._Render(document, render_title=True)
83 self.assertEqual('<h1>title</h1> title and <table-of-contents>', text) 90 self.assertEqual('<h1>title</h1> title and <table-of-contents>', text)
84 self.assertEqual([], warnings) 91 self.assertEqual([], warnings)
85 92
86 def testRefInTitle(self): 93 def testRefInTitle(self):
87 document = '<h1>$(ref:baz.baz_e1 title)</h1> A $(title) was here' 94 document = '<h1>$(ref:baz.baz_e1 title)</h1> A $(title) was here'
88 expected_document_no_title = ('<h1><a href=#type-baz_e1>' 95 href = 'localhost:8000/apps/#type-baz_e1'
89 'title</a></h1> A $(title) was here') 96 expected_document_no_title = _ConstructString(
97 '<h1><a href=%s>title</a></h1> A $(title) was here' % href)
90 98
91 expected_document = ('<h1><a href=#type-baz_e1>title</a></h1>' 99 expected_document = _ConstructString(
92 ' A title was here') 100 '<h1><a href=%s>title</a></h1> A title was here' % href)
93 path = 'apps/some/path/to/document.html'
94 101
95 text, warnings = self._renderer.Render(document, path) 102 text, warnings = self._Render(document)
96 self.assertEqual(expected_document_no_title, text) 103 self.assertEqual(expected_document_no_title, text)
97 self.assertEqual([('Found unexpected title "title"')], warnings) 104 self.assertEqual([('Found unexpected title "title"')], warnings)
98 105
99 text, warnings = self._renderer.Render(document, path, render_title=True) 106 text, warnings = self._Render(document, render_title=True)
100 self.assertEqual(expected_document, text) 107 self.assertEqual(expected_document, text)
101 self.assertEqual([], warnings) 108 self.assertEqual([], warnings)
102 109
103 def testRefSplitAcrossLines(self): 110 def testRefSplitAcrossLines(self):
104 document = 'Hello, $(ref:baz.baz_e1 world). A $(ref:foo.foo_t3\n link)' 111 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=' 112 expected_document = _ConstructString(
106 '#type-foo_t3>link</a>') 113 'Hello, <a href=localhost:8000/apps/#type-baz_e1>world</a>. ',
114 'A <a href=localhost:8000/apps/#type-foo_t3>link</a>')
107 115
108 path = 'apps/some/path/to/document.html'
109 116
110 text, warnings = self._renderer.Render(document, path) 117 text, warnings = self._Render(document)
111 self.assertEqual(expected_document, text) 118 self.assertEqual(expected_document, text)
112 self.assertEqual([], warnings) 119 self.assertEqual([], warnings)
113 120
114 text, warnings = self._renderer.Render(document, path, render_title=True) 121 text, warnings = self._Render(document, render_title=True)
115 self.assertEqual(expected_document, text) 122 self.assertEqual(expected_document, text)
116 self.assertEqual(['Expected a title'], warnings) 123 self.assertEqual(['Expected a title'], warnings)
117 124
118 def testInvalidRef(self): 125 def testInvalidRef(self):
119 # DocumentRenderer attempts to detect unclosed $(ref:...) tags by limiting 126 # DocumentRenderer attempts to detect unclosed $(ref:...) tags by limiting
120 # how far it looks ahead. Lorem Ipsum should be long enough to trigger that. 127 # how far it looks ahead. Lorem Ipsum should be long enough to trigger that.
121 _LOREM_IPSUM = ( 128 _LOREM_IPSUM = (
122 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ' 129 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do '
123 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ' 130 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim '
124 'ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut ' 131 'ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut '
125 'aliquip ex ea commodo consequat. Duis aute irure dolor in ' 132 'aliquip ex ea commodo consequat. Duis aute irure dolor in '
126 'reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla ' 133 'reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla '
127 'pariatur. Excepteur sint occaecat cupidatat non proident, sunt in ' 134 'pariatur. Excepteur sint occaecat cupidatat non proident, sunt in '
128 'culpa qui officia deserunt mollit anim id est laborum.') 135 'culpa qui officia deserunt mollit anim id est laborum.')
129 document = ('An invalid $(ref:foo.foo_t3 a title ' + _LOREM_IPSUM + 136 document = _ConstructString('An invalid $(ref:foo.foo_t3 a title ',
130 '$(ref:baz.baz_e1) here') 137 _LOREM_IPSUM,
131 expected_document = ('An invalid $(ref:foo.foo_t3 a title ' + _LOREM_IPSUM + 138 '$(ref:baz.baz_e1) here')
132 '<a href=#type-baz_e1>baz.baz_e1</a> here') 139 expected_document = _ConstructString(
133 path = 'apps/some/path/to/document_api.html' 140 'An invalid $(ref:foo.foo_t3 a title ',
141 _LOREM_IPSUM +
142 '<a href=localhost:8000/apps/#type-baz_e1>baz.baz_e1</a> here')
134 143
135 text, warnings = self._renderer.Render(document, path) 144 text, warnings = self._Render(document)
136 self.assertEqual(expected_document, text) 145 self.assertEqual(expected_document, text)
137 self.assertEqual([], warnings) 146 self.assertEqual([], warnings)
138 147
139 text, warnings = self._renderer.Render(document, path, render_title=True) 148 text, warnings = self._Render(document, render_title=True)
140 self.assertEqual(expected_document, text) 149 self.assertEqual(expected_document, text)
141 self.assertEqual(['Expected a title'], warnings) 150 self.assertEqual(['Expected a title'], warnings)
142 151
143 152
144 if __name__ == '__main__': 153 if __name__ == '__main__':
145 unittest.main() 154 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698