Exemplo n.º 1
0
    def test_constant_value(self):
        file_name = 'a.mojom'
        mojom_const = mojom_types_mojom.DeclaredConstant(
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='AConst',
                source_file_info=mojom_types_mojom.SourceFileInfo(
                    file_name=file_name)),
            type=mojom_types_mojom.Type(
                simple_type=mojom_types_mojom.SimpleType.INT64),
            value=mojom_types_mojom.Value(
                literal_value=mojom_types_mojom.LiteralValue(int64_value=30)))
        user_defined_value = mojom_types_mojom.UserDefinedValue()
        user_defined_value.declared_constant = mojom_const

        graph = mojom_files_mojom.MojomFileGraph()
        graph.resolved_values = {'value_key': user_defined_value}

        mojom = mojom_types_mojom.Value(
            user_value_reference=mojom_types_mojom.UserValueReference(
                identifier='SOMEID', value_key='value_key'))

        translator = mojom_translator.FileTranslator(graph, file_name)
        const_value = translator.ValueFromMojom(mojom)
        self.assertIs(translator.ConstantFromValueKey('value_key'),
                      const_value.constant)
        self.assertIs(mojom_const.decl_data.short_name, const_value.name)
Exemplo n.º 2
0
    def test_enum_value(self):
        file_name = 'a.mojom'
        mojom_enum = mojom_types_mojom.MojomEnum()
        mojom_enum.decl_data = mojom_types_mojom.DeclarationData(
            short_name='AnEnum',
            source_file_info=mojom_types_mojom.SourceFileInfo(
                file_name=file_name))
        value1 = mojom_types_mojom.EnumValue(
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='val1',
                source_file_info=mojom_types_mojom.SourceFileInfo(
                    file_name=file_name)),
            enum_type_key='enum_key',
            initializer_value=mojom_types_mojom.Value(
                literal_value=mojom_types_mojom.LiteralValue(uint64_value=20)),
            int_value=20)
        value2 = mojom_types_mojom.EnumValue(
            decl_data=mojom_types_mojom.DeclarationData(short_name='val2'),
            enum_type_key='enum_key',
            int_value=70)
        mojom_enum.values = [value1, value2]

        graph = mojom_files_mojom.MojomFileGraph()
        graph.resolved_types = {
            'enum_key': mojom_types_mojom.UserDefinedType(enum_type=mojom_enum)
        }
        graph.resolved_values = {
            'enum_value1':
            mojom_types_mojom.UserDefinedValue(enum_value=value1),
            'enum_value2':
            mojom_types_mojom.UserDefinedValue(enum_value=value2),
        }

        mojom = mojom_types_mojom.Value(
            user_value_reference=mojom_types_mojom.UserValueReference(
                identifier='SOMEID', value_key='enum_value1'))

        translator = mojom_translator.FileTranslator(graph, file_name)
        enum_value = translator.ValueFromMojom(mojom)
        enum = translator.UserDefinedFromTypeKey('enum_key')

        self.assertIs(enum, enum_value.enum)
        self.assertIs(value1.decl_data.short_name, enum_value.name)
Exemplo n.º 3
0
  def test_contained_declarations(self):
    graph = mojom_files_mojom.MojomFileGraph()
    file_name = 'root/f.mojom'

    mojom_enum = mojom_types_mojom.MojomEnum(
        values=[],
        decl_data=mojom_types_mojom.DeclarationData(
          short_name='AnEnum',
          source_file_info=mojom_types_mojom.SourceFileInfo(
            file_name=file_name),
          container_type_key='parent_key'))
    graph.resolved_types = {
        'enum_key': mojom_types_mojom.UserDefinedType(enum_type=mojom_enum)}

    mojom_const = mojom_types_mojom.DeclaredConstant(
        decl_data=mojom_types_mojom.DeclarationData(
          short_name='AConst',
          container_type_key='parent_key'),
        type=mojom_types_mojom.Type(
          simple_type=mojom_types_mojom.SimpleType.INT64),
        value=mojom_types_mojom.Value(
          literal_value=mojom_types_mojom.LiteralValue(
            int64_value=30)))
    user_defined_value = mojom_types_mojom.UserDefinedValue()
    user_defined_value.declared_constant = mojom_const
    graph.resolved_values = {'value_key': user_defined_value}

    contained_declarations = mojom_types_mojom.ContainedDeclarations(
        enums=['enum_key'], constants=['value_key'])

    translator = mojom_translator.FileTranslator(graph, file_name)
    struct = module.Struct(name='parent')
    translator._type_cache['parent_key'] = struct
    translator.PopulateContainedDeclarationsFromMojom(
        struct, contained_declarations)

    self.assertEquals(
        mojom_enum.decl_data.short_name, struct.enums[0].name)
    self.assertEquals(struct, struct.enums[0].parent_kind)
    self.assertEquals(
        mojom_const.decl_data.short_name, struct.constants[0].name)
    self.assertEquals(struct, struct.constants[0].parent_kind)
