- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / template_data_source_test.py
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 sys
9 import unittest
10
11 from server_instance import ServerInstance
12 from template_data_source import TemplateDataSource
13 from test_util import DisableLogging, ReadFile
14 from third_party.handlebar import Handlebar
15
16
17 class TemplateDataSourceTest(unittest.TestCase):
18
19   def setUp(self):
20     self._base_path = os.path.join(sys.path[0],
21                                    'test_data',
22                                    'template_data_source')
23
24   def _CreateTemplateDataSource(self, partial_dir):
25     return TemplateDataSource(
26         ServerInstance.ForLocal(),
27         None,  # Request
28         partial_dir='docs/server2/test_data/template_data_source/%s' %
29                     partial_dir)
30
31   def testSimple(self):
32     template_data_source = self._CreateTemplateDataSource('simple')
33     template_a1 = Handlebar(ReadFile(self._base_path, 'simple', 'test1.html'))
34     context = [{}, {'templates': {}}]
35     self.assertEqual(
36         template_a1.Render(*context).text,
37         template_data_source.get('test1').Render(*context).text)
38     template_a2 = Handlebar(ReadFile(self._base_path, 'simple', 'test2.html'))
39     self.assertEqual(
40         template_a2.Render(*context).text,
41         template_data_source.get('test2').Render(*context).text)
42
43   @DisableLogging('warning')
44   def testNotFound(self):
45     template_data_source = self._CreateTemplateDataSource('simple')
46     self.assertEqual(None, template_data_source.get('junk'))
47
48   @DisableLogging('warning')
49   def testPartials(self):
50     template_data_source = self._CreateTemplateDataSource('partials')
51     context = json.loads(ReadFile(self._base_path, 'partials', 'input.json'))
52     self.assertEqual(
53         ReadFile(self._base_path, 'partials', 'test_expected.html'),
54         template_data_source.get('test_tmpl').Render(
55             context, template_data_source).text)
56
57
58 if __name__ == '__main__':
59   unittest.main()