o
    1ήc                     @   s
  d Z d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 G dd dejZG dd deZedkre  dS dS )z%Test for google.protobuf.json_format.zjieluo@google.com (Jie Luo)    N)any_pb2)duration_pb2)field_mask_pb2)
struct_pb2)timestamp_pb2)wrappers_pb2)any_test_pb2)unittest_mset_pb2)unittest_pb2)test_proto3_optional_pb2descriptor_pool)json_format)json_format_pb2)json_format_proto3_pb2c                   @   s$   e Zd Zdd Zdd Zdd ZdS )JsonFormatBasec                 C   sX  d|_ d|_d|_d|_td|_d|_d|_d|_d	|_	d
|j
_tj|_|jd |jd |jd |jd |jd |jd |jd |jd |jd |jd |jtd |jd |jd |jd |jd |jd |jd	 d
|j _d|j _|jtj |jtj || _d S )N   l   ~t l   4? z-infgo!	@Tfoos   bar
   i   l          l       iil    r   gV瞯<infFzFew symbols!#$,;bars   foo   )int32_valueint64_valueuint32_valueuint64_valuefloatfloat_valuedouble_value
bool_valuestring_valuebytes_valuemessage_valuevaluer   BAR
enum_valuerepeated_int32_valueappendrepeated_int64_valuerepeated_uint32_valuerepeated_uint64_valuerepeated_float_valuerepeated_double_valuerepeated_bool_valuerepeated_string_valuerepeated_bytes_valuerepeated_message_valueaddrepeated_enum_valueFOOmessageselfr6    r9   P/tmp/pip-target-vg8gfxp4/lib/python/google/protobuf/internal/json_format_test.pyFillAllFields;   sB   

zJsonFormatBase.FillAllFieldsc                 C   s"   t t || | || d S N)r   ParseMessageToJsonassertEqualr8   r6   parsed_messager9   r9   r:   CheckParseBack`   s   zJsonFormatBase.CheckParseBackc                 C   s"   t  }| tj|tj|| d S r<   )r   TestMessageassertRaisesRegexr   
ParseErrorr=   )r8   texterror_messager6   r9   r9   r:   
CheckErrore   s   
zJsonFormatBase.CheckErrorN)__name__
__module____qualname__r;   rB   rH   r9   r9   r9   r:   r   9   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%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^ Z1d_d` Z2dadb Z3dcdd Z4dedf Z5dgdh Z6didj Z7dkdl Z8dmdn Z9dodp Z:dqdr Z;dsdt Z<dudv Z=dwdx Z>dydz Z?d{d| Z@d}d~ ZAdS )JsonFormatTestc                 C   s2   t  }| t|d t  }| || d S )N{})r   rC   r?   r   r>   rB   r@   r9   r9   r:   testEmptyMessageToJsonm   s   z%JsonFormatTest.testEmptyMessageToJsonc                 C   sH   t jdddgd}| tt|td t  }| || d S )NtestY      )r"   r(   z6{"stringValue": "test", "repeatedInt32Value": [89, 4]}r   rC   r?   jsonloadsr   r>   rB   r@   r9   r9   r:   testPartialMessageToJsont   s   z'JsonFormatTest.testPartialMessageToJsonc                 C   sX   t  }d}| | | tt|t| t  }t|| | || d S )Na  {"int32Value": 20, "int64Value": "-20", "uint32Value": 3120987654,"uint64Value": "12345678900","floatValue": "-Infinity","doubleValue": 3.1415,"boolValue": true,"stringValue": "foo","bytesValue": "YmFy","messageValue": {"value": 10},"enumValue": "BAR","repeatedInt32Value": [2147483647, -2147483648],"repeatedInt64Value": ["9007199254740992", "-9007199254740992"],"repeatedUint32Value": [268435455, 134217727],"repeatedUint64Value": ["9007199254740992", "9007199254740991"],"repeatedFloatValue": [0],"repeatedDoubleValue": [1e-15, "Infinity"],"repeatedBoolValue": [true, false],"repeatedStringValue": ["Few symbols!#$,;", "bar"],"repeatedBytesValue": ["Zm9v", "YmFy"],"repeatedMessageValue": [{"value": 10}, {"value": 11}],"repeatedEnumValue": ["FOO", "BAR"]})	r   rC   r;   r?   rS   rT   r   r>   r=   r8   r6   rF   rA   r9   r9   r:   testAllFieldsToJson~   s   
z"JsonFormatTest.testAllFieldsToJsonc                 C   sH   d}t  }d|_| t|| t  }t|| | || d S )Nz{
  "enumValue": 999
}i  )r   rC   r'   r?   r   r>   r=   )r8   rF   r6   rA   r9   r9   r:   testUnknownEnumToJsonAndBack   s   z+JsonFormatTest.testUnknownEnumToJsonAndBackc                 C   b   t  }t jj}t jj}d|jj| _d|jj| _t	
|}t  }t	|| | || d S N   r   )r	   TestMessageSetContainerTestMessageSetExtension1message_set_extensionTestMessageSetExtension2message_set
Extensionsistrr   r>   r=   r?   )r8   r6   ext1ext2message_textrA   r9   r9   r:   testExtensionToJsonAndBack      z)JsonFormatTest.testExtensionToJsonAndBackc                 C      |  dd d S )Nz{"[extensionField]": {}}z8Message type proto3.TestMessage does not have extensionsrH   r8   r9   r9   r:   testExtensionErrors      z"JsonFormatTest.testExtensionErrorsc                 C   rY   rZ   )r	   r\   r]   r^   r_   r`   ra   rb   rc   r   MessageToDict	ParseDictr?   )r8   r6   rd   re   message_dictrA   r9   r9   r:   testExtensionToDictAndBack   rh   z)JsonFormatTest.testExtensionToDictAndBackc                 C   sH   t  }t jj}d|j|< t|}t  }t|| | || d S )Ndata)	r
   TestAllExtensionsTestNestedExtensionrO   ra   r   rn   ro   r?   )r8   r6   rd   rp   rA   r9   r9   r:   $testExtensionToDictAndBackWithScalar   s   
