U
    4JeüĪ  ć                   @   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re ”  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ŚappendZrepeated_int64_valueZrepeated_uint32_valueZrepeated_uint64_valueZrepeated_float_valueŚrepeated_double_valueŚrepeated_bool_valueZrepeated_string_valueZrepeated_bytes_valueZrepeated_message_valueŚaddŚrepeated_enum_valueŚFOOŚmessage©Śselfr/   © r2   śM/tmp/pip-unpacked-wheel-0oacnh65/google/protobuf/internal/json_format_test.pyŚFillAllFields;   sB    
zJsonFormatBase.FillAllFieldsc                 C   s"   t  t  |”|” |  ||” d S ©N)r   ŚParseŚMessageToJsonŚassertEqual©r1   r/   Śparsed_messager2   r2   r3   ŚCheckParseBack`   s    ’zJsonFormatBase.CheckParseBackc                 C   s"   t  ” }|  tj|tj||” d S r5   )r   ŚTestMessageŚassertRaisesRegexr   Ś
ParseErrorr6   )r1   ŚtextŚerror_messager/   r2   r2   r3   Ś
CheckErrore   s    
  ’zJsonFormatBase.CheckErrorN)Ś__name__Ś
__module__Ś__qualname__r4   r;   rA   r2   r2   r2   r3   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   r<   r8   r   r7   r;   r9   r2   r2   r3   ŚtestEmptyMessageToJsonm   s    ’z%JsonFormatTest.testEmptyMessageToJsonc                 C   sH   t jdddgd}|  t t |””t d”” t  ” }|  ||” d S )NŚtestéY   é   )r"   r(   z6{"stringValue": "test", "repeatedInt32Value": [89, 4]}©r   r<   r8   ŚjsonŚloadsr   r7   r;   r9   r2   r2   r3   Ś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   r<   r4   r8   rL   rM   r   r7   r6   ©r1   r/   r?   r:   r2   r2   r3   ŚtestAllFieldsToJson~   s    
žz"JsonFormatTest.testAllFieldsToJsonc                 C   sH   d}t  ” }d|_|  t |”|” t  ” }t ||” |  ||” d S )Nz{
  "enumValue": 999
}iē  )r   r<   r'   r8   r   r7   r6   )r1   r?   r/   r:   r2   r2   r3   ŚtestUnknownEnumToJsonAndBack   s    ’z+JsonFormatTest.testUnknownEnumToJsonAndBackc                 C   sb   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   r7   r6   r8   )r1   r/   Śext1Śext2Śmessage_textr:   r2   r2   r3   ŚtestExtensionToJsonAndBack©   s    ’z)JsonFormatTest.testExtensionToJsonAndBackc                 C   s   |   dd” d S )Nz{"[extensionField]": {}}z8Message type proto3.TestMessage does not have extensions©rA   ©r1   r2   r2   r3   ŚtestExtensionErrors¶   s    ’z"JsonFormatTest.testExtensionErrorsc                 C   sb   t  ” }t jj}t jj}d|jj| _d|jj| _t	 
|”}t  ” }t	 ||” |  ||” d S rR   )r	   rT   rU   rV   rW   rX   rY   rZ   r[   r   ŚMessageToDictŚ	ParseDictr8   )r1   r/   r\   r]   Śmessage_dictr:   r2   r2   r3   ŚtestExtensionToDictAndBackŗ   s    ’z)JsonFormatTest.testExtensionToDictAndBackc                 C   sH   t  ” }t jj}d|j|< t |”}t  ” }t ||” |  ||” d S )NŚdata)	r
   ZTestAllExtensionsZTestNestedExtensionrH   rY   r   rc   rd   r8   )r1   r/   r\   re   r:   r2   r2   r3   Ś$testExtensionToDictAndBackWithScalarĒ   s    
’z3JsonFormatTest.testExtensionToDictAndBackWithScalarc                 C   s>   ddd}t  t  |””}t ” }t ||” |  ||” d S )Nr   z&type.googleapis.com/proto3.TestMessage)Ś
int32Valueś@type)rL   rM   Śdumpsr   ŚAnyr   rd   r8   )r1   Z	orig_dictZcopied_dictr:   r2   r2   r3   Ś'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.
    rS   r   Z
