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.
9 from extensions_paths
import SERVER2
10 from server_instance
import ServerInstance
11 from template_data_source
import TemplateDataSource
12 from test_util
import DisableLogging
, ReadFile
13 from third_party
.motemplate
import Motemplate
16 return ReadFile(SERVER2
, 'test_data', 'template_data_source', *path
)
18 def _CreateTestDataSource(base_dir
):
19 '''TemplateDataSource is not instantiated directly, rather, its methods
20 are invoked through a subclass of it, which has as its only data the
21 directory in which TemplateDataSource methods should act on. Thus, we test
22 TemplateDataSource indirectly through the TestDataSource class
24 return TestDataSource(ServerInstance
.ForLocal(),
25 '%stest_data/template_data_source/%s/' %
29 class TestDataSource(TemplateDataSource
):
30 '''Provides a subclass we can use to test the TemplateDataSource methods
32 def __init__(self
, server_instance
, base_dir
):
33 type(self
)._BASE
= base_dir
34 TemplateDataSource
.__init
__(self
, server_instance
)
37 class TemplateDataSourceTest(unittest
.TestCase
):
40 test_data_source
= _CreateTestDataSource('simple')
41 template_a1
= Motemplate(_ReadFile('simple', 'test1.html'))
42 context
= [{}, {'templates': {}}]
44 template_a1
.Render(*context
).text
,
45 test_data_source
.get('test1').Render(*context
).text
)
46 template_a2
= Motemplate(_ReadFile('simple', 'test2.html'))
48 template_a2
.Render(*context
).text
,
49 test_data_source
.get('test2').Render(*context
).text
)
51 @DisableLogging('warning')
52 def testNotFound(self
):
53 test_data_source
= _CreateTestDataSource('simple')
54 self
.assertEqual(None, test_data_source
.get('junk'))
56 @DisableLogging('warning')
57 def testPartials(self
):
58 test_data_source
= _CreateTestDataSource('partials')
59 context
= json
.loads(_ReadFile('partials', 'input.json'))
61 _ReadFile('partials', 'test_expected.html'),
62 test_data_source
.get('test_tmpl').Render(
63 context
, test_data_source
).text
)
66 if __name__
== '__main__':