o
    ˷e                    @   s0  d Z ddlZddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddlm
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlm
Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd dejZG dd dejZeeeG dd deZeeeG dd deZeeeG dd deZ eeeG dd deZ!G dd  d eZ"G d!d" d"eZ#G d#d$ d$ejZ$G d%d& d&ejZ%eeeG d'd( d(eZ&G d)d* d*eZ'G d+d, d,ejZ(e)d-kre*  dS dS ).z%Test for google.protobuf.text_format.    N)any_pb2)
struct_pb2)any_test_pb2)map_unittest_pb2)unittest_custom_options_pb2)unittest_mset_pb2)unittest_pb2)unittest_proto3_arena_pb2)descriptor_pb2)api_implementation)message_set_extensions_pb2)test_proto3_optional_pb2)	test_utildescriptor_pool)text_format)_parameterizedc                   @   s   e Zd Zdd ZdS )SimpleTextFormatTestsc                 C   s    t jD ]
}| dt| qd S )N   )r   _QUOTESassertEquallen)selfquote r   `/var/www/ideatree/venv/lib/python3.10/site-packages/google/protobuf/internal/text_format_test.pytestQuoteMarksAreSingleCharsE   s   
z2SimpleTextFormatTests.testQuoteMarksAreSingleCharsN)__name__
__module____qualname__r   r   r   r   r   r   ?   s    r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TextFormatBasec                 C   sL   t |}ttu r| ndd |D W  d    S 1 sw   Y  d S )Nc                 S   s   g | ]}| d qS )utf-8)decode).0golden_liner   r   r   
<listcomp>P   s    z-TextFormatBase.ReadGolden.<locals>.<listcomp>)r   
GoldenFilestrbytes	readlines)r   golden_filenamefr   r   r   
ReadGoldenM   s   $zTextFormatBase.ReadGoldenc                 C   s    |  |}| |d| d S N )r,   assertMultiLineEqualjoin)r   textr*   golden_linesr   r   r   CompareToGoldenFileR   s   
z"TextFormatBase.CompareToGoldenFilec                 C   s   |  || d S N)r   )r   r1   golden_textr   r   r   CompareToGoldenTextV   s   z"TextFormatBase.CompareToGoldenTextc                 C   s>   | dd dd dd dd}tdtjd|}|S )Nze+0ze+ze-0ze-z\.0$r.   )replacerecompile	MULTILINEsub)r   r1   r   r   r   RemoveRedundantZerosY   s
   z#TextFormatBase.RemoveRedundantZerosN)r   r   r   r,   r3   r6   r<   r   r   r   r   r    K   s
    r    c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zee d%kd&d'd( Zd)S )*TextFormatMessageToStringTestsc                 C   sx   |  }|jd |jd |jd |jd |jd |jd |jd | | t	|d d S )	N             w/^@tQFքD王6< 
	\'"   üꜟzrepeated_int64: -9223372036854775808
repeated_uint64: 18446744073709551615
repeated_double: 123.456
repeated_double: 1.23e+22
repeated_double: 1.23e-18
repeated_string: "\000\001\007\010\014\n\r\t\013\\\'\""
repeated_string: "\303\274\352\234\237"

TestAllTypesrepeated_int64appendrepeated_uint64repeated_doublerepeated_stringr6   r<   r   MessageToStringr   message_modulemessager   r   r   testPrintExoticg   s   z.TextFormatMessageToStringTests.testPrintExoticc                 C   sT  |  }|jd |jd |jd |jd |jd |jd |jd |jd |jd	 |jd
 |jd |jd |jd |jtd |jtd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jd  |jd! |jd" |jd# |jd$ |jd% |jd& | | t|d' d S )(N        g?g      ?333333?gGz?gX9v?gn?82?g   ZBg   Bg   +Bg   Bg   @BNaNinfgS?g][*?gރB?gD?g D?gD?g1D?g8D?g/9D?gcTg,%@v~TgւI0TgTgIsϓTghדTg&ؓTg?ؓTgCؓTg?ؓTgNDؓTgDؓTgDؓTgBDؓTa  repeated_float: 0
repeated_float: 0.8
repeated_float: 1
repeated_float: 1.2
repeated_float: 1.23
repeated_float: 1.234
repeated_float: 1.2345
repeated_float: 1.23456
repeated_float: 12000000000
repeated_float: 12300000000
repeated_float: 12340000000
repeated_float: 12345000000
repeated_float: 12345600000
repeated_float: nan
repeated_float: inf
repeated_double: 0
repeated_double: 0.8
repeated_double: 1
repeated_double: 1.2
repeated_double: 1.23
repeated_double: 1.234
repeated_double: 1.2345
repeated_double: 1.23456
repeated_double: 1.234567
repeated_double: 1.2345678
repeated_double: 1.23456789
repeated_double: 1.234567898
repeated_double: 1.2345678987
repeated_double: 1.23456789876
repeated_double: 1.234567898765
repeated_double: 1.2345678987654
repeated_double: 1.23456789876543
repeated_double: 1.2e+100
repeated_double: 1.23e+100
repeated_double: 1.234e+100
repeated_double: 1.2345e+100
repeated_double: 1.23456e+100
repeated_double: 1.234567e+100
repeated_double: 1.2345678e+100
repeated_double: 1.23456789e+100
repeated_double: 1.234567898e+100
repeated_double: 1.2345678987e+100
repeated_double: 1.23456789876e+100
repeated_double: 1.234567898765e+100
repeated_double: 1.2345678987654e+100
repeated_double: 1.23456789876543e+100
)	rF   repeated_floatrH   floatrJ   r6   r<   r   rL   rM   r   r   r   testPrintFloatPrecision{   sf   z6TextFormatMessageToStringTests.testPrintFloatPrecisionc                 C   s>   G dd dt }| }|j|d | t|d d S )Nc                   @   s   e Zd ZdS )zQTextFormatMessageToStringTests.testPrintExoticUnicodeSubclass.<locals>.UnicodeSubN)r   r   r   r   r   r   r   
UnicodeSub   s    rY   rD   z(repeated_string: "\303\274\352\234\237"
)r'   rF   rK   rH   r6   r   rL   )r   rN   rY   rO   r   r   r   testPrintExoticUnicodeSubclass   s   z=TextFormatMessageToStringTests.testPrintExoticUnicodeSubclassc                 C   s2   |  }|j }d|_| tj|ddd d S )N*   Tas_one_line"repeated_nested_message { bb: 42 }rF   repeated_nested_messageaddbbr6   r   rL   )r   rN   rO   msgr   r   r   testPrintNestedMessageAsOneLine   s   
z>TextFormatMessageToStringTests.testPrintNestedMessageAsOneLinec                 C   s^   |  }|jd |jd |jd |jd |jd | tj|ddd d S )Nr      GoogleZurichTr\   zirepeated_int32: 1 repeated_int32: 1 repeated_int32: 3 repeated_string: "Google" repeated_string: "Zurich")rF   repeated_int32rH   rK   r6   r   rL   rM   r   r   r    testPrintRepeatedFieldsAsOneLine   s   z?TextFormatMessageToStringTests.testPrintRepeatedFieldsAsOneLinec           	      C   s   |  }|jd |jd |jd |jtj |jtj |jtj d|j	_
dD ]
}|j }||_
q1d}|rT|dd}td	d|}td
d|}tj|d|d}| || |  }t|| | || d S )Nr   rf   zHello,Worldre   )       zoptional_nested_message {
  bb: 3
}
repeated_int32: [1]
repeated_string: "Google"
repeated_string: "Hello,World"
repeated_nested_message {
  bb: 21
}
repeated_nested_message {
  bb: 32
}
repeated_foreign_enum: [FOREIGN_FOO, FOREIGN_BAR, FOREIGN_BAZ]

 z\s+z\s$r.   T)use_short_repeated_primitivesr]   )rF   rh   rH   rK   repeated_foreign_enumr   FOREIGN_FOOFOREIGN_BARFOREIGN_BAZoptional_nested_messagerb   r`   ra   r7   r8   r;   r   rL   r6   Parser   )	r   rN   r]   rO   irc   expected_asciiactual_asciiparsed_messager   r   r   $VerifyPrintShortFormatRepeatedFields   s2   
