| 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 unittest | 8 import unittest |
| 9 | 9 |
| 10 from fetcher_cache import FetcherCache |
| 10 from local_fetcher import LocalFetcher | 11 from local_fetcher import LocalFetcher |
| 11 from template_data_source import TemplateDataSource | 12 from template_data_source import TemplateDataSource |
| 12 from third_party.handlebar import Handlebar | 13 from third_party.handlebar import Handlebar |
| 13 | 14 |
| 14 class TemplateDataSourceTest(unittest.TestCase): | 15 class TemplateDataSourceTest(unittest.TestCase): |
| 15 def setUp(self): | 16 def setUp(self): |
| 16 self._base_path = os.path.join('test_data', 'template_data_source') | 17 self._base_path = os.path.join('test_data', 'template_data_source') |
| 17 | 18 |
| 18 def _ReadLocalFile(self, filename): | 19 def _ReadLocalFile(self, filename): |
| 19 with open(os.path.join(self._base_path, filename), 'r') as f: | 20 with open(os.path.join(self._base_path, filename), 'r') as f: |
| 20 return f.read() | 21 return f.read() |
| 21 | 22 |
| 22 def _RenderTest(self, name, data_source): | 23 def _RenderTest(self, name, data_source): |
| 23 template_name = name + '_tmpl.html' | 24 template_name = name + '_tmpl.html' |
| 24 template = Handlebar(self._ReadLocalFile(template_name)) | 25 template = Handlebar(self._ReadLocalFile(template_name)) |
| 25 self.assertEquals(self._ReadLocalFile(name + '_expected.html'), | 26 context = json.loads(self._ReadLocalFile(name + '.json')) |
| 26 data_source.Render(template_name, | 27 self.assertEquals( |
| 27 self._ReadLocalFile(name + '.json'))) | 28 self._ReadLocalFile(name + '_expected.html'), |
| 29 data_source.Render(template_name, context)) |
| 28 | 30 |
| 29 def testSimple(self): | 31 def testSimple(self): |
| 30 self._base_path = os.path.join(self._base_path, 'simple') | 32 self._base_path = os.path.join(self._base_path, 'simple') |
| 31 fetcher = LocalFetcher(self._base_path) | 33 fetcher = LocalFetcher(self._base_path) |
| 32 t_data_source = TemplateDataSource(fetcher, ['./'], 0) | 34 cache_builder = FetcherCache.Builder(fetcher, 0) |
| 35 t_data_source = TemplateDataSource(cache_builder, ['./']) |
| 33 | 36 |
| 34 template_a1 = Handlebar(self._ReadLocalFile('test1.html')) | 37 template_a1 = Handlebar(self._ReadLocalFile('test1.html')) |
| 35 self.assertEqual(template_a1.render({}, {'templates': {}}).text, | 38 self.assertEqual(template_a1.render({}, {'templates': {}}).text, |
| 36 t_data_source['test1'].render({}, {'templates': {}}).text) | 39 t_data_source['test1'].render({}, {'templates': {}}).text) |
| 37 | 40 |
| 38 template_a2 = Handlebar(self._ReadLocalFile('test2.html')) | 41 template_a2 = Handlebar(self._ReadLocalFile('test2.html')) |
| 39 self.assertEqual(template_a2.render({}, {'templates': {}}).text, | 42 self.assertEqual(template_a2.render({}, {'templates': {}}).text, |
| 40 t_data_source['test2'].render({}, {'templates': {}}).text) | 43 t_data_source['test2'].render({}, {'templates': {}}).text) |
| 41 | 44 |
| 42 self.assertEqual(None, t_data_source['junk.html']) | 45 self.assertEqual(None, t_data_source['junk.html']) |
| 43 | 46 |
| 44 def testPartials(self): | 47 def testPartials(self): |
| 45 self._base_path = os.path.join(self._base_path, 'partials') | 48 self._base_path = os.path.join(self._base_path, 'partials') |
| 46 fetcher = LocalFetcher(self._base_path) | 49 fetcher = LocalFetcher(self._base_path) |
| 47 t_data_source = TemplateDataSource(fetcher, ['./'], 0) | 50 cache_builder = FetcherCache.Builder(fetcher, 0) |
| 51 t_data_source = TemplateDataSource(cache_builder, ['./']) |
| 48 | 52 |
| 49 self.assertEqual(self._ReadLocalFile('test.html'), | 53 self.assertEqual(self._ReadLocalFile('test.html'), |
| 50 t_data_source['test_tmpl'].render( | 54 t_data_source['test_tmpl'].render( |
| 51 json.loads(self._ReadLocalFile('input.json')), t_data_source).text) | 55 json.loads(self._ReadLocalFile('input.json')), t_data_source).text) |
| 52 | 56 |
| 53 def testRender(self): | 57 def testRender(self): |
| 54 self._base_path = os.path.join(self._base_path, 'render') | 58 self._base_path = os.path.join(self._base_path, 'render') |
| 55 fetcher = LocalFetcher(self._base_path) | 59 fetcher = LocalFetcher(self._base_path) |
| 56 t_data_source = TemplateDataSource(fetcher, ['./'], 0) | 60 cache_builder = FetcherCache.Builder(fetcher, 0) |
| 61 t_data_source = TemplateDataSource(cache_builder, ['./']) |
| 57 self._RenderTest('test1', t_data_source) | 62 self._RenderTest('test1', t_data_source) |
| 58 self._RenderTest('test2', t_data_source) | 63 self._RenderTest('test2', t_data_source) |
| 59 | 64 |
| 60 | 65 |
| 61 | 66 |
| 62 if __name__ == '__main__': | 67 if __name__ == '__main__': |
| 63 unittest.main() | 68 unittest.main() |
| OLD | NEW |