コード例 #1
0
def _Enum(module, parsed_enum, parent_kind):
  """
  Args:
    module: {mojom.Module} Module currently being constructed.
    parsed_enum: {ast.Enum} Parsed enum.

  Returns:
    {mojom.Enum} AST enum.
  """
  enum = mojom.Enum(module=module)
  enum.mojom_name = parsed_enum.mojom_name
  enum.native_only = parsed_enum.enum_value_list is None
  mojom_name = enum.mojom_name
  if parent_kind:
    mojom_name = parent_kind.mojom_name + '.' + mojom_name
  enum.spec = 'x:%s.%s' % (module.mojom_namespace, mojom_name)
  enum.parent_kind = parent_kind
  enum.attributes = _AttributeListToDict(parsed_enum.attribute_list)
  if not enum.native_only:
    enum.fields = map(
        lambda field: _EnumField(module, enum, field, parent_kind),
        parsed_enum.enum_value_list)
    enum.min_value, enum.max_value = _ResolveNumericEnumValues(enum.fields)

  module.kinds[enum.spec] = enum

  # Enforce that a [Native] attribute is set to make native-only enum
  # declarations more explicit.
  if enum.native_only:
    if not enum.attributes or not enum.attributes.get('Native', False):
      raise Exception("Native-only enum declarations must include a " +
                      "Native attribute.")

  return enum
コード例 #2
0
def _Enum(module, parsed_enum, parent_kind):
    """
  Args:
    module: {mojom.Module} Module currently being constructed.
    parsed_enum: {ast.Enum} Parsed enum.

  Returns:
    {mojom.Enum} AST enum.
  """
    enum = mojom.Enum(module=module)
    enum.mojom_name = parsed_enum.mojom_name
    enum.native_only = parsed_enum.enum_value_list is None
    mojom_name = enum.mojom_name
    if parent_kind:
        mojom_name = parent_kind.mojom_name + '.' + mojom_name
    enum.spec = 'x:%s.%s' % (module.mojom_namespace, mojom_name)
    enum.parent_kind = parent_kind
    enum.attributes = _AttributeListToDict(parsed_enum.attribute_list)

    if not enum.native_only:
        enum.fields = list(
            map(lambda field: _EnumField(module, enum, field),
                parsed_enum.enum_value_list))
        _ResolveNumericEnumValues(enum)
        # TODO(https://crbug.com/731893): Require a default value to be
        # specified.
        for field in enum.fields:
            if field.default:
                if not enum.extensible:
                    raise Exception(
                        'Non-extensible enums may not specify a default')
                if enum.default_field is not None:
                    raise Exception(
                        'Only one enumerator value may be specified as the default'
                    )
                enum.default_field = field

    module.kinds[enum.spec] = enum

    # Enforce that a [Native] attribute is set to make native-only enum
    # declarations more explicit.
    if enum.native_only:
        if not enum.attributes or not enum.attributes.get('Native', False):
            raise Exception("Native-only enum declarations must include a " +
                            "Native attribute.")

    return enum
コード例 #3
0
  def testImportFromDataNoMissingImports(self):
    """Tests that unions, structs, interfaces and enums are imported."""
    module = mojom.Module('test_module', 'test_namespace')
    imported_module = mojom.Module('import_module', 'import_namespace')
    #TODO(azani): Init values in module.py.
    #TODO(azani): Test that values are imported.
    imported_module.values = {}
    imported_data = {'module' : imported_module}


    struct = mojom.Struct('TestStruct', module=module)
    imported_module.kinds[struct.spec] = struct

    union = mojom.Union('TestUnion', module=module)
    imported_module.kinds[union.spec] = union

    interface = mojom.Interface('TestInterface', module=module)
    imported_module.kinds[interface.spec] = interface

    enum = mojom.Enum('TestEnum', module=module)
    imported_module.kinds[enum.spec] = enum

    data.ImportFromData(module, imported_data)

    # Test that the kind was imported.
    self.assertIn(struct.spec, module.kinds)
    self.assertEquals(struct.name, module.kinds[struct.spec].name)

    self.assertIn(union.spec, module.kinds)
    self.assertEquals(union.name, module.kinds[union.spec].name)

    self.assertIn(interface.spec, module.kinds)
    self.assertEquals(interface.name, module.kinds[interface.spec].name)

    self.assertIn(enum.spec, module.kinds)
    self.assertEquals(enum.name, module.kinds[enum.spec].name)

    # Test that the imported kind is a copy and not the original.
    self.assertIsNot(struct, module.kinds[struct.spec])
    self.assertIsNot(union, module.kinds[union.spec])
    self.assertIsNot(interface, module.kinds[interface.spec])
    self.assertIsNot(enum, module.kinds[enum.spec])