zCTextFormatMessageToStringTests.VerifyPrintShortFormatRepeatedFieldsc                 C   s   |  |d |  |d d S )NFT)ry   )r   rN   r   r   r   "testPrintShortFormatRepeatedFields  s   zATextFormatMessageToStringTests.testPrintShortFormatRepeatedFieldsc                 C   s(   |  }d|_| tj|ddd d S )Nz
a
new
lineTr\   zoptional_string: "a\nnew\nline")rF   optional_stringr6   r   rL   rM   r   r   r   'testPrintNestedNewLineInStringAsOneLine#  s   zFTextFormatMessageToStringTests.testPrintNestedNewLineInStringAsOneLinec                 C   s|   |  }|jd |jd |jd |jd |jd |jd |jd | | tj	|dd	d
 d S )Nr>   r?   r@   rA   rB   rC   rD   Tr\   zrepeated_int64: -9223372036854775808 repeated_uint64: 18446744073709551615 repeated_double: 123.456 repeated_double: 1.23e+22 repeated_double: 1.23e-18 repeated_string: "\000\001\007\010\014\n\r\t\013\\\'\"" repeated_string: "\303\274\352\234\237"rE   rM   r   r   r   testPrintExoticAsOneLine*  s   z7TextFormatMessageToStringTests.testPrintExoticAsOneLinec                 C   s   |  }|jd |jd |jd |jd |jd |jd |jd tj|dd	d
}|  }t||}| 	|| | 
|| tj|ddd
}|  }t||}| 	|| | 
||d||f  d S )Nr>   r?   r@   rA   rB   rC   rD   TF)r]   as_utf8z	
%s != %s)rF   rG   rH   rI   rJ   rK   r   rL   rt   assertIsr   )r   rN   rO   	wire_textrx   rr   r   r   testRoundTripExoticAsOneLine?  s4   
z;TextFormatMessageToStringTests.testRoundTripExoticAsOneLinec              
   C   st   |  }|jd tj|dd}d}|}| || |  }t|| | ||d|||jd |jd f  d S )N   ü	ꜟTr~   u   repeated_string: "ü\tꜟ"

%s != %s  (%s != %s)r   )rF   rK   rH   r   rL   r6   rt   r   )r   rN   rO   r1   golden_unicoder5   rx   r   r   r   testPrintRawUtf8String\  s   z5TextFormatMessageToStringTests.testPrintRawUtf8Stringc                 C   s  |  }d|j_d|j_|jjd |jjd g d}tj|dd}| 	| 
|dj|  tj|d	dd
}| 	| 
|dj|  d|j_g d}tj|ddd}| 	| 
|dj|  g d}tj|dd}| 	| 
|dj|  d|j_d|j_g d}t|}| 	| 
|dj|  |  d|j_| t|d d|j_| t|d d|j_| t|d d S )Ng      ?g/_e̾ig⹭?)zoptional_float: 1.25%optional_double: -3.45678901234568e-6repeated_float: -5642repeated_double: 7.89e-5z.15g)float_formatz&payload {{
  {0}
  {1}
  {2}
  {3}
}}
T)r]   r   zpayload {{ {0} {1} {2} {3} }}rR   )optional_float: 1.2r   r   r   z.7g)r   double_format)r   zoptional_double: -3.456789e-6r   r   gTB?)zoptional_float: 1.2345679zoptional_double: 1.2345678912r   r   g?z"payload {
  optional_float: 1.1
}
gɞDu8z-payload {
  optional_float: 1.00000075e-36
}
gVDz+payload {
  optional_float: 1.234568e+24
}
)NestedTestAllTypespayloadoptional_floatoptional_doublerV   rH   rJ   r   rL   r6   r<   formatClearr   )r   rN   rO   formatted_fieldstext_messager   r   r   testPrintFloatFormatk  s~   
z3TextFormatMessageToStringTests.testPrintFloatFormatc                 C   s"   |  }d|_| dt| d S )N{   zc: 123
)ForeignMessagecr   r'   rM   r   r   r   testMessageToString  s   z2TextFormatMessageToStringTests.testMessageToStringc                 C   sJ   d}| d}| }||_||_tj|dd}td}| || d S )N!   Á short desçription and a 🍌.r!   Tr   u   optional_string: "Á short desçription and a 🍌."
optional_bytes: "\303\201 short des\303\247ription and a \360\237\215\214."
)	encoderF   r{   optional_bytesr   rL   textwrapdedentr6   r   rN   r   golden_bytesrO   r1   golden_messager   r   r   testMessageToStringUnicode  s   
z9TextFormatMessageToStringTests.testMessageToStringUnicodec                 C   sD   d}| d}| }||_||_tj|dd}d}| || d S )Nr   r!   Fr   zoptional_string: "\303\201 short des\303\247ription and a \360\237\215\214."
optional_bytes: "\303\201 short des\303\247ription and a \360\237\215\214."
)r   rF   r{   r   r   rL   r6   r   r   r   r   testMessageToStringASCII  s   
z7TextFormatMessageToStringTests.testMessageToStringASCIIc                 C      |  }|jjd }|j}td}t||| | d|  |	  td}t
|}||| | d|  |	  d S )Nr   Fzoptional_float: 0.0
)rF   
DESCRIPTORfields_by_namer   r   
TextWriter
PrintFieldr   getvalueclose_Printerr   rN   rO   fieldvalueoutprinterr   r   r   testPrintField     


z-TextFormatMessageToStringTests.testPrintFieldc                 C   r   )Nr   Fz0.0)rF   r   r   r   r   r   PrintFieldValuer   r   r   r   r   r   r   r   testPrintFieldValue  r   z2TextFormatMessageToStringTests.testPrintFieldValuec                 C   sT   t jj}t }|| d}| |t| t }t	|| | || d S )Na  name: "TestMessageWithCustomOptions"
field {
  name: "field1"
  number: 1
  label: LABEL_OPTIONAL
  type: TYPE_STRING
  options {
    ctype: CORD
    [protobuf_unittest.field_opt1]: 8765432109
  }
}
field {
  name: "oneof_field"
  number: 2
  label: LABEL_OPTIONAL
  type: TYPE_INT32
  oneof_index: 0
}
field {
  name: "map_field"
  number: 3
  label: LABEL_REPEATED
  type: TYPE_MESSAGE
  type_name: ".protobuf_unittest.TestMessageWithCustomOptions.MapFieldEntry"
  options {
    [protobuf_unittest.field_opt1]: 12345
  }
}
nested_type {
  name: "MapFieldEntry"
  field {
    name: "key"
    number: 1
    label: LABEL_OPTIONAL
    type: TYPE_STRING
  }
  field {
    name: "value"
    number: 2
    label: LABEL_OPTIONAL
    type: TYPE_STRING
  }
  options {
    map_entry: true
  }
}
enum_type {
  name: "AnEnum"
  value {
    name: "ANENUM_VAL1"
    number: 1
  }
  value {
    name: "ANENUM_VAL2"
    number: 2
    options {
      [protobuf_unittest.enum_value_opt1]: 123
    }
  }
  options {
    [protobuf_unittest.enum_opt1]: -789
  }
}
options {
  message_set_wire_format: false
  [protobuf_unittest.message_opt1]: -56
}
oneof_decl {
  name: "AnOneof"
  options {
    [protobuf_unittest.oneof_opt1]: -99
  }
}
)
r   TestMessageWithCustomOptionsr   r
   DescriptorProtoCopyToProtor   r   rL   rt   )r   rN   message_descriptormessage_protoexpected_textparsed_protor   r   r   testCustomOptions  s   
Kz0TextFormatMessageToStringTests.testCustomOptionsupbzeupb API doesn't support old UnknownField API. The TextFormat library needs to convert to the new API.c                 C   s   |  }d|_d|_d|_d|_d|j_| }|  }d|_||_| }| }|	| | 
dtj|ddd	 | 
d
tj|ddd d S )Ne        Y@hello   103i   zu  1: 101
  15 {
    1: 101
    12: 4636878028842991616
    14: "hello"
    15: "103"
    18 {
      1: 105
    }
  }
   Tindentprint_unknown_fieldszP1: 101 15 { 1: 101 12: 4636878028842991616 14: "hello" 15: "103" 18 { 1: 105 } }r   r]   )rF   optional_int32r   r{   r   rs   rb   SerializeToStringTestEmptyMessageParseFromStringr   r   rL   )r   rN   	inner_msg
inner_dataouter_messageall_dataempty_messager   r   r   ,testPrintUnknownFieldsEmbeddedMessageInBytesK  s2   

