Chromium Code Reviews

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/generate/mojom_translator_unittest.py

Issue 1958463003: Mojom compiler: Eliminate duplicate representation of enum values in mojom_files.mojom. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Responded to code reveiw comments. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
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 97 matching lines...)
108 graph.resolved_types['enum_key'] = mojom_types_mojom.UserDefinedType( 108 graph.resolved_types['enum_key'] = mojom_types_mojom.UserDefinedType(
109 enum_type=mojom_enum) 109 enum_type=mojom_enum)
110 110
111 mojom_const = mojom_types_mojom.DeclaredConstant( 111 mojom_const = mojom_types_mojom.DeclaredConstant(
112 decl_data=mojom_types_mojom.DeclarationData(short_name='AConst'), 112 decl_data=mojom_types_mojom.DeclarationData(short_name='AConst'),
113 type=mojom_types_mojom.Type( 113 type=mojom_types_mojom.Type(
114 simple_type=mojom_types_mojom.SimpleType.INT64), 114 simple_type=mojom_types_mojom.SimpleType.INT64),
115 value=mojom_types_mojom.Value( 115 value=mojom_types_mojom.Value(
116 literal_value=mojom_types_mojom.LiteralValue( 116 literal_value=mojom_types_mojom.LiteralValue(
117 int64_value=30))) 117 int64_value=30)))
118 user_defined_value = mojom_types_mojom.UserDefinedValue() 118 graph.resolved_constants = {'constant_key': mojom_const}
119 user_defined_value.declared_constant = mojom_const
120 graph.resolved_values = {'value_key': user_defined_value}
121 119
122 mojom_file.declared_mojom_objects = mojom_files_mojom.KeysByType( 120 mojom_file.declared_mojom_objects = mojom_files_mojom.KeysByType(
123 interfaces=['interface_key'], 121 interfaces=['interface_key'],
124 structs=['struct_key'], 122 structs=['struct_key'],
125 unions=['union_key'], 123 unions=['union_key'],
126 top_level_enums=['enum_key'], 124 top_level_enums=['enum_key'],
127 top_level_constants=['value_key'] 125 top_level_constants=['constant_key']
128 ) 126 )
129 127
130 mod = mojom_translator.FileTranslator(graph, file_name).Translate() 128 mod = mojom_translator.FileTranslator(graph, file_name).Translate()
131 129
132 self.assertEquals('f.mojom', mod.name) 130 self.assertEquals('f.mojom', mod.name)
133 self.assertEquals(mojom_file.specified_file_name, mod.specified_name) 131 self.assertEquals(mojom_file.specified_file_name, mod.specified_name)
134 self.assertEquals(mojom_file.file_name, mod.path) 132 self.assertEquals(mojom_file.file_name, mod.path)
135 self.assertEquals(mojom_file.module_namespace, mod.namespace) 133 self.assertEquals(mojom_file.module_namespace, mod.namespace)
136 134
137 self.assertEquals(1, len(mod.imports)) 135 self.assertEquals(1, len(mod.imports))
(...skipping 222 matching lines...)
360 const.parent_kind) 358 const.parent_kind)
361 359
362 def test_enum(self): 360 def test_enum(self):
363 file_name = 'a.mojom' 361 file_name = 'a.mojom'
364 mojom_enum = mojom_types_mojom.MojomEnum() 362 mojom_enum = mojom_types_mojom.MojomEnum()
365 mojom_enum.decl_data = mojom_types_mojom.DeclarationData( 363 mojom_enum.decl_data = mojom_types_mojom.DeclarationData(
366 short_name='AnEnum', 364 short_name='AnEnum',
367 source_file_info=mojom_types_mojom.SourceFileInfo(file_name=file_name)) 365 source_file_info=mojom_types_mojom.SourceFileInfo(file_name=file_name))
368 value1 = mojom_types_mojom.EnumValue( 366 value1 = mojom_types_mojom.EnumValue(
369 decl_data=mojom_types_mojom.DeclarationData(short_name='val1'), 367 decl_data=mojom_types_mojom.DeclarationData(short_name='val1'),
370 enum_type_key='AnEnum',
371 initializer_value=mojom_types_mojom.Value( 368 initializer_value=mojom_types_mojom.Value(
372 literal_value=mojom_types_mojom.LiteralValue(uint64_value=20)), 369 literal_value=mojom_types_mojom.LiteralValue(uint64_value=20)),
373 int_value=20) 370 int_value=20)
374 value2 = mojom_types_mojom.EnumValue( 371 value2 = mojom_types_mojom.EnumValue(
375 decl_data=mojom_types_mojom.DeclarationData(short_name='val2'), 372 decl_data=mojom_types_mojom.DeclarationData(short_name='val2'),
376 enum_type_key='AnEnum',
377 int_value=70) 373 int_value=70)
378 mojom_enum.values = [value1, value2] 374 mojom_enum.values = [value1, value2]
379 375
380 376
381 graph = mojom_files_mojom.MojomFileGraph() 377 graph = mojom_files_mojom.MojomFileGraph()
382 enum = module.Enum() 378 enum = module.Enum()
383 translator = mojom_translator.FileTranslator(graph, file_name) 379 translator = mojom_translator.FileTranslator(graph, file_name)
384 translator.EnumFromMojom( 380 translator.EnumFromMojom(
385 enum, mojom_types_mojom.UserDefinedType(enum_type=mojom_enum)) 381 enum, mojom_types_mojom.UserDefinedType(enum_type=mojom_enum))
386 382
(...skipping 363 matching lines...)
750 746
751 mojom_const = mojom_types_mojom.DeclaredConstant( 747 mojom_const = mojom_types_mojom.DeclaredConstant(
752 decl_data=mojom_types_mojom.DeclarationData( 748 decl_data=mojom_types_mojom.DeclarationData(
753 short_name='AConst', 749 short_name='AConst',
754 container_type_key='parent_key'), 750 container_type_key='parent_key'),
755 type=mojom_types_mojom.Type( 751 type=mojom_types_mojom.Type(
756 simple_type=mojom_types_mojom.SimpleType.INT64), 752 simple_type=mojom_types_mojom.SimpleType.INT64),
757 value=mojom_types_mojom.Value( 753 value=mojom_types_mojom.Value(
758 literal_value=mojom_types_mojom.LiteralValue( 754 literal_value=mojom_types_mojom.LiteralValue(
759 int64_value=30))) 755 int64_value=30)))
760 user_defined_value = mojom_types_mojom.UserDefinedValue() 756 graph.resolved_constants = {'constant_key': mojom_const}
761 user_defined_value.declared_constant = mojom_const
762 graph.resolved_values = {'value_key': user_defined_value}
763 757
764 contained_declarations = mojom_types_mojom.ContainedDeclarations( 758 contained_declarations = mojom_types_mojom.ContainedDeclarations(
765 enums=['enum_key'], constants=['value_key']) 759 enums=['enum_key'], constants=['constant_key'])
766 760
767 translator = mojom_translator.FileTranslator(graph, file_name) 761 translator = mojom_translator.FileTranslator(graph, file_name)
768 struct = module.Struct(name='parent') 762 struct = module.Struct(name='parent')
769 translator._type_cache['parent_key'] = struct 763 translator._type_cache['parent_key'] = struct
770 translator.PopulateContainedDeclarationsFromMojom( 764 translator.PopulateContainedDeclarationsFromMojom(
771 struct, contained_declarations) 765 struct, contained_declarations)
772 766
773 self.assertEquals( 767 self.assertEquals(
774 mojom_enum.decl_data.short_name, struct.enums[0].name) 768 mojom_enum.decl_data.short_name, struct.enums[0].name)
775 self.assertEquals(struct, struct.enums[0].parent_kind) 769 self.assertEquals(struct, struct.enums[0].parent_kind)
(...skipping 55 matching lines...)
831 file_name = 'a.mojom' 825 file_name = 'a.mojom'
832 mojom_enum = mojom_types_mojom.MojomEnum() 826 mojom_enum = mojom_types_mojom.MojomEnum()
833 mojom_enum.decl_data = mojom_types_mojom.DeclarationData( 827 mojom_enum.decl_data = mojom_types_mojom.DeclarationData(
834 short_name='AnEnum', 828 short_name='AnEnum',
835 source_file_info=mojom_types_mojom.SourceFileInfo(file_name=file_name)) 829 source_file_info=mojom_types_mojom.SourceFileInfo(file_name=file_name))
836 value1 = mojom_types_mojom.EnumValue( 830 value1 = mojom_types_mojom.EnumValue(
837 decl_data=mojom_types_mojom.DeclarationData( 831 decl_data=mojom_types_mojom.DeclarationData(
838 short_name='val1', 832 short_name='val1',
839 source_file_info=mojom_types_mojom.SourceFileInfo( 833 source_file_info=mojom_types_mojom.SourceFileInfo(
840 file_name=file_name)), 834 file_name=file_name)),
841 enum_type_key='enum_key',
842 initializer_value=mojom_types_mojom.Value( 835 initializer_value=mojom_types_mojom.Value(
843 literal_value=mojom_types_mojom.LiteralValue(uint64_value=20)), 836 literal_value=mojom_types_mojom.LiteralValue(uint64_value=20)),
844 int_value=20) 837 int_value=20)
845 value2 = mojom_types_mojom.EnumValue( 838 value2 = mojom_types_mojom.EnumValue(
846 decl_data=mojom_types_mojom.DeclarationData(short_name='val2'), 839 decl_data=mojom_types_mojom.DeclarationData(short_name='val2'),
847 enum_type_key='enum_key',
848 int_value=70) 840 int_value=70)
849 mojom_enum.values = [value1, value2] 841 mojom_enum.values = [value1, value2]
850 842
851 graph = mojom_files_mojom.MojomFileGraph() 843 graph = mojom_files_mojom.MojomFileGraph()
852 graph.resolved_types = { 844 graph.resolved_types = {
853 'enum_key': mojom_types_mojom.UserDefinedType(enum_type=mojom_enum)} 845 'enum_key': mojom_types_mojom.UserDefinedType(enum_type=mojom_enum)}
854 graph.resolved_values = {
855 'enum_value1': mojom_types_mojom.UserDefinedValue(enum_value=value1),
856 'enum_value2': mojom_types_mojom.UserDefinedValue(enum_value=value2),
857 }
858 846
859 mojom = mojom_types_mojom.Value( 847 mojom = mojom_types_mojom.Value(
860 user_value_reference=mojom_types_mojom.UserValueReference( 848 enum_value_reference=mojom_types_mojom.EnumValueReference(
861 identifier='SOMEID', 849 identifier='SOMEID',
862 value_key='enum_value1')) 850 enum_type_key='enum_key',
851 enum_value_index=0))
863 852
864 translator = mojom_translator.FileTranslator(graph, file_name) 853 translator = mojom_translator.FileTranslator(graph, file_name)
865 enum_value = translator.ValueFromMojom(mojom) 854 enum_value = translator.ValueFromMojom(mojom)
866 enum = translator.UserDefinedFromTypeKey('enum_key') 855 enum = translator.UserDefinedFromTypeKey('enum_key')
867 856
868 self.assertIs(enum, enum_value.enum) 857 self.assertIs(enum, enum_value.enum)
869 self.assertIs(value1.decl_data.short_name, enum_value.name) 858 self.assertIs(value1.decl_data.short_name, enum_value.name)
870 859
871 def test_constant_value(self): 860 def test_constant_value(self):
872 file_name = 'a.mojom' 861 file_name = 'a.mojom'
873 mojom_const = mojom_types_mojom.DeclaredConstant( 862 mojom_const = mojom_types_mojom.DeclaredConstant(
874 decl_data=mojom_types_mojom.DeclarationData( 863 decl_data=mojom_types_mojom.DeclarationData(
875 short_name='AConst', 864 short_name='AConst',
876 source_file_info=mojom_types_mojom.SourceFileInfo( 865 source_file_info=mojom_types_mojom.SourceFileInfo(
877 file_name=file_name)), 866 file_name=file_name)),
878 type=mojom_types_mojom.Type( 867 type=mojom_types_mojom.Type(
879 simple_type=mojom_types_mojom.SimpleType.INT64), 868 simple_type=mojom_types_mojom.SimpleType.INT64),
880 value=mojom_types_mojom.Value( 869 value=mojom_types_mojom.Value(
881 literal_value=mojom_types_mojom.LiteralValue( 870 literal_value=mojom_types_mojom.LiteralValue(
882 int64_value=30))) 871 int64_value=30)))
883 user_defined_value = mojom_types_mojom.UserDefinedValue()
884 user_defined_value.declared_constant = mojom_const
885 872
886 graph = mojom_files_mojom.MojomFileGraph() 873 graph = mojom_files_mojom.MojomFileGraph()
887 graph.resolved_values = {'value_key': user_defined_value} 874 graph.resolved_constants = {'constant_key': mojom_const}
888 875
889 mojom = mojom_types_mojom.Value( 876 mojom = mojom_types_mojom.Value(
890 user_value_reference=mojom_types_mojom.UserValueReference( 877 constant_reference=mojom_types_mojom.ConstantReference(
891 identifier='SOMEID', 878 identifier='SOMEID',
892 value_key='value_key')) 879 constant_key='constant_key'))
893 880
894 translator = mojom_translator.FileTranslator(graph, file_name) 881 translator = mojom_translator.FileTranslator(graph, file_name)
895 const_value = translator.ValueFromMojom(mojom) 882 const_value = translator.ValueFromMojom(mojom)
896 self.assertIs( 883 self.assertIs(
897 translator.ConstantFromValueKey('value_key'), const_value.constant) 884 translator.ConstantFromKey('constant_key'), const_value.constant)
898 self.assertIs(mojom_const.decl_data.short_name, const_value.name) 885 self.assertIs(mojom_const.decl_data.short_name, const_value.name)
899 886
900 887
901 @unittest.skipUnless(bindings_imported, 'Could not import python bindings.') 888 @unittest.skipUnless(bindings_imported, 'Could not import python bindings.')
902 class TestKindFromMojom(unittest.TestCase): 889 class TestKindFromMojom(unittest.TestCase):
903 890
904 def test_simple_type(self): 891 def test_simple_type(self):
905 simple_types = [ 892 simple_types = [
906 (mojom_types_mojom.SimpleType.BOOL, module.BOOL), 893 (mojom_types_mojom.SimpleType.BOOL, module.BOOL),
907 (mojom_types_mojom.SimpleType.INT8, module.INT8), 894 (mojom_types_mojom.SimpleType.INT8, module.INT8),
(...skipping 193 matching lines...)
1101 """Builds a list containing a single StructVersion with 1088 """Builds a list containing a single StructVersion with
1102 version_number=0, num_bytes=0, and the given value for num_fields. Adds this 1089 version_number=0, num_bytes=0, and the given value for num_fields. Adds this
1103 as the |version_info| attribute of |mojom_struct|. 1090 as the |version_info| attribute of |mojom_struct|.
1104 1091
1105 Args: 1092 Args:
1106 mojom_struct: {any} The Python object to which a |version_info| attribute 1093 mojom_struct: {any} The Python object to which a |version_info| attribute
1107 will be added. 1094 will be added.
1108 num_fields: {int} The value of num_fields to use. 1095 num_fields: {int} The value of num_fields to use.
1109 """ 1096 """
1110 mojom_struct.version_info=build_version_info(num_fields) 1097 mojom_struct.version_info=build_version_info(num_fields)
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/pylib/mojom/generate/mojom_translator.py ('k') | mojom/generated/mojom_files/mojom_files.mojom.go » ('j') | no next file with comments »

Powered by Google App Engine