def testPrintAllFieldsPointy(self):
   message = unittest_pb2.TestAllTypes()
   test_util.SetAllFields(message)
   self.CompareToGoldenFile(
       self.RemoveRedundantZeros(
           text_format.MessageToString(message, pointy_brackets=True)),
       'text_format_unittest_data_pointy_oneof.txt')
Beispiel #2
0
 def setUp(self):
     self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
     self.all_fields = unittest_pb2.TestAllTypes()
     test_util.SetAllFields(self.all_fields)
     self.all_fields_data = self.all_fields.SerializeToString()
     self.empty_message = unittest_pb2.TestEmptyMessage()
     self.empty_message.ParseFromString(self.all_fields_data)
  def testMergeGolden(self):
    golden_text = '\n'.join(self.ReadGolden('text_format_unittest_data.txt'))
    parsed_message = unittest_pb2.TestAllTypes()
    text_format.Merge(golden_text, parsed_message)

    message = unittest_pb2.TestAllTypes()
    test_util.SetAllFields(message)
    self.assertEquals(message, parsed_message)
  def testMergeLinesGolden(self):
    opened = self.ReadGolden('text_format_unittest_data.txt')
    parsed_message = unittest_pb2.TestAllTypes()
    r = text_format.MergeLines(opened, parsed_message)
    self.assertIs(r, parsed_message)

    message = unittest_pb2.TestAllTypes()
    test_util.SetAllFields(message)
    self.assertEqual(message, parsed_message)
  def testParseAllFields(self):
    message = unittest_pb2.TestAllTypes()
    test_util.SetAllFields(message)
    ascii_text = text_format.MessageToString(message)

    parsed_message = unittest_pb2.TestAllTypes()
    text_format.Parse(ascii_text, parsed_message)
    self.assertEqual(message, parsed_message)
    test_util.ExpectAllFieldsSet(self, message)
Beispiel #6
0
    def testParseGolden(self):
        golden_text = '\n'.join(
            self.ReadGolden('text_format_unittest_data_oneof_implemented.txt'))
        parsed_message = unittest_pb2.TestAllTypes()
        r = text_format.Parse(golden_text, parsed_message)
        self.assertIs(r, parsed_message)

        message = unittest_pb2.TestAllTypes()
        test_util.SetAllFields(message)
        self.assertEqual(message, parsed_message)
 def setUp(self):
   self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
   self.all_fields = unittest_pb2.TestAllTypes()
   test_util.SetAllFields(self.all_fields)
   self.all_fields_data = self.all_fields.SerializeToString()
   self.empty_message = unittest_pb2.TestEmptyMessage()
   self.empty_message.ParseFromString(self.all_fields_data)
   if api_implementation.Type() != 'cpp':
     # _unknown_fields is an implementation detail.
     self.unknown_fields = self.empty_message._unknown_fields
 def testMergeErrors(self):
   src = unittest_pb2.TestAllTypes()
   dst = unittest_pb2.TestAllTypes()
   mask = field_mask_pb2.FieldMask()
   test_util.SetAllFields(src)
   mask.FromJsonString('optionalInt32.field')
   with self.assertRaises(ValueError) as e:
     mask.MergeMessage(src, dst)
   self.assertEqual('Error: Field optional_int32 in message '
                    'protobuf_unittest.TestAllTypes is not a singular '
                    'message field and cannot have sub-fields.',
                    str(e.exception))
 def testPrintAllFields(self):
   message = unittest_pb2.TestAllTypes()
   test_util.SetAllFields(message)
   self.CompareToGoldenFile(
       self.RemoveRedundantZeros(text_format.MessageToString(message)),
       'text_format_unittest_data_oneof_implemented.txt')
    def testMergeMessage(self):
        # Test merge one field.
        src = unittest_pb2.TestAllTypes()
        test_util.SetAllFields(src)
        for field in src.DESCRIPTOR.fields:
            if field.containing_oneof:
                continue
            field_name = field.name
            dst = unittest_pb2.TestAllTypes()
            # Only set one path to mask.
            mask = field_mask_pb2.FieldMask()
            mask.paths.append(field_name)
            mask.MergeMessage(src, dst)
            # The expected result message.
            msg = unittest_pb2.TestAllTypes()
            if field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
                repeated_src = getattr(src, field_name)
                repeated_msg = getattr(msg, field_name)
                if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
                    for item in repeated_src:
                        repeated_msg.add().CopyFrom(item)
                else:
                    repeated_msg.extend(repeated_src)
            elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
                getattr(msg, field_name).CopyFrom(getattr(src, field_name))
            else:
                setattr(msg, field_name, getattr(src, field_name))
            # Only field specified in mask is merged.
            self.assertEqual(msg, dst)

        # Test merge nested fields.
        nested_src = unittest_pb2.NestedTestAllTypes()
        nested_dst = unittest_pb2.NestedTestAllTypes()
        nested_src.child.payload.optional_int32 = 1234
        nested_src.child.child.payload.optional_int32 = 5678
        mask = field_mask_pb2.FieldMask()
        mask.FromJsonString('child.payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(0, nested_dst.child.child.payload.optional_int32)

        mask.FromJsonString('child.child.payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(5678, nested_dst.child.child.payload.optional_int32)

        nested_dst.Clear()
        mask.FromJsonString('child.child.payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(0, nested_dst.child.payload.optional_int32)
        self.assertEqual(5678, nested_dst.child.child.payload.optional_int32)

        nested_dst.Clear()
        mask.FromJsonString('child')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(5678, nested_dst.child.child.payload.optional_int32)

        # Test MergeOptions.
        nested_dst.Clear()
        nested_dst.child.payload.optional_int64 = 4321
        # Message fields will be merged by default.
        mask.FromJsonString('child.payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(4321, nested_dst.child.payload.optional_int64)
        # Change the behavior to replace message fields.
        mask.FromJsonString('child.payload')
        mask.MergeMessage(nested_src, nested_dst, True, False)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(0, nested_dst.child.payload.optional_int64)

        # By default, fields missing in source are not cleared in destination.
        nested_dst.payload.optional_int32 = 1234
        self.assertTrue(nested_dst.HasField('payload'))
        mask.FromJsonString('payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertTrue(nested_dst.HasField('payload'))
        # But they are cleared when replacing message fields.
        nested_dst.Clear()
        nested_dst.payload.optional_int32 = 1234
        mask.FromJsonString('payload')
        mask.MergeMessage(nested_src, nested_dst, True, False)
        self.assertFalse(nested_dst.HasField('payload'))

        nested_src.payload.repeated_int32.append(1234)
        nested_dst.payload.repeated_int32.append(5678)
        # Repeated fields will be appended by default.
        mask.FromJsonString('payload.repeatedInt32')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(2, len(nested_dst.payload.repeated_int32))
        self.assertEqual(5678, nested_dst.payload.repeated_int32[0])
        self.assertEqual(1234, nested_dst.payload.repeated_int32[1])
        # Change the behavior to replace repeated fields.
        mask.FromJsonString('payload.repeatedInt32')
        mask.MergeMessage(nested_src, nested_dst, False, True)
        self.assertEqual(1, len(nested_dst.payload.repeated_int32))
        self.assertEqual(1234, nested_dst.payload.repeated_int32[0])
Beispiel #11
0
 def testPrintAllFields(self):
     message = unittest_pb2.TestAllTypes()
     test_util.SetAllFields(message)
     self.CompareToGoldenFile(text_format.MessageToString(message),
                              'text_format_unittest_data.txt')