| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import unittest | 5 import unittest |
| 6 import module | 6 import module |
| 7 | 7 |
| 8 try: | 8 try: |
| 9 import mojom_translator | 9 import mojom_translator |
| 10 from generated import mojom_files_mojom | 10 from generated import mojom_files_mojom |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 file_name = 'a.mojom' | 236 file_name = 'a.mojom' |
| 237 graph = mojom_files_mojom.MojomFileGraph() | 237 graph = mojom_files_mojom.MojomFileGraph() |
| 238 mojom_file = mojom_files_mojom.MojomFile( | 238 mojom_file = mojom_files_mojom.MojomFile( |
| 239 file_name='a.mojom', | 239 file_name='a.mojom', |
| 240 module_namespace='foo.bar') | 240 module_namespace='foo.bar') |
| 241 graph.files = {mojom_file.file_name: mojom_file} | 241 graph.files = {mojom_file.file_name: mojom_file} |
| 242 | 242 |
| 243 mojom_struct = mojom_types_mojom.MojomStruct( | 243 mojom_struct = mojom_types_mojom.MojomStruct( |
| 244 decl_data=mojom_types_mojom.DeclarationData(short_name='FirstStruct')) | 244 decl_data=mojom_types_mojom.DeclarationData(short_name='FirstStruct')) |
| 245 mojom_struct.fields = [ | 245 mojom_struct.fields = [ |
| 246 mojom_types_mojom.StructField( | 246 mojom_types_mojom.StructField( |
| 247 decl_data=mojom_types_mojom.DeclarationData( | 247 decl_data=mojom_types_mojom.DeclarationData( |
| 248 short_name='field01', | 248 short_name='field03', |
| 249 declared_ordinal=5), | 249 declaration_order=2), |
| 250 type=mojom_types_mojom.Type( | 250 type=mojom_types_mojom.Type( |
| 251 simple_type=mojom_types_mojom.SimpleType.BOOL)), | 251 simple_type=mojom_types_mojom.SimpleType.BOOL)), |
| 252 mojom_types_mojom.StructField( | 252 mojom_types_mojom.StructField( |
| 253 decl_data=mojom_types_mojom.DeclarationData( | 253 decl_data=mojom_types_mojom.DeclarationData( |
| 254 short_name='field02'), | 254 short_name='field01', |
| 255 declared_ordinal=1, |
| 256 declaration_order=0), |
| 257 type=mojom_types_mojom.Type( |
| 258 simple_type=mojom_types_mojom.SimpleType.BOOL)), |
| 259 mojom_types_mojom.StructField( |
| 260 decl_data=mojom_types_mojom.DeclarationData( |
| 261 short_name='field02', |
| 262 declaration_order=1), |
| 255 type=mojom_types_mojom.Type( | 263 type=mojom_types_mojom.Type( |
| 256 simple_type=mojom_types_mojom.SimpleType.DOUBLE), | 264 simple_type=mojom_types_mojom.SimpleType.DOUBLE), |
| 257 default_value=mojom_types_mojom.DefaultFieldValue( | 265 default_value=mojom_types_mojom.DefaultFieldValue( |
| 258 value=mojom_types_mojom.Value( | 266 value=mojom_types_mojom.Value( |
| 259 literal_value=mojom_types_mojom.LiteralValue(double_value=15)))), | 267 literal_value=mojom_types_mojom.LiteralValue(double_value=15)))), |
| 260 ] | 268 ] |
| 269 # mojom_fields_declaration_order lists, in declaration order, the indices |
| 270 # of the fields in mojom_types_mojom.StructField. |
| 271 mojom_fields_declaration_order = [1, 2, 0] |
| 261 mojom_struct.decl_data.source_file_info = mojom_types_mojom.SourceFileInfo( | 272 mojom_struct.decl_data.source_file_info = mojom_types_mojom.SourceFileInfo( |
| 262 file_name=mojom_file.file_name) | 273 file_name=mojom_file.file_name) |
| 263 | 274 |
| 264 struct = module.Struct() | 275 struct = module.Struct() |
| 265 translator = mojom_translator.FileTranslator(graph, file_name) | 276 translator = mojom_translator.FileTranslator(graph, file_name) |
| 266 translator.StructFromMojom( | 277 translator.StructFromMojom( |
| 267 struct, mojom_types_mojom.UserDefinedType(struct_type=mojom_struct)) | 278 struct, mojom_types_mojom.UserDefinedType(struct_type=mojom_struct)) |
| 268 | 279 |
| 269 self.assertEquals('FirstStruct', struct.name) | 280 self.assertEquals('FirstStruct', struct.name) |
| 270 self.assertEquals(translator._module, struct.module) | 281 self.assertEquals(translator._module, struct.module) |
| 271 | 282 |
| 272 self.assertEquals(len(mojom_struct.fields), len(struct.fields)) | 283 self.assertEquals(len(mojom_struct.fields), len(struct.fields)) |
| 273 for gold, f in zip(mojom_struct.fields, struct.fields): | 284 for index, gold_index in enumerate(mojom_fields_declaration_order): |
| 285 gold = mojom_struct.fields[gold_index] |
| 286 f = struct.fields[index] |
| 274 self.assertEquals(f.name, gold.decl_data.short_name) | 287 self.assertEquals(f.name, gold.decl_data.short_name) |
| 288 if gold.decl_data.declared_ordinal >= 0: |
| 289 self.assertEquals(gold.decl_data.declared_ordinal, f.ordinal) |
| 290 else: |
| 291 self.assertEquals(None, f.ordinal) |
| 292 self.assertEquals(gold_index, f.computed_ordinal) |
| 275 | 293 |
| 276 self.assertEquals(module.BOOL, struct.fields[0].kind) | 294 self.assertEquals(module.BOOL, struct.fields[0].kind) |
| 277 self.assertEquals(5, struct.fields[0].ordinal) | 295 self.assertEquals(module.DOUBLE, struct.fields[1].kind) |
| 278 | 296 |
| 279 self.assertEquals(module.DOUBLE, struct.fields[1].kind) | |
| 280 self.assertEquals(None, struct.fields[1].ordinal) | |
| 281 self.assertEquals('15.0', struct.fields[1].default) | 297 self.assertEquals('15.0', struct.fields[1].default) |
| 282 | 298 |
| 283 def test_constant(self): | 299 def test_constant(self): |
| 284 file_name = 'a.mojom' | 300 file_name = 'a.mojom' |
| 285 graph = mojom_files_mojom.MojomFileGraph() | 301 graph = mojom_files_mojom.MojomFileGraph() |
| 286 | 302 |
| 287 mojom_const = mojom_types_mojom.DeclaredConstant() | 303 mojom_const = mojom_types_mojom.DeclaredConstant() |
| 288 mojom_const.decl_data = mojom_types_mojom.DeclarationData( | 304 mojom_const.decl_data = mojom_types_mojom.DeclarationData( |
| 289 short_name='foo', container_type_key='struct_key') | 305 short_name='foo', container_type_key='struct_key') |
| 290 mojom_const.type = mojom_types_mojom.Type( | 306 mojom_const.type = mojom_types_mojom.Type( |
| (...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 950 result2 = t.KindFromMojom(mojom_type) | 966 result2 = t.KindFromMojom(mojom_type) |
| 951 self.assertIs(result, result2) | 967 self.assertIs(result, result2) |
| 952 | 968 |
| 953 # Nullable type reference | 969 # Nullable type reference |
| 954 mojom_type.type_reference.nullable = True | 970 mojom_type.type_reference.nullable = True |
| 955 nullable_result = t.KindFromMojom(mojom_type) | 971 nullable_result = t.KindFromMojom(mojom_type) |
| 956 self.assertTrue(module.IsNullableKind(nullable_result)) | 972 self.assertTrue(module.IsNullableKind(nullable_result)) |
| 957 | 973 |
| 958 if __name__ == '__main__': | 974 if __name__ == '__main__': |
| 959 unittest.main() | 975 unittest.main() |
| OLD | NEW |