def testmergeexotic(self):
    message = unittest_pb2.testalltypes()
    text = ('repeated_int64: -9223372036854775808\n'
            'repeated_uint64: 18446744073709551615\n'
            'repeated_double: 123.456\n'
            'repeated_double: 1.23e+22\n'
            'repeated_double: 1.23e-18\n'
            'repeated_string: \n'
            '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
            'repeated_string: "foo" \'corge\' "grault"\n'
            'repeated_string: "\\303\\274\\352\\234\\237"\n'
            'repeated_string: "\\xc3\\xbc"\n'
            'repeated_string: "\xc3\xbc"\n')
    text_format.merge(text, message)

    self.assertequal(-9223372036854775808, message.repeated_int64[0])
    self.assertequal(18446744073709551615, message.repeated_uint64[0])
    self.assertequal(123.456, message.repeated_double[0])
    self.assertequal(1.23e22, message.repeated_double[1])
    self.assertequal(1.23e-18, message.repeated_double[2])
    self.assertequal(
        '\000\001\a\b\f\n\r\t\v\\\'"', message.repeated_string[0])
    self.assertequal('foocorgegrault', message.repeated_string[1])
    self.assertequal(u'\u00fc\ua71f', message.repeated_string[2])
    self.assertequal(u'\u00fc', message.repeated_string[3])
  def _assertprotoequal(self, actual_proto, expected_class, expected_ascii):
    expected_proto = expected_class()
    text_format.merge(expected_ascii, expected_proto)

    self.assertequal(
        actual_proto, expected_proto,
        'not equal,\nactual:\n%s\nexpected:\n%s\n'
        % (str(actual_proto), str(expected_proto)))
  def testmergeallextensions(self):
    message = unittest_pb2.testallextensions()
    test_util.setallextensions(message)
    ascii_text = text_format.messagetostring(message)

    parsed_message = unittest_pb2.testallextensions()
    text_format.merge(ascii_text, parsed_message)
    self.assertequal(message, parsed_message)
  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 testprintrawutf8string(self):
   message = unittest_pb2.testalltypes()
   message.repeated_string.append(u'\u00fc\ua71f')
   text = text_format.messagetostring(message, as_utf8 = true)
   self.comparetogoldentext(text, 'repeated_string: "\303\274\352\234\237"\n')
   parsed_message = unittest_pb2.testalltypes()
   text_format.merge(text, parsed_message)
   self.assertequals(message, parsed_message)
  def testmergeallfields(self):
    message = unittest_pb2.testalltypes()
    test_util.setallfields(message)
    ascii_text = text_format.messagetostring(message)

    parsed_message = unittest_pb2.testalltypes()
    text_format.merge(ascii_text, parsed_message)
    self.assertequal(message, parsed_message)
    test_util.expectallfieldsset(self, message)
  def testmergeemptygroup(self):
    message = unittest_pb2.testalltypes()
    text = 'optionalgroup: {}'
    text_format.merge(text, message)
    self.asserttrue(message.hasfield('optionalgroup'))

    message.clear()

    message = unittest_pb2.testalltypes()
    text = 'optionalgroup: <>'
    text_format.merge(text, message)
    self.asserttrue(message.hasfield('optionalgroup'))
  def testmergestringfieldunescape(self):
    message = unittest_pb2.testalltypes()
    text = r'''repeated_string: "\xf\x62"
               repeated_string: "\\xf\\x62"
               repeated_string: "\\\xf\\\x62"
               repeated_string: "\\\\xf\\\\x62"
               repeated_string: "\\\\\xf\\\\\x62"
               repeated_string: "\x5cx20"'''
    text_format.merge(text, message)

    slash = '\\'
    self.assertequal('\x0fb', message.repeated_string[0])
    self.assertequal(slash + 'xf' + slash + 'x62', message.repeated_string[1])
    self.assertequal(slash + '\x0f' + slash + 'b', message.repeated_string[2])
    self.assertequal(slash + slash + 'xf' + slash + slash + 'x62',
                     message.repeated_string[3])
    self.assertequal(slash + slash + '\x0f' + slash + slash + 'b',
                     message.repeated_string[4])
    self.assertequal(slash + 'x20', message.repeated_string[5])
  def testmergemessageset(self):
    message = unittest_pb2.testalltypes()
    text = ('repeated_uint64: 1\n'
            'repeated_uint64: 2\n')
    text_format.merge(text, message)
    self.assertequal(1, message.repeated_uint64[0])
    self.assertequal(2, message.repeated_uint64[1])

    message = unittest_mset_pb2.testmessagesetcontainer()
    text = ('message_set {\n'
            '  [protobuf_unittest.testmessagesetextension1] {\n'
            '    i: 23\n'
            '  }\n'
            '  [protobuf_unittest.testmessagesetextension2] {\n'
            '    str: \"foo\"\n'
            '  }\n'
            '}\n')
    text_format.merge(text, message)
    ext1 = unittest_mset_pb2.testmessagesetextension1.message_set_extension
    ext2 = unittest_mset_pb2.testmessagesetextension2.message_set_extension
    self.assertequals(23, message.message_set.extensions[ext1].i)
    self.assertequals('foo', message.message_set.extensions[ext2].str)
  def testroundtripexoticasoneline(self):
    message = unittest_pb2.testalltypes()
    message.repeated_int64.append(-9223372036854775808)
    message.repeated_uint64.append(18446744073709551615)
    message.repeated_double.append(123.456)
    message.repeated_double.append(1.23e22)
    message.repeated_double.append(1.23e-18)
    message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
    message.repeated_string.append(u'\u00fc\ua71f')

    # test as_utf8 = false.
    wire_text = text_format.messagetostring(
        message, as_one_line=true, as_utf8=false)
    parsed_message = unittest_pb2.testalltypes()
    text_format.merge(wire_text, parsed_message)
    self.assertequals(message, parsed_message)

    # test as_utf8 = true.
    wire_text = text_format.messagetostring(
        message, as_one_line=true, as_utf8=true)
    parsed_message = unittest_pb2.testalltypes()
    text_format.merge(wire_text, parsed_message)
    self.assertequals(message, parsed_message)
 def testmergeemptytext(self):
   message = unittest_pb2.testalltypes()
   text = ''
   text_format.merge(text, message)
   self.assertequals(unittest_pb2.testalltypes(), message)