<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
3
hġ                 @   s  d Z dZddlZddlZddlZyddlZW n ek
rH   ddlZY nX 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j  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)unittest_mset_pb2)unittest_pb2)well_known_types)json_format)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jd
 |jjd |jjd |jjd |jjd |jjd |jjd |jjd |jjd |jjd |jjtd |jjd |jjd |jjd |jjd |jjd |jjd d	|jj _d|jj _|jjtj |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   ii   l       )int32_valueint64_valueuint32_valueuint64_valuefloatfloat_valuedouble_value
bool_valuestring_valuebytes_valuemessage_valuevaluer   ZBAR
enum_valuerepeated_int32_valueappendZrepeated_int64_valueZrepeated_uint32_valueZrepeated_uint64_valueZrepeated_float_valueZrepeated_double_valuerepeated_bool_valueZrepeated_string_valueZrepeated_bytes_valueZrepeated_message_valueaddZrepeated_enum_valueZFOOmessage)selfr%    r'   &/usr/lib/python3.6/json_format_test.pyFillAllFields<   sB    
zJsonFormatBase.FillAllFieldsc             C   s"   t jt j|| | j|| d S )N)r   ParseMessageToJsonassertEqual)r&   r%   parsed_messager'   r'   r(   CheckParseBacka   s    zJsonFormatBase.CheckParseBackc             C   s"   t j }| jtj|tj|| d S )N)r   TestMessageassertRaisesRegexr   
ParseErrorr*   )r&   textZerror_messager%   r'   r'   r(   
CheckErrorf   s
    zJsonFormatBase.CheckErrorN)__name__
__module____qualname__r)   r.   r3   r'   r'   r'   r(   r   :   s   %r   c               @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%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` Z2daS )bJsonFormatTestc             C   s2   t j }| jtj|d t j }| j|| d S )Nz{})r   r/   r,   r   r+   r.   )r&   r%   r-   r'   r'   r(   testEmptyMessageToJsonp   s
    z%JsonFormatTest.testEmptyMessageToJsonc             C   sH   t jdddgd}| jtjtj|tjd t j }| j|| d S )NZtestY      )r   r!   z6{"stringValue": "test", "repeatedInt32Value": [89, 4]})r   r/   r,   jsonloadsr   r+   r.   )r&   r%   r-   r'   r'   r(   testPartialMessageToJsonw   s    z'JsonFormatTest.testPartialMessageToJsonc             C   sX   t j }d}| j| | jtjtj|tj| t j }tj|| | j|| 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/   r)   r,   r;   r<   r   r+   r*   )r&   r%   r2   r-   r'   r'   r(   testAllFieldsToJson   s    
z"JsonFormatTest.testAllFieldsToJsonc             C   sH   d}t j }d|_| jtj|| t j }tj|| | j|| d S )Nz{
  "enumValue": 999
}i  )r   r/   r    r,   r   r+   r*   )r&   r2   r%   r-   r'   r'   r(   testUnknownEnumToJsonAndBack   s    z+JsonFormatTest.testUnknownEnumToJsonAndBackc             C   sb   t j }t jj}t jj}d|jj| _d|jj| _t	j
|}t j }t	j|| | j|| d S )N   r   )r   TestMessageSetContainerTestMessageSetExtension1message_set_extensionTestMessageSetExtension2message_set
Extensionsistrr   r+   r*   r,   )r&   r%   ext1ext2message_textr-   r'   r'   r(   testExtensionToJsonAndBack   s    z)JsonFormatTest.testExtensionToJsonAndBackc             C   s   | j dd d S )Nz{"[extensionField]": {}}z8Message type proto3.TestMessage does not have extensions)r3   )r&   r'   r'   r(   testExtensionErrors   s    z"JsonFormatTest.testExtensionErrorsc             C   sb   t j }t jj}t jj}d|jj| _d|jj| _t	j
|}t j }t	j|| | j|| d S )Nr@   r   )r   rA   rB   rC   rD   rE   rF   rG   rH   r   MessageToDict	ParseDictr,   )r&   r%   rI   rJ   message_dictr-   r'   r'   r(   testExtensionToDictAndBack   s    z)JsonFormatTest.testExtensionToDictAndBackc             C   sd   t j }t jj}t jj}d|jj| _d|jj| _t	j
|}dddiddidi}| j|| dS )z&See go/proto3-json-spec for spec.
    r@   r   Z
