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 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
|
#!/usr/bin/env python3
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
import web_idl_schema
from web_idl_schema import SchemaCompilerError
# Helper functions for fetching specific parts of a processed API schema
# dictionary.
def getFunction(schema: dict, name: str) -> dict:
"""Gets the function dictionary with the specified name from the schema.
Args:
schema: The processed API schema dictionary to look for the function in.
name: The name of the function to look for.
Returns:
The dictionary for the function with the specified name.
Raises:
KeyError: If the given function name was not found in the list of functions.
"""
for item in schema['functions']:
if item['name'] == name:
return item
raise KeyError('Could not find "function" with name "%s" in schema' % name)
def getType(schema: dict, name: str) -> dict:
"""Gets the custom type dictionary with the specified name from the schema.
Args:
schema: The processed API schema dictionary to look for the type in.
name: The name of the custom type to look for.
Returns:
The dictionary for the custom type with the specified name.
Raises:
KeyError: If the given type name was not found in the list of types.
"""
for item in schema['types']:
if item['id'] == name:
return item
raise KeyError('Could not find "type" with id "%s" in schema' % name)
def getEvent(schema: dict, name: str) -> dict:
"""Gets the event dictionary with the specified name from the schema.
Args:
schema: The processed API schema dictionary to look for the event in.
name: The name of the event to look for.
Returns:
The dictionary for the event with the specified name.
Raises:
KeyError: If the given event name was not found in the list of events.
"""
for item in schema['events']:
if item['name'] == name:
return item
raise KeyError('Could not find "event" with name "%s" in schema' % name)
def getFunctionReturn(schema: dict, name: str) -> dict:
"""Gets the return dictionary for the function with the specified name.
Args:
schema: The processed API schema dictionary to look for the function in.
name: The name of the function to get the return value from.
Returns:
The dictionary representing the return for the specified function name if it
has a return, otherwise None if it does not.
"""
function = getFunction(schema, name)
return function.get('returns', None)
def getFunctionAsyncReturn(schema: dict, name: str) -> dict:
"""Gets the async return dictionary for the function with the specified name.
Args:
schema: The processed API schema dictionary to look for the function in.
name: The name of the function to get the async return value from.
Returns:
The dictionary representing the async return for the function with the
specified name if it has one, otherwise None if it does not.
"""
function = getFunction(schema, name)
return function.get('returns_async', None)
def getFunctionParameters(schema: dict, name: str) -> dict:
"""Gets the list of parameters for the function with the specified name.
Args:
schema: The processed API schema dictionary to look for the function in.
name: The name of the function to get the parameters list from.
Returns:
The list of dictionaries representing the function parameters for the
function with the specified name if it has any, otherwise None if it does
not.
"""
# TODO(crbug.com/340297705): All functions should have the 'parameters' key,
# so we shouldn't have a None fallback and just raise a KeyError if it isn't
# present.
function = getFunction(schema, name)
return function.get('parameters', None)
class WebIdlSchemaTest(unittest.TestCase):
def setUp(self):
loaded = web_idl_schema.Load('test/web_idl/basics.idl')
self.assertEqual(1, len(loaded))
self.assertEqual('testWebIdl', loaded[0]['namespace'])
self.idl_basics = loaded[0]
def testFunctionReturnTypes(self):
schema = self.idl_basics
# Test basic types.
self.assertEqual(
None,
getFunctionReturn(schema, 'returnsUndefined'),
)
self.assertEqual(
{
'name': 'returnsBoolean',
'type': 'boolean'
},
getFunctionReturn(schema, 'returnsBoolean'),
)
self.assertEqual(
{
'name': 'returnsDouble',
'type': 'number'
},
getFunctionReturn(schema, 'returnsDouble'),
)
self.assertEqual(
{
'name': 'returnsLong',
'type': 'integer'
},
getFunctionReturn(schema, 'returnsLong'),
)
self.assertEqual(
{
'name': 'returnsDOMString',
'type': 'string'
},
getFunctionReturn(schema, 'returnsDOMString'),
)
self.assertEqual({
'name': 'returnsCustomType',
'$ref': 'ExampleType'
}, getFunctionReturn(schema, 'returnsCustomType'))
self.assertEqual(
{
'name': 'returnsDOMStringSequence',
'type': 'array',
'items': {
'type': 'string'
}
}, getFunctionReturn(schema, 'returnsDOMStringSequence'))
self.assertEqual(
{
'name': 'returnsCustomTypeSequence',
'type': 'array',
'items': {
'$ref': 'ExampleType'
}
}, getFunctionReturn(schema, 'returnsCustomTypeSequence'))
def testPromiseBasedReturn(self):
schema = self.idl_basics
self.assertEqual(
{
'name': 'callback',
'parameters': [{
'type': 'string'
}],
'type': 'promise'
}, getFunctionAsyncReturn(schema, 'stringPromiseReturn'))
self.assertEqual(
{
'name': 'callback',
'parameters': [{
'optional': True,
'type': 'string'
}],
'type': 'promise'
}, getFunctionAsyncReturn(schema, 'nullablePromiseReturn'))
self.assertEqual(
{
'name': 'callback',
'parameters': [{
'$ref': 'ExampleType'
}],
'type': 'promise'
}, getFunctionAsyncReturn(schema, 'customTypePromiseReturn'))
self.assertEqual({
'name': 'callback',
'parameters': [],
'type': 'promise'
}, getFunctionAsyncReturn(schema, 'undefinedPromiseReturn'))
self.assertEqual(
{
'name': 'callback',
'parameters': [{
'type': 'array',
'items': {
'type': 'integer'
}
}],
'type': 'promise'
}, getFunctionAsyncReturn(schema, 'longSequencePromiseReturn'))
self.assertEqual(
{
'name': 'callback',
'parameters': [{
'type': 'array',
'items': {
'$ref': 'ExampleType'
}
}],
'type': 'promise'
}, getFunctionAsyncReturn(schema, 'customTypeSequencePromiseReturn'))
# Tests function parameters are processed as expected.
def testFunctionParameters(self):
schema = self.idl_basics
# A function with no arguments has an empty array on the "parameters" key.
self.assertEqual([], getFunctionParameters(schema, 'takesNoArguments'))
self.assertEqual([{
'name': 'stringArgument',
'type': 'string'
}], getFunctionParameters(schema, 'takesDOMString'))
self.assertEqual([{
'name': 'optionalBoolean',
'optional': True,
'type': 'boolean'
}], getFunctionParameters(schema, 'takesOptionalBoolean'))
self.assertEqual([{
'name': 'argument1',
'type': 'string'
}, {
'name': 'argument2',
'optional': True,
'type': 'number'
}], getFunctionParameters(schema, 'takesMultipleArguments'))
self.assertEqual([{
'name': 'first',
'type': 'string'
}, {
'name': 'optionalInner',
'optional': True,
'type': 'string'
}, {
'name': 'last',
'type': 'string'
}], getFunctionParameters(schema, 'takesOptionalInnerArgument'))
self.assertEqual([{
'name': 'sequenceArgument',
'type': 'array',
'items': {
'type': 'boolean'
}
}], getFunctionParameters(schema, 'takesSequenceArgument'))
self.assertEqual([{
'name': 'optionalSequenceArgument',
'type': 'array',
'optional': True,
'items': {
'type': 'boolean'
}
}], getFunctionParameters(schema, 'takesOptionalSequenceArgument'))
self.assertEqual([{
'name': 'customTypeArgument',
'$ref': 'ExampleType'
}], getFunctionParameters(schema, 'takesCustomType'))
self.assertEqual([{
'name': 'optionalCustomTypeArgument',
'optional': True,
'$ref': 'ExampleType'
}], getFunctionParameters(schema, 'takesOptionalCustomType'))
# Tests function descriptions are processed as expected.
# TODO(crbug.com/379052294): Add testcases for function return descriptions
# once support for those are added to the processor.
def testFunctionDescriptions(self):
schema = self.idl_basics
# A function without a preceding comment has no 'description' key.
self.assertTrue('description' not in getFunction(schema, 'noDescription'))
self.assertEqual(
'One line description.',
getFunction(schema, 'oneLineDescription').get('description'))
self.assertEqual(
'Multi line description. Split over. Multiple lines.',
getFunction(schema, 'multiLineDescription').get('description'))
self.assertEqual(
'<p>Paragraphed description.</p><p>With blank comment line for'
' paragraph tags.</p>',
getFunction(schema, 'paragraphedDescription').get('description'))
function = getFunction(schema, 'parameterComments')
self.assertEqual('This function has parameter comments.',
function.get('description'))
function_parameters = getFunctionParameters(schema, 'parameterComments')
self.assertEqual(2, len(function_parameters))
self.assertEqual(
{
'description':
('This comment about the argument is split across multiple lines'
' and contains <em>HTML tags</em>.'),
'name':
'arg1',
'type':
'boolean',
},
function_parameters[0],
)
self.assertEqual(
{
'description': 'This second argument uses a custom type.',
'name': 'arg2',
'$ref': 'ExampleType'
}, function_parameters[1])
promise_function = getFunction(schema, 'namedPromiseReturn')
self.assertEqual(
('Promise returning function, with a comment that provides the name and'
' description of the value the promise resolves to.'),
promise_function.get('description'))
promise_function_parameters = getFunctionParameters(schema,
'namedPromiseReturn')
self.assertEqual(1, len(promise_function_parameters))
self.assertEqual(
{
'description': 'This is a normal argument comment.',
'name': 'arg1',
'type': 'boolean',
},
promise_function_parameters[0],
)
promise_function_async_return = getFunctionAsyncReturn(
schema, 'namedPromiseReturn')
self.assertEqual(
{
'name':
'callback',
'type':
'promise',
'parameters': [{
'$ref':
'ExampleType',
'name':
'returnValueName',
'description':
('A description for the value the promise resolves to: with'
' an extra colon for good measure.'),
}],
},
promise_function_async_return,
)
# Tests that API events are processed as expected.
def testEvents(self):
schema = self.idl_basics
event_one = getEvent(schema, 'onTestOne')
# This is a bit of a tautology for now, as getEvent() uses name to retrieve
# the object and raises a KeyError if it is not found.
self.assertEqual('onTestOne', event_one.get('name'))
self.assertEqual('function', event_one.get('type'))
self.assertEqual(
'Comment that acts as a description for onTestOne. Parameter specific'
' comments are down below before the associated callback definition.',
event_one.get('description'))
self.assertEqual(
[{
'name': 'argument1',
'type': 'string',
'description': 'Parameter description for argument1.'
}, {
'name': 'argument2',
'optional': True,
'type': 'number',
'description': 'Another description, this time for argment2.'
}], event_one['parameters'])
event_two = getEvent(schema, 'onTestTwo')
self.assertEqual('onTestTwo', event_two.get('name'))
self.assertEqual('function', event_two.get('type'))
self.assertEqual('Comment for onTestTwo.', event_two.get('description'))
self.assertEqual(
[{
'name': 'customType',
'$ref': 'ExampleType',
'description': 'An ExampleType passed to the event listener.'
}], event_two['parameters'])
# Tests that Dictionaries defined on the top level of the IDL file are
# processed into types on the resulting namespace.
def testApiTypesOnNamespace(self):
schema = self.idl_basics
custom_type = getType(schema, 'ExampleType')
self.assertEqual('ExampleType', custom_type['id'])
self.assertEqual('object', custom_type['type'])
self.assertEqual(
{
'name': 'someString',
'type': 'string',
'description':
'Attribute comment attached to ExampleType.someString.'
}, custom_type['properties']['someString'])
self.assertEqual(
{
'name': 'someNumber',
'type': 'number',
'description':
'Comment where <var>someNumber</var> has some markup.'
}, custom_type['properties']['someNumber'])
# TODO(crbug.com/379052294): using HTML comments like this is a bit of a
# hack to allow us to add comments in IDL files (e.g. for TODOs) and to not
# have them end up on the documentation site. We should probably just filter
# them out during compilation.
self.assertEqual(
{
'name':
'optionalBoolean',
'type':
'boolean',
'optional':
True,
'description':
'Comment with HTML comment. <!-- Which should get through -->'
}, custom_type['properties']['optionalBoolean'])
self.assertEqual(
{
'name': 'booleanSequence',
'type': 'array',
'items': {
'type': 'boolean'
},
'description': 'Comment on sequence type.',
}, custom_type['properties']['booleanSequence'])
# Tests that a top level API comment is processed into a description
# attribute, with HTML paragraph nodes added due to the blank commented line.
def testApiDescriptionComment(self):
schema = self.idl_basics
expected_description = (
'<p>This comment is an example of a top level API description, which'
' will be extracted and added to the processed python dictionary as a'
' description.</p><p>Note: All comment lines preceding the thing they'
' are attached to will be part of the description, until a blank new'
' line or non-comment is reached.</p>')
self.assertEqual(expected_description, schema['description'])
# Tests that if the nodoc extended attribute is not specified on the API
# interface the related attribute is set to false after processing.
def testNodocUnspecifiedOnNamespace(self):
self.assertFalse(self.idl_basics['nodoc'])
# TODO(crbug.com/340297705): This will eventually be relaxed when adding
# support for shared types to the new parser.
def testMissingBrowserInterfaceError(self):
expected_error_regex = (
r'.* File\(test\/web_idl\/missing_browser_interface.idl\): Required'
r' partial Browser interface not found in schema\.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/missing_browser_interface.idl',
)
# Tests that having a Browser interface on an API definition with no attribute
# throws an error.
def testMissingAttributeOnBrowserError(self):
expected_error_regex = (
r'.* Interface\(Browser\): The partial Browser interface should have'
r' exactly one attribute for the name the API will be exposed under\.')
self.assertRaisesRegex(
Exception,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/missing_attribute_on_browser.idl',
)
# Tests that using a valid basic WebIDL type with a "name" the schema compiler
# doesn't support yet throws an error.
def testUnsupportedBasicTypeError(self):
expected_error_regex = (
r'.* PrimitiveType\(float\): Unsupported basic type found when'
r' processing type\.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/unsupported_basic_type.idl',
)
# Tests that using a valid WebIDL type with a node "class" the schema compiler
# doesn't support yet throws an error.
def testUnsupportedTypeClassError(self):
expected_error_regex = (
r'.* Any\(\): Unsupported type class when processing type\.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/unsupported_type_class.idl',
)
# Tests that an event trying to say it uses an Interface that is not defined
# in the IDL file will throw an error. This is largely in place to help catch
# spelling mistakes in event names or forgetting to add the Interface
# definition.
def testMissingEventInterface(self):
expected_error_regex = (
r'.* Error processing node Attribute\(onTestTwo\): Could not find'
r' Interface definition for event\.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/missing_event_interface.idl',
)
# Various tests that ensure validation on event interface definitions.
# Specifically checks that not defining any of the required add/remove/has
# Operations or forgetting the ExtensionEvent inheritance will throw an error.
def testMissingEventInheritance(self):
expected_error_regex = (
r'.* Error processing node Interface\(OnMissingInheritanceEvent\):'
r' Event Interface missing ExtensionEvent Inheritance.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/missing_event_inheritance.idl',
)
def testMissingEventAddListener(self):
expected_error_regex = (
r'.* Error processing node Interface\(OnMissingAddListenerEvent\):'
r' Event Interface missing addListener Operation definition.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/missing_event_add_listener.idl',
)
def testMissingEventRemoveListener(self):
expected_error_regex = (
r'.* Error processing node Interface\(OnMissingRemoveListenerEvent\):'
r' Event Interface missing removeListener Operation definition.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/missing_event_remove_listener.idl',
)
def testMissingEventHasListener(self):
expected_error_regex = (
r'.* Error processing node Interface\(OnMissingHasListenerEvent\):'
r' Event Interface missing hasListener Operation definition.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/missing_event_has_listener.idl',
)
# Tests that if description parsing from file comments reaches the top of the
# file, a schema compiler error is thrown (as the top of the file should
# always be copyright lines and not part of the description).
def testDocumentationCommentReachedTopOfFileError(self):
expected_error_regex = (
r'.* Reached top of file when trying to parse description from file'
r' comment. Make sure there is a blank line before the comment.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/documentation_comment_top_of_file.idl',
)
# Tests that usage of the 'void' type will result in a schema compiler error.
# 'void' has been deprecated and 'undefined' should be used instead.
def testVoidUsageTriggersError(self):
expected_error_regex = (
r'Error processing node PrimitiveType\(void\): Usage of "void" in IDL'
r' is deprecated, use "Undefined" instead.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/void_unsupported.idl',
)
# Tests that a namespace with an extended attribute that we don't have
# processing for results in a schema compiler error.
def testUnknownNamespaceExtendedAttributeNameError(self):
expected_error_regex = (
r'.* Interface\(TestWebIdl\): Unknown extended attribute with name'
r' "UnknownExtendedAttribute" when processing namespace.')
self.assertRaisesRegex(
SchemaCompilerError,
expected_error_regex,
web_idl_schema.Load,
'test/web_idl/unknown_namespace_extended_attribute.idl',
)
# Tests that an API interface that uses the nodoc extended attribute has the
# related nodoc attribute set to true after processing.
def testNoDocOnNamespace(self):
idl = web_idl_schema.Load('test/web_idl/nodoc_on_namespace.idl')
self.assertEqual(1, len(idl))
schema = idl[0]
self.assertEqual('nodocAPI', schema['namespace'])
self.assertTrue(schema['nodoc'])
# Also ensure the description comes through correctly on the node with
# 'nodoc' as an extended attribute.
self.assertEqual(
'The nodoc API. This exists to demonstrate nodoc on the main interface'
' itself.',
schema['description'],
)
# Tests that extended attributes being listed on the the line previous to a
# node come through correctly and don't throw off and associated descriptions.
# TODO(crbug.com/340297705): Add checks for functions here once support for
# processing their descriptions is complete.
def testPreviousLineExtendedAttributes(self):
idl = web_idl_schema.Load('test/web_idl/preceding_extended_attributes.idl')
self.assertEqual(1, len(idl))
schema = idl[0]
self.assertEqual('precedingExtendedAttributes', schema['namespace'])
self.assertTrue(schema['nodoc'])
self.assertEqual(
'Comment on a schema that has extended attributes on a previous line.',
schema['description'],
)
# Tests that an API interface with the platforms extended attribute has these
# values in a platforms attribute after processing.
def testAllPlatformsOnNamespace(self):
platforms_schema = web_idl_schema.Load(
'test/web_idl/all_platforms_on_namespace.idl')
self.assertEqual(1, len(platforms_schema))
self.assertEqual('allPlatformsAPI', platforms_schema[0]['namespace'])
expected = ['chromeos', 'fuchsia', 'linux', 'mac', 'win']
self.assertEqual(expected, platforms_schema[0]['platforms'])
# Tests that an API interface with just chromeos listed in the platforms
# extended attribute just has that after processing.
def testChromeOSPlatformsOnNamespace(self):
platforms_schema = web_idl_schema.Load(
'test/web_idl/chromeos_platforms_on_namespace.idl')
self.assertEqual(1, len(platforms_schema))
self.assertEqual('chromeOSPlatformsAPI', platforms_schema[0]['namespace'])
expected = ['chromeos']
self.assertEqual(expected, platforms_schema[0]['platforms'])
# Tests a variety of default values that are set on an API namespace when they
# are not specified in the source IDL file.
def testNonSpecifiedDefaultValues(self):
defaults_schema = web_idl_schema.Load('test/web_idl/defaults.idl')[0]
self.assertEqual(
{
'compiler_options': {},
'deprecated': None,
'description': '',
'events': [],
'functions': [],
'manifest_keys': None,
'namespace': 'defaultsOnlyWebIdl',
'nodoc': False,
'platforms': None,
'properties': {},
'types': [],
}, defaults_schema)
if __name__ == '__main__':
unittest.main()
|