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