zKTextFormatMessageToStringTests.testPrintUnknownFieldsEmbeddedMessageInBytesN)r   r   r   rP   rX   rZ   rd   ri   ry   rz   r|   r}   r   r   r   r   r   r   r   r   r   unittestskipIfr   Typer   r   r   r   r   r=   d   s0    d!IV
r=   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )!TextFormatMessageToTextBytesTestsc                 C   s$   |  }d|_| dt| d S )Nr   s   c: 123
)r   r   r   r   MessageToBytesrM   r   r   r   testMessageToBytes{  s   z4TextFormatMessageToTextBytesTests.testMessageToBytesc              
   C   sp   |  }|jd tj|dd}d}| || |  }t|| | ||d|||jd |jd f  d S )Nr   Tr   s   repeated_string: "ü\tꜟ"
r   r   rF   rK   rH   r   r   r6   rt   r   )r   rN   rO   	utf8_textr   rx   r   r   r   testRawUtf8RoundTrip  s   z6TextFormatMessageToTextBytesTests.testRawUtf8RoundTripc              
   C   sl   |  }|jd t|}d}| || |  }t|| | ||d|||jd |jd f  d S )Nr   s*   repeated_string: "\303\274\t\352\234\237"
r   r   r   )r   rN   rO   
ascii_textr   rx   r   r   r   testEscapedUtf8ASCIIRoundTrip  s   
z?TextFormatMessageToTextBytesTests.testEscapedUtf8ASCIIRoundTripN)r   r   r   r   r   r   r   r   r   r   r   x  s    r   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!Zed"Zd#Zed"Zd$Zd%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z d4d5 Z!d6d7 Z"d8S )9TextFormatParserTestsc                 C   sX   |  }t| t|}|  }t|| | || |tu r*t| | d S d S r4   )	rF   r   SetAllFieldsr   rL   rt   r   r   ExpectAllFieldsSet)r   rN   rO   r   rx   r   r   r   testParseAllFields  s   

z(TextFormatParserTests.testParseAllFieldsc                 C   s   |  }t| t|}|d}|  }t|| | || |tu r-t	| | |
  t|| | || |tu rGt	| | |  }d}t|| | |jd |
  | |jd t|| | |jd d S )Nr!   u   optional_string: "café"u   cafér.   )rF   r   r   r   rL   r   rt   r   r   r   r   Merger{   )r   rN   rO   r   rx   msg2r1   r   r   r   testParseAndMergeUtf8  s,   


z+TextFormatParserTests.testParseAndMergeUtf8c                 C   sD   |  }d}t|| | |jd td | |jd d d S )Nz<repeated_float: 3.4028235e+39
repeated_float: 1.4028235e-39
r   rU   r   g 7)rF   r   rt   r   rV   rW   assertAlmostEqualr   rN   rO   r1   r   r   r   testParseDoubleToFloat  s
   z,TextFormatParserTests.testParseDoubleToFloatc                 C   s   |  }d}t|| | d|jd  | d|jd  | d|jd  | d|jd  | d|jd	  | d