z3JsonFormatTest.testExtensionToDictAndBackWithScalarc                 C   s>   ddd}t t |}t }t|| | || d S )Nr   z&type.googleapis.com/proto3.TestMessage)
int32Value@type)rS   rT   dumpsr   Anyr   ro   r?   )r8   	orig_dictcopied_dictrA   r9   r9   r:   'testJsonParseDictToAnyDoesNotAlterInput   s   z6JsonFormatTest.testJsonParseDictToAnyDoesNotAlterInputc                 C   s   t  }t jj}t jj}d|jj| _d|jj| _t	
|}dddiddidi}| || t  }t	|| | || dS )&See go/proto3-json-spec for spec.
    r[   r   
messageSetrb   rc   )zB[protobuf_unittest.TestMessageSetExtension1.message_set_extension]zB[protobuf_unittest.TestMessageSetExtension2.message_set_extension]N)r	   r\   r]   r^   r_   r`   ra   rb   rc   r   rn   r?   ro   )r8   r6   rd   re   rp   golden_dict
parsed_msgr9   r9   r:   /testExtensionSerializationDictMatchesProto3Spec   s&   z>JsonFormatTest.testExtensionSerializationDictMatchesProto3Specc                 C   sB   t  }t jj}d|j| _t|}dddii}| || dS )r}   stuffz%[protobuf_unittest.TestExtension.ext]r%   N)	r   TestMessageWithExtensionTestExtensionextra   r%   r   rn   r?   )r8   r6   r   rp   expected_dictr9   r9   r:   3testExtensionSerializationDictMatchesProto3SpecMore   s   zBJsonFormatTest.testExtensionSerializationDictMatchesProto3SpecMorec                 C   sn   t  }t jj}t jj}d|jj| _d|jj| _t	
|}d}d}d||f }| t|t| dS )r}   r[   r   z@protobuf_unittest.TestMessageSetExtension1.message_set_extensionz@protobuf_unittest.TestMessageSetExtension2.message_set_extensionzZ{"messageSet": {    "[%s]": {        "i": 23    },    "[%s]": {        "str": "foo"    }}}N)r	   r\   r]   r^   r_   r`   ra   rb   rc   r   r>   r?   rS   rT   )r8   r6   rd   re   rf   	ext1_text	ext2_textgolden_textr9   r9   r:   /testExtensionSerializationJsonMatchesProto3Spec  s   z>JsonFormatTest.testExtensionSerializationJsonMatchesProto3Specc                 C   sj   t  }d|_| jdd7  _| t|d t  }| || d}t|| | |j	d d S )Nz&
<">	\/s     utf-8z;{
  "stringValue": "&\n<\"\r>\b\t\f\\\u0001/\u2028\u2029"
}z{"int32Value": "1"}   )
r   rC   r"   decoder?   r   r>   rB   r=   r   r8   r6   rA   rF   r9   r9   r:   testJsonEscapeString!  s   z#JsonFormatTest.testJsonEscapeStringc                 C   sD   t jdd}| tt|dtd t  }| || d S )Nr   )r"   Ta  {"repeatedStringValue": [],"stringValue": "foo","repeatedBoolValue": [],"repeatedUint32Value": [],"repeatedInt32Value": [],"enumValue": "FOO","int32Value": 0,"floatValue": 0,"int64Value": "0","uint32Value": 0,"repeatedBytesValue": [],"repeatedUint64Value": [],"repeatedDoubleValue": [],"bytesValue": "","boolValue": false,"repeatedEnumValue": [],"uint64Value": "0","doubleValue": 0,"repeatedFloatValue": [],"repeatedInt64Value": [],"repeatedMessageValue": []}rR   r@   r9   r9   r:   testAlwaysSeriliaze/  s   z"JsonFormatTest.testAlwaysSeriliazec                 C   sV   t  }| ttj|ddtd d|_| ttj|ddtd d S )NT)including_default_value_fieldsrM   r   z{"optionalInt32": 0})r   TestProto3Optionalr?   rS   rT   r   r>   optional_int32r7   r9   r9   r:   testProto3OptionalM  s$   z!JsonFormatTest.testProto3Optionalc                 C   sZ   t  }td| | |jd td| | |jd td| | |jd d S )Nz{"int32Value": -2.147483648e9}r   z{"int32Value": 1e5}i z{"int32Value": 1.0}r   )r   rC   r   r=   r?   r   r7   r9   r9   r:   testIntegersRepresentedAsFloat[  s   z-JsonFormatTest.testIntegersRepresentedAsFloatc                 C   s   t  }| tt|dt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|jd< d|jd< d|jd< d|jd< d|jd jd< | tt|dtd	 t  }| || d S )
NTzj{"boolMap": {},"int32Map": {},"int64Map": {},"uint32Map": {},"uint64Map": {},"stringMap": {},"mapMap": {}}r      F   1nullz{"boolMap": {"false": 2, "true": 1},"int32Map": {"1": 2, "2": 3},"int64Map": {"1": 2, "2": 3},"uint32Map": {"1": 2, "2": 3},"uint64Map": {"1": 2, "2": 3},"stringMap": {"1": 2, "null": 3},"mapMap": {"1": {"boolMap": {"true": 3}}}})r   TestNestedMapr?   rS   rT   r   r>   bool_map	int32_map	int64_map
uint32_map
uint64_map
string_mapmap_maprB   r@   r9   r9   r:   testMapFieldsd  s0   











