OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 # Copyright (c) 2011, 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 import database | |
7 import idlparser | |
8 import logging.config | |
9 import os | |
10 import os.path | |
11 import shutil | |
12 import tempfile | |
13 import unittest | |
14 from databasebuilder import * | |
15 | |
16 | |
17 class DatabaseBuilderTestCase(unittest.TestCase): | |
18 | |
19 def _create_input(self, idl_file_name, content): | |
20 file_name = os.path.join(self._input_dir, idl_file_name) | |
21 f = open(file_name, 'w') | |
22 f.write(content) | |
23 f.close() | |
24 return file_name | |
25 | |
26 def _assert_interface_exists(self, path): | |
27 file_path = os.path.join(self._database_dir, path) | |
28 self.assertTrue(os.path.exists(file_path)) | |
29 | |
30 def _assert_content_equals(self, path, expected_content): | |
31 def clean(content): | |
32 return ' '.join(filter(len, map(str.strip, content.split('\n')))) | |
33 file_path = os.path.join(self._database_dir, path) | |
34 self.assertTrue(os.path.exists(file_path)) | |
35 f = open(file_path, 'r') | |
36 actual_content = f.read() | |
37 f.close() | |
38 if clean(actual_content) != clean(expected_content): | |
39 msg = ''' | |
40 FILE: %s | |
41 EXPECTED: | |
42 %s | |
43 ACTUAL: | |
44 %s | |
45 ''' % (file_path, expected_content, actual_content) | |
46 self.fail(msg) | |
47 | |
48 def setUp(self): | |
49 working_dir = tempfile.mkdtemp() | |
50 self._database_dir = os.path.join(working_dir, 'database') | |
51 self.assertFalse(os.path.exists(self._database_dir)) | |
52 | |
53 self._input_dir = os.path.join(working_dir, 'inputdir') | |
54 os.makedirs(self._input_dir) | |
55 | |
56 self._db = database.Database(self._database_dir) | |
57 self.assertTrue(os.path.exists(self._database_dir)) | |
58 | |
59 self._builder = DatabaseBuilder(self._db) | |
60 | |
61 def tearDown(self): | |
62 shutil.rmtree(self._database_dir) | |
63 | |
64 def test_basic_import(self): | |
65 file_name = self._create_input('input.idl', ''' | |
66 module M { | |
67 interface I { | |
68 attribute int a; | |
69 }; | |
70 };''') | |
71 self._builder.import_idl_file(file_name) | |
72 self._builder.merge_imported_interfaces([]) | |
73 self._db.Save() | |
74 self._assert_interface_exists('I.idl') | |
75 | |
76 def test_splitting(self): | |
77 file_name = self._create_input('input.idl', ''' | |
78 module M { | |
79 interface I { | |
80 readonly attribute int a; | |
81 int o(in int x, in optional int y); | |
82 }; | |
83 };''') | |
84 self._builder.import_idl_file(file_name) | |
85 self._builder.merge_imported_interfaces([]) | |
86 self._db.Save() | |
87 self._assert_content_equals('I.idl', ''' | |
88 interface I { | |
89 /* Attributes */ | |
90 getter attribute int a; | |
91 | |
92 /* Operations */ | |
93 int o(in int x); | |
94 int o(in int x, in int y); | |
95 };''') | |
96 | |
97 def test_renames(self): | |
98 file_name = self._create_input('input.idl', ''' | |
99 module M { | |
100 [Constructor(in T x)] interface I { | |
101 T op(T x); | |
102 readonly attribute N::T attr; | |
103 }; | |
104 };''') | |
105 options = DatabaseBuilderOptions(type_rename_map={'I': 'i', 'T': 't'}) | |
106 self._builder.import_idl_file(file_name, options) | |
107 self._builder.merge_imported_interfaces([]) | |
108 self._db.Save() | |
109 self._assert_content_equals('i.idl', ''' | |
110 [Constructor(in t x)] interface i { | |
111 /* Attributes */ | |
112 getter attribute t attr; | |
113 /* Operations */ | |
114 t op(in t x); | |
115 };''') | |
116 | |
117 def test_type_defs(self): | |
118 file_name = self._create_input('input.idl', ''' | |
119 module M { | |
120 typedef T S; | |
121 interface I : S { | |
122 S op(S x); | |
123 readonly attribute S attr; | |
124 }; | |
125 };''') | |
126 options = DatabaseBuilderOptions() | |
127 self._builder.import_idl_file(file_name, options) | |
128 self._builder.merge_imported_interfaces([]) | |
129 self._db.Save() | |
130 self._assert_content_equals('I.idl', ''' | |
131 interface I : | |
132 T { | |
133 /* Attributes */ | |
134 getter attribute T attr; | |
135 /* Operations */ | |
136 T op(in T x); | |
137 };''') | |
138 | |
139 def test_merge(self): | |
140 file_name1 = self._create_input('input1.idl', ''' | |
141 module M { | |
142 interface I { | |
143 const int CONST_BOTH = 0; | |
144 const int CONST_ONLY_FIRST = 0; | |
145 const int CONST_BOTH_DIFFERENT_VALUE = 0; | |
146 | |
147 readonly attribute int attr_only_first; | |
148 readonly attribute int attr_both; | |
149 readonly attribute int attr_both_readonly_difference; | |
150 readonly attribute int attr_both_int_long_difference; | |
151 | |
152 int op_only_first(); | |
153 int op_both(int a); | |
154 int op_both_optionals_difference(int a, | |
155 in optional int b); | |
156 int op_both_arg_rename(int arg); | |
157 }; | |
158 };''') | |
159 self._builder.import_idl_file(file_name1, | |
160 DatabaseBuilderOptions(source='1st', | |
161 idl_syntax=idlparser.FREMONTCUT_SYNTAX)) | |
162 file_name2 = self._create_input('input2.idl', ''' | |
163 module M { | |
164 interface I { | |
165 const int CONST_BOTH = 0; | |
166 const int CONST_ONLY_SECOND = 0; | |
167 const int CONST_BOTH_DIFFERENT_VALUE = 1; | |
168 | |
169 readonly attribute int attr_only_second; | |
170 readonly attribute int attr_both; | |
171 readonly attribute long attr_both_int_long_difference; | |
172 attribute int attr_both_readonly_difference; | |
173 | |
174 int op_only_second(); | |
175 int op_both(int a); | |
176 int op_both_optionals_difference(int a, | |
177 optional boolean b); | |
178 int op_both_arg_rename(int betterName); | |
179 }; | |
180 };''') | |
181 self._builder.import_idl_file(file_name2, | |
182 DatabaseBuilderOptions(source='2nd', | |
183 idl_syntax=idlparser.FREMONTCUT_SYNTAX)) | |
184 self._builder.set_same_signatures({'int': 'long'}) | |
185 self._builder.merge_imported_interfaces([]) | |
186 self._db.Save() | |
187 self._assert_content_equals('I.idl', ''' | |
188 @1st(module=M) @2nd(module=M) interface I { | |
189 /* Constants */ | |
190 @1st @2nd const int CONST_BOTH = 0; | |
191 @1st const int CONST_BOTH_DIFFERENT_VALUE = 0; | |
192 @2nd const int CONST_BOTH_DIFFERENT_VALUE = 1; | |
193 @1st const int CONST_ONLY_FIRST = 0; | |
194 @2nd const int CONST_ONLY_SECOND = 0; | |
195 | |
196 /* Attributes */ | |
197 @1st @2nd getter attribute int attr_both; | |
198 @1st @2nd getter attribute int attr_both_int_long_difference; | |
199 @1st @2nd getter attribute int attr_both_readonly_difference; | |
200 @2nd setter attribute int attr_both_readonly_difference; | |
201 @1st getter attribute int attr_only_first; | |
202 @2nd getter attribute int attr_only_second; | |
203 | |
204 /* Operations */ | |
205 @1st @2nd int op_both(in t a); | |
206 @1st @2nd int op_both_arg_rename(in t betterName); | |
207 @1st @2nd int op_both_optionals_difference(in t a); | |
208 @1st int op_both_optionals_difference(in t a, in int b); | |
209 @2nd int op_both_optionals_difference(in t a, in boolean b); | |
210 @1st int op_only_first(); | |
211 @2nd int op_only_second(); | |
212 };''') | |
213 | |
214 def test_mergeDartName(self): | |
215 file_name1 = self._create_input('input1.idl', ''' | |
216 module M { | |
217 interface I { | |
218 [ImplementationFunction=foo] int member(in int a); | |
219 }; | |
220 };''') | |
221 self._builder.import_idl_file(file_name1, | |
222 DatabaseBuilderOptions(source='1st', | |
223 idl_syntax=idlparser.FREMONTCUT_SYNTAX)) | |
224 file_name2 = self._create_input('input2.idl', ''' | |
225 module M { | |
226 interface I { | |
227 [DartName=bar] int member(in int a); | |
228 }; | |
229 };''') | |
230 self._builder.import_idl_file(file_name2, | |
231 DatabaseBuilderOptions(source='2nd', | |
232 idl_syntax=idlparser.FREMONTCUT_SYNTAX)) | |
233 self._builder.merge_imported_interfaces([]) | |
234 self._db.Save() | |
235 self._assert_content_equals('I.idl', ''' | |
236 @1st(module=M) @2nd(module=M) interface I { | |
237 /* Operations */ | |
238 @1st @2nd [DartName=bar, ImplementationFunction=foo] int member(in int a
); | |
239 };''') | |
240 | |
241 def test_supplemental(self): | |
242 file_name = self._create_input('input1.idl', ''' | |
243 module M { | |
244 interface I { | |
245 readonly attribute int a; | |
246 }; | |
247 [Supplemental] interface I { | |
248 readonly attribute int b; | |
249 }; | |
250 };''') | |
251 self._builder.import_idl_file(file_name, | |
252 DatabaseBuilderOptions(source='Src')) | |
253 self._builder.merge_imported_interfaces([]) | |
254 self._db.Save() | |
255 self._assert_content_equals('I.idl', ''' | |
256 @Src(module=M) [Supplemental] interface I { | |
257 /* Attributes */ | |
258 @Src getter attribute int a; | |
259 @Src getter attribute int b; | |
260 };''') | |
261 | |
262 def test_impl_stmt(self): | |
263 file_name = self._create_input('input.idl', ''' | |
264 module M { | |
265 interface I {}; | |
266 I implements J; | |
267 };''') | |
268 self._builder.import_idl_file(file_name, | |
269 DatabaseBuilderOptions(source='Src')) | |
270 self._builder.merge_imported_interfaces([]) | |
271 self._db.Save() | |
272 self._assert_content_equals('I.idl', ''' | |
273 @Src(module=M) interface I : | |
274 @Src J { | |
275 };''') | |
276 | |
277 def test_obsolete(self): | |
278 file_name1 = self._create_input('input1.idl', ''' | |
279 module M { | |
280 interface I { | |
281 readonly attribute int keep; | |
282 readonly attribute int obsolete; // Would be removed | |
283 }; | |
284 };''') | |
285 self._builder.import_idl_file(file_name1, | |
286 DatabaseBuilderOptions(source='src')) | |
287 file_name2 = self._create_input('input2.idl', ''' | |
288 module M { | |
289 interface I { | |
290 readonly attribute int keep; | |
291 readonly attribute int new; | |
292 }; | |
293 };''') | |
294 self._builder.import_idl_file(file_name2, | |
295 DatabaseBuilderOptions(source='src', | |
296 obsolete_old_declarations=True)) | |
297 self._builder.merge_imported_interfaces([]) | |
298 self._db.Save() | |
299 self._assert_content_equals('I.idl', ''' | |
300 @src(module=M) interface I { | |
301 /* Attributes */ | |
302 @src getter attribute int keep; | |
303 @src getter attribute int new; | |
304 };''') | |
305 | |
306 def test_annotation_normalization(self): | |
307 file_name = self._create_input('input.idl', ''' | |
308 module M { | |
309 interface I : J{ | |
310 const int C = 0; | |
311 readonly attribute int a; | |
312 int op(); | |
313 }; | |
314 };''') | |
315 self._builder.import_idl_file(file_name, | |
316 DatabaseBuilderOptions(source='Src', source_attributes={'x': 'y'})) | |
317 self._builder.merge_imported_interfaces([]) | |
318 interface = self._db.GetInterface('I') | |
319 interface.parents[0].annotations['Src']['x'] = 'u' | |
320 interface.constants[0].annotations['Src']['z'] = 'w' | |
321 interface.attributes[0].annotations['Src']['x'] = 'u' | |
322 self._db.Save() | |
323 | |
324 # Before normalization | |
325 self._assert_content_equals('I.idl', ''' | |
326 @Src(module=M, x=y) | |
327 interface I : @Src(x=u) J { | |
328 /* Constants */ | |
329 @Src(x=y, z=w) const int C = 0; | |
330 /* Attributes */ | |
331 @Src(x=u) getter attribute int a; | |
332 /* Operations */ | |
333 @Src(x=y) int op(); | |
334 };''') | |
335 | |
336 # Normalize | |
337 self._builder.normalize_annotations(['Src']) | |
338 self._db.Save() | |
339 | |
340 # After normalization | |
341 self._assert_content_equals('I.idl', ''' | |
342 @Src(module=M, x=y) | |
343 interface I : @Src(x=u) J { | |
344 /* Constants */ | |
345 @Src(z=w) const int C = 0; | |
346 /* Attributes */ | |
347 @Src(x=u) getter attribute int a; | |
348 /* Operations */ | |
349 @Src int op(); | |
350 };''') | |
351 | |
352 def test_fix_displacements(self): | |
353 file_name1 = self._create_input('input1.idl', ''' | |
354 module M { | |
355 interface I {}; | |
356 interface J : I { | |
357 readonly attribute int attr; | |
358 }; | |
359 };''') | |
360 self._builder.import_idl_file(file_name1, | |
361 DatabaseBuilderOptions(source='1st')) | |
362 file_name2 = self._create_input('input2.idl', ''' | |
363 module M { | |
364 interface I { | |
365 readonly attribute int attr; | |
366 }; | |
367 interface J : I {}; | |
368 };''') | |
369 self._builder.import_idl_file(file_name2, | |
370 DatabaseBuilderOptions(source='2nd')) | |
371 self._builder.merge_imported_interfaces([]) | |
372 self._builder.fix_displacements('2nd') | |
373 self._db.Save() | |
374 self._assert_content_equals('J.idl', ''' | |
375 @1st(module=M) @2nd(module=M) interface J : | |
376 @1st @2nd I { | |
377 /* Attributes */ | |
378 @1st | |
379 @2nd(via=I) | |
380 getter attribute int attr; | |
381 };''') | |
382 | |
383 | |
384 if __name__ == "__main__": | |
385 logging.config.fileConfig("logging.conf") | |
386 if __name__ == '__main__': | |
387 unittest.main() | |
OLD | NEW |