messageSetrZ   r[   )zB[protobuf_unittest.TestMessageSetExtension1.message_set_extension]zB[protobuf_unittest.TestMessageSetExtension2.message_set_extension]N)r	   rT   rU   rV   rW   rX   rY   rZ   r[   r   rc   r8   rd   )r1   r/   r\   r]   re   Zgolden_dictZ
parsed_msgr2   r2   r3   Ś/testExtensionSerializationDictMatchesProto3SpecÜ   s*    ’ ’ ’ś’z>JsonFormatTest.testExtensionSerializationDictMatchesProto3Specc                 C   sB   t  ” }t jj}d|j| _t |”}dddii}|  ||” dS )rn   Śstuffz%[protobuf_unittest.TestExtension.ext]r%   N)	r   ZTestMessageWithExtensionZTestExtensionŚextrY   r%   r   rc   r8   )r1   r/   rq   re   Śexpected_dictr2   r2   r3   Ś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 )rn   rS   r   z@protobuf_unittest.TestMessageSetExtension1.message_set_extensionz@protobuf_unittest.TestMessageSetExtension2.message_set_extensionzZ{"messageSet": {    "[%s]": {        "i": 23    },    "[%s]": {        "str": "foo"    }}}N)r	   rT   rU   rV   rW   rX   rY   rZ   r[   r   r7   r8   rL   rM   )r1   r/   r\   r]   r^   Z	ext1_textZ	ext2_textZgolden_textr2   r2   r3   Ś/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   r<   r"   Śdecoder8   r   r7   r;   r6   r   ©r1   r/   r:   r?   r2   r2   r3   Ś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": []}rK   r9   r2   r2   r3   Ś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)Zincluding_default_value_fieldsrF   r   z{"optionalInt32": 0})r   ZTestProto3Optionalr8   rL   rM   r   r7   Zoptional_int32r0   r2   r2   r3   Ś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}rv   )r   r<   r   r6   r8   r   r0   r2   r2   r3   Ś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": {}}rv   é   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   ZTestNestedMapr8   rL   rM   r   r7   Zbool_mapZ	int32_mapZ	int64_mapZ
uint32_mapZ
uint64_mapZ
string_mapZmap_mapr;   r9   r2   r2   r3   ŚtestMapFieldsd  s0    ž











žzJsonFormatTest.testMapFieldsc                 C   sN   t  ” }|  t |d”d” d|_|  t |d”d” t  ” }|  ||” d S )NTrF   r   z{
  "oneofInt32Value": 0
})r   Ś	TestOneofr8   r   r7   Zoneof_int32_valuer;   r9   r2   r2   r3   Ś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   šru   Śstrictz{"stringValue": "\uD83D"}z*Invalid \\uXXXX escape|Unpaired.*surrogatez{"stringValue": "\uDE01"})r   r<   r   r6   r8   r"   rw   rA   r0   r2   r2   r3   Ś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   rv   i'  i įõr}   l   Ačė i’É;r~   lż’’’ w#o9 rJ   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"]}i’’i g     āŻĄit  )r   ŚTestTimestampr%   ŚsecondsŚnanosŚrepeated_valuer,   r8   rL   rM   r   r7   r;   r6   rx   r2   r2   r3   Ś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 )Nrv   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   r,   r   r8   rL   rM   r   r7   r;   r9   r2   r2   r3   Ś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)   r8   r   r7   r;   ŚClearr9   r2   r2   r3   Ś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   ZTestWrapperr!   r%   r   r"   r#   r+   r,   r(   r8   rL   rM   r   r7   r;   r9   r2   r2   r3   Ś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”}| ddddd g” d| ” d< d|j ” d< |j ”  |  	t
 t |d””t
 d”” t  ” }|  ||” |jd  |jd  d S )NŚJimŚnamer   ZageTZattendŚemailZSFOŚaddressZcityi   Zhouse_numberZempty_structZ