zJsonFormatTest.testMapFieldsc                 C   sN   t  }| t|dd d|_| t|dd t  }| || d S )NTrM   r   z{
  "oneofInt32Value": 0
})r   	TestOneofr?   r   r>   oneof_int32_valuerB   r@   r9   r9   r:   testOneofFields  s   

zJsonFormatTest.testOneofFieldsc                 C   sF   t  }td| | |jddd | dd | dd d S )Nz{"stringValue": "\uD83D\uDE01"}s   😁r   strictz{"stringValue": "\uD83D"}z*Invalid \\uXXXX escape|Unpaired.*surrogatez{"stringValue": "\uDE01"})r   rC   r   r=   r?   r"   r   rH   r7   r9   r9   r:   testSurrogates  s   
zJsonFormatTest.testSurrogatesc                 C   s   t  }d|j_d|j_d|j _d|jd _d|j _d|jd _d|j _d|jd _d|j _d|jd	 _d
|j _d|jd _| t	t
|dt	d t  }| || d}t
|| | |jjd | |jjd | |jd jd | |jd jd d S )Nr   r   r   i'  i r   l   A iɚ;r   l w#o9 rQ   Tz{"value": "1970-01-01T00:00:00Z","repeatedValue": [  "1970-01-01T00:00:20.000000001Z",  "1970-01-01T00:00:00.000010Z",  "1973-03-03T09:46:40Z",  "9999-12-31T23:59:59.999999999Z",  "0001-01-01T00:00:00Z"]}z}{"value": "1970-01-01T00:00:00.01+08:00","repeatedValue":[  "1970-01-01T00:00:00.01+08:30",  "1970-01-01T00:00:00.01-01:23"]}ii g     it  )r   TestTimestampr%   secondsnanosrepeated_valuer3   r?   rS   rT   r   r>   rB   r=   r   r9   r9   r:   testTimestampMessage  s2   z#JsonFormatTest.testTimestampMessagec                 C   s   t  }d|j_d|j _d|jd _d|j _d|jd _d|j _d|jd _d|j _d	|j _| t	t
|d
t	d t  }| || d S )Nr   r   r   iiا r   l s%l    s%Tzs{"value": "1s","repeatedValue": [  "0.000000010s",  "-1.000001s",  "10.011s",  "-315576000000s",  "315576000000s"]})r   TestDurationr%   r   r   r3   r   r?   rS   rT   r   r>   rB   r@   r9   r9   r:   testDurationMessage  s    z"JsonFormatTest.testDurationMessagec                 C   sz   t  }|jjd |jjd | t|dd t  }| || |j	  | t|dd | || d S )Nfoo.barr   Tz{
  "value": "foo.bar,bar"
}z{
  "value": ""
})
r   TestFieldMaskr%   pathsr)   r?   r   r>   rB   Clearr@   r9   r9   r:   testFieldMaskMessage  s   


z#JsonFormatTest.testFieldMaskMessagec                 C   s   t  }d|j_d|j_d|j_d|j_d|j _d|j _|j	  | 
tt|dtd t  }| || d S )NFr        TaR  {
  "int32Value": 0,  "boolValue": false,  "stringValue": "",  "bytesValue": "",  "repeatedBoolValue": [true, false],  "repeatedInt32Value": [0],  "repeatedUint32Value": [],  "repeatedFloatValue": [],  "repeatedDoubleValue": [],  "repeatedBytesValue": [],  "repeatedInt64Value": [],  "repeatedUint64Value": [],  "repeatedStringValue": []})r   TestWrapperr!   r%   r   r"   r#   r/   r3   r(   r?   rS   rT   r   r>   rB   r@   r9   r9   r:   testWrapperMessage  s   
