def testInvalidIntegerValue(self): message = json_format_proto3_pb2.TestMessage() text = '{"int32Value": 0x12345}' self.assertRaises(json_format.ParseError, json_format.Parse, text, message) self.CheckError( '{"int32Value": 1.5}', 'Failed to parse int32Value field: ' 'Couldn\'t parse integer: 1.5.') self.CheckError('{"int32Value": 012345}', (r'Failed to load JSON: Expecting \'?,\'? delimiter: ' r'line 1.')) self.CheckError( '{"int32Value": " 1 "}', 'Failed to parse int32Value field: ' 'Couldn\'t parse integer: " 1 ".') self.CheckError( '{"int32Value": "1 "}', 'Failed to parse int32Value field: ' 'Couldn\'t parse integer: "1 ".') self.CheckError( '{"int32Value": 12345678901234567890}', 'Failed to parse int32Value field: Value out of range: ' '12345678901234567890.') self.CheckError( '{"uint32Value": -1}', 'Failed to parse uint32Value field: ' 'Value out of range: -1.')
def CheckError(self, text, error_message): message = json_format_proto3_pb2.TestMessage() self.assertRaisesRegexp(json_format.ParseError, error_message, json_format.Parse, text, message)
def testMessageToDict(self): message = json_format_proto3_pb2.TestMessage() message.int32_value = 12345 expected = {'int32Value': 12345} self.assertEqual(expected, json_format.MessageToDict(message))
def testParseDict(self): expected = 12345 js_dict = {'int32Value': expected} message = json_format_proto3_pb2.TestMessage() json_format.ParseDict(js_dict, message) self.assertEqual(expected, message.int32_value)
def testIndent(self): message = json_format_proto3_pb2.TestMessage() message.int32_value = 12345 self.assertEqual('{\n"int32Value": 12345\n}', json_format.MessageToJson(message, indent=0))
def testEmptyMessageToJson(self): message = json_format_proto3_pb2.TestMessage() self.assertEqual(json_format.MessageToJson(message), '{}') parsed_message = json_format_proto3_pb2.TestMessage() self.CheckParseBack(message, parsed_message)
def testParseDoubleToFloat(self): message = json_format_proto3_pb2.TestMessage() text = ('{"repeatedFloatValue": [3.4028235e+39, 1.4028235e-39]\n}') json_format.Parse(text, message) self.assertEqual(message.repeated_float_value[0], float('inf')) self.assertAlmostEqual(message.repeated_float_value[1], 1.4028235e-39)