[Extensions] Fix crash in developerPrivate.inspect API
[chromium-blink-merge.git] / native_client_sdk / src / build_tools / tests / easy_template_test.py
blobf26de77a1e44ead0737fbc578ee00e6166584ac6
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 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)
21 import easy_template
22 import mock
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)
39 def testEmpty(self):
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}.
49 """
50 self._RunTest(template, template, {})
52 def testSimpleInterpolation(self):
53 self._RunTest(
54 '{{foo}} is my favorite number',
55 '42 is my favorite number',
56 {'foo': 42})
58 def testLineContinuations(self):
59 template = "Line 1 \\\nLine 2\n"""
60 self._RunTest(template, template, {})
62 def testIfStatement(self):
63 template = r"""
64 [[if foo:]]
65 foo
66 [[else:]]
67 not foo
68 [[]]"""
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...
75 [[]]"""
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...
79 """
80 self._RunTest(template, expected, {})
82 def testListVariables(self):
83 template = r"""
84 [[for i, item in enumerate(my_list):]]
85 {{i+1}}: {{item}}
86 [[]]
87 """
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):
99 template = r"""
100 struct {{name}} {
101 [[for field in fields:]]
102 [[ if field['type'] == 'array':]]
103 {{field['basetype']}} {{field['name']}}[{{field['size']}}];
104 [[ else:]]
105 {{field['type']}} {{field['name']}};
106 [[ ]]
107 [[]]
108 };"""
109 expected = r"""
110 struct Foo {
111 std::string name;
112 int problems[99];
113 };"""
114 self._RunTest(template, expected, {
115 'name': 'Foo',
116 'fields': [
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__':
135 unittest.main()