z!JsonFormatTest.testWrapperMessagec                 C   s   t  }d|jd< d|jd< d|jd< d |jd< d|jd	d
< d|jd	 d< |jd |jd |jd}|g d d| d< d|j d< |j  | 	t
t|dt
d t  }| || |jd  |jd  d S )NJimnamer   ageTattendemailSFOaddresscityi   house_numberempty_struct
empty_listlist)   sevenTFN	   subkey2r   Fa.  {  "value": {    "address": {      "city": "SFO",       "house_number": 1024    },     "empty_struct": {},     "empty_list": [],     "age": 10,     "name": "Jim",     "attend": true,     "email": null,     "list": [6, "seven", true, false, null, {"subkey2": 9}]  },  "repeatedValue": [{"age": 11}, {}]})r   
TestStructr%   get_or_create_structget_or_create_listextend
add_structr   r3   r?   rS   rT   r   r>   rB   )r8   r6   struct_listrA   r9   r9   r:   testStructMessage  s0   





z JsonFormatTest.testStructMessagec                 C   s   t  }d|j_d|j _d|j _d|j _| 	t
t|dt
d t  }| || |j  | 	t
t|dt
d |  td| | 	|jdd	 d S )
Nhello333333&@Fr   z;{  "value": "hello",  "repeatedValue": [11.1, false, null]}zA{  "value": "hello",  "repeatedValue": [11.1, false, null, null]}z{"value": null}kind
null_value)r   	TestValuer%   r"   r   r3   number_valuer!   r   r?   rS   rT   r   r>   rB   r   r=   
WhichOneofr@   r9   r9   r:   testValueMessageG  s.   
zJsonFormatTest.testValueMessagec                 C   s   t  }d|jj _d|jj _d|jj _d|jj _d|jj j	d< d|j
 j _|j
  | tt|dtd	 t  }| || d S )
Nr   r   Tr   r   r   r   FzS{"value": [11.1, null, true, "hello", {"name": "Jim"}]
,"repeatedValue": [[1], []]})r   TestListValuer%   valuesr3   r   r   r!   r"   struct_valuer   r?   rS   rT   r   r>   rB   r@   r9   r9   r:   testListValueMessagec  s    
z#JsonFormatTest.testListValueMessagec                 C   s^   t  }d|_| t|d t  }| || t  }td| | t|d d S )Nr   z{
  "oneofNullValue": null
}z${
  "oneofNullValue": "NULL_VALUE"
})r   r   oneof_null_valuer?   r   r>   rB   r=   )r8   r6   rA   new_messager9   r9   r:   testNullValuet  s   zJsonFormatTest.testNullValuec              	   C   s   t  }t  }t  }d|_d|_|j| |j | |j | |j  | t	t
|dt	d t  }| || t jdddddddd	}|  |j| | t
|d
dd d d S )N  .  Ta  {
  "repeatedValue": [ {
    "@type": "type.googleapis.com/proto3.MessageType",
    "value": 1234
  }, {
    "@type": "type.googleapis.com/proto3.MessageType",
    "value": 5678
  },
  {}],
  "value": {
    "@type": "type.googleapis.com/proto3.MessageType",
    "value": 1234
  }
}
r   r   gQ	@r   )r!   r   r   r   r   r    r"   Fr   D   zD{
  "value": {
    "@type": "type.googleapis.com/proto3.TestMessage")r   TestAnyMessageTyper%   Packr   r3   r?   rS   rT   r   r>   rB   rC   r   )r8   r6   value1value2rA   test_messager9   r9   r:   testAnyMessage  s@   
zJsonFormatTest.testAnyMessagec                 C   sz   t  }d|_t }|j| t }| 	t
}tj|d|d W d    n1 s-w   Y  | dt|j d S )NstringTr   z\Can not find message descriptor by type_url: type.googleapis.com/protobuf_unittest.OneString)r
   	OneStringrr   r   r   	any_valuer   r   DescriptorPoolassertRaises	TypeErrorr   r>   r?   rc   	exception)r8   packed_messager6   
