def test_enum(self): file_name = 'a.mojom' mojom_enum = fidl_types_fidl.FidlEnum() mojom_enum.decl_data = fidl_types_fidl.DeclarationData( short_name='AnEnum', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name)) value1 = fidl_types_fidl.EnumValue( decl_data=fidl_types_fidl.DeclarationData(short_name='val1'), initializer_value=fidl_types_fidl.Value( literal_value=fidl_types_fidl.LiteralValue(uint64_value=20)), int_value=20) value2 = fidl_types_fidl.EnumValue( decl_data=fidl_types_fidl.DeclarationData(short_name='val2'), int_value=70) mojom_enum.values = [value1, value2] graph = fidl_files_fidl.FidlFileGraph() enum = module.Enum() translator = mojom_translator.FileTranslator(graph, file_name) translator.EnumFromMojom( enum, fidl_types_fidl.UserDefinedType(enum_type=mojom_enum)) self.assertEquals(translator._module, enum.module) self.assertEquals(mojom_enum.decl_data.short_name, enum.name) self.assertEquals(len(mojom_enum.values), len(enum.fields)) self.assertEquals(value1.decl_data.short_name, enum.fields[0].name) self.assertEquals(value2.decl_data.short_name, enum.fields[1].name) self.assertEquals('20', enum.fields[0].value) self.assertIsNone(enum.fields[1].value) self.assertEquals(value1.int_value, enum.fields[0].numeric_value) self.assertEquals(value2.int_value, enum.fields[1].numeric_value)
def test_constant(self): file_name = 'a.mojom' graph = fidl_files_fidl.FidlFileGraph() mojom_const = fidl_types_fidl.DeclaredConstant() mojom_const.decl_data = fidl_types_fidl.DeclarationData( short_name='foo', container_type_key='struct_key') mojom_const.type = fidl_types_fidl.Type( simple_type=fidl_types_fidl.SimpleType.INT64) mojom_const.value = fidl_types_fidl.Value() mojom_const.value.literal_value = fidl_types_fidl.LiteralValue( int64_value=20) mojom_struct = fidl_types_fidl.FidlStruct( fields=[], decl_data=fidl_types_fidl.DeclarationData( short_name='AStruct', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name))) add_version_info(mojom_struct, 0) graph.resolved_types = { 'struct_key': fidl_types_fidl.UserDefinedType(struct_type=mojom_struct) } const = module.Constant() translator = mojom_translator.FileTranslator(graph, file_name) translator.ConstantFromMojom(const, mojom_const) self.assertEquals(mojom_const.decl_data.short_name, const.name) self.assertEquals(module.INT64, const.kind) self.assertEquals('20', const.value) self.assertEquals(translator.UserDefinedFromTypeKey('struct_key'), const.parent_kind)
def test_literal_value(self): mojom_int64 = fidl_types_fidl.Value() mojom_int64.literal_value = fidl_types_fidl.LiteralValue( int64_value=20) mojom_bool = fidl_types_fidl.Value() mojom_bool.literal_value = fidl_types_fidl.LiteralValue( bool_value=True) mojom_double = fidl_types_fidl.Value() mojom_double.literal_value = fidl_types_fidl.LiteralValue( double_value=1234.012345678901) graph = fidl_files_fidl.FidlFileGraph() int64_const = mojom_translator.FileTranslator( graph, None).ValueFromMojom(mojom_int64) bool_const = mojom_translator.FileTranslator( graph, None).ValueFromMojom(mojom_bool) double_const = mojom_translator.FileTranslator( graph, None).ValueFromMojom(mojom_double) self.assertEquals('20', int64_const) self.assertEquals('true', bool_const) self.assertEquals('1234.012345678901', double_const)
def test_enum_value(self): file_name = 'a.mojom' mojom_enum = fidl_types_fidl.FidlEnum() mojom_enum.decl_data = fidl_types_fidl.DeclarationData( short_name='AnEnum', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name)) value1 = fidl_types_fidl.EnumValue( decl_data=fidl_types_fidl.DeclarationData( short_name='val1', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name)), initializer_value=fidl_types_fidl.Value( literal_value=fidl_types_fidl.LiteralValue(uint64_value=20)), int_value=20) value2 = fidl_types_fidl.EnumValue( decl_data=fidl_types_fidl.DeclarationData(short_name='val2'), int_value=70) mojom_enum.values = [value1, value2] graph = fidl_files_fidl.FidlFileGraph() graph.resolved_types = { 'enum_key': fidl_types_fidl.UserDefinedType(enum_type=mojom_enum) } mojom = fidl_types_fidl.Value( enum_value_reference=fidl_types_fidl.EnumValueReference( identifier='SOMEID', enum_type_key='enum_key', enum_value_index=0)) 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)
def test_constant_value(self): file_name = 'a.mojom' mojom_const = fidl_types_fidl.DeclaredConstant( decl_data=fidl_types_fidl.DeclarationData( short_name='AConst', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name)), type=fidl_types_fidl.Type( simple_type=fidl_types_fidl.SimpleType.INT64), value=fidl_types_fidl.Value( literal_value=fidl_types_fidl.LiteralValue(int64_value=30))) graph = fidl_files_fidl.FidlFileGraph() graph.resolved_constants = {'constant_key': mojom_const} mojom = fidl_types_fidl.Value( constant_reference=fidl_types_fidl.ConstantReference( identifier='SOMEID', constant_key='constant_key')) translator = mojom_translator.FileTranslator(graph, file_name) const_value = translator.ValueFromMojom(mojom) self.assertIs(translator.ConstantFromKey('constant_key'), const_value.constant) self.assertIs(mojom_const.decl_data.short_name, const_value.name)
def test_parameter(self): # Parameters are encoded as fields in a struct. mojom_param = fidl_types_fidl.StructField( decl_data=fidl_types_fidl.DeclarationData(short_name='param0', declared_ordinal=5), type=fidl_types_fidl.Type( simple_type=fidl_types_fidl.SimpleType.UINT64), default_value=fidl_types_fidl.Value( literal_value=fidl_types_fidl.LiteralValue(uint64_value=20))) graph = fidl_files_fidl.FidlFileGraph() translator = mojom_translator.FileTranslator(graph, '') param = translator.ParamFromMojom(mojom_param) self.assertEquals(mojom_param.decl_data.short_name, param.name) self.assertEquals(module.UINT64, param.kind) self.assertEquals(mojom_param.decl_data.declared_ordinal, param.ordinal)
def test_contained_declarations(self): graph = fidl_files_fidl.FidlFileGraph() file_name = 'root/f.mojom' mojom_enum = fidl_types_fidl.FidlEnum( values=[], decl_data=fidl_types_fidl.DeclarationData( short_name='AnEnum', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name), container_type_key='parent_key')) graph.resolved_types = { 'enum_key': fidl_types_fidl.UserDefinedType(enum_type=mojom_enum) } mojom_const = fidl_types_fidl.DeclaredConstant( decl_data=fidl_types_fidl.DeclarationData( short_name='AConst', container_type_key='parent_key'), type=fidl_types_fidl.Type( simple_type=fidl_types_fidl.SimpleType.INT64), value=fidl_types_fidl.Value( literal_value=fidl_types_fidl.LiteralValue(int64_value=30))) graph.resolved_constants = {'constant_key': mojom_const} contained_declarations = fidl_types_fidl.ContainedDeclarations( enums=['enum_key'], constants=['constant_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)
def test_builtin_const(self): mojom = fidl_types_fidl.Value() graph = fidl_files_fidl.FidlFileGraph() gold = [ (fidl_types_fidl.BuiltinConstantValue.DOUBLE_INFINITY, 'double.INFINITY'), (fidl_types_fidl.BuiltinConstantValue.DOUBLE_NEGATIVE_INFINITY, 'double.NEGATIVE_INFINITY'), (fidl_types_fidl.BuiltinConstantValue.DOUBLE_NAN, 'double.NAN'), (fidl_types_fidl.BuiltinConstantValue.FLOAT_INFINITY, 'float.INFINITY'), (fidl_types_fidl.BuiltinConstantValue.FLOAT_NEGATIVE_INFINITY, 'float.NEGATIVE_INFINITY'), (fidl_types_fidl.BuiltinConstantValue.FLOAT_NAN, 'float.NAN'), ] for mojom_builtin, string in gold: mojom.builtin_value = mojom_builtin const = mojom_translator.FileTranslator(graph, None).ValueFromMojom(mojom) self.assertIsInstance(const, module.BuiltinValue) self.assertEquals(string, const.value)
def test_basics(self): graph = fidl_files_fidl.FidlFileGraph(resolved_types={}) file_name = 'root/f.mojom' imported_file_name = 'other/a.mojom' second_level_imported_file_name = 'something/other.mojom' mojom_file = fidl_files_fidl.FidlFile( file_name=file_name, specified_file_name='specified_file_name', module_namespace='somens', imports=[imported_file_name]) imported_file = fidl_files_fidl.FidlFile( file_name=imported_file_name, specified_file_name='', module_namespace='somens', imports=[second_level_imported_file_name]) second_level_imported_file = fidl_files_fidl.FidlFile( 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 = fidl_types_fidl.FidlInterface( methods={}, decl_data=fidl_types_fidl.DeclarationData( short_name='AnInterface', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name))) graph.resolved_types[ 'interface_key'] = fidl_types_fidl.UserDefinedType( interface_type=mojom_interface) mojom_struct = fidl_types_fidl.FidlStruct( fields=[], decl_data=fidl_types_fidl.DeclarationData( short_name='AStruct', full_identifier='foo.AStruct', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name))) add_version_info(mojom_struct, 0) graph.resolved_types['struct_key'] = fidl_types_fidl.UserDefinedType( struct_type=mojom_struct) mojom_union = fidl_types_fidl.FidlUnion( fields=[], decl_data=fidl_types_fidl.DeclarationData( short_name='AUnion', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name))) graph.resolved_types['union_key'] = fidl_types_fidl.UserDefinedType( union_type=mojom_union) mojom_enum = fidl_types_fidl.FidlEnum( values=[], decl_data=fidl_types_fidl.DeclarationData( short_name='AnEnum', source_file_info=fidl_types_fidl.SourceFileInfo( file_name=file_name))) graph.resolved_types['enum_key'] = fidl_types_fidl.UserDefinedType( enum_type=mojom_enum) mojom_const = fidl_types_fidl.DeclaredConstant( decl_data=fidl_types_fidl.DeclarationData(short_name='AConst'), type=fidl_types_fidl.Type( simple_type=fidl_types_fidl.SimpleType.INT64), value=fidl_types_fidl.Value( literal_value=fidl_types_fidl.LiteralValue(int64_value=30))) graph.resolved_constants = {'constant_key': mojom_const} mojom_file.declared_fidl_objects = fidl_files_fidl.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)
def test_structs(self): file_name = 'a.mojom' graph = fidl_files_fidl.FidlFileGraph() mojom_file = fidl_files_fidl.FidlFile(file_name='a.mojom', module_namespace='foo.bar') graph.files = {mojom_file.file_name: mojom_file} mojom_struct = fidl_types_fidl.FidlStruct( decl_data=fidl_types_fidl.DeclarationData( short_name='FirstStruct')) mojom_struct.fields = [ fidl_types_fidl.StructField( decl_data=fidl_types_fidl.DeclarationData(short_name='field03', declaration_order=2), type=fidl_types_fidl.Type( simple_type=fidl_types_fidl.SimpleType.BOOL), offset=21, bit=6, min_version=11), fidl_types_fidl.StructField( decl_data=fidl_types_fidl.DeclarationData(short_name='field01', declared_ordinal=1, declaration_order=0), type=fidl_types_fidl.Type( simple_type=fidl_types_fidl.SimpleType.BOOL), offset=17, bit=1, min_version=4), fidl_types_fidl.StructField( decl_data=fidl_types_fidl.DeclarationData(short_name='field02', declaration_order=1), type=fidl_types_fidl.Type( simple_type=fidl_types_fidl.SimpleType.DOUBLE), offset=0, bit=0, min_version=0, default_value=fidl_types_fidl.DefaultFieldValue( value=fidl_types_fidl.Value( literal_value=fidl_types_fidl.LiteralValue( double_value=15)))), ] mojom_struct.version_info = [ fidl_types_fidl.StructVersion(version_number=0, num_bytes=67, num_fields=1), fidl_types_fidl.StructVersion(version_number=1, num_bytes=76, num_fields=3), ] # mojom_fields_declaration_order lists, in declaration order, the indices # of the fields in fidl_types_fidl.StructField. mojom_fields_declaration_order = [1, 2, 0] mojom_struct.decl_data.source_file_info = fidl_types_fidl.SourceFileInfo( file_name=mojom_file.file_name) struct = module.Struct() translator = mojom_translator.FileTranslator(graph, file_name) translator.StructFromMojom( struct, fidl_types_fidl.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)