| 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 unittest | 7 import unittest |
| 8 | 8 |
| 9 from extensions_paths import SERVER2 | 9 from extensions_paths import SERVER2 |
| 10 from server_instance import ServerInstance | 10 from server_instance import ServerInstance |
| 11 from template_data_source import TemplateDataSource | 11 from template_data_source import TemplateDataSource |
| 12 from test_util import DisableLogging, ReadFile | 12 from test_util import DisableLogging, ReadFile |
| 13 from third_party.handlebar import Handlebar | 13 from third_party.motemplate import Motemplate |
| 14 | 14 |
| 15 def _ReadFile(*path): | 15 def _ReadFile(*path): |
| 16 return ReadFile(SERVER2, 'test_data', 'template_data_source', *path) | 16 return ReadFile(SERVER2, 'test_data', 'template_data_source', *path) |
| 17 | 17 |
| 18 def _CreateTestDataSource(base_dir): | 18 def _CreateTestDataSource(base_dir): |
| 19 '''TemplateDataSource is not instantiated directly, rather, its methods | 19 '''TemplateDataSource is not instantiated directly, rather, its methods |
| 20 are invoked through a subclass of it, which has as its only data the | 20 are invoked through a subclass of it, which has as its only data the |
| 21 directory in which TemplateDataSource methods should act on. Thus, we test | 21 directory in which TemplateDataSource methods should act on. Thus, we test |
| 22 TemplateDataSource indirectly through the TestDataSource class | 22 TemplateDataSource indirectly through the TestDataSource class |
| 23 ''' | 23 ''' |
| 24 return TestDataSource(ServerInstance.ForLocal(), | 24 return TestDataSource(ServerInstance.ForLocal(), |
| 25 '%stest_data/template_data_source/%s/' % | 25 '%stest_data/template_data_source/%s/' % |
| 26 (SERVER2, base_dir)) | 26 (SERVER2, base_dir)) |
| 27 | 27 |
| 28 | 28 |
| 29 class TestDataSource(TemplateDataSource): | 29 class TestDataSource(TemplateDataSource): |
| 30 '''Provides a subclass we can use to test the TemplateDataSource methods | 30 '''Provides a subclass we can use to test the TemplateDataSource methods |
| 31 ''' | 31 ''' |
| 32 def __init__(self, server_instance, base_dir): | 32 def __init__(self, server_instance, base_dir): |
| 33 type(self)._BASE = base_dir | 33 type(self)._BASE = base_dir |
| 34 TemplateDataSource.__init__(self, server_instance) | 34 TemplateDataSource.__init__(self, server_instance) |
| 35 | 35 |
| 36 | 36 |
| 37 class TemplateDataSourceTest(unittest.TestCase): | 37 class TemplateDataSourceTest(unittest.TestCase): |
| 38 | 38 |
| 39 def testSimple(self): | 39 def testSimple(self): |
| 40 test_data_source = _CreateTestDataSource('simple') | 40 test_data_source = _CreateTestDataSource('simple') |
| 41 template_a1 = Handlebar(_ReadFile('simple', 'test1.html')) | 41 template_a1 = Motemplate(_ReadFile('simple', 'test1.html')) |
| 42 context = [{}, {'templates': {}}] | 42 context = [{}, {'templates': {}}] |
| 43 self.assertEqual( | 43 self.assertEqual( |
| 44 template_a1.Render(*context).text, | 44 template_a1.Render(*context).text, |
| 45 test_data_source.get('test1').Render(*context).text) | 45 test_data_source.get('test1').Render(*context).text) |
| 46 template_a2 = Handlebar(_ReadFile('simple', 'test2.html')) | 46 template_a2 = Motemplate(_ReadFile('simple', 'test2.html')) |
| 47 self.assertEqual( | 47 self.assertEqual( |
| 48 template_a2.Render(*context).text, | 48 template_a2.Render(*context).text, |
| 49 test_data_source.get('test2').Render(*context).text) | 49 test_data_source.get('test2').Render(*context).text) |
| 50 | 50 |
| 51 @DisableLogging('warning') | 51 @DisableLogging('warning') |
| 52 def testNotFound(self): | 52 def testNotFound(self): |
| 53 test_data_source = _CreateTestDataSource('simple') | 53 test_data_source = _CreateTestDataSource('simple') |
| 54 self.assertEqual(None, test_data_source.get('junk')) | 54 self.assertEqual(None, test_data_source.get('junk')) |
| 55 | 55 |
| 56 @DisableLogging('warning') | 56 @DisableLogging('warning') |
| 57 def testPartials(self): | 57 def testPartials(self): |
| 58 test_data_source = _CreateTestDataSource('partials') | 58 test_data_source = _CreateTestDataSource('partials') |
| 59 context = json.loads(_ReadFile('partials', 'input.json')) | 59 context = json.loads(_ReadFile('partials', 'input.json')) |
| 60 self.assertEqual( | 60 self.assertEqual( |
| 61 _ReadFile('partials', 'test_expected.html'), | 61 _ReadFile('partials', 'test_expected.html'), |
| 62 test_data_source.get('test_tmpl').Render( | 62 test_data_source.get('test_tmpl').Render( |
| 63 context, test_data_source).text) | 63 context, test_data_source).text) |
| 64 | 64 |
| 65 | 65 |
| 66 if __name__ == '__main__': | 66 if __name__ == '__main__': |
| 67 unittest.main() | 67 unittest.main() |
| OLD | NEW |