@ -86,7 +86,9 @@ class TextFormatMessageToStringTests(TextFormatBase):
message . repeated_string . append ( ' \000 \001 \a \b \f \n \r \t \v \\ \' " ' )
message . repeated_string . append ( u ' \u00fc \ua71f ' )
self . CompareToGoldenText (
self . RemoveRedundantZeros ( text_format . MessageToString ( message ) ) ,
self . RemoveRedundantZeros (
text_format . MessageToString ( message , as_utf8 = True )
) ,
' repeated_int64: -9223372036854775808 \n '
' repeated_uint64: 18446744073709551615 \n '
' repeated_double: 123.456 \n '
@ -94,7 +96,8 @@ class TextFormatMessageToStringTests(TextFormatBase):
' repeated_double: 1.23e-18 \n '
' repeated_string: '
' " \\ 000 \\ 001 \\ 007 \\ 010 \\ 014 \\ n \\ r \\ t \\ 013 \\ \\ \\ \' \\ " " \n '
' repeated_string: " \\ 303 \\ 274 \\ 352 \\ 234 \\ 237 " \n ' )
' repeated_string: " üꜟ " \n ' ,
)
def testPrintFloatPrecision ( self , message_module ) :
message = message_module . TestAllTypes ( )
@ -204,8 +207,8 @@ class TextFormatMessageToStringTests(TextFormatBase):
message = message_module . TestAllTypes ( )
message . repeated_string . append ( UnicodeSub ( u ' \u00fc \ua71f ' ) )
self . CompareToGoldenText (
text_format . MessageToString ( message ) ,
' repeated_string: " \\ 303 \\ 274 \\ 352 \\ 234 \\ 237 " \n ' )
text_format . MessageToString ( message , as_utf8 = True ) ,
' repeated_string: " üꜟ " \n ' )
def testPrintNestedMessageAsOneLine ( self , message_module ) :
message = message_module . TestAllTypes ( )
@ -282,7 +285,7 @@ class TextFormatMessageToStringTests(TextFormatBase):
message . repeated_string . append ( u ' \u00fc \ua71f ' )
self . CompareToGoldenText (
self . RemoveRedundantZeros ( text_format . MessageToString (
message , as_one_line = True ) ) ,
message , as_one_line = True , as_utf8 = True ) ) ,
' repeated_int64: -9223372036854775808 '
' repeated_uint64: 18446744073709551615 '
' repeated_double: 123.456 '
@ -290,7 +293,7 @@ class TextFormatMessageToStringTests(TextFormatBase):
' repeated_double: 1.23e-18 '
' repeated_string: '
' " \\ 000 \\ 001 \\ 007 \\ 010 \\ 014 \\ n \\ r \\ t \\ 013 \\ \\ \\ \' \\ " " '
' repeated_string: " \\ 303 \\ 274 \\ 352 \\ 234 \\ 237 " ' )
' repeated_string: " üꜟ " ' )
def testRoundTripExoticAsOneLine ( self , message_module ) :
message = message_module . TestAllTypes ( )
@ -616,8 +619,8 @@ class TextFormatMessageToTextBytesTests(TextFormatBase):
def testRawUtf8RoundTrip ( self , message_module ) :
message = message_module . TestAllTypes ( )
message . repeated_string . append ( u ' \u00fc \t \ua71f ' )
utf8_text = text_format . MessageToBytes ( message , as_utf8 = Tru e)
golden_bytes = b ' repeated_string: " \xc3 \xbc \\ t \xea \x9c \x9f " \n '
utf8_text = text_format . MessageToBytes ( message , as_utf8 = Fals e)
golden_bytes = b ' repeated_string: " \\ 303 \\ 274 \\ t \\ 352 \\ 234 \\ 237 " \n '
self . CompareToGoldenText ( utf8_text , golden_bytes )
parsed_message = message_module . TestAllTypes ( )
text_format . Parse ( utf8_text , parsed_message )
@ -626,10 +629,41 @@ class TextFormatMessageToTextBytesTests(TextFormatBase):
( message , parsed_message , message . repeated_string [ 0 ] ,
parsed_message . repeated_string [ 0 ] ) )
def testRawUtf8RoundTripAsUtf8 ( self , message_module ) :
message = message_module . TestAllTypes ( )
message . repeated_string . append ( u ' \u00fc \t \ua71f ' )
utf8_text = text_format . MessageToString ( message , as_utf8 = True )
parsed_message = message_module . TestAllTypes ( )
text_format . Parse ( utf8_text , parsed_message )
self . assertEqual (
message , parsed_message , ' \n %s != %s ( %s != %s ) ' %
( message , parsed_message , message . repeated_string [ 0 ] ,
parsed_message . repeated_string [ 0 ] ) )
# We can only test this case under proto2, because proto3 will reject invalid
# UTF-8 in the parser, so there should be no way of creating a string field
# that contains invalid UTF-8.
#
# We also can't test it in pure-Python, which validates all string fields for
# UTF-8 even when the spec says it shouldn't.
@unittest . skipIf ( api_implementation . Type ( ) == ' python ' ,
' Python can \' t create invalid UTF-8 strings ' )
def testInvalidUtf8RoundTrip ( self , message_module ) :
if message_module is not unittest_pb2 :
return
one_bytes = unittest_pb2 . OneBytes ( )
one_bytes . data = b ' ABC \xff 123 '
one_string = unittest_pb2 . OneString ( )
one_string . ParseFromString ( one_bytes . SerializeToString ( ) )
self . assertIn (
' data: " ABC \\ 377123 " ' ,
text_format . MessageToString ( one_string , as_utf8 = True ) ,
)
def testEscapedUtf8ASCIIRoundTrip ( self , message_module ) :
message = message_module . TestAllTypes ( )
message . repeated_string . append ( u ' \u00fc \t \ua71f ' )
ascii_text = text_format . MessageToBytes ( message ) # as_utf8=False default
ascii_text = text_format . MessageToBytes ( message , as_utf8 = False )
golden_bytes = b ' repeated_string: " \\ 303 \\ 274 \\ t \\ 352 \\ 234 \\ 237 " \n '
self . CompareToGoldenText ( ascii_text , golden_bytes )
parsed_message = message_module . TestAllTypes ( )