o
    ŽĖ·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 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   ś`/var/www/ideatree/venv/lib/python3.10/site-packages/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ŚtestéY   é   )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"]}i’’i 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Ś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¬   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": {}}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   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   gūPļ’’#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 )Ng6sÓ­łń?z{
  "floatValue": 1.1234568
}é   )Ś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   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}zFailed 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"}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   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 )Né90  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   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   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         "’