empty_listŚlisté   ZsevenFé	   Zsubkey2r   a.  {  "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%   Zget_or_create_structZget_or_create_listŚextendZ
add_structr   r,   r8   rL   rM   r   r7   r;   )r1   r/   Zstruct_listr:   r2   r2   r3   Ś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Śhelloē333333&@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   r,   Śnumber_valuer!   r£   r8   rL   rM   r   r7   r;   r   r6   Z
WhichOneofr9   r2   r2   r3   Ś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   rv   FzS{"value": [11.1, null, true, "hello", {"name": "Jim"}]
,"repeatedValue": [[1], []]})r   ŚTestListValuer%   Śvaluesr,   r„   r£   r!   r"   Zstruct_valuer   r8   rL   rM   r   r7   r;   r9   r2   r2   r3   Ś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   Zoneof_null_valuer8   r   r7   r;   r6   )r1   r/   r:   Znew_messager2   r2   r3   Ś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   ŚTestAnyZMessageTyper%   ŚPackr   r,   r8   rL   rM   r   r7   r;   r<   r   )r1   r/   Zvalue1Zvalue2r:   Ztest_messager2   r2   r3   ŚtestAnyMessage  s@    
’žłžzJsonFormatTest.testAnyMessagec              	   C   sf   t  ” }d|_t ” }|j |” t ” }|  	t
”}tj|d|d W 5 Q R X |  dt|j” d S )NŚstringTr   z\Can not find message descriptor by type_url: type.googleapis.com/protobuf_unittest.OneString)r
   Z	OneStringrg   r   r®   Ś	any_valuerÆ   r   ŚDescriptorPoolŚassertRaisesŚ	TypeErrorr   r7   r8   r[   Ś	exception)r1   Zpacked_messager/   Ś
