Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1163)

Side by Side Diff: lib/dom/scripts/dartgenerator_test.py

Issue 10913125: Remove lib/dom directory! (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/python
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
6 """Tests for dartgenerator."""
7
8 import logging.config
9 import os.path
10 import re
11 import shutil
12 import tempfile
13 import unittest
14 import dartgenerator
15 import database
16 import idlnode
17 import idlparser
18
19
20 class DartGeneratorTestCase(unittest.TestCase):
21
22 def _InDatabase(self, interface_name):
23 return os.path.exists(os.path.join(self._database_dir,
24 '%s.idl' % interface_name))
25
26 def _FilePathForDartInterface(self, interface_name):
27 return os.path.join(self._generator._output_dir, 'src', 'interface',
28 '%s.dart' % interface_name)
29
30 def _InOutput(self, interface_name):
31 return os.path.exists(
32 self._FilePathForDartInterface(interface_name))
33
34 def _ReadOutputFile(self, interface_name):
35 self.assertTrue(self._InOutput(interface_name))
36 file_path = self._FilePathForDartInterface(interface_name)
37 f = open(file_path, 'r')
38 content = f.read()
39 f.close()
40 return content, file_path
41
42 def _AssertOutputSansHeaderEquals(self, interface_name, expected_content):
43 full_actual_content, file_path = self._ReadOutputFile(interface_name)
44 # Remove file header comments in // or multiline /* ... */ syntax.
45 header_re = re.compile(r'^(\s*(//.*|/\*([^*]|\*[^/])*\*/)\s*)*')
46 actual_content = header_re.sub('', full_actual_content)
47 if expected_content != actual_content:
48 msg = """
49 FILE: %s
50 EXPECTED:
51 %s
52 ACTUAL:
53 %s
54 """ % (file_path, expected_content, actual_content)
55 self.fail(msg)
56
57 def _AssertOutputContains(self, interface_name, expected_content):
58 actual_content, file_path = self._ReadOutputFile(interface_name)
59 if expected_content not in actual_content:
60 msg = """
61 STRING: %s
62 Was found not in output file: %s
63 FILE CONTENT:
64 %s
65 """ % (expected_content, file_path, actual_content)
66 self.fail(msg)
67
68 def _AssertOutputDoesNotContain(self, interface_name, expected_content):
69 actual_content, file_path = self._ReadOutputFile(interface_name)
70 if expected_content in actual_content:
71 msg = """
72 STRING: %s
73 Was found in output file: %s
74 FILE CONTENT:
75 %s
76 """ % (expected_content, file_path, actual_content)
77 self.fail(msg)
78
79 def setUp(self):
80 self._working_dir = tempfile.mkdtemp()
81 self._output_dir = os.path.join(self._working_dir, 'output')
82 self._database_dir = os.path.join(self._working_dir, 'database')
83 self._auxiliary_dir = os.path.join(self._working_dir, 'auxiliary')
84 self.assertFalse(os.path.exists(self._database_dir))
85
86 # Create database and add one interface.
87 db = database.Database(self._database_dir)
88 os.mkdir(self._auxiliary_dir)
89 self.assertTrue(os.path.exists(self._database_dir))
90
91 content = """
92 module shapes {
93 @A1 @A2
94 interface Shape {
95 @A1 @A2 getter attribute int attr;
96 @A1 setter attribute int attr;
97 @A3 boolean op();
98 const long CONSTANT = 1;
99 getter attribute DOMString strAttr;
100 Shape create();
101 boolean compare(Shape s);
102 Rectangle createRectangle();
103 void addLine(lines::Line line);
104 void someDartType(File file);
105 void someUnidentifiedType(UnidentifiableType t);
106 };
107 };
108
109 module rectangles {
110 @A3
111 interface Rectangle : @A3 shapes::Shape {
112 void someTemplatedType(List<Shape> list);
113 };
114 };
115
116 module lines {
117 @A1
118 interface Line : shapes::Shape {
119 };
120 };
121 """
122
123 parser = idlparser.IDLParser(idlparser.FREMONTCUT_SYNTAX)
124 ast = parser.parse(content)
125 idl_file = idlnode.IDLFile(ast)
126 for module in idl_file.modules:
127 module_name = module.id
128 for interface in module.interfaces:
129 db.AddInterface(interface)
130 db.Save()
131
132 self.assertTrue(self._InDatabase('Shape'))
133 self.assertTrue(self._InDatabase('Rectangle'))
134 self.assertTrue(self._InDatabase('Line'))
135
136 self._database = database.Database(self._database_dir)
137 self._generator = dartgenerator.DartGenerator(self._auxiliary_dir,
138 '../templates',
139 'test')
140
141 def tearDown(self):
142 shutil.rmtree(self._database_dir)
143 shutil.rmtree(self._auxiliary_dir)
144
145 def testBasicGeneration(self):
146 # Generate all interfaces:
147 self._database.Load()
148 self._generator.Generate(self._database, self._output_dir)
149 self._generator.Flush()
150
151 self.assertTrue(self._InOutput('Shape'))
152 self.assertTrue(self._InOutput('Rectangle'))
153 self.assertTrue(self._InOutput('Line'))
154
155 def testFilterByAnnotations(self):
156 self._database.Load()
157 self._generator.FilterInterfaces(self._database, ['A1', 'A2'], ['A3'])
158 self._generator.Generate(self._database, self._output_dir)
159 self._generator.Flush()
160
161 # Only interfaces with (@A1 and @A2) or @A3 should be generated:
162 self.assertTrue(self._InOutput('Shape'))
163 self.assertTrue(self._InOutput('Rectangle'))
164 self.assertFalse(self._InOutput('Line'))
165
166 # Only members with (@A1 and @A2) or @A3 should be generated:
167 # TODO(sra): make th
168 self._AssertOutputSansHeaderEquals('Shape', """interface Shape {
169
170 final int attr;
171
172 bool op();
173 }
174 """)
175
176 self._AssertOutputContains('Rectangle',
177 'interface Rectangle extends shapes::Shape')
178
179 def testTypeRenames(self):
180 self._database.Load()
181 # Translate 'Shape' to spanish:
182 self._generator.RenameTypes(self._database, {'Shape': 'Forma'}, False)
183 self._generator.Generate(self._database, self._output_dir)
184 self._generator.Flush()
185
186 # Validate that all references to Shape have been converted:
187 self._AssertOutputContains('Forma',
188 'interface Forma')
189 self._AssertOutputContains('Forma', 'Forma create();')
190 self._AssertOutputContains('Forma',
191 'bool compare(Forma s);')
192 self._AssertOutputContains('Rectangle',
193 'interface Rectangle extends Forma')
194
195 def testQualifiedDartTypes(self):
196 self._database.Load()
197 self._generator.FilterMembersWithUnidentifiedTypes(self._database)
198 self._generator.Generate(self._database, self._output_dir)
199 self._generator.Flush()
200
201 # Verify primitive conversions are working:
202 self._AssertOutputContains('Shape',
203 'static const int CONSTANT = 1')
204 self._AssertOutputContains('Shape',
205 'final String strAttr;')
206
207 # Verify interface names are converted:
208 self._AssertOutputContains('Shape',
209 'interface Shape {')
210 self._AssertOutputContains('Shape',
211 ' Shape create();')
212 # TODO(sra): Why is this broken? Output contains qualified type.
213 #self._AssertOutputContains('Shape',
214 # 'void addLine(Line line);')
215 self._AssertOutputContains('Shape',
216 'Rectangle createRectangle();')
217 # TODO(sra): Why is this broken? Output contains qualified type.
218 #self._AssertOutputContains('Rectangle',
219 # 'interface Rectangle extends Shape')
220 # Verify dart names are preserved:
221 # TODO(vsm): Re-enable when package / namespaces are enabled.
222 # self._AssertOutputContains('shapes', 'Shape',
223 # 'void someDartType(File file);')
224
225 # Verify that unidentified types are not removed:
226 self._AssertOutputDoesNotContain('Shape',
227 'someUnidentifiedType')
228
229 # Verify template conversion:
230 # TODO(vsm): Re-enable when core collections are supported.
231 # self._AssertOutputContains('rectangles', 'Rectangle',
232 # 'void someTemplatedType(List<Shape> list)')
233
234
235 if __name__ == '__main__':
236 logging.config.fileConfig('logging.conf')
237 if __name__ == '__main__':
238 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698