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.
12 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
13 BUILD_TOOLS_DIR
= os
.path
.dirname(SCRIPT_DIR
)
15 sys
.path
.append(BUILD_TOOLS_DIR
)
18 class EasyTemplateTestCase(unittest
.TestCase
):
19 def _RunTest(self
, template
, expected
, template_dict
):
20 src
= cStringIO
.StringIO(template
)
21 dst
= cStringIO
.StringIO()
22 easy_template
.RunTemplate(src
, dst
, template_dict
)
23 if dst
.getvalue() != expected
:
24 expected_lines
= expected
.splitlines(1)
25 actual_lines
= dst
.getvalue().splitlines(1)
26 diff
= ''.join(difflib
.unified_diff(
27 expected_lines
, actual_lines
,
28 fromfile
='expected', tofile
='actual'))
29 self
.fail('Unexpected output:\n' + diff
)
32 self
._RunTest
('', '', {})
34 def testNewlines(self
):
35 self
._RunTest
('\n\n', '\n\n', {})
37 def testNoInterpolation(self
):
38 template
= """I love paris in the
39 the springtime [don't you?]
40 {this is not interpolation}.
42 self
._RunTest
(template
, template
, {})
44 def testSimpleInterpolation(self
):
46 '{{foo}} is my favorite number',
47 '42 is my favorite number',
50 def testLineContinuations(self
):
51 template
= "Line 1 \\\nLine 2\n"""
52 self._RunTest(template, template, {})
54 def testIfStatement(self):
61 self._RunTest(template, "\n foo
\n", {'foo': True})
62 self._RunTest(template, "\n not foo
\n", {'foo': False})
64 def testForStatement(self):
65 template = r"""[[for beers in [99, 98, 1]:]]
66 {{beers}} bottle{{(beers != 1) and 's' or ''}} of beer on the wall...
68 expected = r"""99 bottles of beer on the wall...
69 98 bottles of beer on the wall...
70 1 bottle of beer on the wall...
72 self._RunTest(template, expected, {})
74 def testListVariables(self):
76 [[for i, item in enumerate(my_list):]]
80 self._RunTest(template, "\n1: Banana
\n2: Grapes
\n3: Kumquat
\n",
81 {'my_list': ['Banana', 'Grapes', 'Kumquat']})
83 def testListInterpolation(self):
84 template = "{{', '.join(growing
[0:-1]) + ' and ' + growing
[-1]}} grow
..."
85 self._RunTest(template, "Oats
, peas
, beans
and barley grow
...",
86 {'growing': ['Oats', 'peas', 'beans', 'barley']})
87 self._RunTest(template, "Love
and laughter grow
...",
88 {'growing': ['Love', 'laughter']})
90 def testComplex(self):
93 [[for field in fields:]]
94 [[ if field['type'] == 'array':]]
95 {{field['basetype']}} {{field['name']}}[{{field['size']}}];
97 {{field['type']}} {{field['name']}};
106 self._RunTest(template, expected, {
109 {'name': 'name', 'type': 'std::string'},
110 {'name': 'problems', 'type': 'array', 'basetype': 'int', 'size': 99}]})
112 def testModulo(self):
113 self._RunTest('No expression %', 'No expression %', {})
114 self._RunTest('% before {{3 + 4}}', '% before 7', {})
115 self._RunTest('{{2**8}} % after', '256 % after', {})
116 self._RunTest('inside {{8 % 3}}', 'inside 2', {})
117 self._RunTest('Everywhere % {{8 % 3}} %', 'Everywhere % 2 %', {})
120 if __name__ == '__main__':