|
|
|
@ -38,7 +38,10 @@ import re |
|
|
|
|
|
|
|
|
|
import six |
|
|
|
|
|
|
|
|
|
import unittest |
|
|
|
|
try: |
|
|
|
|
import unittest2 as unittest |
|
|
|
|
except ImportError: |
|
|
|
|
import unittest |
|
|
|
|
from google.protobuf.internal import _parameterized |
|
|
|
|
|
|
|
|
|
from google.protobuf import map_unittest_pb2 |
|
|
|
@ -62,7 +65,7 @@ class TextFormatBase(unittest.TestCase): |
|
|
|
|
self.assertMultiLineEqual(text, ''.join(golden_lines)) |
|
|
|
|
|
|
|
|
|
def CompareToGoldenText(self, text, golden_text): |
|
|
|
|
self.assertMultiLineEqual(text, golden_text) |
|
|
|
|
self.assertEqual(text, golden_text) |
|
|
|
|
|
|
|
|
|
def RemoveRedundantZeros(self, text): |
|
|
|
|
# Some platforms print 1e+5 as 1e+005. This is fine, but we need to remove |
|
|
|
@ -218,13 +221,13 @@ class TextFormatTest(TextFormatBase): |
|
|
|
|
text_message = text_format.MessageToString(message, float_format='.15g') |
|
|
|
|
self.CompareToGoldenText( |
|
|
|
|
self.RemoveRedundantZeros(text_message), |
|
|
|
|
'payload {{\n {}\n {}\n {}\n {}\n}}\n'.format(*formatted_fields)) |
|
|
|
|
'payload {{\n {0}\n {1}\n {2}\n {3}\n}}\n'.format(*formatted_fields)) |
|
|
|
|
# as_one_line=True is a separate code branch where float_format is passed. |
|
|
|
|
text_message = text_format.MessageToString(message, as_one_line=True, |
|
|
|
|
float_format='.15g') |
|
|
|
|
self.CompareToGoldenText( |
|
|
|
|
self.RemoveRedundantZeros(text_message), |
|
|
|
|
'payload {{ {} {} {} {} }}'.format(*formatted_fields)) |
|
|
|
|
'payload {{ {0} {1} {2} {3} }}'.format(*formatted_fields)) |
|
|
|
|
|
|
|
|
|
def testMessageToString(self, message_module): |
|
|
|
|
message = message_module.ForeignMessage() |
|
|
|
@ -297,7 +300,7 @@ class TextFormatTest(TextFormatBase): |
|
|
|
|
def testParseSingleWord(self, message_module): |
|
|
|
|
message = message_module.TestAllTypes() |
|
|
|
|
text = 'foo' |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
(r'1:1 : Message type "\w+.TestAllTypes" has no field named ' |
|
|
|
|
r'"foo".'), |
|
|
|
@ -306,7 +309,7 @@ class TextFormatTest(TextFormatBase): |
|
|
|
|
def testParseUnknownField(self, message_module): |
|
|
|
|
message = message_module.TestAllTypes() |
|
|
|
|
text = 'unknown_field: 8\n' |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
(r'1:1 : Message type "\w+.TestAllTypes" has no field named ' |
|
|
|
|
r'"unknown_field".'), |
|
|
|
@ -315,7 +318,7 @@ class TextFormatTest(TextFormatBase): |
|
|
|
|
def testParseBadEnumValue(self, message_module): |
|
|
|
|
message = message_module.TestAllTypes() |
|
|
|
|
text = 'optional_nested_enum: BARR' |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
(r'1:23 : Enum type "\w+.TestAllTypes.NestedEnum" ' |
|
|
|
|
r'has no value named BARR.'), |
|
|
|
@ -323,7 +326,7 @@ class TextFormatTest(TextFormatBase): |
|
|
|
|
|
|
|
|
|
message = message_module.TestAllTypes() |
|
|
|
|
text = 'optional_nested_enum: 100' |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
(r'1:23 : Enum type "\w+.TestAllTypes.NestedEnum" ' |
|
|
|
|
r'has no value with number 100.'), |
|
|
|
@ -332,7 +335,7 @@ class TextFormatTest(TextFormatBase): |
|
|
|
|
def testParseBadIntValue(self, message_module): |
|
|
|
|
message = message_module.TestAllTypes() |
|
|
|
|
text = 'optional_int32: bork' |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
('1:17 : Couldn\'t parse integer: bork'), |
|
|
|
|
text_format.Parse, text, message) |
|
|
|
@ -596,12 +599,12 @@ class Proto2Tests(TextFormatBase): |
|
|
|
|
def testParseBadExtension(self): |
|
|
|
|
message = unittest_pb2.TestAllExtensions() |
|
|
|
|
text = '[unknown_extension]: 8\n' |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
'1:2 : Extension "unknown_extension" not registered.', |
|
|
|
|
text_format.Parse, text, message) |
|
|
|
|
message = unittest_pb2.TestAllTypes() |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
('1:2 : Message type "protobuf_unittest.TestAllTypes" does not have ' |
|
|
|
|
'extensions.'), |
|
|
|
@ -620,7 +623,7 @@ class Proto2Tests(TextFormatBase): |
|
|
|
|
message = unittest_pb2.TestAllExtensions() |
|
|
|
|
text = ('[protobuf_unittest.optional_int32_extension]: 42 ' |
|
|
|
|
'[protobuf_unittest.optional_int32_extension]: 67') |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
('1:96 : Message type "protobuf_unittest.TestAllExtensions" ' |
|
|
|
|
'should not have multiple ' |
|
|
|
@ -631,7 +634,7 @@ class Proto2Tests(TextFormatBase): |
|
|
|
|
message = unittest_pb2.TestAllTypes() |
|
|
|
|
text = ('optional_nested_message { bb: 1 } ' |
|
|
|
|
'optional_nested_message { bb: 2 }') |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
('1:65 : Message type "protobuf_unittest.TestAllTypes.NestedMessage" ' |
|
|
|
|
'should not have multiple "bb" fields.'), |
|
|
|
@ -641,7 +644,7 @@ class Proto2Tests(TextFormatBase): |
|
|
|
|
message = unittest_pb2.TestAllTypes() |
|
|
|
|
text = ('optional_int32: 42 ' |
|
|
|
|
'optional_int32: 67') |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, |
|
|
|
|
('1:36 : Message type "protobuf_unittest.TestAllTypes" should not ' |
|
|
|
|
'have multiple "optional_int32" fields.'), |
|
|
|
@ -650,11 +653,11 @@ class Proto2Tests(TextFormatBase): |
|
|
|
|
def testParseGroupNotClosed(self): |
|
|
|
|
message = unittest_pb2.TestAllTypes() |
|
|
|
|
text = 'RepeatedGroup: <' |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, '1:16 : Expected ">".', |
|
|
|
|
text_format.Parse, text, message) |
|
|
|
|
text = 'RepeatedGroup: {' |
|
|
|
|
self.assertRaisesRegexp( |
|
|
|
|
six.assertRaisesRegex(self, |
|
|
|
|
text_format.ParseError, '1:16 : Expected "}".', |
|
|
|
|
text_format.Parse, text, message) |
|
|
|
|
|
|
|
|
|