empty_poolcmr9   r9   r:   'testAnyMessageDescriptorPoolMissingType  s   z6JsonFormatTest.testAnyMessageDescriptorPoolMissingTypec           	      C   s  t  }t }d|_|| | tt	
|dtd t  }| || t }|| | tt	
|dtd | || t }d|_|| | tt	
|dtd | || t }|jd |jd || | tt	
|dtd	 | || t }d
|d< || | tt	
|dtd | || t  }d|_|| || | tt	
|dtd | || d S )Nr   TzQ{
  "@type": "type.googleapis.com/google.protobuf.Int32Value",
  "value": 1234
}
zb{
  "@type": "type.googleapis.com/google.protobuf.Timestamp",
  "value": "1970-01-01T00:00:00Z"
}
r   zO{
  "@type": "type.googleapis.com/google.protobuf.Duration",
  "value": "1s"
}
r   r   zY{
  "@type": "type.googleapis.com/google.protobuf.FieldMask",
  "value": "foo.bar,bar"
}
r   r   zX{
  "@type": "type.googleapis.com/google.protobuf.Struct",
  "value": {"name": "Jim"}
}
r   z{
  "@type": "type.googleapis.com/google.protobuf.Any",
  "value": {
    "@type": "type.googleapis.com/google.protobuf.Int32Value",
    "value": 5678
  }
}
)r   ry   r   
Int32Valuer%   r   r?   rS   rT   r   r>   rB   r   	Timestampr   Durationr   r   	FieldMaskr   r)   r   Struct)	r8   r6   r   rA   	timestampduration
field_maskstruct_message
nested_anyr9   r9   r:   testWellKnownInAnyMessage  s~   







z(JsonFormatTest.testWellKnownInAnyMessagec                 C   s   t  }t  }| | td| | || | |d td| | |d | 	tj
dtjd| | dd d S )Na  {"int32Value": null, "int64Value": null, "uint32Value": null,"uint64Value": null,"floatValue": null,"doubleValue": null,"boolValue": null,"stringValue": null,"bytesValue": null,"messageValue": null,"enumValue": null,"repeatedInt32Value": null,"repeatedInt64Value": null,"repeatedUint32Value": null,"repeatedUint64Value": null,"repeatedFloatValue": null,"repeatedDoubleValue": null,"repeatedBoolValue": null,"repeatedStringValue": null,"repeatedBytesValue": null,"repeatedMessageValue": null,"repeatedEnumValue": null}r$   z{"messageValue": {}}zFailed to parse repeatedInt32Value field: null is not allowed to be used as an element in a repeated field at TestMessage.repeatedInt32Value\[1\].z {"repeatedInt32Value":[1, null]}z{"repeatedMessageValue":[null]}zFailed to parse repeatedMessageValue field: null is not allowed to be used as an element in a repeated field at TestMessage.repeatedMessageValue\[0\].)r   rC   r;   r   r=   r?   assertFalseHasField
assertTruerD   rE   rH   r@   r9   r9   r:   testParseNull  s&   
zJsonFormatTest.testParseNullc                 C   sR   t  }td|_d}| t|| t  }t|| | t	
|j d S )Nnanz{
  "floatValue": "NaN"
})r   rC   r   r   r?   r   r>   r=   r  mathisnanrV   r9   r9   r:   testNanFloat8  s   
zJsonFormatTest.testNanFloatc                 C   sP   t  }d}t|| | |jd d | |jd d d}| |d d S )Nz8{"repeatedDoubleValue": [3.4028235e+39, 1.4028235e-39]
}r   gP#Hr   g 7z7{"repeatedFloatValue": [3.4028235e+39, 1.4028235e-39]
}zgFailed to parse repeatedFloatValue field: Float value too large at TestMessage.repeatedFloatValue\[0\].)r   rC   r   r=   r?   r.   rH   r8   r6   rF   r9   r9   r:   testParseDoubleToFloatA  s   z%JsonFormatTest.testParseDoubleToFloatc                 C   s   t  }d|_d}| tj|dd| d}| tj|dd| d|_d}| t|| d	|_d
}| t|| d|_d}| t|| g d}t  }tddD ]%}||d< tddD ]}||d< t|}t	d|d |_| 
|| qcqXd S )Ng6sӭ?z{
  "floatValue": 1.1234568
}   )float_precisionz{
  "floatValue": 1.123457
}   g1噙?z{
  "floatValue": 1.1
}gɞDu8z"{
  "floatValue": 1.00000075e-36
}gVDz {
  "floatValue": 1.234568e+24
})r      n   r   r      r   z<f)r   rC   r   r?   r   r>   range	bytearraystructunpackrB   )r8   r6   rF   
value_listmsg2ab
byte_arrayr9   r9   r:   testFloatPrecisionL  sH   