|jd  | d|jd  | d|jd	  | d|jd  d S )NuU  repeated_int64: -9223372036854775808
repeated_uint64: 18446744073709551615
repeated_double: 123.456
repeated_double: 1.23e+22
repeated_double: 1.23e-18
repeated_string: 
"\000\001\007\010\014\n\r\t\013\\\'\""
repeated_string: "foo" 'corge' "grault"
repeated_string: "\303\274\352\234\237"
repeated_string: "\xc3\xbc"
repeated_string: "Ã¼"
r>   r   r?   r@   rA   r   rB   r   rC   foocorgegraultrD      üre   )rF   r   rt   r   rG   rI   rJ   rK   r   r   r   r   testParseExotic  s   z%TextFormatParserTests.testParseExoticc                 C   v   |  }d}t|| | d|jd  | d|jd  | d|jd  | d|jd  | d	|jd  d S )
Nzorepeated_int64: 100;
repeated_int64: 200;
repeated_int64: 300,
repeated_string: "one",
repeated_string: "two";
d   r      r   ,  r   onetworF   r   rt   r   rG   rK   r   r   r   r   testParseTrailingCommas  s   z-TextFormatParserTests.testParseTrailingCommasc                 C   r   )
Nzerepeated_int64: [100, 200];
repeated_int64: []
repeated_int64: 300,
repeated_string: ["one", "two"];
r   r   r   r   r   r   r   r   r   r   r   r   r   "testParseRepeatedScalarShortFormat  s   z8TextFormatParserTests.testParseRepeatedScalarShortFormatc                 C   sl   |  }d}t|| | d|jd j | d|jd j | d|jd j | d|jd	 j d S )
Nzyrepeated_nested_message: [{bb: 100}, {bb: 200}],
repeated_nested_message: {bb: 300}
repeated_nested_message [{bb: 400}];
r   r   r   r   r   r   i  re   )rF   r   rt   r   r`   rb   r   r   r   r   #testParseRepeatedMessageShortFormat  s   z9TextFormatParserTests.testParseRepeatedMessageShortFormatc                 C   s,   |  }d}t|| | |  | d S r-   )rF   r   rt   r   r   r   r   r   testParseEmptyText  s   z(TextFormatParserTests.testParseEmptyTextc                 C   sl   |  }d}| tj}t|| W d    n1 sw   Y  | |j d | |j d d S )Nzrepeated_string: "\xc3\xc3"r      )	rF   assertRaisesr   
ParseErrorrt   r   	exceptionGetLine	GetColumn)r   rN   rO   r1   er   r   r   testParseInvalidUtf8  s   z*TextFormatParserTests.testParseInvalidUtf8c                 C   &   |  }d}| tjdtj|| d S )Nfooz?1:1 : Message type "\w+.TestAllTypes" has no field named "foo".rF   assertRaisesRegexr   r   rt   r   r   r   r   testParseSingleWord  s   z)TextFormatParserTests.testParseSingleWordc                 C   sX   |  }d}| tjdtj|| d}tj||dd | |jjd | |jd d S )Nzunknown_field: 8
zI1:1 : Message type "\w+.TestAllTypes" has no field named "unknown_field".zGoptional_int32: 123
unknown_field: 8
optional_nested_message { bb: 45 }T)allow_unknown_field-   r   )	rF   r  r   r   rt   r   rs   rb   r   r   r   r   r   testParseUnknownField$  s   z+TextFormatParserTests.testParseUnknownFieldc                 C   r   )Nzoptional_nested_enum: BARRzg1:23 : \'optional_nested_enum: BARR\': Enum type "\w+.TestAllTypes.NestedEnum" has no value named BARR.r   r   r   r   r   testParseBadEnumValue2  s   z+TextFormatParserTests.testParseBadEnumValuec                 C   r   )Nzoptional_int32: borkz;1:17 : 'optional_int32: bork': Couldn't parse integer: borkr   r   r   r   r   testParseBadIntValue;  s   z*TextFormatParserTests.testParseBadIntValuec                 C   s   |  }d}t|| d}| d|jd  | |d | d |jd  | |d | d	 |jd
  | || d | | d |jd  | || d | | d	 |jd  | |d |jd  d S )Na   repeated_string: "\xf\x62"
               repeated_string: "\\xf\\x62"
               repeated_string: "\\\xf\\\x62"
               repeated_string: "\\\\xf\\\\x62"
               repeated_string: "\\\\\xf\\\\\x62"
               repeated_string: "\x5cx20"\zbr   xfx62r   br   re      x20   )rF   r   rt   r   rK   )r   rN   rO   r1   SLASHr   r   r   testParseStringFieldUnescapeC  s   z2TextFormatParserTests.testParseStringFieldUnescapec                 C   s>   |  }d|_|  }tt|| | d|d d S )N   oneof_uint32oneof_field)rF   r  r   rt   rL   r   
WhichOneof)r   rN   mm2r   r   r   testParseOneofX  s
   z$TextFormatParserTests.testParseOneofc                 C   sV   d ddg}| }| tjd t|| W d    d S 1 s$w   Y  d S )Nrl   oneof_uint32: 11oneof_string: "foo"z is specified along with field )r0   rF   r  r   r   rt   r   rN   m_stringr  r   r   r   testParseMultipleOneof_  s   "z,TextFormatParserTests.testParseMultipleOneofu!  
      optional_bytes: 'Á short desçription'
      optional_string: 'Á short desçription'
      repeated_bytes: '\303\201 short des\303\247ription'
      repeated_bytes: '\x12\x34\x56\x78\x90\xab\xcd\xef'
      repeated_string: '\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'
      r!   u   Á short desçriptions   4Vxu   Приветc                 C   v   |  }t| j| | |j| j | |j| j | |j	d | j | |j	d | j
 | |jd | j d S Nr   r   )rF   r   rt   _UNICODE_SAMPLEr   r   _GOLDEN_BYTESr{   _GOLDEN_UNICODErepeated_bytes_GOLDEN_BYTES_1rK   _GOLDEN_STR_0r   rN   r  r   r   r   testParseUnicodex     z&TextFormatParserTests.testParseUnicodec                 C   r  r  )rF   r   rt   _BYTES_SAMPLEr   r   r!  r{   r"  r#  r$  rK   r%  r&  r   r   r   testParseBytes  r(  z$TextFormatParserTests.testParseBytesc                 C   X   |  }t| j}t|| | |j| j | |j	| j
 | |jd | j d S Nr   )rF   ioBytesIOr)  r   
ParseLinesr   r   r!  r{   r"  r#  r   rN   r  r+   r   r   r   testFromBytesFile     z'TextFormatParserTests.testFromBytesFilec                 C   r+  r,  )rF   r-  StringIOr   r   r/  r   r   r!  r{   r"  r#  r0  r   r   r   testFromUnicodeFile  r2  z)TextFormatParserTests.testFromUnicodeFilec                 C   T   |  }t| jd| | |j| j | |j| j	 | |j
d | j d S )N   
r   )rF   r   r/  r)  splitr   r   r!  r{   r"  r#  r&  r   r   r   testFromBytesLines  
   z(TextFormatParserTests.testFromBytesLinesc                 C   r5  )Nrl   r   )rF   r   r/  r   r7  r   r   r!  r{   r"  r#  r&  r   r   r   testFromUnicodeLines  r9  z*TextFormatParserTests.testFromUnicodeLinesc                 C   r   )NCoptional_nested_message { bb: 1 } optional_nested_message { bb: 2 }za1:59 : Message type "\w+.TestAllTypes" should not have multiple "optional_nested_message" fields.r   r   r   r   r   testParseDuplicateMessages  s   z0TextFormatParserTests.testParseDuplicateMessagesc                 C   r   )N%optional_int32: 42 optional_int32: 67zX1:36 : Message type "\w+.TestAllTypes" should not have multiple "optional_int32" fields.r   r   r   r   r   testParseDuplicateScalars  s   z/TextFormatParserTests.testParseDuplicateScalarsc                 C   s*   |j dd}d}| tjdtj|| d S )Nr[   r   optional_int32: 67zQMessage type "\w+.TestAllTypes" should not have multiple "optional_int32" fields.r   r   r   r   r    testParseExistingScalarInMessage  s   z6TextFormatParserTests.testParseExistingScalarInMessageN)#r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r   r   r)  r"  r!  r$  r%  r'  r*  r1  r4  r8  r:  r<  r>  rA  r   r   r   r   r     s@    	


	r   c                   @   4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TextFormatMergeTestsc                 C   s6   |  }d}t||}| || | d|j d S )Nr=  C   rF   r   r   r   r   r   r   rN   rO   r1   r   r   r   r   testMergeDuplicateScalarsInText  s
   z4TextFormatMergeTests.testMergeDuplicateScalarsInTextc                 C   s:   |  }d}t||}| ||u  | d|jj d S )Nr;  r   )rF   r   r   
assertTruer   rs   rb   rF  r   r   r   &testMergeDuplicateNestedMessageScalars  s
   z;TextFormatMergeTests.testMergeDuplicateNestedMessageScalarsc                 C   s:   |j dd}d}t||}| || | d|j d S )Nr[   r?  r@  rD  rE  rF  r   r   r   testReplaceScalarInMessage  s
   z/TextFormatMergeTests.testReplaceScalarInMessagec                 C   sP   |j dt d}| |d d}t||}| || | d|jj	 d S )Nr[   )r   rs   rs   z optional_nested_message{ bb: 3 }re   )
rF   dictrH  HasFieldr   r   r   r   rs   rb   rF  r   r   r   testReplaceMessageInMessage  s   z0TextFormatMergeTests.testReplaceMessageInMessagec                 C   s8   d ddg}| }t|| | d|d d S )Nrl   r  r  oneof_stringr  )r0   rF   r   r   r   r  r  r   r   r   testMergeMultipleOneof  s   z+TextFormatMergeTests.testMergeMultipleOneofN)r   r   r   rG  rI  rJ  rM  rO  r   r   r   r   rC    s    	rC  c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS ) OnlyWorksWithProto2RightNowTestsc                 C   2   t  }t| | | tj|ddd d S )NTpointy_bracketsz*text_format_unittest_data_pointy_oneof.txtr   rF   r   r   r3   r<   r   rL   r   rO   r   r   r   testPrintAllFieldsPointy     
z9OnlyWorksWithProto2RightNowTests.testPrintAllFieldsPointyc                 C   sR   d | d}t }t||}| || t }t| | 	|| d S )Nrl   /text_format_unittest_data_oneof_implemented.txt)
r0   r,   r   rF   r   rt   r   r   r   r   )r   r5   rx   r   rO   r   r   r   testParseGolden   s   
z0OnlyWorksWithProto2RightNowTests.testParseGoldenc                 C   .   t  }t| | | t|d d S NrX  rT  rU  r   r   r   testPrintAllFields     
z3OnlyWorksWithProto2RightNowTests.testPrintAllFieldsc                 C   s~   t  }d|_d|_d|_d|_d|j_d|j_	|
 }t  }|| | dtj|dd	d
 | dtj|d	d	d d S )Nr   r   r   r   h   r   zj  1: 101
  12: 4636878028842991616
  14: "hello"
  15: "103"
  16 {
    17: 104
  }
  18 {
    1: 105
  }
r   Tr   zQ1: 101 12: 4636878028842991616 14: "hello" 15: "103" 16 { 17: 104 } 18 { 1: 105 }r   )r   rF   r   r   r{   r   optionalgroupars   rb   r   r   r   r   r   rL   )r   rO   r   r   r   r   r   testPrintUnknownFields  s,   

z7OnlyWorksWithProto2RightNowTests.testPrintUnknownFieldsc                 C   s   t  }d|_d|_d|_d|j_d|j_d|jt j	< d|jt j
j _d|jt jj _d	|jt j
jj _| | tj|d
dd | | t|d d S )Nr'   r   o   r   r   ext_str0ext_str2ext_str1ext_str3T)use_index_ordera  my_string: "str"
my_int: 101
my_float: 111
optional_nested_message {
  oo: 0
  bb: 1
}
[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] {
  my_string: "ext_str2"
}
[protobuf_unittest.TestExtensionOrderings1.test_ext_orderings1] {
  my_string: "ext_str1"
}
[protobuf_unittest.TestExtensionOrderings2.TestExtensionOrderings3.test_ext_orderings3] {
  my_string: "ext_str3"
}
[protobuf_unittest.my_extension_string]: "ext_str0"
a  my_int: 101
my_string: "str"
[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] {
  my_string: "ext_str2"
}
[protobuf_unittest.TestExtensionOrderings1.test_ext_orderings1] {
  my_string: "ext_str1"
}
[protobuf_unittest.TestExtensionOrderings2.TestExtensionOrderings3.test_ext_orderings3] {
  my_string: "ext_str3"
}
[protobuf_unittest.my_extension_string]: "ext_str0"
my_float: 111
optional_nested_message {
  bb: 1
  oo: 0
}
)r   TestFieldOrderings	my_stringmy_intmy_floatrs   oorb   
Extensionsmy_extension_stringTestExtensionOrderings2test_ext_orderings2TestExtensionOrderings1test_ext_orderings1TestExtensionOrderings3test_ext_orderings3r6   r<   r   rL   rU  r   r   r   testPrintInIndexOrder4  sB   z6OnlyWorksWithProto2RightNowTests.testPrintInIndexOrderc                 C   L   |  d}t }t||}| || t }t| | || d S r[  )	r,   r   rF   r   
MergeLinesr   r   r   r   r   openedrx   r   rO   r   r   r   testMergeLinesGoldenu     

z5OnlyWorksWithProto2RightNowTests.testMergeLinesGoldenc                 C   rv  r[  )	r,   r   rF   r   r/  r   r   r   r   rx  r   r   r   testParseLinesGolden  r{  z5OnlyWorksWithProto2RightNowTests.testParseLinesGoldenc                 C   s\   t  }d|jd< d|jd< d|jd< d|jd< d	|jd
< d|jd _| 	t
|d d S )N8            r                   123abcr  rb  H  map_int32_int32 {
  key: -123
  value: -456
}
map_int64_int64 {
  key: -8589934592
  value: -17179869184
}
map_uint32_uint32 {
  key: 123
  value: 456
}
map_uint64_uint64 {
  key: 8589934592
  value: 17179869184
}
map_string_string {
  key: "abc"
  value: "123"
}
map_int32_foreign_message {
  key: 111
  value {
    c: 5
  }
}
)r   TestMapmap_int32_int32map_int64_int64map_uint32_uint32map_uint64_uint64map_string_stringmap_int32_foreign_messager   r6   r   rL   rU  r   r   r   testPrintMap  s   





z-OnlyWorksWithProto2RightNowTests.testPrintMapc                 C   s.   t  }d}t|| | t|d d S )NaM  map_uint64_uint64 {
  key: 123
  value: 17179869184
}
map_string_string {
  key: "abc"
  value: "first"
}
map_int32_foreign_message {
  key: 111
  value {
    c: 5
  }
}
map_uint64_uint64 {
  key: 123
  value: 321
}
map_string_string {
  key: "abc"
  value: "second"
}
map_int32_foreign_message {
  key: 111
  value {
    d: 5
  }
}
zmap_uint64_uint64 {
  key: 123
  value: 321
}
map_string_string {
  key: "abc"
  value: "second"
}
map_int32_foreign_message {
  key: 111
  value {
    d: 5
  }
}
)r   r  r   rt   r6   rL   r   rO   r1   r   r   r   testDuplicateMapKey  s   
z4OnlyWorksWithProto2RightNowTests.testDuplicateMapKeyc                 C   sB   t  }|jd }d|_| t|d d|_| t|d d S )Nrb  r   z@map_int32_foreign_message {
  key: 111
  value {
    c: 1
  }
}
r   z@map_int32_foreign_message {
  key: 111
  value {
    c: 2
  }
}
)r   r  r  r   r   r'   )r   rO   r   r   r   r   "testPrintMapUsingCppImplementation  s   
zCOnlyWorksWithProto2RightNowTests.testPrintMapUsingCppImplementationc                 C   st   t  }tjdd D ]}d|j|< qttjdd D ]}d|j|< qddd tjD }| t	|| d S )N      dummyr   r.   c                 s   s    | ]}d |f V  qdS )z3map_string_string {
  key: "%c"
  value: "dummy"
}
Nr   )r#   letterr   r   r   	<genexpr>  s    zKOnlyWorksWithProto2RightNowTests.testMapOrderEnforcement.<locals>.<genexpr>)
r   r  stringascii_uppercaser  reversedr0   r6   r   rL   )r   rO   r  goldenr   r   r   testMapOrderEnforcement  s   
z8OnlyWorksWithProto2RightNowTests.testMapOrderEnforcementN)r   r   r   rV  rY  r\  ra  ru  rz  r|  r  r  r  r  r   r   r   r   rP    s    "A

(1rP  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )*Proto2Testsc                 C   sv   t  }t jj}t jj}d|jj| _d|jj| _| 	t
|d t }tj}d|j| _| 	t
|d d S )N   r   message_set {
  [protobuf_unittest.TestMessageSetExtension1] {
    i: 23
  }
  [protobuf_unittest.TestMessageSetExtension2] {
    str: "foo"
  }
}
barzF[google.protobuf.internal.TestMessageSetExtension3] {
  text: "bar"
}
)r   TestMessageSetContainerTestMessageSetExtension1message_set_extensionTestMessageSetExtension2message_setrm  ru   r'   r6   r   rL   r   TestMessageSetmessage_set_extension3r1   )r   rO   ext1ext2extr   r   r   testPrintMessageSet  s   

zProto2Tests.testPrintMessageSetc                 C   sj   t d}t }tjj}tjj}d|jj| _	d|jj| _
t j||dd | | d |  d S )NFr  r   T)use_field_number?1 {
  1545008 {
    15: 23
  }
  1547769 {
    25: "foo"
  }
}
)r   r   r   r  r  r  r  r  rm  ru   r'   PrintMessager6   r   r   )r   r   rO   r  r  r   r   r    testPrintMessageSetByFieldNumber.  s   
z,Proto2Tests.testPrintMessageSetByFieldNumberc                 C   sN   t  }t jj}t jj}d|jj| _d|jj| _| 	t
j|ddd d S )Nr  r   Tr\   zmessage_set { [protobuf_unittest.TestMessageSetExtension1] { i: 23 } [protobuf_unittest.TestMessageSetExtension2] { str: "foo" } })r   r  r  r  r  r  rm  ru   r'   r6   r   rL   )r   rO   r  r  r   r   r   testPrintMessageSetAsOneLine@  s   z(Proto2Tests.testPrintMessageSetAsOneLinec                 C   s   t  }d}t|| | d|jd  | d|jd  t }d}t|| tjj	}tj
j	}| d|jj| j | d|jj| j d S )Nz&repeated_uint64: 1
repeated_uint64: 2
r   r   r   r  r  r   )r   rF   r   rt   r   rI   r   r  r  r  r  r  rm  ru   r'   r   rO   r1   r  r  r   r   r   testParseMessageSetQ  s   zProto2Tests.testParseMessageSetc                 C   s>   t  }d}tj||t d | tj|t d| d S )Nzvalue {
  [type.googleapis.com/google.protobuf.internal.TestAny] {
    [google.protobuf.internal.TestAnyExtension1.extension1] {
      i: 10
    }
  }
}
r   )test_extend_anyTestAnyr   r   r   Defaultr6   rL   r  r   r   r   testExtensionInsideAnyMessageg  s   z)Proto2Tests.testExtensionInsideAnyMessagec                 C   s   t  }d}tj||dd | d|jd  | d|jd  t }d}tj||dd tjj	}tj
j	}| d|jj| j | d	|jj| j t  }d
}| tjdtj|| d}| jtjdtj||dd d S )Nz34: 1
repeated_uint64: 2
T)allow_field_numberr   r   r   r  r  r   z34:1
z>1:1 : Message type "\w+.TestAllTypes" has no field named "34".z1234:1
z@1:1 : Message type "\w+.TestAllTypes" has no field named "1234".)r   rF   r   rt   r   rI   r   r  r  r  r  r  rm  ru   r'   r  r   r  r   r   r   testParseMessageByFieldNumberv  s8   
z)Proto2Tests.testParseMessageByFieldNumberc                 C   rZ  )N(text_format_unittest_extensions_data.txtr   TestAllExtensionsr   SetAllExtensionsr3   r<   r   rL   rU  r   r   r   testPrintAllExtensions  r]  z"Proto2Tests.testPrintAllExtensionsc                 C   rQ  )NTrR  z/text_format_unittest_extensions_data_pointy.txtr  rU  r   r   r   testPrintAllExtensionsPointy  rW  z(Proto2Tests.testPrintAllExtensionsPointyc                 C   sF   d | d}t }t|| t }t| | || d S )Nrl   r  )	r0   r,   r   r  r   rt   r   r  r   )r   r5   rx   rO   r   r   r   testParseGoldenExtensions  s   
z%Proto2Tests.testParseGoldenExtensionsc                 C   s@   t  }t| t|}t  }t|| | || d S r4   )r   r  r   r  r   rL   rt   r   )r   rO   r   rx   r   r   r   testParseAllExtensions  s   

z"Proto2Tests.testParseAllExtensionsc                 C   sL  t  }d}tj||dd d}| t|| t  }d}| jtjdtj||dd t  }d}| jtjdtj||dd t  }d	}| jtjdtj||dd t  }d
}| jtjdtj||dd t  }d}| jtjdtj||dd t  }d}tj||dd t jj	}t j
j	}| d|jj| j | d|jj| j d S )Nu  message_set {
  [unknown_extension] {
    i: 23
    repeated_i: []
    bin: "à"
    [nested_unknown_ext]: {
      i: 23
      repeated_i: [1, 2]
      x: x
      test: "test_string"
      floaty_float: -0.315
      num: -inf
      multiline_str: "abc"
          "def"
          "xyz."
      [nested_unknown_ext.ext]: <
        i: 23
        i: 24
        pointfloat: .3
        test: "test_string"
        repeated_test: ["test_string1", "test_string2"]
        floaty_float: -0.315
        num: -inf
        long_string: "test" "test2" 
      >
    }
  }
  [unknown_extension]: 5
  [unknown_extension_with_number_field] {
    1: "some_field"
    2: -0.451
  }
}
T)allow_unknown_extensionzmessage_set {
}
z3message_set {
  [unknown_extension] {
    i:
  }
}
zInvalid field value: }zGmessage_set {
  [unknown_extension] {
    str: "malformed string
  }
}
zInvalid field value: "zSmessage_set {
  [unknown_extension] {
    str: "malformed
 multiline
 string
  }
}
z7message_set {
  [malformed_extension] <
    i: -5
  
}
z5:1 : '}': Expected ">".z&message_set {
  unknown_field: true
}
zb2:3 : Message type "proto2_wireformat_unittest.TestMessageSet" has no field named "unknown_field".r  r  r   )r   r  r   rt   r6   rL   r  r   r  r  r  r   r  rm  ru   r'   )r   rO   r1   r  	malformedr  r  r   r   r    testParseAllowedUnknownExtension  s|   !	z,Proto2Tests.testParseAllowedUnknownExtensionc                 C   Z   t  }d}| tj}t|| W d    n1 sw   Y  | t|jd d S )Nz#optional_nested_message { "bb": 1 }zV1:27 : 'optional_nested_message { "bb": 1 }': Expected identifier or number, got "bb".)	r   rF   r   r   r   rt   r   r'   r   r   rO   r1   r   r   r   r   testParseBadIdentifierA  s   z"Proto2Tests.testParseBadIdentifierc                 C   sD   t  }d}| tjdtj|| t  }| tjdtj|| d S )Nz[unknown_extension]: 8
z31:2 : Extension "unknown_extension" not registered.zM1:2 : Message type "protobuf_unittest.TestAllTypes" does not have extensions.)r   r  r  r   r   rt   rF   r  r   r   r   testParseBadExtensionJ  s   z!Proto2Tests.testParseBadExtensionc                 C   &   t  }d}| tjdtj|| d S )Nzoptional_nested_enum: 100zk1:23 : \'optional_nested_enum: 100\': Enum type "\w+.TestAllTypes.NestedEnum" has no value with number 100.r   rF   r  r   r   rt   r  r   r   r   testParseNumericUnknownEnumW  s   z'Proto2Tests.testParseNumericUnknownEnumc                 C   s0   t  }d}t|| | d|jt j  d S )Na[protobuf_unittest.optional_int32_extension]: 42 [protobuf_unittest.optional_int32_extension]: 67rD  )r   r  r   r   r   rm  optional_int32_extensionr  r   r   r   "testMergeDuplicateExtensionScalars`  s   
z.Proto2Tests.testMergeDuplicateExtensionScalarsc                 C   r  )Nr  z1:96 : Message type "protobuf_unittest.TestAllExtensions" should not have multiple "protobuf_unittest.optional_int32_extension" extensions.r   r  r  r   r   rt   r  r   r   r   "testParseDuplicateExtensionScalarsh     z.Proto2Tests.testParseDuplicateExtensionScalarsc                 C   r  )Nzs[protobuf_unittest.optional_nested_message_extension]: {} [protobuf_unittest.optional_nested_message_extension]: {}z1:114 : Message type "protobuf_unittest.TestAllExtensions" should not have multiple "protobuf_unittest.optional_nested_message_extension" extensions.r  r  r   r   r   #testParseDuplicateExtensionMessagess  r  z/Proto2Tests.testParseDuplicateExtensionMessagesc                 C   s@   t  }d}| tjdtj|| d}| tjdtj|| d S )NzRepeatedGroup: <z1:16 : Expected ">".zRepeatedGroup: {z1:16 : Expected "}".r  r  r   r   r   testParseGroupNotClosed~  s   

