Mojo C++ bindings: better log message for serialization warnings.
[chromium-blink-merge.git] / native_client_sdk / src / build_tools / tests / easy_template_test.py
blob56017045c80fca8c6728359def3fadf8b4c94b9b
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.
6 import cStringIO
7 import difflib
8 import os
9 import sys
10 import unittest
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)
16 import easy_template
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)
31 def testEmpty(self):
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}.
41 """
42 self._RunTest(template, template, {})
44 def testSimpleInterpolation(self):
45 self._RunTest(
46 '{{foo}} is my favorite number',
47 '42 is my favorite number',
48 {'foo': 42})
50 def testLineContinuations(self):
51 template = "Line 1 \\\nLine 2\n"""
52 self._RunTest(template, template, {})
54 def testIfStatement(self):
55 template = r"""
56 [[if foo:]]
57 foo
58 [[else:]]
59 not foo
60 [[]]"""
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...
67 [[]]"""
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...
71 """
72 self._RunTest(template, expected, {})
74 def testListVariables(self):
75 template = r"""
76 [[for i, item in enumerate(my_list):]]
77 {{i+1}}: {{item}}
78 [[]]
79 """
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):
91 template = r"""
92 struct {{name}} {
93 [[for field in fields:]]
94 [[ if field['type'] == 'array':]]
95 {{field['basetype']}} {{field['name']}}[{{field['size']}}];
96 [[ else:]]
97 {{field['type']}} {{field['name']}};
98 [[ ]]
99 [[]]
100 };"""
101 expected = r"""
102 struct Foo {
103 std::string name;
104 int problems[99];
105 };"""
106 self._RunTest(template, expected, {
107 'name': 'Foo',
108 'fields': [
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__':
121 unittest.main()