Exemplo n.º 4
0
    def test_basics(self):
        graph = mojom_files_mojom.MojomFileGraph(resolved_types={})

        file_name = 'root/f.mojom'
        imported_file_name = 'other/a.mojom'
        second_level_imported_file_name = 'something/other.mojom'
        mojom_file = mojom_files_mojom.MojomFile(file_name=file_name,
                                                 module_namespace='somens',
                                                 imports=[imported_file_name])
        imported_file = mojom_files_mojom.MojomFile(
            file_name=imported_file_name,
            module_namespace='somens',
            imports=[second_level_imported_file_name])
        second_level_imported_file = mojom_files_mojom.MojomFile(
            file_name=second_level_imported_file_name,
            module_namespace='somens')
        graph.files = {
            file_name: mojom_file,
            imported_file_name: imported_file,
            second_level_imported_file_name: second_level_imported_file
        }

        mojom_interface = mojom_types_mojom.MojomInterface(
            methods={},
            decl_data=mojom_types_mojom.DeclarationData(
                source_file_info=mojom_types_mojom.SourceFileInfo(
                    file_name=file_name)),
            interface_name='AnInterface')
        graph.resolved_types[
            'interface_key'] = mojom_types_mojom.UserDefinedType(
                interface_type=mojom_interface)

        mojom_struct = mojom_types_mojom.MojomStruct(
            fields=[],
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='AStruct',
                full_identifier='foo.AStruct',
                source_file_info=mojom_types_mojom.SourceFileInfo(
                    file_name=file_name)))
        graph.resolved_types['struct_key'] = mojom_types_mojom.UserDefinedType(
            struct_type=mojom_struct)

        mojom_union = mojom_types_mojom.MojomUnion(
            fields=[],
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='AUnion',
                source_file_info=mojom_types_mojom.SourceFileInfo(
                    file_name=file_name)))
        graph.resolved_types['union_key'] = mojom_types_mojom.UserDefinedType(
            union_type=mojom_union)

        mojom_enum = mojom_types_mojom.MojomEnum(
            values=[],
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='AnEnum',
                source_file_info=mojom_types_mojom.SourceFileInfo(
                    file_name=file_name)))
        graph.resolved_types['enum_key'] = mojom_types_mojom.UserDefinedType(
            enum_type=mojom_enum)

        mojom_const = mojom_types_mojom.DeclaredConstant(
            decl_data=mojom_types_mojom.DeclarationData(short_name='AConst'),
            type=mojom_types_mojom.Type(
                simple_type=mojom_types_mojom.SimpleType.INT64),
            value=mojom_types_mojom.Value(
                literal_value=mojom_types_mojom.LiteralValue(int64_value=30)))
        user_defined_value = mojom_types_mojom.UserDefinedValue()
        user_defined_value.declared_constant = mojom_const
        graph.resolved_values = {'value_key': user_defined_value}

        mojom_file.declared_mojom_objects = mojom_files_mojom.KeysByType(
            interfaces=['interface_key'],
            structs=['struct_key'],
            unions=['union_key'],
            top_level_enums=['enum_key'],
            top_level_constants=['value_key'])

        mod = mojom_translator.FileTranslator(graph, file_name).Translate()

        self.assertEquals('f.mojom', mod.name)
        self.assertEquals(mojom_file.file_name, mod.path)
        self.assertEquals(mojom_file.module_namespace, mod.namespace)

        self.assertEquals(1, len(mod.imports))
        self.assertEquals('a.mojom', mod.imports[0]['module_name'])
        self.assertEquals(imported_file.module_namespace,
                          mod.imports[0]['namespace'])
        self.assertEquals(imported_file.file_name,
                          mod.imports[0]['module'].path)

        self.assertEquals(2, len(mod.transitive_imports))
        transitive_imports_paths = [
            imp['module'].path for imp in mod.transitive_imports
        ]
        self.assertIn(imported_file_name, transitive_imports_paths)
        self.assertIn(second_level_imported_file_name,
                      transitive_imports_paths)

        self.assertEquals(mojom_interface.interface_name,
                          mod.interfaces[0].name)
        # Interfaces should be assigned their name as their spec.
        self.assertEquals('AnInterface', mod.interfaces[0].spec)
        self.assertEquals(mojom_struct.decl_data.short_name,
                          mod.structs[0].name)
        # The struct was given a full_identifier so its spec should be that.
        self.assertEquals(mojom_struct.decl_data.full_identifier,
                          mod.structs[0].spec)
        self.assertEquals(mojom_union.decl_data.short_name, mod.unions[0].name)
        # The union was given a short name but not a full_identifier so its spec
        # should be the short name.
        self.assertEquals(mojom_union.decl_data.short_name, mod.unions[0].spec)
        self.assertEquals(mojom_enum.decl_data.short_name, mod.enums[0].name)
        self.assertEquals(mojom_const.decl_data.short_name,
                          mod.constants[0].name)