z#Proto2Tests.testParseGroupNotClosedc                 C   s\   t  }d}t|| | |d |  t  }d}t|| | |d d S )NzOptionalGroup: {}r_  zOptionalGroup: <>)r   rF   r   rt   rH  rL  r   r  r   r   r   testParseEmptyGroup  s   zProto2Tests.testParseEmptyGroupc                 C   s   d}t  }t|| | d|jd  | d|jd  | d|jd  | d|jd	  | d
|j	d  | d|j
d j d S )Nr  r}  r~  r  r  r  r   r  r  r  r  r  rb  )r   r  r   rt   r   r  r  r  r  r  r  r   )r   r1   rO   r   r   r   testParseMap  s   zProto2Tests.testParseMapN)r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    s,    )
	 			r  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%S )&Proto3Testsc                 C   s@   t  }d|_t }|j| | tj	|t
 dd d S )Nr  r   [any_value {
  [type.googleapis.com/protobuf_unittest.OneString] {
    data: "string"
  }
}
r   	OneStringdatar   r  	any_valuePackr   r   rL   r   r  r   packed_messagerO   r   r   r   testPrintMessageExpandAny  s   z%Proto3Tests.testPrintMessageExpandAnyc                 C   sD   t  }t }|| t|}t|t }| || d S r4   )	r   r  r   Anyr  r   rL   rt   r   )r   
packed_msgrc   r1   	other_msgr   r   r   testTopAnyMessage  s   

