OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 import json | |
7 import os | |
8 import unittest | |
9 | |
10 from local_fetcher import LocalFetcher | |
11 from template_data_source import TemplateDataSource | |
12 from third_party.handlebar import Handlebar | |
13 | |
14 class TemplateDataSourceTest(unittest.TestCase): | |
15 def setUp(self): | |
16 self._base_path = os.path.join('test_data', 'template_data_source') | |
17 | |
18 def _ReadLocalFile(self, filename): | |
19 with open(os.path.join(self._base_path, filename), 'r') as f: | |
20 return f.read() | |
21 | |
22 def _RenderTest(self, name, data_source): | |
23 template_name = name + '_tmpl.html' | |
24 template = Handlebar(self._ReadLocalFile(template_name)) | |
25 self.assertEquals(self._ReadLocalFile(name + '_expected.html'), | |
26 data_source.Render(template_name, | |
27 self._ReadLocalFile(name + '.json'))) | |
not at google - send to devlin
2012/06/08 02:23:44
Btw, in general, if you think it's more readable t
| |
28 | |
29 def testSimple(self): | |
30 self._base_path = os.path.join(self._base_path, 'simple') | |
31 fetcher = LocalFetcher(self._base_path) | |
32 t_data_source = TemplateDataSource(fetcher, ['./'], 0) | |
33 | |
34 template_a1 = Handlebar(self._ReadLocalFile('test1.html')) | |
35 self.assertEqual(template_a1.render({}, {'templates': {}}).text, | |
36 t_data_source['test1'].render({}, {'templates': {}}).text) | |
37 | |
38 template_a2 = Handlebar(self._ReadLocalFile('test2.html')) | |
39 self.assertEqual(template_a2.render({}, {'templates': {}}).text, | |
40 t_data_source['test2'].render({}, {'templates': {}}).text) | |
41 | |
42 self.assertEqual(None, t_data_source['junk.html']) | |
43 | |
44 def testPartials(self): | |
45 self._base_path = os.path.join(self._base_path, 'partials') | |
46 fetcher = LocalFetcher(self._base_path) | |
47 t_data_source = TemplateDataSource(fetcher, ['./'], 0) | |
48 | |
49 self.assertEqual(self._ReadLocalFile('test.html'), | |
50 t_data_source['test_tmpl'].render( | |
51 json.loads(self._ReadLocalFile('input.json')), t_data_source).text) | |
52 | |
53 def testRender(self): | |
54 self._base_path = os.path.join(self._base_path, 'render') | |
55 fetcher = LocalFetcher(self._base_path) | |
56 t_data_source = TemplateDataSource(fetcher, ['./'], 0) | |
57 self._RenderTest('test1', t_data_source) | |
58 self._RenderTest('test2', t_data_source) | |
59 | |
60 | |
61 | |
62 if __name__ == '__main__': | |
63 unittest.main() | |
OLD | NEW |