def testSingleField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' optional int64 integer_field = 1;\n' '}\n', self.result)
def testSingleFieldWithDefaultEmptyString(self): field = descriptor.FieldDescriptor() field.name = 'string_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.STRING field.default_value = '' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional string string_field = 1 [default=''];\n" '}\n', self.result)
def testRepeatedFieldWithDefault(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.REPEATED field.variant = descriptor.FieldDescriptor.Variant.INT64 field.default_value = '[10, 20]' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' repeated int64 integer_field = 1;\n' '}\n', self.result)
def testSingleFieldWithDefaultMessage(self): field = descriptor.FieldDescriptor() field.name = 'message_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.MESSAGE field.type_name = 'MyNestedMessage' field.default_value = 'not valid' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional MyNestedMessage message_field = 1;\n" '}\n', self.result)
def testSingleFieldWithDefaultEnum(self): field = descriptor.FieldDescriptor() field.name = 'enum_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'my_package.MyEnum' field.default_value = '17' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional my_package.MyEnum enum_field = 1 " "[default=17];\n" '}\n', self.result)
def testEmptyPackage(self): self.file_descriptor.package = 'my_package' generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('package my_package;\n', self.result)
def testBlankPackage(self): self.file_descriptor.package = None generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('', self.result)
def format_proto_module(module, output, indent_space=2): file_descriptor = descriptor.describe_file(module) format_proto_file(file_descriptor, output, indent_space=indent_space)