empty_poolŚcmr2   r2   r3   Ś'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"
}
rv   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   rl   r   Z
Int32Valuer%   rÆ   r8   rL   rM   r   r7   r;   r   Z	Timestampr   ZDurationr   r   Z	FieldMaskr   r)   r   ŚStruct)	r1   r/   r   r:   Ś	timestampŚdurationZ
field_maskZstruct_messageZ
nested_anyr2   r2   r3   Ś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": {}}zFailed 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]}zFailed to parse repeatedMessageValue field: null is not allowed to be used as an element in a repeated field at TestMessage.repeatedMessageValue\[0\].)r   r<   r4   r   r6   r8   ZassertFalseZHasFieldŚ
assertTruer=   r>   rA   r9   r2   r2   r3   ŚtestParseNull  s*    
é  üžzJsonFormatTest.testParseNullc                 C   sR   t  ” }td|_d}|  t |”|” t  ” }t ||” |  t	 
|j”” d S )NŚnanz{
  "floatValue": "NaN"
})r   r<   r   r   r8   r   r7   r6   r¾   ŚmathŚisnanrO   r2   r2   r3   Ś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   gūPļ’’#Hrv   gø Ž7z7{"repeatedFloatValue": [3.4028235e+39, 1.4028235e-39]
}zgFailed to parse repeatedFloatValue field: Float value too large at TestMessage.repeatedFloatValue\[0\].)r   r<   r   r6   r8   r*   rA   ©r1   r/   r?   r2   r2   r3   Ś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 |”|” ddddg}t  ” }tddD ]J}||d< tddD ]2}||d< t|}t 	d|”d |_|  
||” qŹq“d S )Ng6sÓ­łń?z{
  "floatValue": 1.1234568
}é   )Zfloat_precisionz{
  "floatValue": 1.123457
}é   g1åń?z{
  "floatValue": 1.1
}gÉDu8z"{
  "floatValue": 1.00000075e-36
}g©óąVšDz {
  "floatValue": 1.234568e+24
}r   éŲ   én   é   r~   z<f)r   r<   r   r8   r   r7   ŚrangeŚ	bytearrayŚstructŚunpackr;   )r1   r/   r?   Z
value_listZmsg2ŚaŚbZ
byte_arrayr2   r2   r3   ŚtestFloatPrecisionL  sN     ’ ’ ’ ’ ’z!JsonFormatTest.testFloatPrecisionc                 C   s   |   dd” d S )Nr   z1Failed to load JSON: (Expecting value)|(No JSON).r`   ra   r2   r2   r3   ŚtestParseEmptyTextr  s    ’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}zFailed to parse optionalNestedEnum field: Invalid enum value 12345 for enum type protobuf_unittest.TestAllTypes.NestedEnum at TestAllTypes.optionalNestedEnum.z{"optionalNestedEnum": 12345})	r   r<   r   r6   rA   r
   ZTestAllTypesr=   r>   rÄ   r2   r2   r3   Ś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   r<   r   r6   r8   r#   rÄ   r2   r2   r3   Ś	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".r`   ra   r2   r2   r3   ŚtestParseBadIdentifer  s    ’žz$JsonFormatTest.testParseBadIdentiferc                 C   s<   d}t  ” }tj||dd d}t  ” }tj||dd d S )Nr×   T)Zignore_unknown_fieldszl{
  "repeatedValue": [ {
    "@type": "type.googleapis.com/proto3.MessageType",
    "unknownName": 1
  }]
}
)r   r<   r   r6   r®   )r1   r?   r:   r2   r2   r3   ŚtestIgnoreUnknownField¦  s    z%JsonFormatTest.testIgnoreUnknownFieldc                 C   s   |   dd” d S )Nz!{"int32Value": 1,
"int32Value":2}z.Failed to load JSON: duplicate key int32Value.r`   ra   r2   r2   r3   ŚtestDuplicateField³  s    ’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"}r`   ra   r2   r2   r3   Ś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   r<   r“   r   r>   r6   rA   rÄ   r2   r2   r3   ŚtestInvalidIntegerValueæ  s@      ’ ’’ ’ ’ž’’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.r`   ra   r2   r2   r3   ŚtestInvalidFloatValueŪ  s,     ’’’’’’’z$JsonFormatTest.testInvalidFloatValuec                 C   s   |   dd” d S )Nz{"repeatedInt32Value": 12345}zzFailed to parse repeatedInt32Value field: repeated field repeatedInt32Value must be in \[\] which is 12345 at TestMessage.r`   ra   r2   r2   r3   Ś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   ZTestMapr=   r   r>   r6   rÄ   r2   r2   r3   ŚtestInvalidMapų  sL      ž  ż  ż  ž ü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 5 Q R X |  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"}zFailed 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   r>   r6   r=   r%   r   ŚOverflowErrorr7   r“   r8   r[   r¶   )r1   r/   r?   Śer2   r2   r3   ŚtestInvalidTimestamp  sX       ü  ż  ž  ż ’üz#JsonFormatTest.testInvalidTimestampc                 C   s&   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   r>   r6   rÄ   r2   r2   r3   Ś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                   @   s   e Zd Zdd ZdS )z9JsonFormatTest.testInvalidListValue.<locals>.UnknownClassc                 S   s   dS ©NŚvr2   ra   r2   r2   r3   Ś__str__J  s    zAJsonFormatTest.testInvalidListValue.<locals>.UnknownClass.__str__N)rB   rC   rD   rč   r2   r2   r2   r3   ŚUnknownClassH  s   ré   z" at TestListValue.value\[1\].fake.r%   r    Zfake)r   r§   r=   r   r>   r6   Śobjectrd   )r1   r/   r?   ré   r2   r2   r3   ŚtestInvalidListValue@  s"      ż üz#JsonFormatTest.testInvalidListValuec                 C   s&   t  ” }d}|  tjdtj||” d S )Nrå   zWFailed to parse value field: Struct must be in a dict which is 1234 at TestStruct.value)r   r   r=   r   r>   r6   rÄ   r2   r2   r3   ŚtestInvalidStructR  s      żz JsonFormatTest.testInvalidStructc                 C   s&   t  ” }d}|  tjdtj||” d S )Nś{"value": {"foo": 123}}z1Timestamp JSON value not a string: {u?'foo': 123})r   r   r=   r   r>   r6   rÄ   r2   r2   r3   ŚtestTimestampInvalidStringValueZ  s      żz.JsonFormatTest.testTimestampInvalidStringValuec                 C   s&   t  ” }d}|  tjdtj||” d S )Nrķ   z0Duration JSON value not a string: {u?'foo': 123})r   r   r=   r   r>   r6   rÄ   r2   r2   r3   ŚtestDurationInvalidStringValueb  s      žz-JsonFormatTest.testDurationInvalidStringValuec                 C   s&   t  ” }d}|  tjdtj||” d S )Nrķ   z1FieldMask JSON value not a string: {u?'foo': 123})r   r   r=   r   r>   r6   rÄ   r2   r2   r3   ŚtestFieldMaskInvalidStringValuei  s      ż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%   rå   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   rl   r=   ŚKeyErrorr   r6   r>   rÄ   r2   r2   r3   Ś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 )Né90  z{
  "int32Value": 12345
}z{
  "int32_value": 12345
}FTz{
  "repeatedValue": []
}z{
  "repeated_value": []
}z{"int32Value": 54321}i1Ō  z{"int32_value": 12345})r   r<   r   r8   r   r7   r   r6   r0   r2   r2   r3   ŚtestPreservingProtoFieldNames  s(    ’’’’z,JsonFormatTest.testPreservingProtoFieldNamesc                 C   s(   t  ” }d|_|  dtj|dd” d S )Nró   z{
"int32Value": 12345
}r   )Śindent)r   r<   r   r8   r   r7   r0   r2   r2   r3   Ś
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)Zuse_integers_for_enums)r   r<   r&   r'   r-   r)   r.   r8   rL   rM   r   r7   r0   r2   r2   r3   ŚtestFormatEnumsAsInts  s     ’üz$JsonFormatTest.testFormatEnumsAsIntsc                 C   s2   d}d|i}t  ” }t ||” |  ||j” d S ©Nró   ri   )r   r<   r   rd   r8   r   )r1   ŚexpectedŚjs_dictr/   r2   r2   r3   ŚtestParseDictŖ  s
    zJsonFormatTest.testParseDictc              	   C   sv   ddddi}t  |t ” ” ddddi}|  t j”"}t ” }t j|t ” |d W 5 Q R X |  t	|j
