template_data_source_test.py revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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 6import json 7import os 8import sys 9import unittest 10 11from server_instance import ServerInstance 12from template_data_source import TemplateDataSource 13from test_util import DisableLogging, ReadFile 14from third_party.handlebar import Handlebar 15 16 17class 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 58if __name__ == '__main__': 59 unittest.main() 60