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