messageSetrG   rH   )z@[protobuf_unittest.TestMessageSetExtension1.messageSetExtension]z@[protobuf_unittest.TestMessageSetExtension2.messageSetExtension]N)r   rA   rB   rC   rD   rE   rF   rG   rH   r   rN   r,   )r&   r%   rI   rJ   rP   Zgolden_dictr'   r'   r(   /testExtensionSerializationDictMatchesProto3Spec   s    z>JsonFormatTest.testExtensionSerializationDictMatchesProto3Specc             C   sn   t j }t jj}t jj}d|jj| _d|jj| _t	j
|}d}d}d||f }| jtj|tj| dS )z&See go/proto3-json-spec for spec.
    r@   r   z>protobuf_unittest.TestMessageSetExtension1.messageSetExtensionz>protobuf_unittest.TestMessageSetExtension2.messageSetExtensionzZ{"messageSet": {    "[%s]": {        "i": 23    },    "[%s]": {        "str": "foo"    }}}N)r   rA   rB   rC   rD   rE   rF   rG   rH   r   r+   r,   r;   r<   )r&   r%   rI   rJ   rK   Z	ext1_textZ	ext2_textZgolden_textr'   r'   r(   /testExtensionSerializationJsonMatchesProto3Spec   s    
z>JsonFormatTest.testExtensionSerializationJsonMatchesProto3Specc             C   s   t j }tjd dk rd|_nd|_| jdjd7  _| jtj|d t j }| j	|| d}tj
|| | j|jd	 d S )
Nr      u   &
<">	\/â¨â©z&
<">	\/s     zutf-8z;{
  "stringValue": "&\n<\"\r>\b\t\f\\\u0001/\u2028\u2029"
}z{"int32Value": "1"}   )r   r/   sysversion_infor   decoder,   r   r+   r.   r*   r   )r&   r%   r-   r2   r'   r'   r(   testJsonEscapeString   s    z#JsonFormatTest.testJsonEscapeStringc             C   sD   t jdd}| jtjtj|dtjd t j }| j|| 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": []})r   r/   r,   r;   r<   r   r+   r.   )r&   r%   r-   r'   r'   r(   testAlwaysSeriliaze  s    z"JsonFormatTest.testAlwaysSeriliazec             C   sZ   t j }tjd| | j|jd tjd| | j|jd tjd| | j|jd d S )Nz{"int32Value": -2.147483648e9}l        z{"int32Value": 1e5}i z{"int32Value": 1.0}rU   i   )r   r/   r   r*   r,   r   )r&   r%   r'   r'   r(   testIntegersRepresentedAsFloat-  s    z-JsonFormatTest.testIntegersRepresentedAsFloatc             C   s   t j }| jtjtj|dt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< d|jd jd< | jtjtj|dtjd	 t j }| j|| d S )
NTzj{"boolMap": {},"int32Map": {},"int64Map": {},"uint32Map": {},"uint64Map": {},"stringMap": {},"mapMap": {}}rU      FrT   1Z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TestNestedMapr,   r;   r<   r   r+   Zbool_mapZ	int32_mapZ	int64_mapZ
uint32_mapZ
uint64_mapZ
string_mapZmap_mapr.   )r&   r%   r-   r'   r'   r(   testMapFields6  s,    	











	zJsonFormatTest.testMapFieldsc             C   sN   t j }| jtj|dd d|_| jtj|dd t j }| j|| d S )NTz{}r   z{
  "oneofInt32Value": 0
})r   	TestOneofr,   r   r+   Zoneof_int32_valuer.   )r&   r%   r-   r'   r'   r(   testOneofFields^  s    

zJsonFormatTest.testOneofFieldsc             C   sF   t j }tjd| | j|jdjdd | jdd | jdd d S )Nz{"stringValue": "\uD83D\uDE01"}s   😁zutf-8strictz{"stringValue": "\uD83D"}z*Invalid \\uXXXX escape|Unpaired.*surrogatez{"stringValue": "\uDE01"})r   r/   r   r*   r,   r   rX   r3   )r&   r%   r'   r'   r(   testSurrogatesm  s    zJsonFormatTest.testSurrogatesc             C   s   t j }d|j_d|j_d|jj _d|jd _d|jj _d|jd _d|jj _d|jd _d|jj _d|jd	 _d|jj _d|jd _| jtj	t
j|dtj	d t j }| j|| d}t
j|| | j|jjd | j|jjd | j|jd jd | j|jd jd d S )Nr   r   rU   i'  i r\   l   A iɚ;rT   l    w#o9 r:   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      !@r@   <   l w#o9 iig      !g     id  it  )r   TestTimestampr   secondsnanosrepeated_valuer$   r,   r;   r<   r   r+   r.   r*   )r&   r%   r-   r2   r'   r'   r(   testTimestampMessage~  s0    
z#JsonFormatTest.testTimestampMessagec             C   s   t j }d|j_d|jj _d|jd _d
|jj _d|jd _d|jj _d|jd _d|jj _d|jj _| jtj	t
j|dtj	d	 t j }| j|| d S )NrU   r   r   i  iا r\   l    s%Tzs{"value": "1s","repeatedValue": [  "0.000000010s",  "-1.000001s",  "10.011s",  "-315576000000s",  "315576000000s"]}il s%)r   ZTestDurationr   rf   rh   r$   rg   r,   r;   r<   r   r+   r.   )r&   r%   r-   r'   r'   r(   testDurationMessage  s    
z"JsonFormatTest.testDurationMessagec             C   sP   t j }|jjjd |jjjd | jtj|dd t j }| j|| d S )Nzfoo.barr   Tz{
  "value": "foo.bar,bar"
})	r   ZTestFieldMaskr   pathsr"   r,   r   r+   r.   )r&   r%   r-   r'   r'   r(   testFieldMaskMessage  s    
z#JsonFormatTest.testFieldMaskMessagec             C   s   t j }d|j_d|j_d|j_d|j_d|jj _d|jj _|j	j  | j
tjtj|dtjd t j }| j|| 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!   r,   r;   r<   r   r+   r.   )r&   r%   r-   r'   r'   r(   testWrapperMessage  s    
z!JsonFormatTest.testWrapperMessagec             C   s   t j }d|jd< d|jd< d|jd< d |jd< d|jjd	d
< d|jd	 d< |jjd}|jddddd g d|j d< d|jj d< |jj  | j	t
jtj|dt
jd t j }| j|| d S )NJimnamer   ZageTZattendZemailZSFOZaddressZcityi   Zhouse_numberlist   ZsevenF	   Zsubkey2r   a   {  "value": {    "address": {      "city": "SFO",       "house_number": 1024    },     "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_structrh   r$   r,   r;   r<   r   r+   r.   )r&   r%   Zstruct_listr-   r'   r'   r(   testStructMessage  s$    




z JsonFormatTest.testStructMessagec             C   s   t j }d|j_d|jj _d|jj _d|jj _| j	t
jtj|dt
jd t j }| j|| |jj  | j	t
jtj|dt
jd |j  tjd| | j	|jjdd	 d S )
Nhellog333333&@Fr   z;{  "value": "hello",  "repeatedValue": [11.1, false, null]}zA{  "value": "hello",  "repeatedValue": [11.1, false, null, null]}z{"value": null}Zkind
null_value)r   Z	TestValuer   r   rh   r$   number_valuer   rz   r,   r;   r<   r   r+   r.   Clearr*   Z
WhichOneof)r&   r%   r-   r'   r'   r(   testValueMessage
  s&    
zJsonFormatTest.testValueMessagec             C   s   t j }d|jjj _d|jjj _d|jjj _d|jjj _d|jjj j	d< d|j
j jj _|j
j  | jtjtj|dtjd	 t j }| j|| d S )
Ng333333&@r   Try   rq   rr   rU   FzS{"value": [11.1, null, true, "hello", {"name": "Jim"}]
,"repeatedValue": [[1], []]})r   TestListValuer   valuesr$   r{   rz   r   r   Zstruct_valuerh   r,   r;   r<   r   r+   r.   )r&   r%   r-   r'   r'   r(   testListValueMessage&  s    
z#JsonFormatTest.testListValueMessagec          	   C   s   t j }t j }t j }d|_d|_|jj| |jj j| |jj j| |jj  | jtj	t
j|dtj	d t j }| j|| t jdddddddd}|j  |jj| | jt
j|d	d
d d d S )Ni  i.  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   gQ	@r   )r   r   r   r   r   r   r   Fr   D   zD{
  "value": {
    "@type": "type.googleapis.com/proto3.TestMessage"i)r   TestAnyZMessageTyper   Packrh   r$   r,   r;   r<   r   r+   r.   r/   r|   )r&   r%   Zvalue1Zvalue2r-   Ztest_messager'   r'   r(   testAnyMessage7  s8    
zJsonFormatTest.testAnyMessagec       	      C   s  t j }tj }d|_|j| | jtjt	j
|dtjd t j }| j|| tj }|j| | jtjt	j
|dtjd | j|| tj }d|_|j| | jtjt	j
|dtjd | j|| tj }|jjd |jjd |j| | jtjt	j
|dtjd	 | j|| tj }d
|d< |j| | jtjt	j
|dtjd | j|| t j }d|_|j| |j| | jtjt	j
|dtjd | j|| d S )Ni  TzQ{
  "@type": "type.googleapis.com/google.protobuf.Int32Value",
  "value": 1234
}
zb{
  "@type": "type.googleapis.com/google.protobuf.Timestamp",
  "value": "1970-01-01T00:00:00Z"
}
rU   zO{
  "@type": "type.googleapis.com/google.protobuf.Duration",
  "value": "1s"
}
zfoo.barr   zY{
  "@type": "type.googleapis.com/google.protobuf.FieldMask",
  "value": "foo.bar,bar"
}
rq   rr   zX{
  "@type": "type.googleapis.com/google.protobuf.Struct",
  "value": {"name": "Jim"}
}
i.  z{
  "@type": "type.googleapis.com/google.protobuf.Any",
  "value": {
    "@type": "type.googleapis.com/google.protobuf.Int32Value",
    "value": 5678
  }
}
)r   Anyr   Z
Int32Valuer   r   r,   r;   r<   r   r+   r.   r   Z	Timestampr   ZDurationrf   r   Z	FieldMaskrl   r"   r   Struct)	r&   r%   r   r-   Z	timestampZdurationZ
field_maskZstruct_messageZ
nested_anyr'   r'   r(   testWellKnownInAnyMessagee  sf    






z(JsonFormatTest.testWellKnownInAnyMessagec             C   s   t j }t j }| j| tjd| | j|| | j|jd tjd| | j|jd | j	tj
dtjd| | j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": {}}zkFailed to parse repeatedInt32Value field: null is not allowed to be used as an element in a repeated field.z {"repeatedInt32Value":[1, null]}z{"repeatedMessageValue":[null]}zmFailed to parse repeatedMessageValue field: null is not allowed to be used as an element in a repeated field.)r   r/   r)   r   r*   r,   ZassertFalseZHasField
assertTruer0   r1   r3   )r&   r%   r-   r'   r'   r(   testParseNull  s"    
zJsonFormatTest.testParseNullc             C   sR   t j }td|_d}| jtj|| t j }tj|| | jt	j
|j d S )Nnanz{
  "floatValue": "NaN"
})r   r/   r   r   r,   r   r+   r*   r   mathZisnan)r&   r%   r2   r-   r'   r'   r(   testNanFloat  s    
zJsonFormatTest.testNanFloatc             C   s   | j dd d S )Nrn   z1Failed to load JSON: (Expecting value)|(No JSON).)r3   )r&   r'   r'   r(   testParseEmptyText  s    z!JsonFormatTest.testParseEmptyTextc             C   sf   t j }d}tj|| d}tj|| | jdd d}tj|| tj }| jtjdtjd| d S )Nz{"enumValue": 0}z{"enumValue": 1}z{"enumValue": "baz"}zVFailed to parse enumValue field: Invalid enum value baz for enum type proto3.EnumType.z{"enumValue": 12345}z{Failed to parse optionalNestedEnum field: Invalid enum value 12345 for enum type protobuf_unittest.TestAllTypes.NestedEnum.z{"optionalNestedEnum": 12345})	r   r/   r   r*   r3   r	   ZTestAllTypesr0   r1   )r&   r%   r2   r'   r'   r(   testParseEnumValue  s    z!JsonFormatTest.testParseEnumValuec             C   s   | j dd | j dd d S )Nz{int32Value: 1}zQFailed to load JSON: Expecting property name( enclosed in double quotes)?: line 1z{"unknownName": 1}zCMessage type "proto3.TestMessage" has no field named "unknownName".)r3   )r&   r'   r'   r(   testParseBadIdentifer  s    z$JsonFormatTest.testParseBadIdentiferc             C   s<   d}t j }tj||dd d}t j }tj||dd d S )Nz{"unknownName": 1}T)Zignore_unknown_fieldszl{
  "repeatedValue": [ {
    "@type": "type.googleapis.com/proto3.MessageType",
    "unknownName": 1
  }]
}
)r   r/   r   r*   r   )r&   r2   r-   r'   r'   r(   testIgnoreUnknownField  s    z%JsonFormatTest.testIgnoreUnknownFieldc             C   s   t jdk rd S | jdd d S )Nr\      z!{"int32Value": 1,
"int32Value":2}z.Failed to load JSON: duplicate key int32Value.)r\   r   )rV   rW   r3   )r&   r'   r'   r(   testDuplicateField  s    
z!JsonFormatTest.testDuplicateFieldc             C   s   | j dd | j dd d S )Nz{"boolValue": 1}zGFailed to parse boolValue field: Expected true or false without quotes.z{"boolValue": "true"})r3   )r&   r'   r'   r(   testInvalidBoolValue  s    z#JsonFormatTest.testInvalidBoolValuec             C   sl   t j }d}| jtjtj|| | jdd | jdd | jdd | jdd	 | jd
d | jdd d S )Nz{"int32Value": 0x12345}z{"int32Value": 1.5}z>Failed to parse int32Value field: Couldn't parse integer: 1.5.z{"int32Value": 012345}z9Failed to load JSON: Expecting \'?,\'? delimiter: line 1.z{"int32Value": " 1 "}z@Failed to parse int32Value field: Couldn't parse integer: " 1 ".z{"int32Value": "1 "}z?Failed to parse int32Value field: Couldn't parse integer: "1 ".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/   ZassertRaisesr   r1   r*   r3   )r&   r%   r2   r'   r'   r(   testInvalidIntegerValue'  s     z&JsonFormatTest.testInvalidIntegerValuec             C   s   | j dd d S )Nz{"floatValue": "nan"}zPFailed to parse floatValue field: Couldn't parse float "nan", use "NaN" instead.)r3   )r&   r'   r'   r(   testInvalidFloatValue?  s    z$JsonFormatTest.testInvalidFloatValuec             C   s   | j dd | j dd d S )Nz{"bytesValue": "AQI"}z4Failed to parse bytesValue field: Incorrect padding.z{"bytesValue": "AQI*"})r3   )r&   r'   r'   r(   testInvalidBytesValueD  s    z$JsonFormatTest.testInvalidBytesValuec             C   s   | j dd d S )Nz{"repeatedInt32Value": 12345}zkFailed to parse repeatedInt32Value field: repeated field repeatedInt32Value must be in \[\] which is 12345.)r3   )r&   r'   r'   r(   testInvalidRepeatedJ  s    z"JsonFormatTest.testInvalidRepeatedc             C   s   t j }d}| jtjdtj|| d}| jtjdtj|| d}| jtjdtj|| tjdk rdd S d	}| jtjd
tj|| d}| j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}}zQFailed to load JSON: Expecting property name( enclosed in double quotes)?: line 1z{"boolMap": {"null": 1}}zDFailed to parse boolMap field: Expected "true" or "false", not null.r\   r   z${"stringMap": {"a": 3, "\u0061": 2}}z$Failed to load JSON: duplicate key az{"stringMap": 0}zSFailed to parse stringMap field: Map field string_map must be in a dict which is 0.)r\   r   )r   ZTestMapr0   r   r1   r*   rV   rW   )r&   r%   r2   r'   r'   r(   testInvalidMapO  s8    
zJsonFormatTest.testInvalidMapc             C   s   t j }d}| jtjdtj|| d}| jtjdtj|| d}| jtjdtj|| d}| jtjdtj|| d	|j_| jt	d
tj
| d S )Nz%{"value": "10000-01-01T00:00:00.00Z"}zKtime data '10000-01-01T00:00:00' does not match format '%Y-%m-%dT%H:%M:%S'.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 range)r   re   r0   r   r1   r*   r
   r   rf   OverflowErrorr+   )r&   r%   r2   r'   r'   r(   testInvalidTimestampo  s4    z#JsonFormatTest.testInvalidTimestampc             C   s&   t j }d}| jtjdtj|| d S )Nz/{"oneofInt32Value": 1, "oneofStringValue": "2"}zTMessage type "proto3.TestOneof" should not have multiple "oneof_value" oneof fields.)r   r_   r0   r   r1   r*   )r&   r%   r2   r'   r'   r(   testInvalidOneof  s    zJsonFormatTest.testInvalidOneofc             C   s&   t j }d}| jtjdtj|| d S )Nz{"value": 1234}zDFailed to parse value field: ListValue must be in \[\] which is 1234)r   r~   r0   r   r1   r*   )r&   r%   r2   r'   r'   r(   testInvalidListValue  s    z#JsonFormatTest.testInvalidListValuec             C   s&   t j }d}| jtjdtj|| d S )Nz{"value": 1234}zCFailed to parse value field: Struct must be in a dict which is 1234)r   rv   r0   r   r1   r*   )r&   r%   r2   r'   r'   r(   testInvalidStruct  s    z JsonFormatTest.testInvalidStructc             C   sf   t j }d}| jtdtj|| d}| jtjdtj|| d}| jtdtj|| d}tj|| d S )Nz;{"@type": "type.googleapis.com/google.protobuf.Int32Value"}r   z{"value": 1234}z*@type is missing when parsing any message.z?{"@type": "type.googleapis.com/MessageNotExist", "value": 1234}zQCan not find message descriptor by type_url: type.googleapis.com/MessageNotExist.zN{"@type": "incorrect.googleapis.com/google.protobuf.Int32Value","value": 1234})r   r   r0   KeyErrorr   r*   r1   	TypeError)r&   r%   r2   r'   r'   r(   testInvalidAny  s$    zJsonFormatTest.testInvalidAnyc             C   s   t j }d|_| jdtj| | jdtj|dd t j }| jdtj|dd | jdtj|dd t j }tjd| | jd	|j tjd
| | jd|j d S )Ni90  z{
  "int32Value": 12345
}z{
  "int32_value": 12345
}FTz{
  "repeatedValue": []
}z{
  "repeated_value": []
}z{"int32Value": 54321}i1  z{"int32_value": 12345})r   r/   r   r,   r   r+   re   r*   )r&   r%   r'   r'   r(   testPreservingProtoFieldNames  s     z,JsonFormatTest.testPreservingProtoFieldNamesc             C   s(   t j }d|_| jdtj|dd d S )Ni90  z{
"int32Value": 12345
}r   )indent)r   r/   r   r,   r   r+   )r&   r%   r'   r'   r(   
testIndent  s    zJsonFormatTest.testIndentc             C   s2   d}d|i}t j }tj|| | j||j d S )Ni90  
int32Value)r   r/   r   rO   r,   r   )r&   expectedZjs_dictr%   r'   r'   r(   testParseDict  s
    zJsonFormatTest.testParseDictc             C   s,   t j }d|_ddi}| j|tj| d S )Ni90  r   )r   r/   r   r,   r   rN   )r&   r%   r   r'   r'   r(   testMessageToDict  s
    z JsonFormatTest.testMessageToDictc             C   s8   t j }d|_| jdtj| t j }| j|| d S )Ni90  z{
  "@value": 12345
})r   ZTestCustomJsonNamer   r,   r   r+   r.   )r&   r%   r-   r'   r'   r(   testJsonName  s    zJsonFormatTest.testJsonNamec          	   C   sF   t jdddddd}| jtj|ddtjdddddd	d
dd d S )NTrU   rT   r:   Zbla)r   r   r   r   r   )	sort_keys3)Z	boolValuer   Z
int64ValueZuint32ValueZstringValuer\   )r   r   )r   r/   r,   r   r+   r;   dumps)r&   r%   r'   r'   r(   testSortKeys  s    
zJsonFormatTest.testSortKeysN)3r4   r5   r6   r8   r=   r>   r?   rL   rM   rQ   rR   rS   rY   rZ   r[   r^   r`   rb   ri   rk   rm   rp   rx   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(   r7   n   s`   
!
	((!.O-	 	r7   __main__)__doc__
__author__r;   r   rV   Z	unittest2ZunittestImportErrorZgoogle.protobufr   r   r   r   r   r   r   r	   Zgoogle.protobuf.internalr
   r   Zgoogle.protobuf.utilr   ZTestCaser   r7   r4   mainr'   r'   r'   r(   <module>    s<   4       
