我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用google.protobuf.text_format.ParseLines()。
def testMapOrderSemantics(self): golden_lines = self.ReadGolden('map_test_data.txt') # The C++ implementation emits defaulted-value fields, while the Python # implementation does not. Adjusting for this is awkward, but it is # valuable to test against a common golden file. line_blacklist = (' key: 0\n', ' value: 0\n', ' key: false\n', ' value: false\n') golden_lines = [line for line in golden_lines if line not in line_blacklist] message = map_unittest_pb2.TestMap() text_format.ParseLines(golden_lines, message) candidate = text_format.MessageToString(message) # The Python implementation emits "1.0" for the double value that the C++ # implementation emits as "1". candidate = candidate.replace('1.0', '1', 2) self.assertMultiLineEqual(candidate, ''.join(golden_lines)) # Tests of proto2-only features (MessageSet, extensions, etc.).
def testParseLinesGolden(self): opened = self.ReadGolden('text_format_unittest_data_oneof_implemented.txt') parsed_message = unittest_pb2.TestAllTypes() r = text_format.ParseLines(opened, parsed_message) self.assertIs(r, parsed_message) message = unittest_pb2.TestAllTypes() test_util.SetAllFields(message) self.assertEqual(message, parsed_message)
def testMapOrderEnforcement(self): message = map_unittest_pb2.TestMap() for letter in string.ascii_uppercase[13:26]: message.map_string_string[letter] = 'dummy' for letter in reversed(string.ascii_uppercase[0:13]): message.map_string_string[letter] = 'dummy' golden = ''.join(('map_string_string {\n key: "%c"\n value: "dummy"\n}\n' % (letter,) for letter in string.ascii_uppercase)) self.CompareToGoldenText(text_format.MessageToString(message), golden) # TODO(teboring): In c/137553523, not serializing default value for map entry # message has been fixed. This test needs to be disabled in order to submit # that cl. Add this back when c/137553523 has been submitted. # def testMapOrderSemantics(self): # golden_lines = self.ReadGolden('map_test_data.txt') # message = map_unittest_pb2.TestMap() # text_format.ParseLines(golden_lines, message) # candidate = text_format.MessageToString(message) # # The Python implementation emits "1.0" for the double value that the C++ # # implementation emits as "1". # candidate = candidate.replace('1.0', '1', 2) # candidate = candidate.replace('0.0', '0', 2) # self.assertMultiLineEqual(candidate, ''.join(golden_lines)) # Tests of proto2-only features (MessageSet, extensions, etc.).
def testParseLinesGolden(self): opened = self.ReadGolden('text_format_unittest_data.txt') parsed_message = unittest_pb2.TestAllTypes() r = text_format.ParseLines(opened, parsed_message) self.assertIs(r, parsed_message) message = unittest_pb2.TestAllTypes() test_util.SetAllFields(message) self.assertEquals(message, parsed_message)
def testParseLinesGolden(self): opened = self.ReadGolden('text_format_unittest_data.txt') parsed_message = unittest_pb2.TestAllTypes() r = text_format.ParseLines(opened, parsed_message) self.assertIs(r, parsed_message) message = unittest_pb2.TestAllTypes() test_util.SetAllFields(message) self.assertEqual(message, parsed_message)