File: idl_schema_test.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (682 lines) | stat: -rwxr-xr-x 21,169 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#!/usr/bin/env python3
# Copyright 2012 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import idl_schema
import unittest

from json_parse import OrderedDict


def getFunction(schema, name):
  for item in schema['functions']:
    if item['name'] == name:
      return item
  raise KeyError('Missing function %s' % name)


def getParams(schema, name):
  function = getFunction(schema, name)
  return function['parameters']


def getReturnsAsync(schema, name):
  function = getFunction(schema, name)
  return function.get('returns_async', False)


def getReturns(schema, name):
  function = getFunction(schema, name)
  return function['returns']


def getType(schema, id):
  for item in schema['types']:
    if item['id'] == id:
      return item


class IdlSchemaTest(unittest.TestCase):

  def setUp(self):
    loaded = idl_schema.Load('test/idl_basics.idl')
    self.assertEqual(1, len(loaded))
    self.assertEqual('idl_basics', loaded[0]['namespace'])
    self.idl_basics = loaded[0]
    self.maxDiff = None

  def testNamespaceDescription(self):
    # Tests the top level namespace description is cleaned up and joined
    # together as expected.
    schema = self.idl_basics
    self.assertEqual(
        'Tests a variety of basic API definition features, ensuring things are'
        ' parsed and processed as expected.',
        schema['description'],
    )

  def testSimpleCallbacks(self):
    schema = self.idl_basics
    expected = {'name': 'cb', 'parameters': []}
    self.assertEqual(expected, getReturnsAsync(schema, 'function4'))

    expected = {'name': 'cb', 'parameters': [{'name': 'x', 'type': 'integer'}]}
    self.assertEqual(expected, getReturnsAsync(schema, 'function5'))

    expected = {
        'name': 'cb',
        'parameters': [{
            'name': 'arg',
            '$ref': 'MyType1'
        }]
    }
    self.assertEqual(expected, getReturnsAsync(schema, 'function6'))

  def testCallbackWithArrayArgument(self):
    schema = self.idl_basics
    expected = {
        'name':
        'cb',
        'parameters': [{
            'name': 'arg',
            'type': 'array',
            'items': {
                '$ref': 'MyType2'
            }
        }]
    }
    self.assertEqual(expected, getReturnsAsync(schema, 'function12'))

  def testArrayOfCallbacks(self):
    schema = idl_schema.Load('test/idl_function_types.idl')[0]
    expected = [{
        'type': 'array',
        'name': 'callbacks',
        'items': {
            'type': 'function',
            'name': 'MyCallback',
            'parameters': [{
                'type': 'integer',
                'name': 'x'
            }]
        }
    }]
    self.assertEqual(expected, getParams(schema, 'whatever'))

  def testProperties(self):
    self.assertEqual(
        {
            'x': {
                'name': 'x',
                'type': 'integer',
                'description': 'This comment tests "double-quotes".'
            },
            'y': {
                'name': 'y',
                'type': 'string'
            },
            'z': {
                'name': 'z',
                'type': 'string'
            },
            'a': {
                'name': 'a',
                'type': 'string'
            },
            'b': {
                'name': 'b',
                'type': 'string'
            },
            'c': {
                'name': 'c',
                'type': 'string'
            }
        },
        getType(self.idl_basics, 'MyType1')['properties'])

  def testMemberOrdering(self):
    self.assertEqual(['x', 'y', 'z', 'a', 'b', 'c'],
                     list(
                         getType(self.idl_basics,
                                 'MyType1')['properties'].keys()))

  def testEnum(self):
    schema = self.idl_basics
    expected = {
        'enum': [{
            'name': 'name1',
            'description': 'comment1'
        }, {
            'name': 'name2'
        }],
        'description': 'Enum description',
        'type': 'string',
        'id': 'EnumType'
    }
    self.assertEqual(expected, getType(schema, expected['id']))

    expected_params = [{'name': 'type', '$ref': 'EnumType'}]
    expected_returns_async = {
        'name': 'cb',
        'parameters': [{
            'name': 'type',
            '$ref': 'EnumType'
        }]
    }
    self.assertEqual(expected_params, getParams(schema, 'function13'))
    self.assertEqual(expected_returns_async,
                     getReturnsAsync(schema, 'function13'))

    expected = [{
        'items': {
            '$ref': 'EnumType'
        },
        'name': 'types',
        'type': 'array'
    }]
    self.assertEqual(expected, getParams(schema, 'function14'))

  def testScopedArguments(self):
    schema = self.idl_basics
    expected = [{'name': 'value', '$ref': 'idl_other_namespace.SomeType'}]
    self.assertEqual(expected, getParams(schema, 'function20'))

    expected = [{
        'items': {
            '$ref': 'idl_other_namespace.SomeType'
        },
        'name': 'values',
        'type': 'array'
    }]
    self.assertEqual(expected, getParams(schema, 'function21'))

    expected = [{
        'name': 'value',
        '$ref': 'idl_other_namespace.sub_namespace.AnotherType'
    }]
    self.assertEqual(expected, getParams(schema, 'function22'))

    expected = [{
        'items': {
            '$ref': 'idl_other_namespace.sub_namespace.'
            'AnotherType'
        },
        'name': 'values',
        'type': 'array'
    }]
    self.assertEqual(expected, getParams(schema, 'function23'))

  def testNoCompile(self):
    schema = self.idl_basics
    func = getFunction(schema, 'function15')
    self.assertTrue(func is not None)
    self.assertTrue(func['nocompile'])

  def testNoDocOnEnum(self):
    schema = self.idl_basics
    enum_with_nodoc = getType(schema, 'EnumTypeWithNoDoc')
    self.assertTrue(enum_with_nodoc is not None)
    self.assertTrue(enum_with_nodoc['nodoc'])

  def testNoDocOnEnumValue(self):
    schema = self.idl_basics
    expected = {
        'enum': [{
            'name': 'name1'
        }, {
            'name': 'name2',
            'nodoc': True,
            'description': 'comment2'
        }, {
            'name': 'name3',
            'description': 'comment3'
        }],
        'type':
        'string',
        'id':
        'EnumTypeWithNoDocValue',
        'description':
        ''
    }
    self.assertEqual(expected, getType(schema, expected['id']))

  def testReturnTypes(self):
    schema = self.idl_basics
    self.assertEqual({
        'name': 'function24',
        'type': 'integer'
    }, getReturns(schema, 'function24'))
    self.assertEqual({
        'name': 'function25',
        '$ref': 'MyType1',
        'optional': True
    }, getReturns(schema, 'function25'))
    self.assertEqual(
        {
            'name': 'function26',
            'type': 'array',
            'items': {
                '$ref': 'MyType1'
            }
        }, getReturns(schema, 'function26'))
    self.assertEqual(
        {
            'name': 'function27',
            '$ref': 'EnumType',
            'optional': True
        }, getReturns(schema, 'function27'))
    self.assertEqual(
        {
            'name': 'function28',
            'type': 'array',
            'items': {
                '$ref': 'EnumType'
            }
        }, getReturns(schema, 'function28'))
    self.assertEqual(
        {
            'name': 'function29',
            '$ref': 'idl_other_namespace.SomeType',
            'optional': True
        }, getReturns(schema, 'function29'))
    self.assertEqual(
        {
            'name': 'function30',
            'type': 'array',
            'items': {
                '$ref': 'idl_other_namespace.SomeType'
            }
        }, getReturns(schema, 'function30'))

  def testIgnoresAdditionalPropertiesOnType(self):
    self.assertTrue(
        getType(self.idl_basics,
                'IgnoreAdditionalPropertiesType')['ignoreAdditionalProperties'])

  def testChromeOSPlatformsNamespace(self):
    schema = idl_schema.Load('test/idl_namespace_chromeos.idl')[0]
    self.assertEqual('idl_namespace_chromeos', schema['namespace'])
    expected = ['chromeos']
    self.assertEqual(expected, schema['platforms'])

  def testAllPlatformsNamespace(self):
    schema = idl_schema.Load('test/idl_namespace_all_platforms.idl')[0]
    self.assertEqual('idl_namespace_all_platforms', schema['namespace'])
    expected = ['chromeos', 'desktop_android', 'fuchsia', 'linux', 'mac', 'win']
    self.assertEqual(expected, schema['platforms'])

  def testNonSpecificPlatformsNamespace(self):
    schema = idl_schema.Load('test/idl_namespace_non_specific_platforms.idl')[0]
    self.assertEqual('idl_namespace_non_specific_platforms',
                     schema['namespace'])
    expected = None
    self.assertEqual(expected, schema['platforms'])

  def testGenerateErrorMessages(self):
    schema = idl_schema.Load('test/idl_generate_error_messages.idl')[0]
    self.assertEqual('idl_generate_error_messages', schema['namespace'])
    self.assertTrue(schema['compiler_options'].get('generate_error_messages',
                                                   False))

    schema = idl_schema.Load('test/idl_basics.idl')[0]
    self.assertEqual('idl_basics', schema['namespace'])
    self.assertFalse(schema['compiler_options'].get('generate_error_messages',
                                                    False))

  def testSpecificImplementNamespace(self):
    schema = idl_schema.Load('test/idl_namespace_specific_implement.idl')[0]
    self.assertEqual('idl_namespace_specific_implement', schema['namespace'])
    expected = 'idl_namespace_specific_implement.idl'
    self.assertEqual(expected, schema['compiler_options']['implemented_in'])

  def testSpecificImplementOnChromeOSNamespace(self):
    schema = idl_schema.Load(
        'test/idl_namespace_specific_implement_chromeos.idl')[0]
    self.assertEqual('idl_namespace_specific_implement_chromeos',
                     schema['namespace'])
    expected_implemented_path = 'idl_namespace_specific_implement_chromeos.idl'
    expected_platform = ['chromeos']
    self.assertEqual(expected_implemented_path,
                     schema['compiler_options']['implemented_in'])
    self.assertEqual(expected_platform, schema['platforms'])

  def testCallbackComment(self):
    schema = self.idl_basics
    self.assertEqual('A comment on a callback.',
                     getReturnsAsync(schema, 'function16')['description'])
    self.assertEqual(
        'A parameter.',
        getReturnsAsync(schema, 'function16')['parameters'][0]['description'])
    self.assertEqual(
        'Just a parameter comment, with no comment on the callback.',
        getReturnsAsync(schema, 'function17')['parameters'][0]['description'])
    self.assertEqual('Override callback comment.',
                     getReturnsAsync(schema, 'function18')['description'])

  def testFunctionComment(self):
    schema = self.idl_basics
    func = getFunction(schema, 'function3')
    self.assertEqual(('This comment should appear in the documentation, '
                      'despite occupying multiple lines.'), func['description'])
    self.assertEqual([{
        'description': ('So should this comment about the argument. '
                        '<em>HTML</em> is fine too.'),
        'name':
        'arg',
        '$ref':
        'MyType1'
    }], func['parameters'])
    func = getFunction(schema, 'function4')
    self.assertEqual(
        '<p>This tests if "double-quotes" are escaped correctly.</p>'
        '<p>It also tests a comment with two newlines.</p>',
        func['description'])

  def testReservedWords(self):
    schema = idl_schema.Load('test/idl_reserved_words.idl')[0]

    foo_type = getType(schema, 'Foo')
    self.assertEqual([{
        'name': 'float'
    }, {
        'name': 'DOMString'
    }], foo_type['enum'])

    enum_type = getType(schema, 'enum')
    self.assertEqual([{
        'name': 'callback'
    }, {
        'name': 'namespace'
    }], enum_type['enum'])

    dictionary = getType(schema, 'dictionary')
    self.assertEqual('integer', dictionary['properties']['long']['type'])

    mytype = getType(schema, 'MyType')
    self.assertEqual('string', mytype['properties']['interface']['type'])

    params = getParams(schema, 'static')
    self.assertEqual('Foo', params[0]['$ref'])
    self.assertEqual('enum', params[1]['$ref'])

  def testObjectTypes(self):
    schema = idl_schema.Load('test/idl_object_types.idl')[0]

    foo_type = getType(schema, 'FooType')
    self.assertEqual('object', foo_type['type'])
    self.assertEqual('integer', foo_type['properties']['x']['type'])
    self.assertEqual('object', foo_type['properties']['y']['type'])
    self.assertEqual(
        'any', foo_type['properties']['y']['additionalProperties']['type'])
    self.assertEqual('object', foo_type['properties']['z']['type'])
    self.assertEqual(
        'any', foo_type['properties']['z']['additionalProperties']['type'])
    self.assertEqual('Window', foo_type['properties']['z']['isInstanceOf'])

    bar_type = getType(schema, 'BarType')
    self.assertEqual('object', bar_type['type'])
    self.assertEqual('any', bar_type['properties']['x']['type'])

  def testObjectTypesInFunctions(self):
    schema = idl_schema.Load('test/idl_object_types.idl')[0]

    params = getParams(schema, 'objectFunction1')
    self.assertEqual('object', params[0]['type'])
    self.assertEqual('any', params[0]['additionalProperties']['type'])
    self.assertEqual('ImageData', params[0]['isInstanceOf'])

    params = getParams(schema, 'objectFunction2')
    self.assertEqual('any', params[0]['type'])

  def testObjectTypesWithOptionalFields(self):
    schema = idl_schema.Load('test/idl_object_types.idl')[0]

    baz_type = getType(schema, 'BazType')
    self.assertEqual(True, baz_type['properties']['x']['optional'])
    self.assertEqual('integer', baz_type['properties']['x']['type'])
    self.assertEqual(True, baz_type['properties']['foo']['optional'])
    self.assertEqual('FooType', baz_type['properties']['foo']['$ref'])

  def testObjectTypesWithUnions(self):
    schema = idl_schema.Load('test/idl_object_types.idl')[0]

    union_type = getType(schema, 'UnionType')
    expected = {
        'type': 'object',
        'id': 'UnionType',
        'properties': {
            'x': {
                'name': 'x',
                'optional': True,
                'choices': [
                    {
                        'type': 'integer'
                    },
                    {
                        '$ref': 'FooType'
                    },
                ]
            },
            'y': {
                'name':
                'y',
                'choices': [{
                    'type': 'string'
                }, {
                    'type': 'object',
                    'additionalProperties': {
                        'type': 'any'
                    }
                }]
            },
            'z': {
                'name':
                'z',
                'choices': [{
                    'type': 'object',
                    'isInstanceOf': 'ImageData',
                    'additionalProperties': {
                        'type': 'any'
                    }
                }, {
                    'type': 'integer'
                }]
            }
        },
    }

    self.assertEqual(expected, union_type)

  def testUnionsWithModifiers(self):
    schema = idl_schema.Load('test/idl_object_types.idl')[0]

    union_type = getType(schema, 'ModifiedUnionType')
    expected = {
        'type': 'object',
        'id': 'ModifiedUnionType',
        'properties': {
            'x': {
                'name': 'x',
                'nodoc': True,
                'choices': [{
                    'type': 'integer'
                }, {
                    'type': 'string'
                }]
            }
        }
    }

    self.assertEqual(expected, union_type)

  def testSerializableFunctionType(self):
    schema = idl_schema.Load('test/idl_object_types.idl')[0]
    object_type = getType(schema, 'SerializableFunctionObject')
    expected = {
        'type': 'object',
        'id': 'SerializableFunctionObject',
        'properties': {
            'func': {
                'name': 'func',
                'serializableFunction': True,
                'type': 'function',
                'parameters': []
            }
        }
    }
    self.assertEqual(expected, object_type)

  def testUnionsWithFunctions(self):
    schema = idl_schema.Load('test/idl_function_types.idl')[0]

    union_params = getParams(schema, 'union_params')
    expected = [{
        'name': 'x',
        'choices': [{
            'type': 'integer'
        }, {
            'type': 'string'
        }]
    }]

    self.assertEqual(expected, union_params)

  def testUnionsWithCallbacks(self):
    schema = idl_schema.Load('test/idl_function_types.idl')[0]

    blah_params = getReturnsAsync(schema, 'blah')
    expected = {
        'name':
        'callback',
        'parameters': [{
            'name': 'x',
            'choices': [{
                'type': 'integer'
            }, {
                'type': 'string'
            }]
        }]
    }

    badabish_params = getReturnsAsync(schema, 'badabish')
    expected = {
        'name':
        'callback',
        'parameters': [{
            'name': 'x',
            'optional': True,
            'choices': [{
                'type': 'integer'
            }, {
                'type': 'string'
            }]
        }]
    }

    self.assertEqual(expected, badabish_params)

  def testFunctionWithoutPromiseSupport(self):
    schema = idl_schema.Load('test/idl_function_types.idl')[0]

    expected_params = []
    expected_returns_async = {
        'name': 'callback',
        'parameters': [{
            'name': 'x',
            'type': 'integer'
        }],
        'does_not_support_promises': 'Test'
    }
    params = getParams(schema, 'non_promise_supporting')
    returns_async = getReturnsAsync(schema, 'non_promise_supporting')

    self.assertEqual(expected_params, params)
    self.assertEqual(expected_returns_async, returns_async)

  def testFunctionWithoutPromiseSupportAndParams(self):
    schema = idl_schema.Load('test/idl_function_types.idl')[0]

    expected_params = [{
        'name': 'z',
        'type': 'integer'
    }, {
        'name': 'y',
        'choices': [{
            'type': 'integer'
        }, {
            'type': 'string'
        }]
    }]
    expected_returns_async = {
        'name': 'callback',
        'parameters': [{
            'name': 'x',
            'type': 'integer'
        }],
        'does_not_support_promises': 'Test'
    }
    params = getParams(schema, 'non_promise_supporting_with_params')
    returns_async = getReturnsAsync(schema,
                                    'non_promise_supporting_with_params')

    self.assertEqual(expected_params, params)
    self.assertEqual(expected_returns_async, returns_async)

  def testProperties(self):
    schema = idl_schema.Load('test/idl_properties.idl')[0]
    self.assertEqual(
        OrderedDict([
            ('first',
             OrderedDict([
                 ('description', 'Integer property.'),
                 ('type', 'integer'),
                 ('value', 42),
             ])),
            ('second',
             OrderedDict([
                 ('description', 'Double property.'),
                 ('type', 'number'),
                 ('value', 42.1),
             ])),
            ('third',
             OrderedDict([
                 ('description', 'String property.'),
                 ('type', 'string'),
                 ('value', 'hello world'),
             ])),
            ('fourth',
             OrderedDict([
                 ('description', 'Unvalued property.'),
                 ('type', 'integer'),
             ])),
        ]), schema.get('properties'))

  def testManifestKeys(self):
    schema = self.idl_basics
    # Test a smattering of the manifest key generation. We don't make this
    # exhaustive so we don't have to update it each time we add a new key in the
    # test file.
    manifest_keys = schema.get('manifest_keys')
    self.assertEqual(
        manifest_keys['key_str'],
        OrderedDict([('description', 'String manifest key.'),
                     ('name', 'key_str'), ('type', 'string')]))
    self.assertEqual(manifest_keys['key_ref'],
                     OrderedDict([('name', 'key_ref'), ('$ref', 'MyType2')])),
    self.assertEqual(
        manifest_keys['choice_with_arrays'],
        OrderedDict([('name', 'choice_with_arrays'),
                     ('$ref', 'ChoiceWithArraysType')])),
    self.assertEqual(
        manifest_keys['choice_with_optional'],
        OrderedDict([('name', 'choice_with_optional'),
                     ('$ref', 'ChoiceWithOptionalType')]))

  def testNoManifestKeys(self):
    schema = idl_schema.Load('test/idl_properties.idl')[0]
    self.assertIsNone(schema.get('manifest_keys'))


if __name__ == '__main__':
  unittest.main()