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 TOOLS_DIR
= os
.path
.dirname(SCRIPT_DIR
)
14 CHROME_SRC
= os
.path
.dirname(os
.path
.dirname(os
.path
.dirname(TOOLS_DIR
)))
15 MOCK_DIR
= os
.path
.join(CHROME_SRC
, 'third_party', 'pymock')
16 BUILD_TOOLS_DIR
= os
.path
.dirname(SCRIPT_DIR
)
18 sys
.path
.append(BUILD_TOOLS_DIR
)
19 sys
.path
.append(MOCK_DIR
)
24 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
26 class EasyTemplateTestCase(unittest
.TestCase
):
27 def _RunTest(self
, template
, expected
, template_dict
):
28 src
= cStringIO
.StringIO(template
)
29 dst
= cStringIO
.StringIO()
30 easy_template
.RunTemplate(src
, dst
, template_dict
)
31 if dst
.getvalue() != expected
:
32 expected_lines
= expected
.splitlines(1)
33 actual_lines
= dst
.getvalue().splitlines(1)
34 diff
= ''.join(difflib
.unified_diff(
35 expected_lines
, actual_lines
,
36 fromfile
='expected', tofile
='actual'))
37 self
.fail('Unexpected output:\n' + diff
)
40 self
._RunTest
('', '', {})
42 def testNewlines(self
):
43 self
._RunTest
('\n\n', '\n\n', {})
45 def testNoInterpolation(self
):
46 template
= """I love paris in the
47 the springtime [don't you?]
48 {this is not interpolation}.
50 self
._RunTest
(template
, template
, {})
52 def testSimpleInterpolation(self
):
54 '{{foo}} is my favorite number',
55 '42 is my favorite number',
58 def testLineContinuations(self
):
59 template
= "Line 1 \\\nLine 2\n"""
60 self._RunTest(template, template, {})
62 def testIfStatement(self):
69 self._RunTest(template, "\n foo
\n", {'foo': True})
70 self._RunTest(template, "\n not foo
\n", {'foo': False})
72 def testForStatement(self):
73 template = r"""[[for beers in [99, 98, 1]:]]
74 {{beers}} bottle{{(beers != 1) and 's' or ''}} of beer on the wall...
76 expected = r"""99 bottles of beer on the wall...
77 98 bottles of beer on the wall...
78 1 bottle of beer on the wall...
80 self._RunTest(template, expected, {})
82 def testListVariables(self):
84 [[for i, item in enumerate(my_list):]]
88 self._RunTest(template, "\n1: Banana
\n2: Grapes
\n3: Kumquat
\n",
89 {'my_list': ['Banana', 'Grapes', 'Kumquat']})
91 def testListInterpolation(self):
92 template = "{{', '.join(growing
[0:-1]) + ' and ' + growing
[-1]}} grow
..."
93 self._RunTest(template, "Oats
, peas
, beans
and barley grow
...",
94 {'growing': ['Oats', 'peas', 'beans', 'barley']})
95 self._RunTest(template, "Love
and laughter grow
...",
96 {'growing': ['Love', 'laughter']})
98 def testComplex(self):
101 [[for field in fields:]]
102 [[ if field['type'] == 'array':]]
103 {{field['basetype']}} {{field['name']}}[{{field['size']}}];
105 {{field['type']}} {{field['name']}};
114 self._RunTest(template, expected, {
117 {'name': 'name', 'type': 'std::string'},
118 {'name': 'problems', 'type': 'array', 'basetype': 'int', 'size': 99}]})
120 def testModulo(self):
121 self._RunTest('No expression %', 'No expression %', {})
122 self._RunTest('% before {{3 + 4}}', '% before 7', {})
123 self._RunTest('{{2**8}} % after', '256 % after', {})
124 self._RunTest('inside {{8 % 3}}', 'inside 2', {})
125 self._RunTest('Everywhere % {{8 % 3}} %', 'Everywhere % 2 %', {})
127 @mock.patch('easy_template.TemplateToPython')
128 @mock.patch('sys.stdout', mock.Mock())
129 def testMainArgParsing(self, mock_template_to_python):
130 easy_template.main([__file__])
131 mock_template_to_python.assert_called()
134 if __name__ == '__main__':