コード例 #1
0
  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)
コード例 #2
0
  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)
コード例 #3
0
  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)
コード例 #4
0
  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)
コード例 #5
0
  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)
コード例 #6
0
  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)
コード例 #7
0
  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)
コード例 #8
0
  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)
コード例 #9
0
  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)
コード例 #10
0
  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)
コード例 #11
0
 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)
コード例 #12
0
 def testBlankPackage(self):
   self.file_descriptor.package = None
   generate_proto.format_proto_file(self.file_descriptor, self.output)
   self.assertEquals('', self.result)
コード例 #13
0
 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)
コード例 #14
0
 def testBlankPackage(self):
   self.file_descriptor.package = None
   generate_proto.format_proto_file(self.file_descriptor, self.output)
   self.assertEquals('', self.result)
コード例 #15
0
ファイル: snippet.py プロジェクト: someburner/GistsHub
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)