zProto3Tests.testTopAnyMessagec                 C   sR   t  }t }d|_|j | d|_|j | | t	
|d d S )Nstring0string1repeated_any_value {
  [type.googleapis.com/protobuf_unittest.OneString] {
    data: "string0"
  }
}
repeated_any_value {
  [type.googleapis.com/protobuf_unittest.OneString] {
    data: "string1"
  }
}
)r   r  r   r  r  repeated_any_valuera   r  r   r   rL   r  r   r   r   !testPrintMessageExpandAnyRepeated  s   z-Proto3Tests.testPrintMessageExpandAnyRepeatedc                 C   sD   t  }d|_t }|j| t }| 	t
j||dd d S )Nr  r   dany_value {
  type_url: "type.googleapis.com/protobuf_unittest.OneString"
  value: "\n\006string"
}
)r   r  r  r   r  r  r  r   DescriptorPoolr   r   rL   )r   r  rO   
empty_poolr   r   r   2testPrintMessageExpandAnyDescriptorPoolMissingType  s   z>Proto3Tests.testPrintMessageExpandAnyDescriptorPoolMissingTypec                 C   <   t  }d|_t }|j| | tj	|ddd d S )Nr  TrR  z[any_value <
  [type.googleapis.com/protobuf_unittest.OneString] <
    data: "string"
  >
>

r   r  r  r   r  r  r  r   r   rL   r  r   r   r   'testPrintMessageExpandAnyPointyBrackets     z3Proto3Tests.testPrintMessageExpandAnyPointyBracketsc                 C   r  )Nr  Tr\   zRany_value { [type.googleapis.com/protobuf_unittest.OneString] { data: "string" } }r  r  r   r   r   "testPrintMessageExpandAnyAsOneLine  r  z.Proto3Tests.testPrintMessageExpandAnyAsOneLinec                 C   sD   t  }d|_t }|j| | tj	|ddt
 dd d S )Nr  T)r]   rS  r   zRany_value < [type.googleapis.com/protobuf_unittest.OneString] < data: "string" > >r  r  r   r   r   0testPrintMessageExpandAnyAsOneLinePointyBrackets  s   z<Proto3Tests.testPrintMessageExpandAnyAsOneLinePointyBracketsc                 C   sj   t  }d|_t }|j| |j |j_t	
