Example #1
0
    def test_basics(self):
        g = mojom_files_mojom.MojomFileGraph()

        # File names need to be set so the file can be translated at all.
        g.files = {
            'a.mojom':
            mojom_files_mojom.MojomFile(file_name='a.mojom', imports=[]),
            'b.mojom':
            mojom_files_mojom.MojomFile(file_name='b.mojom', imports=[]),
            'root/c.mojom':
            mojom_files_mojom.MojomFile(file_name='root/c.mojom', imports=[]),
        }

        modules = mojom_translator.TranslateFileGraph(g)
        self.assertEquals(len(modules), len(g.files))
Example #2
0
    def test_imported_struct(self):
        graph = mojom_files_mojom.MojomFileGraph()

        graph.files = {
            'a.mojom':
            mojom_files_mojom.MojomFile(file_name='a.mojom',
                                        specified_file_name='',
                                        module_namespace='namespace',
                                        imports=['root/c.mojom']),
            'root/c.mojom':
            mojom_files_mojom.MojomFile(file_name='root/c.mojom',
                                        specified_file_name='',
                                        module_namespace='otherns',
                                        imports=[]),
        }

        mojom_struct = mojom_types_mojom.MojomStruct()
        mojom_struct.decl_data = mojom_types_mojom.DeclarationData(
            short_name='AStruct',
            source_file_info=mojom_types_mojom.SourceFileInfo(
                file_name='root/c.mojom'))
        mojom_struct.fields = []
        add_version_info(mojom_struct, 0)

        type_key = 'some_type_key'
        graph.resolved_types = {
            type_key:
            mojom_types_mojom.UserDefinedType(struct_type=mojom_struct)
        }
        struct = module.Struct()

        # Translate should create the imports.
        translator = mojom_translator.FileTranslator(graph, 'a.mojom')
        translator.Translate()

        struct = translator.UserDefinedFromTypeRef(
            mojom_types_mojom.Type(
                type_reference=mojom_types_mojom.TypeReference(
                    type_key=type_key)))

        self.assertEquals(
            translator._transitive_imports['root/c.mojom']['module'],
            struct.module)
        self.assertEquals(translator._transitive_imports['root/c.mojom'],
                          struct.imported_from)
Example #3
0
    def test_no_imports(self):
        graph = mojom_files_mojom.MojomFileGraph(resolved_types={})
        file_name = 'root/f.mojom'
        mojom_file = mojom_files_mojom.MojomFile(file_name=file_name,
                                                 module_namespace='somens')
        graph.files = {file_name: mojom_file}

        # Should not throw exceptions despite imports not being set on the file.
        mod = mojom_translator.FileTranslator(graph, file_name).Translate()

        self.assertEquals([], mod.imports)
Example #4
0
    def test_structs(self):
        file_name = 'a.mojom'
        graph = mojom_files_mojom.MojomFileGraph()
        mojom_file = mojom_files_mojom.MojomFile(file_name='a.mojom',
                                                 module_namespace='foo.bar')
        graph.files = {mojom_file.file_name: mojom_file}

        mojom_struct = mojom_types_mojom.MojomStruct(
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='FirstStruct'))
        mojom_struct.fields = [
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field01', declared_ordinal=5),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.BOOL)),
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field02'),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.DOUBLE),
                default_value=mojom_types_mojom.DefaultFieldValue(
                    value=mojom_types_mojom.Value(
                        literal_value=mojom_types_mojom.LiteralValue(
                            double_value=15)))),
        ]
        mojom_struct.decl_data.source_file_info = mojom_types_mojom.SourceFileInfo(
            file_name=mojom_file.file_name)

        struct = module.Struct()
        translator = mojom_translator.FileTranslator(graph, file_name)
        translator.StructFromMojom(
            struct,
            mojom_types_mojom.UserDefinedType(struct_type=mojom_struct))

        self.assertEquals('FirstStruct', struct.name)
        self.assertEquals(translator._module, struct.module)

        self.assertEquals(len(mojom_struct.fields), len(struct.fields))
        for gold, f in zip(mojom_struct.fields, struct.fields):
            self.assertEquals(f.name, gold.decl_data.short_name)

        self.assertEquals(module.BOOL, struct.fields[0].kind)
        self.assertEquals(5, struct.fields[0].ordinal)

        self.assertEquals(module.DOUBLE, struct.fields[1].kind)
        self.assertEquals(None, struct.fields[1].ordinal)
        self.assertEquals('15.0', struct.fields[1].default)
