Flutter Engine
The Flutter Engine
idlnode_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 idlnode
7import idlparser
8import logging.config
9import sys
10import unittest
11
12
13class IDLNodeTestCase(unittest.TestCase):
14
15 def _run_test(self, syntax, content, expected):
16 """Utility run tests and prints extra contextual information.
17
18 Args:
19 syntax -- IDL grammar to use (either idlparser.WEBKIT_SYNTAX,
20 WEBIDL_SYNTAX or FREMONTCUT_SYNTAX). If None, will run
21 multiple tests, each with a different syntax.
22 content -- input text for the parser.
23 expected -- expected parse result.
24 """
25 if syntax is None:
26 self._run_test(idlparser.WEBIDL_SYNTAX, content, expected)
27 self._run_test(idlparser.WEBKIT_SYNTAX, content, expected)
28 self._run_test(idlparser.FREMONTCUT_SYNTAX, content, expected)
29 return
30
31 actual = None
32 error = None
33 ast = None
34 parseResult = None
35 try:
36 parser = idlparser.IDLParser(syntax)
37 ast = parser.parse(content)
38 node = idlnode.IDLFile(ast)
39 actual = node.to_dict() if node else None
40 except SyntaxError as e:
41 error = e
42 pass
43 if actual == expected:
44 return
45 else:
46 msg = '''
47SYNTAX : %s
48CONTENT :
49%s
50EXPECTED:
51%s
52ACTUAL :
53%s
54ERROR : %s
55AST :
56%s
57 ''' % (syntax, content, expected, actual, error, ast)
58 self.fail(msg)
59
61 self._run_test(None, 'module TestModule {};',
62 {'modules': [{
63 'id': 'TestModule'
64 }]})
65
67 self._run_test(
68 None, 'module TestModule { interface Interface1 {}; };', {
69 'modules': [{
70 'interfaces': [{
71 'javascript_binding_name': 'Interface1',
72 'doc_js_name': 'Interface1',
73 'id': 'Interface1'
74 }],
75 'id':
76 'TestModule'
77 }]
78 })
79
81 self._run_test(idlparser.WEBKIT_SYNTAX,
82 '#if 1\nmodule TestModule {};\n#endif\n',
83 {'modules': [{
84 'id': 'TestModule'
85 }]})
86
88 self._run_test(
89 idlparser.WEBKIT_SYNTAX,
90 'module M { interface [ExAt1, ExAt2] I {};};', {
91 'modules': [{
92 'interfaces': [{
93 'javascript_binding_name': 'I',
94 'doc_js_name': 'I',
95 'ext_attrs': {
96 'ExAt1': None,
97 'ExAt2': None
98 },
99 'id': 'I'
100 }],
101 'id':
102 'M'
103 }]
104 })
105
107 self._run_test(
108 idlparser.WEBIDL_SYNTAX, 'module M { X implements Y; };', {
109 'modules': [{
110 'implementsStatements': [{
111 'implementor': {
112 'id': 'X'
113 },
114 'implemented': {
115 'id': 'Y'
116 }
117 }],
118 'id':
119 'M'
120 }]
121 })
122
124 self._run_test(
125 idlparser.WEBIDL_SYNTAX, '''interface I {
126 attribute long a1;
127 readonly attribute DOMString a2;
128 attribute any a3;
129 };''', {
130 'interfaces': [{
131 'javascript_binding_name':
132 'I',
133 'attributes': [{
134 'type': {
135 'id': 'long'
136 },
137 'id': 'a1',
138 'doc_js_interface_name': 'I'
139 },
140 {
141 'type': {
142 'id': 'DOMString'
143 },
144 'is_read_only': True,
145 'id': 'a2',
146 'doc_js_interface_name': 'I'
147 },
148 {
149 'type': {
150 'id': 'any'
151 },
152 'id': 'a3',
153 'doc_js_interface_name': 'I'
154 }],
155 'id':
156 'I',
157 'doc_js_name':
158 'I'
159 }]
160 })
161
163 self._run_test(
164 idlparser.WEBIDL_SYNTAX, '''interface I {
165 [ExAttr] t1 op1();
166 t2 op2(in int arg1, in long arg2);
167 getter any item(in long index);
168 };''', {
169 'interfaces': [{
170 'operations':
171 [{
172 'doc_js_interface_name': 'I',
173 'type': {
174 'id': 't1'
175 },
176 'ext_attrs': {
177 'ExAttr': None
178 },
179 'id': 'op1'
180 },
181 {
182 'doc_js_interface_name':
183 'I',
184 'type': {
185 'id': 't2'
186 },
187 'id':
188 'op2',
189 'arguments': [{
190 'type': {
191 'id': 'int'
192 },
193 'id': 'arg1'
194 }, {
195 'type': {
196 'id': 'long'
197 },
198 'id': 'arg2'
199 }]
200 },
201 {
202 'specials': ['getter'],
203 'doc_js_interface_name': 'I',
204 'type': {
205 'id': 'any'
206 },
207 'id': 'item',
208 'arguments': [{
209 'type': {
210 'id': 'long'
211 },
212 'id': 'index'
213 }]
214 },
215 {
216 'is_stringifier': True,
217 'type': {
218 'id': 'name'
219 },
220 'doc_js_interface_name': 'I'
221 }],
222 'javascript_binding_name':
223 'I',
224 'id':
225 'I',
226 'doc_js_name':
227 'I'
228 }]
229 })
230
231 def test_constants(self):
232 self._run_test(
233 None, '''interface I {
234 const long c1 = 0;
235 const long c2 = 1;
236 const long c3 = 0x01;
237 const long c4 = 10;
238 const boolean b1 = false;
239 const boolean b2 = true;
240 };''', {
241 'interfaces': [{
242 'javascript_binding_name':
243 'I',
244 'doc_js_name':
245 'I',
246 'id':
247 'I',
248 'constants': [{
249 'type': {
250 'id': 'long'
251 },
252 'id': 'c1',
253 'value': '0',
254 'doc_js_interface_name': 'I'
255 },
256 {
257 'type': {
258 'id': 'long'
259 },
260 'id': 'c2',
261 'value': '1',
262 'doc_js_interface_name': 'I'
263 },
264 {
265 'type': {
266 'id': 'long'
267 },
268 'id': 'c3',
269 'value': '0x01',
270 'doc_js_interface_name': 'I'
271 },
272 {
273 'type': {
274 'id': 'long'
275 },
276 'id': 'c4',
277 'value': '10',
278 'doc_js_interface_name': 'I'
279 },
280 {
281 'type': {
282 'id': 'boolean'
283 },
284 'id': 'b1',
285 'value': 'false',
286 'doc_js_interface_name': 'I'
287 },
288 {
289 'type': {
290 'id': 'boolean'
291 },
292 'id': 'b2',
293 'value': 'true',
294 'doc_js_interface_name': 'I'
295 }]
296 }]
297 })
298
300 self._run_test(
301 idlparser.FREMONTCUT_SYNTAX,
302 '@Ano1 @Ano2() @Ano3(x=1) @Ano4(x,y=2) interface I {};', {
303 'interfaces': [{
304 'javascript_binding_name': 'I',
305 'doc_js_name': 'I',
306 'id': 'I',
307 'annotations': {
308 'Ano4': {
309 'y': '2',
310 'x': None
311 },
312 'Ano1': {},
313 'Ano2': {},
314 'Ano3': {
315 'x': '1'
316 }
317 }
318 }]
319 })
320 self._run_test(
321 idlparser.FREMONTCUT_SYNTAX, '''interface I : @Ano1 J {
322 @Ano2 attribute int someAttr;
323 @Ano3 void someOp();
324 @Ano3 const int someConst = 0;
325 };''', {
326 'interfaces': [{
327 'operations': [{
328 'annotations': {
329 'Ano3': {}
330 },
331 'type': {
332 'id': 'void'
333 },
334 'id': 'someOp',
335 'doc_js_interface_name': 'I'
336 }],
337 'javascript_binding_name':
338 'I',
339 'parents': [{
340 'type': {
341 'id': 'J'
342 },
343 'annotations': {
344 'Ano1': {}
345 }
346 }],
347 'attributes': [{
348 'annotations': {
349 'Ano2': {}
350 },
351 'type': {
352 'id': 'int'
353 },
354 'id': 'someAttr',
355 'doc_js_interface_name': 'I'
356 }],
357 'doc_js_name':
358 'I',
359 'id':
360 'I',
361 'constants': [{
362 'annotations': {
363 'Ano3': {}
364 },
365 'type': {
366 'id': 'int'
367 },
368 'id': 'someConst',
369 'value': '0',
370 'doc_js_interface_name': 'I'
371 }]
372 }]
373 })
374
376 self._run_test(
377 None,
378 'interface Shape {}; interface Rectangle : Shape {}; interface Square : Rectangle, Shape {};',
379 {
380 'interfaces': [{
381 'javascript_binding_name': 'Shape',
382 'doc_js_name': 'Shape',
383 'id': 'Shape'
384 },
385 {
386 'javascript_binding_name': 'Rectangle',
387 'doc_js_name': 'Rectangle',
388 'parents': [{
389 'type': {
390 'id': 'Shape'
391 }
392 }],
393 'id': 'Rectangle'
394 },
395 {
396 'javascript_binding_name':
397 'Square',
398 'doc_js_name':
399 'Square',
400 'parents': [{
401 'type': {
402 'id': 'Rectangle'
403 }
404 }, {
405 'type': {
406 'id': 'Shape'
407 }
408 }],
409 'id':
410 'Square'
411 }]
412 })
413
414
415if __name__ == "__main__":
416 logging.config.fileConfig("logging.conf")
417 if __name__ == '__main__':
418 unittest.main()
def _run_test(self, syntax, content, expected)
Definition: idlnode_test.py:15