|}| |d t }t	|| | || d S )Nr  zPany_value {
  type_url: "protobuf_unittest.OneString"
  value: "\n\006string"
}
)r   r  r  r   r  r  r  TypeNametype_urlr   rL   r   rt   )r   r  rO   r1   rx   r   r   r   "testPrintAndParseMessageInvalidAny  s   
z.Proto3Tests.testPrintAndParseMessageInvalidAnyc                 C   s>   t  }t  }d|_t|}t|| | d|j d S )Ni  )r	   rF   optional_nested_enumr   rL   rt   r   )r   rO   message2text_stringr   r   r   testUnknownEnums1  s   
zProto3Tests.testUnknownEnumsc                 C   st   t  }d}t|| t }|j| | d|j	 |
  t|| t }|j| | d|j	 d S )Nr  r  )r   r  r   r   r   r  r  Unpackr   r  r   rt   r   rO   r1   r  r   r   r   testMergeExpandedAny9  s   z Proto3Tests.testMergeExpandedAnyc                 C   s`   t  }d}t|| t }|jd | | d|j	 |jd | | d|j	 d S )Nr  r   r  r   r  )
r   r  r   r   r   r  r  r  r   r  r  r   r   r   testMergeExpandedAnyRepeatedJ  s   
z(Proto3Tests.testMergeExpandedAnyRepeatedc                 C   s>   t  }d}t|| t }|j| | d|j	 d S )Nz[any_value {
  [type.googleapis.com/protobuf_unittest.OneString] <
    data: "string"
  >
}
r  
r   r  r   r   r   r  r  r  r   r  r  r   r   r   "testMergeExpandedAnyPointyBrackets]  s   z.Proto3Tests.testMergeExpandedAnyPointyBracketsc                 C   s4   t  }d}t|| t }| d|jj d S )NzYany_value {
  [type.otherapi.com/protobuf_unittest.OneString] {
    data: "string"
  }
}
z-type.otherapi.com/protobuf_unittest.OneString)	r   r  r   r   r   r  r   r  r  r  r   r   r   testMergeAlternativeUrli  s   z#Proto3Tests.testMergeAlternativeUrlc                 C   sf   t  }d}| tj}t }tj|||d W d    n1 s#w   Y  | t	|j
d d S )Nr  r   z=Type protobuf_unittest.OneString not found in descriptor pool)r   r  r   r   r   r   r  r   r   r'   r   )r   rO   r1   r   r  r   r   r   -testMergeExpandedAnyDescriptorPoolMissingTypeu  s   z9Proto3Tests.testMergeExpandedAnyDescriptorPoolMissingTypec                 C   s>   d}t  }t|| t }|j| | d|j	 d S )Nr  r  r  )r   r1   rO   r  r   r   r   testMergeUnexpandedAny  s   z"Proto3Tests.testMergeUnexpandedAnyc                 C   r  )NzUany_value {
  [type.googleapis.com/protobuf_unittest.OneString] {
    data: "string"
z3:11 : Expected "}".)	r   r  r   r   r   r   r   r'   r   r  r   r   r   testMergeMissingAnyEndToken  s   z'Proto3Tests.testMergeMissingAnyEndTokenc                 C   sF   t  }|t  tj|d}d}t|t }| 	|| d S )N)r  zCany_value {
  [type.googleapis.com/google.protobuf.ListValue] {}
}
)
r   r  r  r   	ListValuer   r  r   rt   r   )r   any_msgrc   r1   
parsed_msgr   r   r   testParseExpandedAnyListValue  s   z)Proto3Tests.testParseExpandedAnyListValuec                 C   st   t  }| t|d d|_d|_d|_d|j_	d}| t|| t  }t
|| | t|| d S )Nr.   r   rQ   z^optional_int32: 0
optional_float: 0.0
optional_string: ""
optional_nested_message {
  bb: 0
}
)r   TestProto3Optionalr   r   rL   r   r   r{   rs   rb   rt   )r   rc   r1   r   r   r   r   testProto3Optional  s   zProto3Tests.testProto3OptionalN)r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r   r   r   r   r    s&    	
r  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) TokenizerTestc                 C   sX  d}t | }g |jdfd|jdf|jdfd|jdf|jdfd|jdf|jd	fd|jd
f|jdfd|jdf|jdfd|jdf|jdfd|jdf|jdfdd|jdfd|jtdf|jdfd|jtd f|jdfd|jdf|jdfd|jdfd|jdfd|jdf|jdfd|jdf|jdfd|jd f|jd!fd|jd"f|jd#fd|jd$f|jd%fd|jd&f|jd'fd|jdf|jd(fd|jdf|jd)fd|jdf|jd*fd|jdf|jd+fd|jdf|jd,fd|jdf|jd-fd|jtdf|jd.fd|jtd/f|jd0fd|jtd1f}d2}|	 s|| }t
|trx|j}| || |  n't
|d3 trt|d3 r| t|d2   n| |d3 |d2   |d37 }|	 r`d S d S )4Na  identifier1:"string1"
     

identifier2 : 
 
123  
  identifier3 :'string'
identifiER_4 : 1.1e+2 ID5:-0.23 ID6:'aaaa\'bbbb'
ID7 : "aa\"bb"



 ID8: {A:inf B:-inf C:true D:false}
ID9: 22 ID10: -111111111111111111 ID11: -22
ID12: 2222222222222222222 ID13: 1.23456f ID14: 1.2e+2f false_bool:  0 true_BOOL:t 
 true_bool1:  1 false_BOOL1:f False_bool: False True_bool: True X:iNf Y:-inF Z:nANidentifier1:r  identifier2r   identifier3r  identifiER_4g     [@ID5gq=
ףpͿID6z	aaaa'bbbbID7zaa"bbID8{ArU   BCTDF}ID9   ID10lq{UID11iID12l   c/Y-v ID13rS   ID14g      ^@
false_bool	true_BOOL
true_bool1false_BOOL1
False_bool	True_boolXYz-infZnanr   r   )r   	Tokenizer
splitlinesConsumeIdentifierConsumeStringConsumeIntegerConsumeFloatrW   ConsumeBoolAtEnd
isinstancer'   tokenr   	NextTokenmathisnanrH  )r   r1   	tokenizermethodsru   r  r2  r   r   r   testSimpleTokenCases  s^  	

 !!"##$%%&''())*++,--.//01123346

 z"TokenizerTest.testSimpleTokenCasesc                 C   s  d}d}d|d |d f }t | }| d|  | |d |  | |d |  | |  d}t | }| d|  | d|  | d|  | d	|  | t j	 |  W d    n1 svw   Y  | d
|
  | |  d S )N        -1 %d %dr   z
-0 0 0 1.2r   TFrR   )r   r)  r*  r   r-  rH  r0  TryConsumeIntegerr   r   r.  r   	int64_max
uint32_maxr1   r6  r   r   r   testConsumeAbstractIntegers  s&   
z)TokenizerTest.testConsumeAbstractIntegersc                 C   s<  d}d}d|d |d f }t | }| t jt j| | t jt j| | dt | | t jt j| | t jt j| | |d t 	| | t jt j	| | |d t | | 
|  d}t | }| dt | | dt | | dt | | dt | | 
|  d S )Nr9  r:  r;  r   r<  z	-0 -0 0 0r   )r   r)  r*  r   r   _ConsumeUint32_ConsumeUint64r   _ConsumeInt32_ConsumeInt64rH  r0  r>  r   r   r   testConsumeIntegers  s<   z!TokenizerTest.testConsumeIntegersc                 C   s  d}t | }| d|  | d|  | d|  | d|  | d|  | d|  | d|  | t j |  W d   n1 sVw   Y  |  | t j |  W d   n1 stw   Y  |  | d	|  | |	  dS )
z(Test support for C style octal integers.z'00 -00 04 0755 -010 007 -0033 08 -09 01r   r  i  i   iNr   )
r   r)  r*  r   r-  r   r   r3  rH  r0  r   r1   r6  r   r   r   testConsumeOctalIntegers<  s&   

z&TokenizerTest.testConsumeOctalIntegersc                 C   s   d}t | }| t j|j d}t | }| t j|j d}t | }| t j|j d}t | }| t j|j d}t | }| t j|j d S )Nz	"string1'zstring1"z
"\xt"z
"\"z
"\x")r   r)  r*  r   r   ConsumeByteStringrH  r   r   r   testConsumeByteStringP  s   z#TokenizerTest.testConsumeByteStringc                 C   s&   d}t | }| t j|j d S )Nz
not-a-bool)r   r)  r*  r   r   r/  rH  r   r   r   testConsumeBoole  s   zTokenizerTest.testConsumeBoolc                 C   s0   t d }| |  | t j|j d S )N# some comment)r   r)  r*  rH  r0  r   r   ConsumeCommentr   r6  r   r   r   testSkipCommentj  s   zTokenizerTest.testSkipCommentc                 C   sB   t jd dd}| |  | d|  | |  d S )NrM  Fskip_comments)r   r)  r*  assertFalser0  r   rN  rH  rO  r   r   r   testConsumeCommento  s   
z TokenizerTest.testConsumeCommentc                 C   V   d}t j| dd}| d|  | |  | d|  | |  d S )N # some comment
# another commentFrQ  rM  # another comment)r   r)  r*  r   rN  rS  r0  rH  rH  r   r   r   testConsumeTwoCommentsv  s   z$TokenizerTest.testConsumeTwoCommentsc                 C      d}t j| dd}| t j|j | d|  | |jd |	  | t j|j | d|
  | |  | d|  | |  d S )Nzsome_number: 4
