Flutter Engine
The Flutter Engine
dartgenerator_test.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5"""Tests for dartgenerator."""
6
7import logging.config
8import os.path
9import re
10import shutil
11import tempfile
12import unittest
13import dartgenerator
14import database
15import idlnode
16import idlparser
17
18
19class DartGeneratorTestCase(unittest.TestCase):
20
21 def _InDatabase(self, interface_name):
22 return os.path.exists(
23 os.path.join(self._database_dir, '%s.idl' % interface_name))
24
25 def _FilePathForDartInterface(self, interface_name):
26 return os.path.join(self._generator._output_dir, 'src', 'interface',
27 '%s.dart' % interface_name)
28
29 def _InOutput(self, interface_name):
30 return os.path.exists(self._FilePathForDartInterface(interface_name))
31
32 def _ReadOutputFile(self, interface_name):
33 self.assertTrue(self._InOutput(interface_name))
34 file_path = self._FilePathForDartInterface(interface_name)
35 f = open(file_path, 'r')
36 content = f.read()
37 f.close()
38 return content, file_path
39
40 def _AssertOutputSansHeaderEquals(self, interface_name, expected_content):
41 full_actual_content, file_path = self._ReadOutputFile(interface_name)
42 # Remove file header comments in // or multiline /* ... */ syntax.
43 header_re = re.compile(r'^(\s*(//.*|/\*([^*]|\*[^/])*\*/)\s*)*')
44 actual_content = header_re.sub('', full_actual_content)
45 if expected_content != actual_content:
46 msg = """
47FILE: %s
48EXPECTED:
49%s
50ACTUAL:
51%s
52""" % (file_path, expected_content, actual_content)
53 self.fail(msg)
54
55 def _AssertOutputContains(self, interface_name, expected_content):
56 actual_content, file_path = self._ReadOutputFile(interface_name)
57 if expected_content not in actual_content:
58 msg = """
59STRING: %s
60Was found not in output file: %s
61FILE CONTENT:
62%s
63""" % (expected_content, file_path, actual_content)
64 self.fail(msg)
65
66 def _AssertOutputDoesNotContain(self, interface_name, expected_content):
67 actual_content, file_path = self._ReadOutputFile(interface_name)
68 if expected_content in actual_content:
69 msg = """
70STRING: %s
71Was found in output file: %s
72FILE CONTENT:
73%s
74""" % (expected_content, file_path, actual_content)
75 self.fail(msg)
76
77 def setUp(self):
78 self._working_dir = tempfile.mkdtemp()
79 self._output_dir = os.path.join(self._working_dir, 'output')
80 self._database_dir = os.path.join(self._working_dir, 'database')
81 self._auxiliary_dir = os.path.join(self._working_dir, 'auxiliary')
82 self.assertFalse(os.path.exists(self._database_dir))
83
84 # Create database and add one interface.
86 os.mkdir(self._auxiliary_dir)
87 self.assertTrue(os.path.exists(self._database_dir))
88
89 content = """
90 module shapes {
91 @A1 @A2
92 interface Shape {
93 @A1 @A2 getter attribute int attr;
94 @A1 setter attribute int attr;
95 @A3 boolean op();
96 const long CONSTANT = 1;
97 getter attribute DOMString strAttr;
98 Shape create();
99 boolean compare(Shape s);
100 Rectangle createRectangle();
101 void addLine(lines::Line line);
102 void someDartType(File file);
103 void someUnidentifiedType(UnidentifiableType t);
104 };
105 };
106
107 module rectangles {
108 @A3
109 interface Rectangle : @A3 shapes::Shape {
110 void someTemplatedType(List<Shape> list);
111 };
112 };
113
114 module lines {
115 @A1
116 interface Line : shapes::Shape {
117 };
118 };
119 """
120
121 parser = idlparser.IDLParser(idlparser.FREMONTCUT_SYNTAX)
122 ast = parser.parse(content)
123 idl_file = idlnode.IDLFile(ast)
124 for interface in idl_file.interfaces:
125 db.AddInterface(interface)
126 db.Save()
127
128 self.assertTrue(self._InDatabase('Shape'))
129 self.assertTrue(self._InDatabase('Rectangle'))
130 self.assertTrue(self._InDatabase('Line'))
131
134 '../templates', 'test')
135
136 def tearDown(self):
137 shutil.rmtree(self._database_dir)
138 shutil.rmtree(self._auxiliary_dir)
139
141 # Generate all interfaces:
142 self._database.Load()
143 self._generator.Generate(self._database, self._output_dir)
144 self._generator.Flush()
145
146 self.assertTrue(self._InOutput('Shape'))
147 self.assertTrue(self._InOutput('Rectangle'))
148 self.assertTrue(self._InOutput('Line'))
149
151 self._database.Load()
152 self._generator.FilterInterfaces(self._database, ['A1', 'A2'], ['A3'])
153 self._generator.Generate(self._database, self._output_dir)
154 self._generator.Flush()
155
156 # Only interfaces with (@A1 and @A2) or @A3 should be generated:
157 self.assertTrue(self._InOutput('Shape'))
158 self.assertTrue(self._InOutput('Rectangle'))
159 self.assertFalse(self._InOutput('Line'))
160
161 # Only members with (@A1 and @A2) or @A3 should be generated:
162 # TODO(sra): make th
164 'Shape', """interface Shape {
165
166 final int attr;
167
168 bool op();
169}
170""")
171
172 self._AssertOutputContains('Rectangle',
173 'interface Rectangle extends shapes::Shape')
174
176 self._database.Load()
177 # Translate 'Shape' to spanish:
178 self._generator.RenameTypes(self._database, {'Shape': 'Forma'}, False)
179 self._generator.Generate(self._database, self._output_dir)
180 self._generator.Flush()
181
182 # Validate that all references to Shape have been converted:
183 self._AssertOutputContains('Forma', 'interface Forma')
184 self._AssertOutputContains('Forma', 'Forma create();')
185 self._AssertOutputContains('Forma', 'bool compare(Forma s);')
186 self._AssertOutputContains('Rectangle',
187 'interface Rectangle extends Forma')
188
190 self._database.Load()
191 self._generator.FilterMembersWithUnidentifiedTypes(self._database)
192 self._generator.Generate(self._database, self._output_dir)
193 self._generator.Flush()
194
195 # Verify primitive conversions are working:
196 self._AssertOutputContains('Shape', 'static const int CONSTANT = 1')
197 self._AssertOutputContains('Shape', 'final String strAttr;')
198
199 # Verify interface names are converted:
200 self._AssertOutputContains('Shape', 'interface Shape {')
201 self._AssertOutputContains('Shape', ' Shape create();')
202 # TODO(sra): Why is this broken? Output contains qualified type.
203 #self._AssertOutputContains('Shape',
204 # 'void addLine(Line line);')
205 self._AssertOutputContains('Shape', 'Rectangle createRectangle();')
206 # TODO(sra): Why is this broken? Output contains qualified type.
207 #self._AssertOutputContains('Rectangle',
208 # 'interface Rectangle extends Shape')
209 # Verify dart names are preserved:
210 # TODO(vsm): Re-enable when package / namespaces are enabled.
211 # self._AssertOutputContains('shapes', 'Shape',
212 # 'void someDartType(File file);')
213
214 # Verify that unidentified types are not removed:
215 self._AssertOutputDoesNotContain('Shape', 'someUnidentifiedType')
216
217 # Verify template conversion:
218 # TODO(vsm): Re-enable when core collections are supported.
219 # self._AssertOutputContains('rectangles', 'Rectangle',
220 # 'void someTemplatedType(List<Shape> list)')
221
222
223if __name__ == '__main__':
224 logging.config.fileConfig('logging.conf')
225 if __name__ == '__main__':
226 unittest.main()
def _AssertOutputDoesNotContain(self, interface_name, expected_content)
def _AssertOutputSansHeaderEquals(self, interface_name, expected_content)
def _AssertOutputContains(self, interface_name, expected_content)
Definition: create.py:1
void Flush(SkSurface *surface)
Definition: GpuTools.h:25
int compare(const void *untyped_lhs, const void *untyped_rhs)
Definition: skdiff.h:161