| OLD | NEW |
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 # | 2 # |
| 3 # Protocol Buffers - Google's data interchange format | 3 # Protocol Buffers - Google's data interchange format |
| 4 # Copyright 2008 Google Inc. All rights reserved. | 4 # Copyright 2008 Google Inc. All rights reserved. |
| 5 # https://developers.google.com/protocol-buffers/ | 5 # https://developers.google.com/protocol-buffers/ |
| 6 # | 6 # |
| 7 # Redistribution and use in source and binary forms, with or without | 7 # Redistribution and use in source and binary forms, with or without |
| 8 # modification, are permitted provided that the following conditions are | 8 # modification, are permitted provided that the following conditions are |
| 9 # met: | 9 # met: |
| 10 # | 10 # |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 name='bb', | 70 name='bb', |
| 71 number=1, | 71 number=1, |
| 72 type=descriptor_pb2.FieldDescriptorProto.TYPE_INT32, | 72 type=descriptor_pb2.FieldDescriptorProto.TYPE_INT32, |
| 73 label=descriptor_pb2.FieldDescriptorProto.LABEL_OPTIONAL) | 73 label=descriptor_pb2.FieldDescriptorProto.LABEL_OPTIONAL) |
| 74 enum_proto = message_proto.enum_type.add( | 74 enum_proto = message_proto.enum_type.add( |
| 75 name='ForeignEnum') | 75 name='ForeignEnum') |
| 76 enum_proto.value.add(name='FOREIGN_FOO', number=4) | 76 enum_proto.value.add(name='FOREIGN_FOO', number=4) |
| 77 enum_proto.value.add(name='FOREIGN_BAR', number=5) | 77 enum_proto.value.add(name='FOREIGN_BAR', number=5) |
| 78 enum_proto.value.add(name='FOREIGN_BAZ', number=6) | 78 enum_proto.value.add(name='FOREIGN_BAZ', number=6) |
| 79 | 79 |
| 80 file_proto.message_type.add(name='ResponseMessage') |
| 81 service_proto = file_proto.service.add( |
| 82 name='Service') |
| 83 method_proto = service_proto.method.add( |
| 84 name='CallMethod', |
| 85 input_type='.protobuf_unittest.NestedMessage', |
| 86 output_type='.protobuf_unittest.ResponseMessage') |
| 87 |
| 88 # Note: Calling DescriptorPool.Add() multiple times with the same file only |
| 89 # works if the input is canonical; in particular, all type names must be |
| 90 # fully qualified. |
| 80 self.pool = self.GetDescriptorPool() | 91 self.pool = self.GetDescriptorPool() |
| 81 self.pool.Add(file_proto) | 92 self.pool.Add(file_proto) |
| 82 self.my_file = self.pool.FindFileByName(file_proto.name) | 93 self.my_file = self.pool.FindFileByName(file_proto.name) |
| 83 self.my_message = self.my_file.message_types_by_name[message_proto.name] | 94 self.my_message = self.my_file.message_types_by_name[message_proto.name] |
| 84 self.my_enum = self.my_message.enum_types_by_name[enum_proto.name] | 95 self.my_enum = self.my_message.enum_types_by_name[enum_proto.name] |
| 85 | 96 self.my_service = self.my_file.services_by_name[service_proto.name] |
| 86 self.my_method = descriptor.MethodDescriptor( | 97 self.my_method = self.my_service.methods_by_name[method_proto.name] |
| 87 name='Bar', | |
| 88 full_name='protobuf_unittest.TestService.Bar', | |
| 89 index=0, | |
| 90 containing_service=None, | |
| 91 input_type=None, | |
| 92 output_type=None) | |
| 93 self.my_service = descriptor.ServiceDescriptor( | |
| 94 name='TestServiceWithOptions', | |
| 95 full_name='protobuf_unittest.TestServiceWithOptions', | |
| 96 file=self.my_file, | |
| 97 index=0, | |
| 98 methods=[ | |
| 99 self.my_method | |
| 100 ]) | |
| 101 | 98 |
| 102 def GetDescriptorPool(self): | 99 def GetDescriptorPool(self): |
| 103 return symbol_database.Default().pool | 100 return symbol_database.Default().pool |
| 104 | 101 |
| 105 def testEnumValueName(self): | 102 def testEnumValueName(self): |
| 106 self.assertEqual(self.my_message.EnumValueName('ForeignEnum', 4), | 103 self.assertEqual(self.my_message.EnumValueName('ForeignEnum', 4), |
| 107 'FOREIGN_FOO') | 104 'FOREIGN_FOO') |
| 108 | 105 |
| 109 self.assertEqual( | 106 self.assertEqual( |
| 110 self.my_message.enum_types_by_name[ | 107 self.my_message.enum_types_by_name[ |
| (...skipping 21 matching lines...) Expand all Loading... |
| 132 descriptor_pb2.FieldOptions()) | 129 descriptor_pb2.FieldOptions()) |
| 133 self.assertEqual(self.my_method.GetOptions(), | 130 self.assertEqual(self.my_method.GetOptions(), |
| 134 descriptor_pb2.MethodOptions()) | 131 descriptor_pb2.MethodOptions()) |
| 135 self.assertEqual(self.my_service.GetOptions(), | 132 self.assertEqual(self.my_service.GetOptions(), |
| 136 descriptor_pb2.ServiceOptions()) | 133 descriptor_pb2.ServiceOptions()) |
| 137 | 134 |
| 138 def testSimpleCustomOptions(self): | 135 def testSimpleCustomOptions(self): |
| 139 file_descriptor = unittest_custom_options_pb2.DESCRIPTOR | 136 file_descriptor = unittest_custom_options_pb2.DESCRIPTOR |
| 140 message_descriptor =\ | 137 message_descriptor =\ |
| 141 unittest_custom_options_pb2.TestMessageWithCustomOptions.DESCRIPTOR | 138 unittest_custom_options_pb2.TestMessageWithCustomOptions.DESCRIPTOR |
| 142 field_descriptor = message_descriptor.fields_by_name["field1"] | 139 field_descriptor = message_descriptor.fields_by_name['field1'] |
| 143 enum_descriptor = message_descriptor.enum_types_by_name["AnEnum"] | 140 oneof_descriptor = message_descriptor.oneofs_by_name['AnOneof'] |
| 141 enum_descriptor = message_descriptor.enum_types_by_name['AnEnum'] |
| 144 enum_value_descriptor =\ | 142 enum_value_descriptor =\ |
| 145 message_descriptor.enum_values_by_name["ANENUM_VAL2"] | 143 message_descriptor.enum_values_by_name['ANENUM_VAL2'] |
| 146 service_descriptor =\ | 144 service_descriptor =\ |
| 147 unittest_custom_options_pb2.TestServiceWithCustomOptions.DESCRIPTOR | 145 unittest_custom_options_pb2.TestServiceWithCustomOptions.DESCRIPTOR |
| 148 method_descriptor = service_descriptor.FindMethodByName("Foo") | 146 method_descriptor = service_descriptor.FindMethodByName('Foo') |
| 149 | 147 |
| 150 file_options = file_descriptor.GetOptions() | 148 file_options = file_descriptor.GetOptions() |
| 151 file_opt1 = unittest_custom_options_pb2.file_opt1 | 149 file_opt1 = unittest_custom_options_pb2.file_opt1 |
| 152 self.assertEqual(9876543210, file_options.Extensions[file_opt1]) | 150 self.assertEqual(9876543210, file_options.Extensions[file_opt1]) |
| 153 message_options = message_descriptor.GetOptions() | 151 message_options = message_descriptor.GetOptions() |
| 154 message_opt1 = unittest_custom_options_pb2.message_opt1 | 152 message_opt1 = unittest_custom_options_pb2.message_opt1 |
| 155 self.assertEqual(-56, message_options.Extensions[message_opt1]) | 153 self.assertEqual(-56, message_options.Extensions[message_opt1]) |
| 156 field_options = field_descriptor.GetOptions() | 154 field_options = field_descriptor.GetOptions() |
| 157 field_opt1 = unittest_custom_options_pb2.field_opt1 | 155 field_opt1 = unittest_custom_options_pb2.field_opt1 |
| 158 self.assertEqual(8765432109, field_options.Extensions[field_opt1]) | 156 self.assertEqual(8765432109, field_options.Extensions[field_opt1]) |
| 159 field_opt2 = unittest_custom_options_pb2.field_opt2 | 157 field_opt2 = unittest_custom_options_pb2.field_opt2 |
| 160 self.assertEqual(42, field_options.Extensions[field_opt2]) | 158 self.assertEqual(42, field_options.Extensions[field_opt2]) |
| 159 oneof_options = oneof_descriptor.GetOptions() |
| 160 oneof_opt1 = unittest_custom_options_pb2.oneof_opt1 |
| 161 self.assertEqual(-99, oneof_options.Extensions[oneof_opt1]) |
| 161 enum_options = enum_descriptor.GetOptions() | 162 enum_options = enum_descriptor.GetOptions() |
| 162 enum_opt1 = unittest_custom_options_pb2.enum_opt1 | 163 enum_opt1 = unittest_custom_options_pb2.enum_opt1 |
| 163 self.assertEqual(-789, enum_options.Extensions[enum_opt1]) | 164 self.assertEqual(-789, enum_options.Extensions[enum_opt1]) |
| 164 enum_value_options = enum_value_descriptor.GetOptions() | 165 enum_value_options = enum_value_descriptor.GetOptions() |
| 165 enum_value_opt1 = unittest_custom_options_pb2.enum_value_opt1 | 166 enum_value_opt1 = unittest_custom_options_pb2.enum_value_opt1 |
| 166 self.assertEqual(123, enum_value_options.Extensions[enum_value_opt1]) | 167 self.assertEqual(123, enum_value_options.Extensions[enum_value_opt1]) |
| 167 | 168 |
| 168 service_options = service_descriptor.GetOptions() | 169 service_options = service_descriptor.GetOptions() |
| 169 service_opt1 = unittest_custom_options_pb2.service_opt1 | 170 service_opt1 = unittest_custom_options_pb2.service_opt1 |
| 170 self.assertEqual(-9876543210, service_options.Extensions[service_opt1]) | 171 self.assertEqual(-9876543210, service_options.Extensions[service_opt1]) |
| (...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 813 field.number = index + 1 | 814 field.number = index + 1 |
| 814 field.name = names[index] | 815 field.name = names[index] |
| 815 result = descriptor.MakeDescriptor(descriptor_proto) | 816 result = descriptor.MakeDescriptor(descriptor_proto) |
| 816 for index in range(len(camelcase_names)): | 817 for index in range(len(camelcase_names)): |
| 817 self.assertEqual(result.fields[index].camelcase_name, | 818 self.assertEqual(result.fields[index].camelcase_name, |
| 818 camelcase_names[index]) | 819 camelcase_names[index]) |
| 819 | 820 |
| 820 | 821 |
| 821 if __name__ == '__main__': | 822 if __name__ == '__main__': |
| 822 unittest.main() | 823 unittest.main() |
| OLD | NEW |