def test_user_defined_type_type(self): graph = mojom_files_mojom.MojomFileGraph() mojom_struct = mojom_types_mojom.MojomStruct( decl_data=mojom_types_mojom.DeclarationData(short_name='FirstStruct')) type_key = 'some opaque string' mojom_struct.fields = [ # Make sure recursive structs are correctly handled. mojom_types_mojom.StructField( decl_data=mojom_types_mojom.DeclarationData(short_name='field00'), type=mojom_types_mojom.Type( type_reference=mojom_types_mojom.TypeReference(type_key=type_key))) ] add_version_info(mojom_struct, 1) graph.resolved_types = { type_key: mojom_types_mojom.UserDefinedType(struct_type=mojom_struct)} mojom_type = mojom_types_mojom.Type() mojom_type.type_reference = mojom_types_mojom.TypeReference( type_key=type_key) t = mojom_translator.FileTranslator(graph, None) result = t.KindFromMojom(mojom_type) self.assertTrue(module.IsStructKind(result)) self.assertEquals(mojom_struct.decl_data.short_name, result.name) self.assertEquals(result, result.fields[0].kind) self.assertEquals(type_key, result.type_key) # Make sure we create only one module object per type. result2 = t.KindFromMojom(mojom_type) self.assertIs(result, result2) # Nullable type reference mojom_type.type_reference.nullable = True nullable_result = t.KindFromMojom(mojom_type) self.assertTrue(module.IsNullableKind(nullable_result))
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)
def do_interface_test(self, nullable, interface_request): # Build a MojomInterface file_name = 'a.mojom' mojom_interface = mojom_types_mojom.MojomInterface( decl_data=mojom_types_mojom.DeclarationData( short_name='AnInterface', source_file_info=mojom_types_mojom.SourceFileInfo( file_name=file_name))) mojom_interface.methods = {} # Register the MojomInterface in a MojomFileGraph graph = mojom_files_mojom.MojomFileGraph() type_key = 'some_type_key' graph.resolved_types = { type_key: mojom_types_mojom.UserDefinedType(interface_type=mojom_interface) } # Build a reference to the interface. type_ref = mojom_types_mojom.Type( type_reference=mojom_types_mojom.TypeReference( type_key=type_key, nullable=nullable, is_interface_request=interface_request)) # Construct a translator translator = mojom_translator.FileTranslator(graph, file_name) # Translate the MojomInterface referenced by type_ref. interface = translator.UserDefinedFromTypeRef(type_ref) # Check the translation if interface_request: self.assertEquals('AnInterface', interface.kind.name) self.assertEquals('some_type_key', interface.kind.type_key) else: self.assertEquals('AnInterface', interface.name) self.assertEquals('some_type_key', interface.type_key) self.assertEquals(nullable, interface.is_nullable) self.assertEquals(interface_request, isinstance(interface, module.InterfaceRequest))