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)
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)