z!JsonFormatTest.testFloatPrecisionc                 C   ri   )Nr   z1Failed to load JSON: (Expecting value)|(No JSON).rj   rk   r9   r9   r:   testParseEmptyTextr  rm   z!JsonFormatTest.testParseEmptyTextc                 C   sf   t  }d}t|| d}t|| | dd d}t|| t }| tjdtjd| d S )Nz{"enumValue": 0}z{"enumValue": 1}z{"enumValue": "baz"}zoFailed to parse enumValue field: Invalid enum value baz for enum type proto3.EnumType at TestMessage.enumValue.z{"enumValue": 12345}zFailed to parse optionalNestedEnum field: Invalid enum value 12345 for enum type protobuf_unittest.TestAllTypes.NestedEnum at TestAllTypes.optionalNestedEnum.z{"optionalNestedEnum": 12345})	r   rC   r   r=   rH   r
   TestAllTypesrD   rE   r  r9   r9   r:   testParseEnumValuev  s$   z!JsonFormatTest.testParseEnumValuec                 C   s   t  }d}t|| | |jd d}t|| | |jd d}t|| | |jd d}t|| | |jd d S )Nz{"bytesValue": "-_"}   z{"bytesValue": "AQI="}s   z{"bytesValue": "AQI"}z{"bytesValue": "AQI*"})r   rC   r   r=   r?   r#   r  r9   r9   r:   	testBytes  s   zJsonFormatTest.testBytesc                 C   s   |  dd |  dd d S )Nz{int32Value: 1}QFailed to load JSON: Expecting property name( enclosed in double quotes)?: line 1{"unknownName": 1}zTMessage type "proto3.TestMessage" has no field named "unknownName" at "TestMessage".rj   rk   r9   r9   r:   testParseBadIdentifer  s   z$JsonFormatTest.testParseBadIdentiferc                 C   s<   d}t  }tj||dd d}t  }tj||dd d S )Nr   T)ignore_unknown_fieldszl{
  "repeatedValue": [ {
    "@type": "type.googleapis.com/proto3.MessageType",
    "unknownName": 1
  }]
}
)r   rC   r   r=   r   )r8   rF   rA   r9   r9   r:   testIgnoreUnknownField  s   z%JsonFormatTest.testIgnoreUnknownFieldc                 C   ri   )Nz!{"int32Value": 1,
"int32Value":2}z.Failed to load JSON: duplicate key int32Value.rj   rk   r9   r9   r:   testDuplicateField  rm   z!JsonFormatTest.testDuplicateFieldc                 C   s   |  dd |  dd d S )Nz{"boolValue": 1}z`Failed to parse boolValue field: Expected true or false without quotes at TestMessage.boolValue.z{"boolValue": "true"}rj   rk   r9   r9   r:   testInvalidBoolValue  s   z#JsonFormatTest.testInvalidBoolValuec                 C   sx   t  }d}| tjtj|| | dd | dd | dd | dd	 | d
d | dd | dd d S )Nz{"int32Value": 0x12345}z{"int32Value": 1.5}zXFailed to parse int32Value field: Couldn't parse integer: 1.5 at TestMessage.int32Value.z{"int32Value": 012345}z9Failed to load JSON: Expecting \'?,\'? delimiter: line 1.z{"int32Value": " 1 "}zZFailed to parse int32Value field: Couldn't parse integer: " 1 " at TestMessage.int32Value.z{"int32Value": "1 "}zYFailed to parse int32Value field: Couldn't parse integer: "1 " at TestMessage.int32Value.z{"int32Value": false}zqFailed to parse int32Value field: Bool value False is not acceptable for integer field at TestMessage.int32Value.z${"int32Value": 12345678901234567890}zKFailed to parse int32Value field: Value out of range: 12345678901234567890.z{"uint32Value": -1}z:Failed to parse uint32Value field: Value out of range: -1.)r   rC   r   r   rE   r=   rH   r  r9   r9   r:   testInvalidIntegerValue  s6   z&JsonFormatTest.testInvalidIntegerValuec                 C   sX   |  dd |  dd |  dd |  dd |  d	d
 |  dd |  dd d S )Nz{"floatValue": "nan"}zjFailed to parse floatValue field: Couldn't parse float "nan", use "NaN" instead at TestMessage.floatValue.z{"floatValue": NaN}zOFailed to parse floatValue field: Couldn't parse NaN, use quoted "NaN" instead.z{"floatValue": Infinity}zlFailed to parse floatValue field: Couldn't parse Infinity or value too large, use quoted "Infinity" instead.z{"floatValue": -Infinity}znFailed to parse floatValue field: Couldn't parse -Infinity or value too small, use quoted "-Infinity" instead.z{"doubleValue": -1.89769e+308}zoFailed to parse doubleValue field: Couldn't parse -Infinity or value too small, use quoted "-Infinity" instead.z{"floatValue": 3.4028235e+39}z8Failed to parse floatValue field: Float value too large.z{"floatValue": -3.502823e+38}z8Failed to parse floatValue field: Float value too small.rj   rk   r9   r9   r:   testInvalidFloatValue  s*   z$JsonFormatTest.testInvalidFloatValuec                 C   ri   )Nz{"repeatedInt32Value": 12345}zzFailed to parse repeatedInt32Value field: repeated field repeatedInt32Value must be in \[\] which is 12345 at TestMessage.rj   rk   r9   r9   r:   testInvalidRepeated  s   z"JsonFormatTest.testInvalidRepeatedc                 C   s   t  }d}| tjdtj|| d}| tjdtj|| d}| tjdtj|| d}| tjdtj|| d	}| tjd
tj|| d S )Nz!{"int32Map": {"null": 2, "2": 3}}z/Failed to parse int32Map field: invalid literalz{"int32Map": {1: 2, "2": 3}}r  z{"boolMap": {"null": 1}}zZFailed to parse boolMap field: Expected "true" or "false", not null at TestMap.boolMap.keyz${"stringMap": {"a": 3, "\u0061": 2}}z$Failed to load JSON: duplicate key az{"stringMap": 0}zhFailed to parse stringMap field: Map field string_map must be in a dict which is 0 at TestMap.stringMap.)r   TestMaprD   r   rE   r=   r  r9   r9   r:   testInvalidMap  s:   zJsonFormatTest.testInvalidMapc                 C   s   t  }d}| tjdtj|| d}| tjdtj|| d}| tjdtj|| d}| tjdtj|| d	|j_| t	d
tj
| d}| tj}t|| W d    n1 s^w   Y  | dt|j d S )Nz%{"value": "10000-01-01T00:00:00.00Z"}zFailed to parse value field: time data '10000-01-01T00:00:00' does not match format '%Y-%m-%dT%H:%M:%S' at TestTimestamp.value.z/{"value": "1970-01-01T00:00:00.0123456789012Z"}z2nanos 0123456789012 more than 9 fractional digits.z&{"value": "1972-01-01T01:00:00.01+08"}z$Invalid timezone offset value: \+08.z!{"value": "0000-01-01T00:00:00Z"}z7Failed to parse value field: year (0 )?is out of range.l   A zdate value out of rangez!{"value": "0001-01-01t00:00:00Z"}zFailed to parse value field: time data '0001-01-01t00:00:00' does not match format '%Y-%m-%dT%H:%M:%S', lowercase 't' is not accepted at TestTimestamp.value.)r   r   assertRaisesRegexpr   rE   r=   rD   r%   r   OverflowErrorr>   r   r?   rc   r   )r8   r6   rF   er9   r9   r:   testInvalidTimestamp  sF   z#JsonFormatTest.testInvalidTimestampc                 C   &   t  }d}| tjdtj|| d S )Nz/{"oneofInt32Value": 1, "oneofStringValue": "2"}zcMessage type "proto3.TestOneof" should not have multiple "oneof_value" oneof fields at "TestOneof".)r   r   r+  r   rE   r=   r  r9   r9   r:   testInvalidOneof8  s   zJsonFormatTest.testInvalidOneofc              	   C   sZ   t  }d}| tjdtj|| G dd dt}| tjdtjddd| igi| d S )	N{"value": 1234}z\Failed to parse value field: ListValue must be in \[\] which is 1234 at TestListValue.value.c                   @      e Zd Zdd ZdS )z9JsonFormatTest.testInvalidListValue.<locals>.UnknownClassc                 S      dS Nvr9   rk   r9   r9   r:   __str__J     zAJsonFormatTest.testInvalidListValue.<locals>.UnknownClass.__str__N)rI   rJ   rK   r6  r9   r9   r9   r:   UnknownClassH      r8  z" at TestListValue.value\[1\].fake.r%   r   fake)r   r   rD   r   rE   r=   objectro   )r8   r6   rF   r8  r9   r9   r:   testInvalidListValue@  s   z#JsonFormatTest.testInvalidListValuec                 C   r/  )Nr1  zWFailed to parse value field: Struct must be in a dict which is 1234 at TestStruct.value)r   r   rD   r   rE   r=   r  r9   r9   r:   testInvalidStructR  s   z JsonFormatTest.testInvalidStructc                 C   r/  )N{"value": {"foo": 123}}z1Timestamp JSON value not a string: {u?'foo': 123})r   r   rD   r   rE   r=   r  r9   r9   r:   testTimestampInvalidStringValueZ     z.JsonFormatTest.testTimestampInvalidStringValuec                 C   r/  )Nr>  z0Duration JSON value not a string: {u?'foo': 123})r   r   rD   r   rE   r=   r  r9   r9   r:   testDurationInvalidStringValueb  s   z-JsonFormatTest.testDurationInvalidStringValuec                 C   r/  )Nr>  z1FieldMask JSON value not a string: {u?'foo': 123})r   r   rD   r   rE   r=   r  r9   r9   r:   testFieldMaskInvalidStringValuei  r@  z.JsonFormatTest.testFieldMaskInvalidStringValuec                 C   sh   t  }d}| tdtj|| d}| tjdtj|| d}| tjdtj|| d}t|| d S )Nz;{"@type": "type.googleapis.com/google.protobuf.Int32Value"}r%   r1  z0@type is missing when parsing any message at Anyz?{"@type": "type.googleapis.com/MessageNotExist", "value": 1234}zWCan not find message descriptor by type_url: type.googleapis.com/MessageNotExist at AnyzN{"@type": "incorrect.googleapis.com/google.protobuf.Int32Value","value": 1234})r   ry   rD   KeyErrorr   r=   rE   r  r9   r9   r:   testInvalidAnyq  s    zJsonFormatTest.testInvalidAnyc                 C   s   t  }d|_| dt| | dt|dd t  }| dt|dd | dt|dd t  }td| | d	|j td
| | d|j d S )N90  z{
  "int32Value": 12345
}z{
  "int32_value": 12345
}FTz{
  "repeatedValue": []
}z{
  "repeated_value": []
}z{"int32Value": 54321}i1  z{"int32_value": 12345})r   rC   r   r?   r   r>   r   r=   r7   r9   r9   r:   testPreservingProtoFieldNames  s(   z,JsonFormatTest.testPreservingProtoFieldNamesc                 C   s(   t  }d|_| dtj|dd d S )NrE  z{
"int32Value": 12345
}r   )indent)r   rC   r   r?   r   r>   r7   r9   r9   r:   
testIndent  s
   zJsonFormatTest.testIndentc              	   C   sR   t  }t j|_|jt j |jt j | t	dt	t
j|dd d S )Nz4{
  "enumValue": 1,
  "repeatedEnumValue": [0, 1]
}
T)use_integers_for_enums)r   rC   r&   r'   r4   r)   r5   r?   rS   rT   r   r>   r7   r9   r9   r:   testFormatEnumsAsInts  s   z$JsonFormatTest.testFormatEnumsAsIntsc                 C   s2   d}d|i}t  }t|| | ||j d S NrE  rv   )r   rC   r   ro   r?   r   )r8   expectedjs_dictr6   r9   r9   r:   testParseDict  s
   zJsonFormatTest.testParseDictc                 C   s   ddddi}t |t  ddddi}| t j}t }t j|t |d W d    n1 s5w   Y  | t	|j
d d S )Nr   z&type.googleapis.com/proto3.MessageTyper   )rw   r%   r   zFailed to parse any_value field: Can not find message descriptor by type_url: type.googleapis.com/proto3.MessageType at TestAny.any_value.)r   ro   r   r   r   rE   r   r   r?   rc   r   )r8   rM  r   r   r9   r9   r:   )testParseDictAnyDescriptorPoolMissingType  s,   z8JsonFormatTest.testParseDictAnyDescriptorPoolMissingTypec                 C   s8   G dd dt }t }| tjdtjd| i| d S )Nc                   @   r2  )zBJsonFormatTest.testParseDictUnknownValueType.<locals>.UnknownClassc                 S   r3  r4  r9   rk   r9   r9   r:   __repr__  r7  zKJsonFormatTest.testParseDictUnknownValueType.<locals>.UnknownClass.__repr__N)rI   rJ   rK   rP  r9   r9   r9   r:   r8    r9  r8  z7Value v has unexpected type <class '.*\.UnknownClass'>.r%   )r;  r   r   rD   r   rE   ro   )r8   r8  r6   r9   r9   r:   testParseDictUnknownValueType  s   z,JsonFormatTest.testParseDictUnknownValueTypec                 C   s,   t  }d|_ddi}| |t| d S rK  )r   rC   r   r?   r   rn   )r8   r6   rL  r9   r9   r:   testMessageToDict  s   z JsonFormatTest.testMessageToDictc                 C   s8   t  }d|_| dt| t  }| || d S )NrE  z{
  "@value": 12345
})r   TestCustomJsonNamer%   r?   r   r>   rB   r@   r9   r9   r:   testJsonName  s   zJsonFormatTest.testJsonNamec              
   C   sF   t jdddddd}| tj|ddtjdddddd	d
dd d S )NTr   r   rQ   bla)r!   r   r   r   r"   )	sort_keys3)	boolValuerv   
int64Valueuint32ValuestringValuer   )rG  rV  )r   rC   r?   r   r>   rS   rx   r7   r9   r9   r:   testSortKeys  s   
zJsonFormatTest.testSortKeysc                 C   s6   t  }| jtjdtjd|dd tjd|dd d S )Nz*Message too deep. Max recursion depth is 3z${"child": {"child": {"child" : {}}}}r   )max_recursion_depthz&{"payload": {}, "child": {"child":{}}})r
   NestedTestAllTypesrD   r   rE   r=   r7   r9   r9   r:   testNestedRecursiveLimit  s   
z'JsonFormatTest.testNestedRecursiveLimitN)BrI   rJ   rK   rN   rU   rW   rX   rg   rl   rq   ru   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  r  r!  r#  r$  r%  r&  r'  r(  r*  r.  r0  r<  r=  r?  rA  rB  rD  rF  rH  rJ  rN  rO  rQ  rR  rT  r\  r_  r9   r9   r9   r:   rL   k   s    
!

	(((.O-	&	%rL   __main__)__doc__
__author__rS   r  r  unittestgoogle.protobufr   r   r   r   r   r   r   r	   r
   google.protobuf.internalr   r   r   google.protobuf.utilr   r   TestCaser   rL   rI   mainr9   r9   r9   r:   <module>   sD   2         "