Upstream version 5.34.104.0
[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 extensions_paths import SERVER2
12 from server_instance import ServerInstance
13 from template_data_source import TemplateDataSource
14 from test_util import DisableLogging, ReadFile, Server2Path
15 from third_party.handlebar import Handlebar
16
17
18 class TemplateDataSourceTest(unittest.TestCase):
19
20   def _ReadFile(self, *path):
21     return ReadFile(SERVER2, 'test_data', 'template_data_source', *path)
22
23   def _CreateTemplateDataSource(self, partial_dir):
24     return TemplateDataSource(
25         ServerInstance.ForLocal(),
26         None,  # Request
27         partial_dir='%stest_data/template_data_source/%s/' %
28                     (SERVER2, partial_dir))
29
30   def testSimple(self):
31     template_data_source = self._CreateTemplateDataSource('simple')
32     template_a1 = Handlebar(self._ReadFile('simple', 'test1.html'))
33     context = [{}, {'templates': {}}]
34     self.assertEqual(
35         template_a1.Render(*context).text,
36         template_data_source.get('test1').Render(*context).text)
37     template_a2 = Handlebar(self._ReadFile('simple', 'test2.html'))
38     self.assertEqual(
39         template_a2.Render(*context).text,
40         template_data_source.get('test2').Render(*context).text)
41
42   @DisableLogging('warning')
43   def testNotFound(self):
44     template_data_source = self._CreateTemplateDataSource('simple')
45     self.assertEqual(None, template_data_source.get('junk'))
46
47   @DisableLogging('warning')
48   def testPartials(self):
49     template_data_source = self._CreateTemplateDataSource('partials')
50     context = json.loads(self._ReadFile('partials', 'input.json'))
51     self.assertEqual(
52         self._ReadFile('partials', 'test_expected.html'),
53         template_data_source.get('test_tmpl').Render(
54             context, template_data_source).text)
55
56
57 if __name__ == '__main__':
58   unittest.main()