d” d S )Nr²   z&type.googleapis.com/proto3.MessageTyper«   )rj   r%   r   zFailed to parse any_value field: Can not find message descriptor by type_url: type.googleapis.com/proto3.MessageType at TestAny.any_value.)r   rd   r   r®   r“   r>   r   r³   r8   r[   r¶   )r1   rś   rø   r·   r2   r2   r3   Ś)testParseDictAnyDescriptorPoolMissingType±  s*    ž’ž’žžz8JsonFormatTest.testParseDictAnyDescriptorPoolMissingTypec                 C   s8   G dd dt }t ” }|  tjdtjd| i|” d S )Nc                   @   s   e Zd Zdd ZdS )zBJsonFormatTest.testParseDictUnknownValueType.<locals>.UnknownClassc                 S   s   dS rę   r2   ra   r2   r2   r3   Ś__repr__Š  s    zKJsonFormatTest.testParseDictUnknownValueType.<locals>.UnknownClass.__repr__N)rB   rC   rD   rż   r2   r2   r2   r3   ré   Ī  s   ré   z7Value v has unexpected type <class '.*\.UnknownClass'>.r%   )rź   r   r¤   r=   r   r>   rd   )r1   ré   r/   r2   r2   r3   ŚtestParseDictUnknownValueTypeĶ  s      żz,JsonFormatTest.testParseDictUnknownValueTypec                 C   s,   t  ” }d|_ddi}|  |t |”” d S rų   )r   r<   r   r8   r   rc   )r1   r/   rł   r2   r2   r3   ŚtestMessageToDictŲ  s    ’z JsonFormatTest.testMessageToDictc                 C   s8   t  ” }d|_|  dt |”” t  ” }|  ||” d S )Nró   z{
  "@value": 12345
})r   ZTestCustomJsonNamer%   r8   r   r7   r;   r9   r2   r2   r3   Ś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 )NTrv   r~   rJ   Zbla)r!   r   r   r   r"   )Ś	sort_keysŚ3)Z	boolValueri   Z
int64ValueZuint32ValueZstringValuer}   )rõ   r  )r   r<   r8   r   r7   rL   rk   r0   r2   r2   r3   Ś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~   )Zmax_recursion_depthz&{"payload": {}, "child": {"child":{}}})r
   ZNestedTestAllTypesr=   r   r>   r6   r0   r2   r2   r3   ŚtestNestedRecursiveLimit÷  s    ś ’z'JsonFormatTest.testNestedRecursiveLimitN)BrB   rC   rD   rG   rN   rP   rQ   r_   rb   rf   rh   rm   ro   rs   rt   ry   rz   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š   rņ   rō   rö   r÷   rū   rü   rž   r’   r   r  r  r2   r2   r2   r3   rE   k   s~   
!

	(((.O-	&	%rE   Ś__main__)Ś__doc__Ś
__author__rL   rĮ   rĶ   ZunittestZgoogle.protobufr   r   r   r   r   r   r   r	   r
   Zgoogle.protobuf.internalr   r   r   Zgoogle.protobuf.utilr   r   ZTestCaser   rE   rB   Śmainr2   r2   r2   r3   Ś<module>   s@   2         "
