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 from server_instance import ServerInstance | |
7 import svn_constants | |
8 from test_file_system import TestFileSystem | |
9 import unittest | 6 import unittest |
10 | 7 |
11 _TEST_DATA = TestFileSystem.MoveTo(svn_constants.INTRO_PATH, { | 8 from file_system import FileNotFoundError |
12 'test.html': '<h1>hello</h1>world<h2>first</h2><h3>inner</h3><h2>second</h2>' | 9 from intro_data_source import _MakeIntroDict, IntroDataSource |
13 }) | 10 |
| 11 class MockRefResolver(object): |
| 12 class Factory(object): |
| 13 def Create(self): |
| 14 return False |
| 15 |
| 16 def ResolveAllLinks(self, text, namespace): |
| 17 return text |
| 18 |
| 19 class MockCompiledFileSystem(object): |
| 20 """Mocks a Compiled File System. Demonstrates that IntroDataSource uses the |
| 21 same Compiled File System for all children. |
| 22 """ |
| 23 class Factory(object): |
| 24 def Create(self, _, __): |
| 25 return MockCompiledFileSystem() |
| 26 def CreateIdentity(self, _): |
| 27 return MockCompiledFileSystem() |
| 28 |
| 29 def GetFromFile(self, path): |
| 30 if path == 'top/topfile.html': |
| 31 return 'topfile' |
| 32 elif path == 'top/dir-one/file.html': |
| 33 return 'dir-one-file' |
| 34 |
| 35 raise FileNotFoundError("not important") |
| 36 |
| 37 def GetFromFileListing(self, path): |
| 38 if path == 'top/dir-one': |
| 39 return True |
14 | 40 |
15 class IntroDataSourceTest(unittest.TestCase): | 41 class IntroDataSourceTest(unittest.TestCase): |
16 def setUp(self): | |
17 self._server = ServerInstance.CreateForTest(TestFileSystem(_TEST_DATA)) | |
18 | |
19 def testIntro(self): | 42 def testIntro(self): |
20 intro_data_source = self._server.intro_data_source_factory.Create() | 43 data = _MakeIntroDict(MockRefResolver(), 'path', |
21 data = intro_data_source.get('test') | 44 '<h1>hello</h1>world<h2>first</h2><h3>inner</h3><h2>second</h2>') |
22 self.assertEqual('hello', data['title']) | 45 self.assertEqual('hello', data['title']) |
23 # TODO(kalman): test links. | |
24 expected_toc = [{'subheadings': [{'link': '', 'title': u'inner'}], | 46 expected_toc = [{'subheadings': [{'link': '', 'title': u'inner'}], |
25 'link': '', | 47 'link': '', 'title': u'first'}, |
26 'title': u'first'}, | |
27 {'subheadings': [], 'link': '', 'title': u'second'}] | 48 {'subheadings': [], 'link': '', 'title': u'second'}] |
28 self.assertEqual(expected_toc, data['apps_toc']) | 49 self.assertEqual(expected_toc, data['apps_toc']) |
29 self.assertEqual(expected_toc, data['extensions_toc']) | 50 self.assertEqual(expected_toc, data['extensions_toc']) |
30 self.assertEqual('world<h2>first</h2><h3>inner</h3><h2>second</h2>', | 51 |
31 data['intro'].render().text) | 52 def testLookup(self): |
| 53 ids = IntroDataSource.Factory( |
| 54 MockCompiledFileSystem.Factory(), MockRefResolver.Factory(), |
| 55 ['top']).Create() |
| 56 |
| 57 self.assertEqual('topfile', ids.get('topfile')) |
| 58 # Simulates how a template would access a subdirectory. |
| 59 self.assertEqual('dir-one-file', ids.get('dir-one').get('file')) |
32 | 60 |
33 if __name__ == '__main__': | 61 if __name__ == '__main__': |
34 unittest.main() | 62 unittest.main() |
OLD | NEW |