Example #5
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,
            specified_file_name='specified_file_name',
            module_namespace='somens',
            imports=[imported_file_name])
        imported_file = mojom_files_mojom.MojomFile(
            file_name=imported_file_name,
            specified_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,
            specified_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(
                short_name='AnInterface',
                source_file_info=mojom_types_mojom.SourceFileInfo(
                    file_name=file_name)))
        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)))
        add_version_info(mojom_struct, 0)
        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)))
        graph.resolved_constants = {'constant_key': mojom_const}

        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=['constant_key'])

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

        self.assertEquals('f.mojom', mod.name)
        self.assertEquals(mojom_file.specified_file_name, mod.specified_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('AnInterface', 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)

        imported_mod = mojom_translator.FileTranslator(
            graph, imported_file_name).Translate()
        self.assertFalse(imported_mod.specified_name)
Example #6
0
    def test_structs(self):
        file_name = 'a.mojom'
        graph = mojom_files_mojom.MojomFileGraph()
        mojom_file = mojom_files_mojom.MojomFile(file_name='a.mojom',
                                                 module_namespace='foo.bar')
        graph.files = {mojom_file.file_name: mojom_file}

        mojom_struct = mojom_types_mojom.MojomStruct(
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='FirstStruct'))
        mojom_struct.fields = [
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field03', declaration_order=2),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.BOOL),
                offset=21,
                bit=6,
                min_version=11),
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field01',
                    declared_ordinal=1,
                    declaration_order=0),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.BOOL),
                offset=17,
                bit=1,
                min_version=4),
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field02', declaration_order=1),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.DOUBLE),
                offset=0,
                bit=0,
                min_version=0,
                default_value=mojom_types_mojom.DefaultFieldValue(
                    value=mojom_types_mojom.Value(
                        literal_value=mojom_types_mojom.LiteralValue(
                            double_value=15)))),
        ]
        mojom_struct.version_info = [
            mojom_types_mojom.StructVersion(version_number=0,
                                            num_bytes=67,
                                            num_fields=1),
            mojom_types_mojom.StructVersion(version_number=1,
                                            num_bytes=76,
                                            num_fields=3),
        ]
        # mojom_fields_declaration_order lists, in declaration order, the indices
        # of the fields in mojom_types_mojom.StructField.
        mojom_fields_declaration_order = [1, 2, 0]
        mojom_struct.decl_data.source_file_info = mojom_types_mojom.SourceFileInfo(
            file_name=mojom_file.file_name)

        struct = module.Struct()
        translator = mojom_translator.FileTranslator(graph, file_name)
        translator.StructFromMojom(
            struct,
            mojom_types_mojom.UserDefinedType(struct_type=mojom_struct))

        self.assertEquals('FirstStruct', struct.name)
        self.assertEquals(translator._module, struct.module)

        self.assertEquals(len(mojom_struct.fields), len(struct.fields))
        for index, gold_index in enumerate(mojom_fields_declaration_order):
            gold = mojom_struct.fields[gold_index]
            f = struct.fields[index]
            self.assertEquals(f.name, gold.decl_data.short_name)
            if gold.decl_data.declared_ordinal >= 0:
                self.assertEquals(gold.decl_data.declared_ordinal, f.ordinal)
            else:
                self.assertEquals(None, f.ordinal)
            self.assertEquals(gold_index, f.computed_ordinal)
            self.assertEquals(gold.offset, f.computed_offset)
            self.assertEquals(gold.bit, f.computed_bit)
            self.assertEquals(gold.min_version, f.computed_min_version)
            self.assertEquals(struct.fields_in_ordinal_order[index].name,
                              mojom_struct.fields[index].decl_data.short_name)

        self.assertEquals(2, len(struct.versions))
        for i in xrange(0, 2):
            self.assertEquals(mojom_struct.version_info[i].version_number,
                              struct.versions[i].version)
            self.assertEquals(mojom_struct.version_info[i].num_bytes,
                              struct.versions[i].num_bytes)
            self.assertEquals(mojom_struct.version_info[i].num_fields,
                              struct.versions[i].num_fields)

        self.assertEquals(module.BOOL, struct.fields[0].kind)
        self.assertEquals(module.DOUBLE, struct.fields[1].kind)

        self.assertEquals('15.0', struct.fields[1].default)