# some commentFrQ  some_numberr
  r  rM  )r   r)  r*  r   r   rN  r   r+  r2  r3  r-  rS  r0  rH  rH  r   r   r   testConsumeTrailingComment~  s   z(TokenizerTest.testConsumeTrailingCommentc                 C   sB   t jd dd}| |  | d|  | |  d S )NrM  FrQ  FrM  )r   r)  r*  rS  r0  r   ConsumeCommentOrTrailingCommentrH  rO  r   r   r   testConsumeLineComment  s   
z$TokenizerTest.testConsumeLineCommentc                 C   rU  )NrV  FrQ  r\  )FrW  )r   r)  r*  r   r]  rS  r0  rH  rH  r   r   r   testConsumeTwoLineComments  s   z(TokenizerTest.testConsumeTwoLineCommentsc                 C   rY  )Nzsome_number: 4  # some commentFrQ  rZ  r
  r  )TrM  )r   r)  r*  r   r   r]  r   r+  r2  r3  r-  rS  r0  rH  rH  r   r   r   "testConsumeAndCheckTrailingComment  s"   z0TokenizerTest.testConsumeAndCheckTrailingCommentc                 C   sn   d}t j| dd}| d|  | |jd |  | d|  | d|  | 	|
  d S )Nz2some_number: 4  # some comment # not a new commentFrQ  rZ  r
  r  )Tz"# some comment # not a new comment)r   r)  r*  r   r+  r2  r3  r-  r]  rH  r0  rH  r   r   r   testHashinComment  s   zTokenizerTest.testHashinCommentc                 C   s.   ddd  d }t j| dd}|  d S )N"r`  i   FrQ  )r   r)  r*  r,  rH  r   r   r   testHugeString  s   zTokenizerTest.testHugeStringN)r   r   r   r8  rA  rF  rI  rK  rL  rP  rT  rX  r[  r^  r_  r`  ra  rc  r   r   r   r   r    s     M 
r  c                   @   rB  )PrettyPrinterTestc                 C   s<   dd }|  }|j }d|_| tj|d|dd d S )Nc                 S   s
   ~ ~~d S r4   r   )rO   r   r]   r   r   r   r     s   z9PrettyPrinterTest.testPrettyPrintNoMatch.<locals>.printerr[   Tr]   message_formatterr^   r_   r   rN   r   rO   rc   r   r   r   testPrettyPrintNoMatch  s   
z(PrettyPrinterTest.testPrettyPrintNoMatchc                    s@    fdd}   }|j }d|_| tj|d|dd d S )Nc                    "   ~~| j  jjj krd| j S d S NzMy lucky number is %sr   rF   NestedMessagerb   r  r   r]   rN   r   r   r     s   
z9PrettyPrinterTest.testPrettyPrintOneLine.<locals>.printerr[   Tre  z1repeated_nested_message { My lucky number is 42 }r_   rg  r   rn  r   testPrettyPrintOneLine  s   
z(PrettyPrinterTest.testPrettyPrintOneLinec                    sX    fdd}   }|j }d|_| tj|d|dd | tj|d|dd d S )	Nc                    s6   | j  jjj kr|rdndd|  }d|| jf S d S )Nrm   rl   zMy lucky number is:%s%srk  )r  r   r]   line_deliminatorrn  r   r   r     s   z;PrettyPrinterTest.testPrettyPrintMultiLine.<locals>.printerr[   Tre  z2repeated_nested_message { My lucky number is: 42 }Fz7repeated_nested_message {
  My lucky number is:
  42
}
r_   rg  r   rn  r   testPrettyPrintMultiLine  s    
z*PrettyPrinterTest.testPrettyPrintMultiLinec                    sH    fdd}   }| tj|d|dd | tj|d|dd d S )Nc                    s   ~~| j  jj krdS d S )NThe is the message!)r   rF   rm  rn  r   r   r     s   z?PrettyPrinterTest.testPrettyPrintEntireMessage.<locals>.printerFre  zThe is the message!
Trr  )rF   r6   r   rL   )r   rN   r   rO   r   rn  r   testPrettyPrintEntireMessage  s   z.PrettyPrinterTest.testPrettyPrintEntireMessagec                    s    fdd}   }d|_|j }d|_|j }d|_|j}d|_| tj|d|dd	 t	d
}tj
 j jjd |j||d | d|  |  t	d
}tj j jjd |j||d | d|  |  d S )Nc                    ri  rj  rk  rm  rn  r   r   r   	  s   
z?PrettyPrinterTest.testPrettyPrintMultipleParts.<locals>.printer=   r[   c   r   Tre  zoptional_int32: 61 optional_nested_message { My lucky number is 1 } repeated_nested_message { My lucky number is 42 } repeated_nested_message { My lucky number is 99 }Frs   )rf  z3optional_nested_message {
  My lucky number is 1
}
z{
  My lucky number is 1
})rF   r   r`   ra   rb   rs   r6   r   rL   r   r   r   r   r   r   r   r   )r   rN   r   rO   rc   r   r   rn  r   testPrettyPrintMultipleParts
	  sV   



z.PrettyPrinterTest.testPrettyPrintMultiplePartsN)r   r   r   rh  ro  rq  rs  rv  r   r   r   r   rd    s    rd  c                   @   rB  )WhitespaceTestc                 C   sN   t d| _| | jj t | _d| jjj	_
| jjjd | _| jj| _d S )NFr   child)r   r   r   
addCleanupr   r   r   rO   rx  r   r{   r   r   r   r   r   r   r   r   setUp=	  s   
zWhitespaceTest.setUpc                 C   s   |  t| jtd d S Nz            child {
              payload {
                optional_string: "value"
              }
            }
            )r6   r   rL   rO   r   r   rz  r   r   r   r   E	  s   
z"WhitespaceTest.testMessageToStringc                 C   s,   t | j| j | | j td d S r|  )r   r  rO   r   r6   r   r   r   rz  r   r   r   testPrintMessageP	  s
   zWhitespaceTest.testPrintMessagec                 C   0   t | j| j| j | | j td d S r|  )	r   r   r   r   r   r6   r   r   r   rz  r   r   r   r   \	  s
   zWhitespaceTest.testPrintFieldc                 C   r~  )Nzl            {
              payload {
                optional_string: "value"
              }
            })	r   r   r   r   r   r6   r   r   r   rz  r   r   r   r   h	  s   z"WhitespaceTest.testPrintFieldValueN)r   r   r   r{  r   r}  r   r   r   r   r   r   rw  ;	  s    rw  c                   @   s   e Zd Zdd Zdd ZdS ) OptionalColonMessageToStringTestc                 C   sD   t  }d|_t }|j| tj|dd}d}| 	|| d S )Nr  T)force_colonz]any_value: {
  [type.googleapis.com/protobuf_unittest.OneString]: {
    data: "string"
  }
}
)
r   r  r  r   r  r  r  r   rL   r   )r   r  rO   outputexpectedr   r   r   testForcePrintOptionalColonw	  s   z<OptionalColonMessageToStringTest.testForcePrintOptionalColonc                 C   s4   t  }|jd tj|ddd}| d| d S )Nr   T)rn   r  zrepeated_int32: [1]
)r   rF   rh   rH   r   rL   r   )r   rO   r  r   r   r   rz   	  s   zCOptionalColonMessageToStringTest.testPrintShortFormatRepeatedFieldsN)r   r   r   r  rz   r   r   r   r   r  u	  s    r  __main__)+__doc__r-  r4  r8   r  r   r   google.protobufr   r   r   r   r   r   r   r	   r
   google.protobuf.internalr  r   r   r   r   r   r   r   TestCaser   r    
parametersr=   r   r   rC  rP  r  r  r  rd  rw  r  r   mainr   r   r   r   <module>   sv   
    

"  
0+     , y  
v:
