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