| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 json | 6 import json |
| 7 import os | 7 import os |
| 8 import sys | 8 import sys |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 data_source.Render(template_name)) | 50 data_source.Render(template_name)) |
| 51 | 51 |
| 52 def _CreateTemplateDataSource(self, compiled_fs_factory, api_data=None): | 52 def _CreateTemplateDataSource(self, compiled_fs_factory, api_data=None): |
| 53 if api_data is None: | 53 if api_data is None: |
| 54 api_data_factory = APIDataSource.Factory(compiled_fs_factory, 'fake_path') | 54 api_data_factory = APIDataSource.Factory(compiled_fs_factory, 'fake_path') |
| 55 else: | 55 else: |
| 56 api_data_factory = _FakeFactory(api_data) | 56 api_data_factory = _FakeFactory(api_data) |
| 57 reference_resolver_factory = ReferenceResolver.Factory( | 57 reference_resolver_factory = ReferenceResolver.Factory( |
| 58 api_data_factory, | 58 api_data_factory, |
| 59 self._fake_api_list_data_source_factory, | 59 self._fake_api_list_data_source_factory, |
| 60 ObjectStoreCreator.TestFactory()) | 60 ObjectStoreCreator.ForTest()) |
| 61 @DisableLogging('error') # "was never set" error | 61 @DisableLogging('error') # "was never set" error |
| 62 def create_from_factory(factory): | 62 def create_from_factory(factory): |
| 63 path = 'extensions/foo' | 63 path = 'extensions/foo' |
| 64 return factory.Create(Request(path, {}), path) | 64 return factory.Create(Request(path, {}), path) |
| 65 return create_from_factory(TemplateDataSource.Factory( | 65 return create_from_factory(TemplateDataSource.Factory( |
| 66 'fake_channel', | 66 'fake_channel', |
| 67 api_data_factory, | 67 api_data_factory, |
| 68 self._fake_api_list_data_source_factory, | 68 self._fake_api_list_data_source_factory, |
| 69 self._fake_intro_data_source_factory, | 69 self._fake_intro_data_source_factory, |
| 70 self._fake_samples_data_source_factory, | 70 self._fake_samples_data_source_factory, |
| 71 self._fake_sidenav_data_source_factory, | 71 self._fake_sidenav_data_source_factory, |
| 72 compiled_fs_factory, | 72 compiled_fs_factory, |
| 73 reference_resolver_factory, | 73 reference_resolver_factory, |
| 74 '.', | 74 '.', |
| 75 '.')) | 75 '.')) |
| 76 | 76 |
| 77 def testSimple(self): | 77 def testSimple(self): |
| 78 self._base_path = os.path.join(self._base_path, 'simple') | 78 self._base_path = os.path.join(self._base_path, 'simple') |
| 79 fetcher = LocalFileSystem(self._base_path) | 79 fetcher = LocalFileSystem(self._base_path) |
| 80 compiled_fs_factory = CompiledFileSystem.Factory( | 80 compiled_fs_factory = CompiledFileSystem.Factory( |
| 81 fetcher, | 81 fetcher, |
| 82 ObjectStoreCreator.TestFactory()) | 82 ObjectStoreCreator.ForTest()) |
| 83 t_data_source = self._CreateTemplateDataSource( | 83 t_data_source = self._CreateTemplateDataSource( |
| 84 compiled_fs_factory, | 84 compiled_fs_factory, |
| 85 ObjectStoreCreator.TestFactory()) | 85 ObjectStoreCreator.ForTest()) |
| 86 template_a1 = Handlebar(self._ReadLocalFile('test1.html')) | 86 template_a1 = Handlebar(self._ReadLocalFile('test1.html')) |
| 87 self.assertEqual(template_a1.render({}, {'templates': {}}).text, | 87 self.assertEqual(template_a1.render({}, {'templates': {}}).text, |
| 88 t_data_source.get('test1').render({}, {'templates': {}}).text) | 88 t_data_source.get('test1').render({}, {'templates': {}}).text) |
| 89 | 89 |
| 90 template_a2 = Handlebar(self._ReadLocalFile('test2.html')) | 90 template_a2 = Handlebar(self._ReadLocalFile('test2.html')) |
| 91 self.assertEqual(template_a2.render({}, {'templates': {}}).text, | 91 self.assertEqual(template_a2.render({}, {'templates': {}}).text, |
| 92 t_data_source.get('test2').render({}, {'templates': {}}).text) | 92 t_data_source.get('test2').render({}, {'templates': {}}).text) |
| 93 | 93 |
| 94 @DisableLogging('warning') | 94 @DisableLogging('warning') |
| 95 def testNotFound(self): | 95 def testNotFound(self): |
| 96 self._base_path = os.path.join(self._base_path, 'simple') | 96 self._base_path = os.path.join(self._base_path, 'simple') |
| 97 fetcher = LocalFileSystem(self._base_path) | 97 fetcher = LocalFileSystem(self._base_path) |
| 98 compiled_fs_factory = CompiledFileSystem.Factory( | 98 compiled_fs_factory = CompiledFileSystem.Factory( |
| 99 fetcher, | 99 fetcher, |
| 100 ObjectStoreCreator.TestFactory()) | 100 ObjectStoreCreator.ForTest()) |
| 101 t_data_source = self._CreateTemplateDataSource( | 101 t_data_source = self._CreateTemplateDataSource( |
| 102 compiled_fs_factory, | 102 compiled_fs_factory, |
| 103 ObjectStoreCreator.TestFactory()) | 103 ObjectStoreCreator.ForTest()) |
| 104 self.assertEqual(None, t_data_source.get('junk.html')) | 104 self.assertEqual(None, t_data_source.get('junk.html')) |
| 105 | 105 |
| 106 def testPartials(self): | 106 def testPartials(self): |
| 107 self._base_path = os.path.join(self._base_path, 'partials') | 107 self._base_path = os.path.join(self._base_path, 'partials') |
| 108 fetcher = LocalFileSystem(self._base_path) | 108 fetcher = LocalFileSystem(self._base_path) |
| 109 compiled_fs_factory = CompiledFileSystem.Factory( | 109 compiled_fs_factory = CompiledFileSystem.Factory( |
| 110 fetcher, | 110 fetcher, |
| 111 ObjectStoreCreator.TestFactory()) | 111 ObjectStoreCreator.ForTest()) |
| 112 t_data_source = self._CreateTemplateDataSource(compiled_fs_factory) | 112 t_data_source = self._CreateTemplateDataSource(compiled_fs_factory) |
| 113 self.assertEqual( | 113 self.assertEqual( |
| 114 self._ReadLocalFile('test_expected.html'), | 114 self._ReadLocalFile('test_expected.html'), |
| 115 t_data_source.get('test_tmpl').render( | 115 t_data_source.get('test_tmpl').render( |
| 116 json.loads(self._ReadLocalFile('input.json')), t_data_source).text) | 116 json.loads(self._ReadLocalFile('input.json')), t_data_source).text) |
| 117 | 117 |
| 118 def testRender(self): | 118 def testRender(self): |
| 119 self._base_path = os.path.join(self._base_path, 'render') | 119 self._base_path = os.path.join(self._base_path, 'render') |
| 120 fetcher = LocalFileSystem(self._base_path) | 120 fetcher = LocalFileSystem(self._base_path) |
| 121 context = json.loads(self._ReadLocalFile('test1.json')) | 121 context = json.loads(self._ReadLocalFile('test1.json')) |
| 122 compiled_fs_factory = CompiledFileSystem.Factory( | 122 compiled_fs_factory = CompiledFileSystem.Factory( |
| 123 fetcher, | 123 fetcher, |
| 124 ObjectStoreCreator.TestFactory()) | 124 ObjectStoreCreator.ForTest()) |
| 125 self._RenderTest( | 125 self._RenderTest( |
| 126 'test1', | 126 'test1', |
| 127 self._CreateTemplateDataSource( | 127 self._CreateTemplateDataSource( |
| 128 compiled_fs_factory, | 128 compiled_fs_factory, |
| 129 api_data=json.loads(self._ReadLocalFile('test1.json')))) | 129 api_data=json.loads(self._ReadLocalFile('test1.json')))) |
| 130 self._RenderTest( | 130 self._RenderTest( |
| 131 'test2', | 131 'test2', |
| 132 self._CreateTemplateDataSource( | 132 self._CreateTemplateDataSource( |
| 133 compiled_fs_factory, | 133 compiled_fs_factory, |
| 134 api_data=json.loads(self._ReadLocalFile('test2.json')))) | 134 api_data=json.loads(self._ReadLocalFile('test2.json')))) |
| 135 | 135 |
| 136 if __name__ == '__main__': | 136 if __name__ == '__main__': |
| 137 unittest.main() | 137 unittest.main() |
| OLD | NEW |