3
\^|                @   s   d Z ddlZddlZddlZddlZddlZyddlZW n ek
rT   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 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 ejr"eZG dd deZej G dd dej Z!ej G dd dej Z"ej G dd dej Z#ej G dd dej Z$ej G dd dej Z%ej G dd  d ej Z&ej G d!d" d"ej Z'ej G d#d$ d$ej Z(ej G d%d& d&ej Z)e*d'krej+  dS )(ziUnittest for reflection.py, which also indirectly tests the output of the
pure-Python protocol compiler.
    N)unittest_import_pb2)unittest_mset_pb2)unittest_pb2)descriptor_pb2)
descriptor)message)
reflection)text_format)api_implementation)more_extensions_pb2)more_messages_pb2)message_set_extensions_pb2)wire_format)	test_util)testing_refleaks)decoderc               @   s\   e Zd ZdZdd Zdd ZeZeZeZeZ	dd Z
e
Zdd	 Zd
d Zdd Zdd ZdS )_MiniDecodera  Decodes a stream of values from a string.

  Once upon a time we actually had a class called decoder.Decoder.  Then we
  got rid of it during a redesign that made decoding much, much faster overall.
  But a couple tests in this file used it to check that the serialized form of
  a message was correct.  So, this class implements just the methods that were
  used by said tests, so that we don't have to rewrite the tests.
  c             C   s   || _ d| _d S )Nr   )_bytes_pos)selfbytes r   L/tmp/pip-build-8app2_gc/protobuf/google/protobuf/internal/reflection_test.py__init__Q   s    z_MiniDecoder.__init__c             C   s   t j| j| j\}| _|S )N)r   Z_DecodeVarintr   r   )r   resultr   r   r   
ReadVarintU   s    z_MiniDecoder.ReadVarintc             C   s   t j| j S )N)r   ZZigZagDecoder   )r   r   r   r   
ReadSInt64^   s    z_MiniDecoder.ReadSInt64c             C   s   t j| j S )N)r   Z	UnpackTagr   )r   r   r   r   ReadFieldNumberAndWireTypec   s    z'_MiniDecoder.ReadFieldNumberAndWireTypec             C   s4   t jd| j| j| jd  d }|  jd7  _|S )Nz<f   r   )structunpackr   r   )r   r   r   r   r   	ReadFloatf   s    "z_MiniDecoder.ReadFloatc             C   s4   t jd| j| j| jd  d }|  jd7  _|S )Nz<d   r   )r   r    r   r   )r   r   r   r   r   
ReadDoublek   s    "z_MiniDecoder.ReadDoublec             C   s   | j t| jkS )N)r   lenr   )r   r   r   r   EndOfStreamp   s    z_MiniDecoder.EndOfStreamN)__name__
__module____qualname____doc__r   r   	ReadInt32	ReadInt64
ReadUInt32
ReadUInt64r   
ReadSInt32r   r!   r#   r%   r   r   r   r   r   G   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` Z2e3j4dadbdc Z5ddde Z6dfdg Z7dhdi Z8djdk Z9dldm Z:dndo Z;dpdq Z<drds Z=dtdu Z>dvdw Z?dxdy Z@dzd{ ZAd|d} ZBd~d ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdd ZVdddZWdd ZXeYjZe[j\ dkpe[j] dkddd Z^dd Z_dd Z`dd Zadd Zbdd Zcdd ZddS )ReflectionTestc             C   sB   | j t|t| x(tt|D ]}| j || ||  q"W d S )N)assertEqualr$   range)r   valuesZothersir   r   r   assertListsEqualw   s    zReflectionTest.assertListsEqualc             C   sP   t jdddd d}| jd|j | jd|j | jd|j | j|jd d S )N   g+)K@optional_string)optional_int32optional_doubler6   optional_floatr9   )r   TestAllTypesr0   r7   r8   r6   assertFalseHasField)r   protor   r   r   testScalarConstructor|   s    z$ReflectionTest.testScalarConstructorc             C   s   t jddddgddgdddgd	gd d
}| jddddgt|j | jddgt|j | jdddgt|j | jd	gt|j | jg t|j d S )N         r   gGz?g+)K@TFr6   )repeated_int32repeated_doublerepeated_boolrepeated_stringrepeated_float)	r   r:   r0   listrB   rC   rD   rE   rF   )r   r=   r   r   r   testRepeatedScalarConstructor   s    
z,ReflectionTest.testRepeatedScalarConstructorc             C   s  t jt jjt jjdt jjt jjdgt jd
dt jddt jddgt jj t jjddt jjddgd	}| jt jjt jjdt jjt jjdgt|j	 | jt jddt jddt jddgt|j
 | jt jj t jjddt jjddgt|j d S )N)bb+   )ci     r?   )ar@   )repeated_nested_messagerepeated_foreign_messagerepeatedgroupii)r   r:   NestedMessageFOOBARForeignMessageZRepeatedGroupr0   rG   rN   rO   rP   )r   r=   r   r   r    testRepeatedCompositeConstructor   s6    





z/ReflectionTest.testRepeatedCompositeConstructorc             C   s   t jddddgdddgt jjt jjdt jjt jjdgt jdd	t jd
d	t jdd	gd d}| jd|j | jd|j | jddgt	|j
 | jdddgt	|j | jt jjt jjdt jjt jjdgt	|j | jt jdd	t jd
d	t jdd	gt	|j | j|jd d S )Nr5   r6   gGz?g+)K@TF)rI   rJ   )rK   i  rL   )r7   r6   rC   rD   rN   rO   optional_nested_messagerV   ii)r   r:   rQ   rR   rS   rT   r0   r7   r6   rG   rC   rD   rN   rO   r;   r<   )r   r=   r   r   r   testMixedConstructor   s:    





z#ReflectionTest.testMixedConstructorc             C   s   | j ttjdd | j ttjdd | j ttjdd | j ttjdd | j ttjdgd | j ttjdd | j ttjdgd | j ttjdd | j ttjdgd d S )	Nfoo)r7   i  )r6   )rV   )rB   )rE   )rN   )assertRaises	TypeErrorr   r:   )r   r   r   r   testConstructorTypeError   s$    z'ReflectionTest.testConstructorTypeErrorc             C   s   t jdd}| jd|j  t jt jj d}| jd|j  t jdgd}| jd|j  t jt jj gd}| jd|j  d S )NrL   )r7   r@   )rV   rA   )rB   )rN   )r   r:   r0   ByteSizerQ   )r   r   r   r   r   (testConstructorInvalidatesCachedByteSize   s    z7ReflectionTest.testConstructorInvalidatesCachedByteSizec             C   sp   t j }| j|jd  | jd|j | j|jd  d|_| j|jd |jd | j|jd  d S )Nr7   r   r?   )r   r:   
assertTruer<   r0   r7   
ClearField)r   r=   r   r   r   testSimpleHasBits   s    
z ReflectionTest.testSimpleHasBitsc                s8    fdd}|dd |dd |dd |d	d
 d S )Nc                s0  t j }t|| }t||} jd|  j|j|   j|j|   d}t|||  j|t|| |} j|j|  j|j|  |j|  t|| } j|j|   j|j|    jdt||  j||k	 t|||  j|j|   j|j|    jdt|| d S )Nr      )r   r:   getattrr0   r^   r<   setattrr_   )Zcomposite_field_nameZscalar_field_namer=   Zcomposite_fieldZoriginal_scalar_valuenew_valZold_composite_field)r   r   r   TestCompositeHasBits1  s,    



zNReflectionTest.testHasBitsWithSinglyNestedScalar.<locals>.TestCompositeHasBitsoptionalgrouprM   rV   rI   optional_foreign_messagerK   Zoptional_import_messagedr   )r   re   r   )r   r   !testHasBitsWithSinglyNestedScalar  s
    "*


z0ReflectionTest.testHasBitsWithSinglyNestedScalarc             C   s   t j }|j}~d|_d S )N   )r   r:   rV   rI   )r   r=   nestedr   r   r   testReferencesToNestedMessage`  s    z,ReflectionTest.testReferencesToNestedMessagec             C   sT   t j }|j}|jd | j||jk	 d|_| j|jd  | jd|jj d S )NrV   rj   r   )r   r:   rV   r_   r^   rI   r<   r0   )r   r=   rk   r   r   r   0testDisconnectingNestedMessageBeforeSettingFieldh  s    
z?ReflectionTest.testDisconnectingNestedMessageBeforeSettingFieldc             C   s6   t j }|j}|jd ~~tj  t j }|j}d S )NrV   )r   r:   rV   r_   gccollect)r   r=   rk   r   r   r   5testGetDefaultMessageAfterDisconnectingDefaultMessageq  s    
zDReflectionTest.testGetDefaultMessageAfterDisconnectingDefaultMessagec             C   s   t j }|j}d|_| j|jd |jd | jd|j | jd|jj | j||jk	 d|_| j|jd  | jd|jj d S )N   rV   r   rj   )r   r:   rV   rI   r^   r<   r_   r0   )r   r=   rk   r   r   r   /testDisconnectingNestedMessageAfterSettingField~  s    
z>ReflectionTest.testDisconnectingNestedMessageAfterSettingFieldc             C   s:   t j }| j|jd  |jd | j|jd  d S )NrV   )r   r:   r^   r<   r_   )r   r=   r   r   r   0testDisconnectingNestedMessageBeforeGettingField  s    
z?ReflectionTest.testDisconnectingNestedMessageBeforeGettingFieldc             C   sR   t j }t j }d|j_|j| | j|jd |jd | j|jd  d S )Nrq   rV   )r   r:   rV   rI   	MergeFromr^   r<   r_   )r   proto1proto2r   r   r   (testDisconnectingNestedMessageAfterMerge  s    

z7ReflectionTest.testDisconnectingNestedMessageAfterMergec             C   s8   t j dkrd S tj }d|j_|jd ~tj  d S )Npythonrq   optional_lazy_message)	r
   Typer   r:   ry   rI   r_   rn   ro   )r   r=   r   r   r   "testDisconnectingLazyNestedMessage  s    
z1ReflectionTest.testDisconnectingLazyNestedMessagec             C   st   t j }|jjjd | jdg|jj | j|jd |jd | j|jd  |jj	j
  | j|jd d S )Nrq   rV   )r   ZTestNestedMessageHasBitsrV   Znestedmessage_repeated_int32appendr0   r^   r<   r_   Z%nestedmessage_repeated_foreignmessageadd)r   r=   r   r   r   &testHasBitsWhenModifyingRepeatedFields  s    
z5ReflectionTest.testHasBitsWhenModifyingRepeatedFieldsc             C   s
  t j }| j|jd  | jd|jjjjjj | j|jd  d|jjjjj_| jd|jjjjjj | j|jd | j|jjd | j|jjjd | j|jjjjd | j|jjjjjd | j|jjjjjjd  | j|jjjjjjd d S )NrI   r   rq   rM   r7   )r   TestMutualRecursionAr^   r<   r0   rI   rM   r7   )r   Zrecursive_protor   r   r   !testHasBitsForManyLevelsOfNesting  s    z0ReflectionTest.testHasBitsForManyLevelsOfNestingc             C   s   t j }d|_d|_d|_|j}| j|jjd df|jjd df|jjd dfg|j	  d|j_
| j|jjd df|jjd df|jjd df|jjd |fg|j	  d S )	Nr?   rq   rX   r7   optional_fixed32r6   {   rV   )r   r:   r   r7   r6   rV   r0   
DESCRIPTORfields_by_name
ListFieldsrI   )r   r=   Znested_messager   r   r   testSingularListFields  s$    

z%ReflectionTest.testSingularListFieldsc          
   C   s   t j }|jjd |jjd |jjd |jjddg |jjg  |jjd |jjdd td	D  d
|_|j	 | j
|jjd d
f|jjd ddgf|jjd dgf|jjd dddddgfg|j  d S )Nr?   rq      rX   barbazc             s   s   | ]}t |V  qd S )N)str).0xr   r   r   	<genexpr>  s    z8ReflectionTest.testRepeatedListFields.<locals>.<genexpr>r@      r7   rB   repeated_fixed32rE   01)r   r:   r   r|   rB   rE   extendr1   r7   rD   r0   r   r   r   )r   r=   r   r   r   testRepeatedListFields  s"    
z%ReflectionTest.testRepeatedListFieldsc             C   s   t j }d|jt j< d|jt j< d|jt j< | jt jdft jdft jdfg|j  |jt j= | jt jdft jdfg|j  d S )Nr?   rq   rX   )r   TestAllExtensions
ExtensionsZoptional_fixed32_extensionoptional_int32_extensionZoptional_string_extensionr0   r   )r   r=   r   r   r   testSingularListExtensions  s    



z)ReflectionTest.testSingularListExtensionsc             C   s   t j }|jt j jd |jt j jd |jt j jd |jt j jd |jt j jd |jt j jd d|jt j< | jt jdft jddgft jdgft jdddgfg|j	  |jt j= |jt j= | jt jdft jdgfg|j	  d S )Nr?   rq   r   rX   r   r   r   )
r   r   r   Zrepeated_fixed32_extensionr|   repeated_int32_extensionrepeated_string_extensionr   r0   r   )r   r=   r   r   r   testRepeatedListExtensions  s(    



z)ReflectionTest.testRepeatedListExtensionsc             C   sf   t j }tj| t j | j|jjd dft jdf|jjd dft jdf|jjd dfg|j	  d S )	NZmy_intr?   rj   Z	my_stringrX   r   Zmy_floatg      ?)
r   TestFieldOrderingsr   SetAllFieldsAndExtensionsZmy_extension_intr0   r   r   Zmy_extension_stringr   )r   r=   r   r   r   testListFieldsAndExtensions  s    
z*ReflectionTest.testListFieldsAndExtensionsc             C   s  t 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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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 jj!|j" | jt j#|j$ | jt%j&|j' t j( }| jd|j) d S )Nr   g        F     )   *   rJ   ,   -   .   /   0   1   2   g     I@g     d@TZhellos   worldu   ሴii)*r   r:   r0   r7   optional_int64optional_uint32optional_uint64optional_sint32optional_sint64r   optional_fixed64optional_sfixed32optional_sfixed64r9   r8   optional_boolr6   optional_bytesZdefault_int32Zdefault_int64Zdefault_uint32Zdefault_uint64Zdefault_sint32Zdefault_sint64Zdefault_fixed32Zdefault_fixed64Zdefault_sfixed32Zdefault_sfixed64Zdefault_floatZdefault_doubleZdefault_boolZdefault_stringZdefault_bytesrS   Zdefault_nested_enumFOREIGN_BARZdefault_foreign_enumr   Z
IMPORT_BARZdefault_import_enumZTestExtremeDefaultValuesZutf8_string)r   r=   r   r   r   testDefaultValues%  sJ    z ReflectionTest.testDefaultValuesc             C   s   t j }| jt|jd d S )Nnonexistent_field)r   r:   rY   
ValueErrorr<   )r   r=   r   r   r    testHasFieldWithUnknownFieldNameN  s    z/ReflectionTest.testHasFieldWithUnknownFieldNamec             C   s,   t j }| jt|jd | jt|jd d S )Nr   s   nonexistent_field)r   r:   rY   r   r_   )r   r=   r   r   r   "testClearFieldWithUnknownFieldNameR  s    z1ReflectionTest.testClearFieldWithUnknownFieldNamec                sN   t j  xtdD ]} jj  qW t j } j| | jt fdd d S )N
   c                  s
    j d S )Nrq   )repeated_messager   )r=   r   r   <lambda>`  s    z9ReflectionTest.testClearRemovesChildren.<locals>.<lambda>)r   TestRequiredForeignr1   r   r}   CopyFromrY   
IndexError)r   r3   rv   r   )r=   r   testClearRemovesChildrenW  s    
z'ReflectionTest.testClearRemovesChildrenc             C   s~   t j }| jtt|dd | jtt|ddg | jtt|dd | jtt|jdd | jtt|jdd | jtt|dd d S )	NrB   r   rV   rj   rI   "   Zsome_attributer   )r   r:   rY   AttributeErrorrc   rN   rF   )r   r=   r   r   r   testDisallowedAssignmentsb  s    
z(ReflectionTest.testDisallowedAssignmentsc             C   s   t j }| jtt|dd | jtt|dd | jtt|dd | jtt|dd | jtt|dd | jtt|dd | jtt|d	d tj d
kr| jtt|dd nd|_d S )Nr7   g?rX   r6   r   r   r   r9   r8   rx   )r   r:   rY   rZ   rc   r
   rz   r   )r   r=   r   r   r   testSingleScalarTypeSafetyw  s    z)ReflectionTest.testSingleScalarTypeSafetyc                s    fdd}|ddt  |ddt  |ddt  t}tjddkrR|dd| n|ddt  |d
d| |d
d| |dd| |dd| dS )z}Verifies setting of scalar integers.

    Args:
      integer_fn: A function to wrap the integers that will be assigned.
    c                sZ   t j } |}t|| | jt|| | t j }|j|j  jt|| | d S )N)r   r:   rc   assertIsInstancerb   ParseFromStringSerializeToString)
field_namevalueZexpected_typer=   rv   )
integer_fnr   r   r   TestGetAndDeserialize  s    z@ReflectionTest.assertIntegerTypes.<locals>.TestGetAndDeserializer7   r?      r   Lr      r   <   r   Ni   @i   @l        l        i   @l            i   @l            )intlongr   calcsize)r   r   r   Z
integer_64r   )r   r   r   assertIntegerTypes  s    	z!ReflectionTest.assertIntegerTypesc             C   s   | j dd  d S )Nc             S   s   | S )Nr   )r   r   r   r   r     s    z1ReflectionTest.testIntegerTypes.<locals>.<lambda>)r   )r   r   r   r   testIntegerTypes  s    zReflectionTest.testIntegerTypesc             C   s   | j tj d S )N)r   r   NonStandardInteger)r   r   r   r   testNonStandardIntegerTypes  s    z*ReflectionTest.testNonStandardIntegerTypesc             C   sN   t j }| jt d|_W d Q R X | jtd tjdd|_W d Q R X d S )N2Zmy_errorrq   )	r   r:   rY   rZ   r   ZassertRaisesRegexpRuntimeErrorr   r   )r   pbr   r   r   testIllegalValuesForIntegers  s
    z+ReflectionTest.testIllegalValuesForIntegersc          
      s    fdd}|ddd |ddd |d	dd |ddd t j }jttf  d|_W dQ R X t j } d|_jd|j dS )zVerifies bounds checking for scalar integer fields.

    Args:
      integer_fn: A function to wrap the integers that will be assigned.
    c                s   t j } |} |}t|| | j|t||  t|| | j|t||  jttft|| |d  jttft|| |d  d S )Nr?   )r   r:   rc   r0   rb   rY   r   rZ   )r   Zexpected_minZexpected_maxr   )r   r   r   r   TestMinAndMaxIntegers  s    
zHReflectionTest.assetIntegerBoundsChecking.<locals>.TestMinAndMaxIntegersr7   r?   r   r   r   l    r   ?   r   l    Nl        i   l        il            l         l            l    l            l         )r   r:   rY   r   rZ   r   optional_nested_enumr0   )r   r   r   r   r   )r   r   r   assetIntegerBoundsChecking  s    
z)ReflectionTest.assetIntegerBoundsCheckingc             C   s   | j dd  d S )Nc             S   s   | S )Nr   )r   r   r   r   r     s    z?ReflectionTest.testSingleScalarBoundsChecking.<locals>.<lambda>)r   )r   r   r   r   testSingleScalarBoundsChecking  s    z-ReflectionTest.testSingleScalarBoundsCheckingc             C   s   | j tj d S )N)r   r   r   )r   r   r   r   )testNonStandardSingleScalarBoundsChecking  s    z8ReflectionTest.testNonStandardSingleScalarBoundsCheckingc             C   s   t j }| jt|jjd | jt|jjd | jt|jd | jt|jd |jjd d|jd< | jt|jj	dd | jt|jj	dd | jt|jj	dg  | jt|jj	dd |jjd	 | jt|jj	dd d S )
Ng?rX   r   rj   r   i  abcindexr   )
r   r:   rY   rZ   rB   r|   rE   Zrepeated_bytesr   __setitem__)r   r=   r   r   r   testRepeatedScalarTypeSafety  s    
z+ReflectionTest.testRepeatedScalarTypeSafetyc             C   sV   t j }| jd|j d|_| jd|j d|_| jd|j d|_| jd|j d S )Nr   r?   l    l    )r   r:   r0   r7   r   )r   r=   r   r   r   !testSingleScalarGettersAndSetters  s    z0ReflectionTest.testSingleScalarGettersAndSettersc             C   sV   t j }|jd d|_| j|jd |jd | jd|j | j|jd  d S )Nr7   r?   r   )r   r:   r_   r7   r^   r<   r0   )r   r=   r   r   r   testSingleScalarClearField  s    

z)ReflectionTest.testSingleScalarClearFieldc             C   sf   t j }| jd|j | jdt jj | jd|j | jdt jj | jd|j | jdt jj d S )Nr?   r@   rA   )r   r:   r0   rR   rS   BAZ)r   r=   r   r   r   	testEnums	  s    zReflectionTest.testEnumsc             C   s  | j dtjjtj | j dtjjtj | j dtjjtj | jttjjd tj	 }| j d|j
j|j | j dtj	j
j|j | j d|j
j|j | j dtj	j
j|j | j d|j
j|j | j dtj	j
j|j | jt|j
jd | jttj	j
jd d S )NFOREIGN_FOOr   FOREIGN_BAZi0,  rR   rS   r   )r0   r   ForeignEnumNamer   r   r   rY   r   r:   
NestedEnumrR   rS   r   )r   r=   r   r   r   testEnum_Name  s2    zReflectionTest.testEnum_Namec             C   s  | j tjtjjd | j tjtjj | j tjtjjd | j tjtjj | j tjtjjd | j tjtjj | jttjjd | jt	 tjj
 W d Q R X tj }| j |j|jjd | j |j|jj | j |jtjjjd | j |jtjjj | j |j|jjd | j |j|jj | j |jtjjjd | j |jtjjj | j |j|jjd | j |j|jj | j |jtjjjd | j |jtjjj | jt|jjd | jt	 |jjj W d Q R X | jttjjjd | jt	 tjjjj W d Q R X d S )	Nr   r   r   FOrR   rS   r   Foo)r0   r   r   r   Valuer   r   rY   r   r   r   r:   rR   r   rS   r   r   )r   r=   r   r   r   testEnum_Value.  sb    





zReflectionTest.testEnum_Valuec             C   s   | j dddgttjj  | j dddgttjj  | j dddgttjj  tj }| j ddd	d
gt|jj  | j ddddgt|jj  | j ddddgt|jj  d S )Nr   r   r   r   rq      rR   rS   r   NEGr?   r@   rA   )r   r   )r   rq   )r   r   )rR   r?   )rS   r@   )r   rA   r   )r   r   )	r0   rG   r   r   keysr2   itemsr:   r   )r   r=   r   r   r   testEnum_KeysAndValuesl  s    z%ReflectionTest.testEnum_KeysAndValuesc             C   s|  t j }| j|j  | jdt|j |jjd |jjd |jjd | j|j | jdt|j | jdddg|j | jd|jd  | jd|jd  | jt|jj	d | jt|jj	d | jt
|jj	d | jt
|jj	d  d	|jd< | jdd	dg|j |jjdd
 | jdd
d	dg|j |jjd | jd
d	dg|jdd  | jdd
d	ddg|jd d   dd tdD |jdd< | jdddddg|j dddg|jdd< | jdddddg|j g }x|jD ]}|j| qW | jdddddg| |jd= | jddddg|j |jdd = | jddg|j |jjddg | jddddg|j |jd | j|j  | jdt|j |jjd | jd|jd  d|jd< | jd|jd  ddddg|jd d < |jd= | jdddg|j |jd= | jddg|j | jt|jjd | jt|jjd |jd d!= | jdg|j |jdd= | jdg|j d S )"Nr   rq   r      rA   r?   i  rX   ra      r   r   c             s   s   | ]
}|V  qd S )Nr   )r   r3   r   r   r   r     s    z5ReflectionTest.testRepeatedScalars.<locals>.<genexpr>r@   #   (   r      rB   i,  d   i'  r   i.r   r   r   r   r   r   )r   r:   r^   rB   r0   r$   r|   rY   r   __getitem__rZ   insertr1   r   r_   __delitem__)r   r=   r   r3   r   r   r   testRepeatedScalars{  sl    
 

z"ReflectionTest.testRepeatedScalarsc             C   sF  t j }| j|j  | jdt|j |jjd |jjd |jjd |jjd | jdt|j |jjd | jdt|j | jd|jd  | jd|jd  | jd|jd  |jjd | jdt|j | jd|jd  | jd|jd  |jjd | jdt|j | jd|jd  | jt	|jjd d S )	Nr   rq   r   r   rA   r?   r@   r   )
r   r:   r^   rB   r0   r$   r|   removerY   r   )r   r=   r   r   r   testRepeatedScalarsRemove  s*    z(ReflectionTest.testRepeatedScalarsRemovec          
   C   s(  t j }| j|j  | jdt|j |jj }|jj }| j|j | jdt|j | j||g|j | j|t jj	 | j
t|jjd | j
t|jjd | j
t|jjd | j
t|jjd  |jj }|jj }|jj }| j|||g|jdd  | j|||||g|jd d   | j||g|jd d  | j|||g|jdd   | j||jd  | j|g|jd d  g }x|jD ]}|j| qW | j|||||g| |jd= | j||||g|j |jdd = | j||g|j t jj	dd}	t jj	dd}
|jj|	|
g | jdt|j | j|	|jd  | j|
|jd  | j
t|jj|	 | j
t|jjdg t j }| j
t|jj|g |jd	 | j|j  | jdt|j |jjd
d | jdt|j | jd
|jd j | j
t|jjd
 | j
t d
|jd< W d Q R X d S )Nr   r@   i  rX   r?   r   )rI   rA   rN   rj   i.)r   r:   r^   rN   r0   r$   r}   r4   r   rQ   rY   r   r   rZ   r|   r   r_   rI   	Exception)r   r=   m0m1m2Zm3Zm4r   r3   Zn1Zn2Zwrong_message_typer   r   r   testRepeatedComposites  s~    





z%ReflectionTest.testRepeatedCompositesc             C   s<  t j }| jdt|j |jj }t|j|_|jj }t|j|_| j||k |jj }t|j|_| j|||g|j | jdt|j |jj	| | jdt|j | j||jd  | j||jd  | j
t|jj	| | j
t|jj	d  | jdt|j |jj	| | jdt|j | j||jd  d S )Nr   rA   r@   r?   )r   r:   r0   r$   rN   r}   rI   r^   r4   r  rY   r   )r   r=   r  r  r	  r   r   r   testRepeatedCompositeRemoveE  s*    


z*ReflectionTest.testRepeatedCompositeRemovec                s   t j dkrd S tj}|dddd|j|j|jdd d d dd tj d}tj	ddd	d g g |gg tj
 d
	 G  fdddtjtjtj}| }| jd|j | j|jd  d|_| jd|j | j|jd d S )Nrx   	foo_fieldzMyProto.foo_fieldr   r?   F)name	full_namer   numbertypeZcpp_typelabeldefault_valuecontaining_typemessage_typeZ	enum_typeZis_extensionZextension_scopeoptionsZMyProtoignored)	r  r  filenamer  nested_types
enum_typesfields
extensionsr  c                   s   e Zd Z ZdS )z>ReflectionTest.testHandWrittenReflection.<locals>.MyProtoClassN)r&   r'   r(   r   r   )mydescriptorr   r   MyProtoClassv  s   r  rj   )r
   rz   r   FieldDescriptor
TYPE_INT64ZCPPTYPE_INT64LABEL_OPTIONALr   ZFieldOptions
DescriptorZMessageOptionssixwith_metaclassr   GeneratedProtocolMessageTyper   Messager0   r  r^   r<   )r   r  Zfoo_field_descriptorr  Zmyproto_instancer   )r  r   testHandWrittenReflectionb  s.     z(ReflectionTest.testHandWrittenReflectionz MakeDescriptor is not repeatablec                s  t j dkrd S  fdd d _tj }d|_tj} |d|j  |d|j  |d|j	  |d	|j
   jd
7  _|jj }d|_|j|_ j|_tjj|_tj|| jdjk | jdjk | jdjk | jd	jk | jdjk G fdddtjtjtj}| }d|_d|_d|_d|_|jjddg |j  }tj!|}| j||k	 | j"|| | j"|j|j | j"|j|j | j"|j|j | j"|j|j | j"|j|j d S )Nrx   c                s:     j d7  _ | jj }||_||_ j |_tjj|_	d S )Nr?   )
field_indexfieldr}   r  r  r  r   FieldDescriptorProtor   r  )r=   r   Z
field_type	new_field)AddDescriptorFieldr   r   r+    s    
zEReflectionTest.testDescriptorProtoSupport.<locals>.AddDescriptorFieldr   ZCarr  year	automaticpricer?   ownersc                   s   e Zd Z ZdS )z=ReflectionTest.testDescriptorProtoSupport.<locals>.CarMessageN)r&   r'   r(   r   r   )descr   r   
CarMessage  s   r1  priusi  Tg    @ZbobZsusan)#r
   rz   r'  r   ZDescriptorProtor  r)  ZTYPE_STRINGr  Z	TYPE_BOOLZTYPE_DOUBLEr(  r}   r  r  LABEL_REPEATEDr  r   MakeDescriptorr^   r   r"  r#  r   r$  r   r%  r,  r-  r.  r/  r   r   ZParseMessager0   )r   Z
desc_protoZfdpr*  r1  r2  Zserialized_priusZ	new_priusr   )r+  r0  r   testDescriptorProtoSupport  sP    



z)ReflectionTest.testDescriptorProtoSupportc             C   s   t j }t j}d|j|< t j}|j| jd t j}d|j| _| jt	|jd |j|= | jt	|jd |j|= | jt	|jd |j|= | jt	|jd |j|= | jt	|jd d S )Nrj   r   8   rA   r@   r?   r   )
r   ExtendedMessageoptional_int_extensionr   repeated_int_extensionr|   optional_message_extensionforeign_message_intr0   r$   )r   extendee_protoextension_int32extension_repeatedextension_msgr   r   r   testExtensionDelete  s     
z"ReflectionTest.testExtensionDeletec             C   s   t j }t j}d|j|< t j}|j| jd t j}d|j| _d|_|j	jd |||f}d}x8|jD ].}| j
|j|| j | j||j |d7 }qhW | j
|d d S )Nrj   r   r6  r?   hir   rA   )r   r7  r8  r   r9  r|   r:  r;  r7   rE   r0   r  assertIn)r   r<  r=  r>  r?  expectedcountitemr   r   r   testExtensionIter  s     

z ReflectionTest.testExtensionIterc             C   s>   t j }| jt|jjd t jjjd }| jt|jj| d S )Nr   r7   )r   r7  rY   KeyErrorr   __contains__r   r   )r   r<  r(  r   r   r   testExtensionContainsError  s
    z)ReflectionTest.testExtensionContainsErrorc             C   s   t j }t j}| j|j|  | j||j | jd|j|  | j|j|  | j||j d|j|< | jd|j|  | j|j| | j||j |j	| | jd|j|  | j|j|  | j||j d S )Nr   rj   )
r   r   r   r^   HasExtensionassertNotInr   r0   rB  ClearExtension)r   r<  	extensionr   r   r   'testTopLevelExtensionsForOptionalScalar  s    

z6ReflectionTest.testTopLevelExtensionsForOptionalScalarc             C   s   t j }t j}| jdt|j|  | j||j |j| jd | jdg|j|  | j||j |j| }|j	| | jdt|j|  | j||j | j
||j| k	 | jttj|j|d d S )Nr   rX   rM   )r   r   r   r0   r$   r   rK  r|   rB  rL  r^   rY   rZ   operatorsetitem)r   r<  rM  Zstring_listr   r   r   'testTopLevelExtensionsForRepeatedScalar  s    

z6ReflectionTest.testTopLevelExtensionsForRepeatedScalarc             C   s&  t j }t j}| j|j|  | j||j | jd|j| j | j|j|  | j||j d|j| _| jd|j| j | j|j| | j	||j |j| }|j
| | j||j| k	 d|_| jd|j | j|jd | j|j|  | j||j | jttj|j|d d S )Nr   rj   r   rK   rM   )r   r   Z"optional_foreign_message_extensionr^   rJ  rK  r   r0   rK   rB  rL  r<   rY   rZ   rO  rP  )r   r<  rM  Zforeign_messager   r   r   (testTopLevelExtensionsForOptionalMessage  s*    

z7ReflectionTest.testTopLevelExtensionsForOptionalMessagec             C   s   t j }t j}| jdt|j|  |j| j }d|_| jd|j| d j d|_| jd|j| d j |j| }|j| | jdt|j|  | j	||j| k	 | j
ttj|j|d d S )Nr   rj   r   rM   )r   r   repeatedgroup_extensionr0   r$   r   r}   rM   rL  r^   rY   rZ   rO  rP  )r   r<  rM  groupZ
group_listr   r   r   (testTopLevelExtensionsForRepeatedMessage;  s    

z7ReflectionTest.testTopLevelExtensionsForRepeatedMessagec             C   s   t j }t jj}| j|j|  | j||j |j| }| jd|j	 | j|j|  | j||j d|_	| jd|j| j	 | j|j| | j
||j |j| | j||j| k	 | j|j|  | j||j d S )Nr   rj   )r   r   TestRequiredsingler^   rJ  rK  r   r0   rM   rB  rL  )r   r<  rM  requiredr   r   r   testNestedExtensionsL  s     

z#ReflectionTest.testNestedExtensionsc             C   sT   t jj}| j|jt jjd | j|jdjt jj | j	dt
|jt jj d S )Nr?   z*protobuf_unittest.optional_int32_extensionr   )r   r   poolr^   ZFindExtensionByNumberr   assertIsZFindExtensionByNamer  r0   r$   ZFindAllExtensionsr:   )r   rZ  r   r   r   testRegisteredExtensions`  s    
z'ReflectionTest.testRegisteredExtensionsc             C   s  t j }| j|jd  | jd|jjt j  | j|jd  d|jjt j< | jd|jjt j  | j|jd t j }| j|jd  | jg |jjt j  | j|jd  |jjt j j	d | jdg|jjt j  | j|jd t j }| j|jd  | jd|jjt j
 j | j|jd  d|jjt j
 _| jd|jjt j
 j | j|jd t j }| j|jd  | jdt|jjt j  | j|jd  |jjt j j }| j||jjt j d  | j|jd d S )N
submessager   rj   )r   TopLevelMessager^   r<   r0   r]  r   r8  r9  r|   r:  r;  r$   repeated_message_extensionr}   )r   toplevelforeignr   r   r   (testHasBitsForAncestorsOfExtendedMessager  sL    



z7ReflectionTest.testHasBitsForAncestorsOfExtendedMessagec             C   sF   t j }|j}t j}|j| }|j| d|_| j||j| k	 d S )Nrj   )r   r^  r]  r:  r   rL  r;  r^   )r   r`  r<  rM  Zextension_protor   r   r   *testDisconnectionAfterClearingEmptyMessage  s    

z9ReflectionTest.testDisconnectionAfterClearingEmptyMessagec             C   s   t j }| jt|jd | jt|jd | jt|jjd | jt|jjdd xbt	j
t	jt	jt	jfD ]J}| jt|j| | jt|j| | jt|jj| | jt|jj|d qdW | jt|jt j d S )Ni  rq   )r   r   rY   rG  rJ  rL  r   r   r   r   r8  r:  r9  r_  r   )r   r<  Zunknown_handler   r   r   testExtensionFailureModes  s&    


z(ReflectionTest.testExtensionFailureModesc             C   s6   t j }tj| |j }t jj|}| j|| d S )N)r   r:   r   SetAllFieldsr   
FromStringr0   )r   ru   string1rv   r   r   r   testStaticParseFrom  s
    
z"ReflectionTest.testStaticParseFromc             C   sF   t j }d|_t j }d|_|j| | jd|j | jd|j d S )Nr?   r   )r   r:   r7   r6   rt   r0   )r   ru   rv   r   r   r   testMergeFromSingularField  s    
z)ReflectionTest.testMergeFromSingularFieldc             C   sx   t j }|jjd |jjd t j }|jjd |j| | jd|jd  | jd|jd  | jd|jd  d S )Nr?   r@   r   )r   r:   rB   r|   rt   r0   )r   ru   rv   r   r   r   testMergeFromRepeatedField  s    
z)ReflectionTest.testMergeFromRepeatedFieldc             C   s6   t j }d|j_t j }|j| | jd|jj d S )NrL   )r   r:   rf   rM   rt   r0   )r   ru   rv   r   r   r   testMergeFromOptionalGroup  s
    
z)ReflectionTest.testMergeFromOptionalGroupc             C   s   t j }|jj }d|_|jj }d|_t j }|jj }d|_|j| | jd|jd j | jd|jd j | jd|jd j t j }|jj|j | jd|jd j | jd|jd j | jd|jd j d S )Nr   iA  i  r   r?   r@   )r   r:   rN   r}   rI   rt   r0   )r   ru   mrv   Zproto3r   r   r   "testMergeFromRepeatedNestedMessage  s"    



z1ReflectionTest.testMergeFromRepeatedNestedMessagec             C   sP   t j }tj| t j }|j| | j|| |j }|j }| j|| d S )N)r   r:   r   re  rt   r0   r   )r   ru   rv   rg  string2r   r   r   testMergeFromAllFields  s    

z%ReflectionTest.testMergeFromAllFieldsc             C   s>   t j }d|jt j< t j }|j| | jd|jt j  d S )Nr?   )r   r   r   r   rt   r0   )r   ru   rv   r   r   r   testMergeFromExtensionsSingular  s    
z.ReflectionTest.testMergeFromExtensionsSingularc             C   s   t j }|jt j jd |jt j jd t j }|jt j jd |j| | jdt|jt j  | jd|jt j d  | jd|jt j d  | jd|jt j d  d S )Nr?   r@   r   rA   )r   r   r   r   r|   rt   r0   r$   )r   ru   rv   r   r   r   testMergeFromExtensionsRepeated'  s    
z.ReflectionTest.testMergeFromExtensionsRepeatedc             C   s   t j }|jt j }|j }d|_|j }d|_t j }|jt j }|j }d|_|j| |jt j }| jdt| | jd|d j | jd|d j | jd|d j d S )N   iM  o   rA   r   r?   r@   )	r   r   r   !repeated_nested_message_extensionr}   rI   rt   r0   r$   )r   ru   Zext1rl  rv   Zext2r   r   r   $testMergeFromExtensionsNestedMessage8  s&    
z3ReflectionTest.testMergeFromExtensionsNestedMessagec             C   sD   t j }t j }|j | j|jd |j| | j|jd d S )NrV   )r   r:   rV   r;   r<   rt   )r   message1message2r   r   r   testMergeFromBugO  s    
zReflectionTest.testMergeFromBugc             C   sL   t j }d|_d|_t j }d|_|j| | jd|j | jd|j d S )Nr?   zimportant-textr   )r   r:   r7   r6   r   r0   )r   ru   rv   r   r   r   testCopyFromSingularField\  s    
z(ReflectionTest.testCopyFromSingularFieldc             C   sf   t j }|jjd |jjd t j }|jjd |j| | jd|jd  | jd|jd  d S )Nr?   r@   r   )r   r:   rB   r|   r   r0   )r   ru   rv   r   r   r   testCopyFromRepeatedFieldi  s    
z(ReflectionTest.testCopyFromRepeatedFieldc             C   sP   t j }tj| t j }|j| | j|| |j }|j }| j|| d S )N)r   r:   r   re  r   r0   r   )r   ru   rv   rg  rn  r   r   r   testCopyFromAllFieldsv  s    

z$ReflectionTest.testCopyFromAllFieldsc             C   s\   t j }|jjd d|_d|_|j| | jd|jd  | jd|j | jd|j d S )Nr?   r@   zimportant-textr   )r   r:   rB   r|   r7   r6   r   r0   )r   ru   r   r   r   testCopyFromSelf  s    
zReflectionTest.testCopyFromSelfc             C   s4   t j dkrd S tj }tj }| jt|j| d S )Nrx   )r
   rz   r   r:   r   rY   rZ   r   )r   ru   rv   r   r   r   testCopyFromBadType  s
    z"ReflectionTest.testCopyFromBadTypec             C   s   t j }d|_tj|}| jd|j |jjd |jjd tj|j}| jddg| |j|d  | jdg| |j	j
 }d|_tj|j	}| j|j	| d|_| j|j	| |j|d  | jt|d d S )Nr?   r@   rA   r   )r   r:   r7   copydeepcopyr0   rB   r|   r  rN   r}   rI   assertNotEqualr$   )r   ru   rv   	containerrv  messagesr   r   r   testDeepCopy  s$    

zReflectionTest.testDeepCopyc             C   s   t j }tj dkr tj| n
tj| |j  | j|j	 d t j }| j|| t j
 }tj| |j  | j|j	 d t j
 }| j|| d S )Nrx   r   )r   r:   r
   rz   r   re  ZSetAllNonLazyFieldsClearr0   r\   r   SetAllExtensions)r   r=   Zempty_protor   r   r   	testClear  s    

zReflectionTest.testClearc             C   s  t j }|j}|j  | j||jk	 d|_| j|jd  | jd|jj t j }|j}d|_|j}d|_	|j  | j||jk	 | j||jk	 | jd|j | jd|j	 d|_d|_	| j
|jd | jd|jj | j
|jd | jd|jj	 d S )	Nrj   rV   r   rq   r   r      rg   )r   r:   rV   r  r^   rI   r<   r0   rg   rK   r;   )r   r=   rk   ra  r   r   r   testDisconnectingBeforeClear  s.    z+ReflectionTest.testDisconnectingBeforeClearc             C   sf   t j }d|j_|j}| j|jjd | j|jjd d|j_| j|jjd | j|jd d|_d S )Nrq   r   r      )r   Z
TestOneof2Zfoo_messageZqux_intr0   Zfoo_lazy_message)r   rl  Zsub_messager   r   r   testDisconnectingInOneof  s    z'ReflectionTest.testDisconnectingInOneofc             C   sr   t j }d|_d|j_| jd|jj | j|jd |j}d|_| jd|j | jd|j | j|jd d S )Nr   r   oneof_uint32r   oneof_nested_message)	r   r:   r  r  rI   r0   r;   r<   Zoneof_string)r   r=   rk   r   r   r   	testOneOf  s    zReflectionTest.testOneOfc             C   s"   | j |j  |j  |j  d S )N)r^   IsInitializedr   SerializePartialToString)r   r=   r   r   r   assertInitialized  s    z ReflectionTest.assertInitializedNc             C   sN   g }| j |j  | j |j| | j|t| | jtj|j |j  d S )N)	r;   r  r0   r$   rY   r   EncodeErrorr   r  )r   r=   Z
error_sizeerrorsr   r   r   assertNotInitialized  s    z#ReflectionTest.assertNotInitializedc             C   s  t j }| j| t j }| j| t j }| j|d d |_ |_|_| j| t j	 }| j| d|j
_| j|d d|j
_d|j
_| j| |jj }| j|d d |_ |_|_| j| t j }t jj}|j| j }|j| j }| j|d d|_d|_d|_| j|d d|_d|_d|_| j| t j }t jj}d|j| _| j|d d|j| _d|j| _| j| g }t j }| j|j| | j|dddg | jt|jddd d S )	NrA   r@   r?   r   r   rM   brK   )r   r:   r  r   rV  r  rM   r  rK   r   optional_messager   r}   multir   rW  r;   r  r0   rY   rZ   )r   r=   rv  rM  rw  r  r   r   r   testIsInitialized  sX    








z ReflectionTest.testIsInitializedcppr@   zBErrors are only available from the most recent C++ implementation.c             C   s   d}d}t j }||_||_|jj }d|_tj|||j d d}||_|jj }d|_| j	t
`}tj|||j d | jt|ddt|jd|j  | jdt|j | jd	t|j W d Q R X d S )
Nz!test_file_descriptor_errors.protoZmsg1)Zserialized_pbz)another_test_file_descriptor_errors.protoZmsg2	exceptionz%s not raisedr&   z test_file_descriptor_errors.msg1)r   FileDescriptorProtor  packager  r}   r   ZFileDescriptorr   rY   rZ   r^   hasattrrb   rC  rB  r   r  )r   	file_namepackage_nameZfile_descriptor_protor  Zanother_file_namer	  cmr   r   r   testFileDescriptorErrorsG  s0    

z'ReflectionTest.testFileDescriptorErrorsc             C   s   t j }| jtt|dd | jt|jtj	 tj	d|_| j|jt
d t
d|_| j|jtj	d | jtt|dd djd}||_d|_d|_d S )	Nr   zunicode objectZTestingr6   s   aau   Тестzutf-8r   )r   r:   rY   rZ   rc   r0   r  r6   r"  	text_typer   r   encode)r   r=   Z
utf8_bytesr   r   r   testStringUTF8Encodingi  s    

z%ReflectionTest.testStringUTF8Encodingc             C   sj  t j }t j}|j}d}|jd}||j| _|j }| j|j	 t
| tj }|j|}| jt
|| t j }	| jdt
|j | j|jd jd | j|jd jj| |	j|jd j}| jt
|jd j| | jt|	jtj | j|	j| |jd jj|t
|d }
d}y|	j|
 W n tk
rF   d}Y nX |	j}| j|pbt|tk d S )	Nu   Тестzutf-8r?   r   iʿ   FT)r   TestMessageSetTestMessageSetExtension2message_set_extensionr  r   r   r   r0   r\   r$   r   RawMessageSetMergeFromStringrE  type_idr^   r   endswithr  r"  r  replaceUnicodeDecodeErrorr   )r   r=   Zextension_messagerM  Z	test_utf8Ztest_utf8_bytes
serializedraw
bytes_readrw  ZbadbytesZunicode_decode_failedZstring_fieldr   r   r   testStringUTF8Serialization  s8    

	
z*ReflectionTest.testStringUTF8Serializationc             C   s"   t jdd}| jdtj| d S )Ns    )r   z#optional_bytes: "\000\177\200\377"
)r   r:   r0   r"  r  )r   r=   r   r   r   testBytesInTextFormat  s    z$ReflectionTest.testBytesInTextFormatc             C   s   t j }|jjt jj  | j|jd t j }|jjt jj  | j|jd t j }|jjd}| j	d| | j|jd t j }|jj
d | j|jd |j }t j }| j	t||j| | j|jd d S )NrV   r   r   )r   r:   rV   rt   rQ   r^   r<   r   r  r0   r   r   r$   )r   r=   r  r  rv   r   r   r   testEmptyNestedMessage  s*    z%ReflectionTest.testEmptyNestedMessagec             C   s6   t j }| j|jd |jj  | j|jd d S )Nrf   )r   r:   r;   r<   rf   ZSetInParentr^   )r   r=   r   r   r   testSetInParent  s    
zReflectionTest.testSetInParentc             C   s*   ddl m} |jj }| jd|jj dS )a  Test that we can import nested messages from their __init__.py.

    Such setup is not trivial since at the time of processing of __init__.py one
    can't refer to its submodules by name in code, so expressions like
    google.protobuf.internal.import_test_package.inner_pb2
    don't work. They do work in imports, so we have assign an alias at import
    and then use that alias in generated code.
    r   )import_test_package9   N)google.protobuf.internalr  ZmyprotoZOuterr0   innerr   )r   r  msgr   r   r   testPackageInitializationImport  s    
z.ReflectionTest.testPackageInitializationImport)N)er&   r'   r(   r4   r>   rH   rU   rW   r[   r]   r`   ri   rl   rm   rp   rr   rs   rw   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   SkipReferenceLeakCheckerr5  r@  rF  rI  rN  rQ  rR  rU  rY  r\  rb  rc  rd  rh  ri  rj  rk  rm  ro  rp  rq  ru  rx  ry  rz  r{  r|  r}  r  r  r  r  r  r  r  r  unittestskipIfr
   rz   Versionr  r  r  r  r  r  r  r   r   r   r   r/   t   s   "#Q	)!	>ZS?1

		
	; 6r/   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestAllTypesEqualityTestc             C   s   t j | _t j | _d S )N)r   r:   first_protosecond_proto)r   r   r   r   setUp  s    
zTestAllTypesEqualityTest.setUpc             C   s   | j tt| j d S )N)rY   rZ   hashr  )r   r   r   r   testNotHashable  s    z(TestAllTypesEqualityTest.testNotHashablec             C   s   | j | j| j d S )N)r0   r  )r   r   r   r   testSelfEquality  s    z)TestAllTypesEqualityTest.testSelfEqualityc             C   s   | j | j| j d S )N)r0   r  r  )r   r   r   r   testEmptyProtosEqual  s    z-TestAllTypesEqualityTest.testEmptyProtosEqualN)r&   r'   r(   r  r  r  r  r   r   r   r   r    s   r  c               @   sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )FullProtosEqualityTestz@Equality tests using completely-full protos as a starting point.c             C   s0   t j | _t j | _tj| j tj| j d S )N)r   r:   r  r  r   re  )r   r   r   r   r    s    

zFullProtosEqualityTest.setUpc             C   s   | j tt| j d S )N)rY   rZ   r  r  )r   r   r   r   r    s    z&FullProtosEqualityTest.testNotHashablec             C   s    | j | jd  | j d | j d S )N)r  r  r  )r   r   r   r   testNoneNotEqual  s    z'FullProtosEqualityTest.testNoneNotEqualc             C   s(   t j }| j| j| | j|| j d S )N)r   rV  r  r  r  )r   Zthird_protor   r   r   testNotEqualToOtherMessage  s    z1FullProtosEqualityTest.testNotEqualToOtherMessagec             C   s   | j | j| j d S )N)r0   r  r  )r   r   r   r   testAllFieldsFilledEquality  s    z2FullProtosEqualityTest.testAllFieldsFilledEqualityc             C   s@   | j  jd7  _| j| j | j | j jd | j| j | j d S )Nr?   r7   )r  r7   r  r  r_   )r   r   r   r   testNonRepeatedScalar"  s    z,FullProtosEqualityTest.testNonRepeatedScalarc             C   s   | j j jd7  _| j| j | j | j j jd8  _| j| j | j | j jjd | j| j | j | jjj| j j_| j| j | j | j jd | j| j | j d S )Nr?   rI   rV   )r  rV   rI   r  r  r0   r_   )r   r   r   r   testNonRepeatedComposite*  s    z/FullProtosEqualityTest.testNonRepeatedCompositec             C   s>   | j jjd | j| j | j | j jd | j| j | j d S )Nrq   rB   )r  rB   r|   r  r  r_   )r   r   r   r   testRepeatedScalar:  s    z)FullProtosEqualityTest.testRepeatedScalarc             C   s   | j jd  jd7  _| j| j | j | j jd  jd8  _| j| j | j | j jj  | j| j | j | jjj  | j| j | j d S )Nr   r?   )r  rN   rI   r  r  r0   r}   )r   r   r   r   testRepeatedCompositeA  s    z,FullProtosEqualityTest.testRepeatedCompositec             C   s(   | j jd d| j_| j| j | j d S )Nr7   r   )r  r_   r  r7   r  )r   r   r   r   testNonRepeatedScalarHasBitsM  s    z3FullProtosEqualityTest.testNonRepeatedScalarHasBitsc             C   sV   | j jd | jjjd | j| j | j d| j j_| j jjd | j| j | j d S )NrV   rI   r   )r  r_   r  rV   r  rI   r0   )r   r   r   r   testNonRepeatedCompositeHasBitsT  s    
z6FullProtosEqualityTest.testNonRepeatedCompositeHasBitsN)r&   r'   r(   r)   r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    s   r  c               @   s   e Zd Zdd ZdS )ExtensionEqualityTestc             C   s   t j }t j }| j|| tj| | j|| tj| | j|| |jt j  d7  < | j|| |jt j  d8  < | j|| |jt j d|jt j< | j|| d|jt j< | j|| t j }t j }| jd|jt j  | j|| d S )Nr?   r   )	r   r   r0   r   r  r  r   r   rL  )r   r  r  r   r   r   testExtensionEqualityb  s*    

z+ExtensionEqualityTest.testExtensionEqualityN)r&   r'   r(   r  r   r   r   r   r  _  s   r  c               @   s   e Zd Zdd ZdS )MutualRecursionEqualityTestc             C   sP   t j }t j }| j|| d|jjj_| j|| d|jjj_| j|| d S )Nrj   )r   r   r0   rI   rM   r7   r  )r   r  r  r   r   r   testEqualityWithMutualRecursion  s    z;MutualRecursionEqualityTest.testEqualityWithMutualRecursionN)r&   r'   r(   r  r   r   r   r   r    s   r  c               @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-S ).ByteSizeTestc             C   s,   t j | _tj | _t j | _t j | _	d S )N)
r   r:   r=   r   r7  extended_protoTestPackedTypespacked_protoTestPackedExtensionspacked_extended_proto)r   r   r   r   r    s    


zByteSizeTest.setUpc             C   s
   | j j S )N)r=   r\   )r   r   r   r   Size  s    zByteSizeTest.Sizec             C   s   | j d| jj  d S )Nr   )r0   r=   r\   )r   r   r   r   testEmptyMessage  s    zByteSizeTest.testEmptyMessagec             C   s8   t j }| jd|j  t jdd}| jd|j  d S )Nr   r?   )r   r@   )r   r:   r0   r\   )r   r=   Zproto_kwargsr   r   r   testSizedOnKwargs  s    zByteSizeTest.testSizedOnKwargsc                sz    fdd}|dd |dd x6t tdddtddD ]\}}|d|> d | q:W |d
d |dd |dd d S )Nc                s*    j j  |  j _ j|d  j  d S )Nr?   )r=   r  r   r0   r  )r3   Zexpected_varint_size)r   r   r   Test  s    
z&ByteSizeTest.testVarints.<locals>.Testr   r?   r  r   i'  r   r@   r   r   l            l         )zipr1   )r   r  r3   	num_bytesr   )r   r   testVarints  s    

"

zByteSizeTest.testVarintsc             C   sh   d| j _| jd| j  d| j _| jdt| j j | j  dd | j _| jdt| j j | j  d S )Nr   r@   r   r      rA   )r=   r6   r0   r  r$   )r   r   r   r   testStrings  s    zByteSizeTest.testStringsc             C   s   d| j _| jd| j  tj | _ d| j _| jd| j  tj | _ d| j _| jd| j  tj | _ d| j _| jd| j  tj | _ d| j _	| jd| j  tj | _ d S )Ni  rq   	   gX9v?@   rA   )
r=   r   r0   r  r   r:   r   r9   r8   r   )r   r   r   r   testOtherNumerics  s    



zByteSizeTest.testOtherNumericsc             C   s   d| j j_| jd| j  d S )	Nr?      rA   r@   i @  r   rq   r  )r=   rV   rI   r0   r  )r   r   r   r   testComposites  s    
zByteSizeTest.testCompositesc             C   s   d| j j_| jd| j  d S )	Nr?   r   r   r@   i    r   r   r   )r=   rf   rM   r0   r  )r   r   r   r   
testGroups  s    
zByteSizeTest.testGroupsc             C   s0   | j jjd | j jjd | jd| j  d S )Nr   r  r?   r@   rA   r   r  )r=   rB   r|   r0   r  )r   r   r   r   r    s    z ByteSizeTest.testRepeatedScalarsc             C   s&   | j jjddg | jd| j  d S )Nr   r  r?   r@   rA   r   r  )r=   rB   r   r0   r  )r   r   r   r   testRepeatedScalarsExtend  s    z&ByteSizeTest.testRepeatedScalarsExtendc             C   sN   | j jjd | j jjd | jd| j  | j jjd | jd| j  d S )	Nr   r  r?   r@   rA   r   r  rA   )r=   rB   r|   r0   r  r  )r   r   r   r   r    s
    z&ByteSizeTest.testRepeatedScalarsRemovec             C   s2   | j jj }| j jj }d|_| jd| j  d S )	Nr  r@   r?   rA   rq   r   r  r"   )r=   rN   r}   rI   r0   r  )r   foreign_message_0foreign_message_1r   r   r   r
    s    z#ByteSizeTest.testRepeatedCompositesc             C   s  | j jj }| j jj }d|_| jd| j  tj| j j}| j jd= | jd| j  | j jj }d|_| jd| j  | j jd= | jd| j  | j jd= | jd| j  | jdt| |dd= t	j
 dk r| jdt| |d= t	j
 dkr| jdt| d S )Nr  r@   r?   r   rL   rx   rA   rq   r   r  r"   rA   r   rq   rA   r   rq   r  r"   r  r   rA   r   rq   r   )r=   rN   r}   rI   r0   r  r~  r  r$   r
   rz   )r   r  r  rN   Zforeign_message_2r   r   r   testRepeatedCompositesDelete	  s,    




z)ByteSizeTest.testRepeatedCompositesDeletec             C   s2   | j jj }| j jj }d|_| jd| j  d S )	Nr  r@   r?   r   r   r"   r  r   )r=   rP   r}   rM   r0   r  )r   Zgroup_0Zgroup_1r   r   r   testRepeatedGroups,	  s    zByteSizeTest.testRepeatedGroupsc          
   C   sj   t j }| jd|j  t j}d|j|< | jd|j  t jjjd }| j	t
 d|j|< W d Q R X d S )Nr   rj   r@   r7   )r   r   r0   r\   r   r   r:   r   r   rY   rG  )r   r=   rM  r(  r   r   r   testExtensions5	  s    
zByteSizeTest.testExtensionsc             C   s   d| j _| jd| j j  d| j _| jd| j j  | j jd | jd| j j  tj}d| jj|< | jd| jj  d| jj|< | jd| jj  | jj	| | jd| jj  d S )Nr?   r@   r  rA   r7   r   )
r=   r7   r0   r\   r_   r   r8  r  r   rL  )r   rM  r   r   r   )testCacheInvalidationForNonrepeatedScalarA	  s    z6ByteSizeTest.testCacheInvalidationForNonrepeatedScalarc             C   s  | j jjd | jd| j j  | j jjd | jd| j j  d| j jd< | jd| j j  | j jd | jd| j j  tj}| jj	| }|jd | jd| jj  |jd | jd	| jj  d|d< | jd
| jj  | jj
| | jd| jj  d S )Nr?   rA   r   r  r  rB   r   r@   r   rq   )r=   rB   r|   r0   r\   r_   r   r9  r  r   rL  )r   rM  Zrepeatedr   r   r   &testCacheInvalidationForRepeatedScalarS	  s$    

z3ByteSizeTest.testCacheInvalidationForRepeatedScalarc             C   s$  d| j j_| jd| j j  d| j j_| jd| j j  | j jjd | jd| j j  | j jd | jd| j j  tj d	kr| j j}| j jd d|_| jd| j j  tj	}| j
j| }| jd| j
j  d|_| jd
| j
j  d|_| jd| j
j  | j
j| | jd| j
j  d S )Nr?   rq   r  r   rK   rA   rg   r   rx   r   )r=   rg   rK   r0   r\   r_   r
   rz   r   r:  r  r   r;  rL  )r   childrM  r   r   r   *testCacheInvalidationForNonrepeatedMessagej	  s,    

z7ByteSizeTest.testCacheInvalidationForNonrepeatedMessagec             C   s  | j jj }| jd| j j  | j jj  | jd| j j  d|_| jd| j j  | j jd | jd| j j  tj}| j	j
| }|j }| jd| j	j  |j  | jd| j	j  d|_| jd| j	j  |jd	 | jd| j	j  | j	j| | jd| j	j  d S )
NrA   r   r?   r"   rO   r   r@   r   r;  )r=   rO   r}   r0   r\   rK   r_   r   r_  r  r   r;  rL  )r   Zchild0rM  Z
child_listr   r   r   'testCacheInvalidationForRepeatedMessage	  s(    
z4ByteSizeTest.testCacheInvalidationForRepeatedMessagec             C   s   | j d| jj  | jjjd | jjjd d}| j || jj  | jjjd | jjjd d}| j || | jj  | jjd
 | j || jj  d S )Nr   r   r  r?   r@   rA   g@g      
@r"   packed_int32rA   r   r     )r0   r  r\   r  r|   packed_doubler_   )r   Zint_sizeZdouble_sizer   r   r   testPackedRepeatedScalars	  s    z&ByteSizeTest.testPackedRepeatedScalarsc             C   sH   | j d| jj  | jjtj }|jddddg | j d| jj  d S )Nr   r?   r@   rA   r   r  )r0   r  r\   r   r   Zpacked_fixed32_extensionr   )r   rM  r   r   r   testPackedExtensions	  s
    z!ByteSizeTest.testPackedExtensionsN)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    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=S )>SerializationTestc             C   sR   t j }t j }|j }| j|j t| | jt||j| | j|| d S )N)r   r:   r   r0   r\   r$   r  )r   r  r  r  r   r   r   testSerializeEmtpyMessage	  s    z+SerializationTest.testSerializeEmtpyMessagec             C   s\   t j }t j }tj| |j }| j|j t| | jt||j| | j|| d S )N)	r   r:   r   re  r   r0   r\   r$   r  )r   r  r  r  r   r   r   testSerializeAllFields	  s    
z(SerializationTest.testSerializeAllFieldsc             C   sH   t j }t j }tj| |j }| jt||j| | j|| d S )N)r   r   r   r  r   r0   r$   r  )r   r  r  r  r   r   r   testSerializeAllExtensions	  s    
z,SerializationTest.testSerializeAllExtensionsc             C   sF   t j }t j }d|j_|j }| jt||j| | j|| d S )N   )r   r:   rf   rM   r   r0   r$   r  )r   r  r  r  r   r   r   testSerializeWithOptionalGroup	  s    z0SerializationTest.testSerializeWithOptionalGroupc             C   sL   t j }d|_d
|_d|_d|_d|_d|_t jj|j	 }| j
|| d S )Nr?   r@   r   rA   r   rq   r   r   l        l     r   l        l     l        l     )r   r:   r7   r   r   r   r   r   rf  r   r0   )r   r  r  r   r   r   testSerializeNegativeValues	  s    
z-SerializationTest.testSerializeNegativeValuesc             C   s   t j dkrd S tj }tj| t|j }xtt	|d D ]}yntj }tj
 }|j|d|}| j|| y|j|d|}| j|| W n  tjk
r   | jd Y nX W q@ tjk
r   | jtj|j|d| Y q@X q@W d S )Nrx   r?   r   z@Parsing unknown fields failed when parsing known fields did not.)r
   rz   r   r:   r   re  
memoryviewr   r1   r$   TestEmptyMessageZ_InternalParser0   r   DecodeErrorZfailrY   )r   r  r  Ztruncation_pointr  Zunknown_fieldspospos2r   r   r   testParseTruncated
  s&    
z$SerializationTest.testParseTruncatedc             C   s  t j }d|_d|jt j< d|_d|jt j< d|_|j }| j	|j
 t| t|}|j}| j	dtjf|  | j	d|j  | j	dtjf|  | j	d|j  | j	dtjf|  | j	d|j  | j	dtjf|  | j	d|j  | j	dtjf|  | j	d|j  d S )Nrq   r   rA   r@   r?   )r   ZOutOfOrderFieldsr   r   r   r   r   r7   r   r0   r\   r$   r   r   r   ZWIRETYPE_VARINTr*   r+   r,   r-   r.   )r   r=   r  rh   ReadTagr   r   r   testCanonicalSerializationOrder 
  s(    z1SerializationTest.testCanonicalSerializationOrderc             C   s(   t j }tj| |j }tj| d S )N)r   r   r   r   r   Z#ExpectAllFieldsAndExtensionsInOrder)r   r=   r  r   r   r   (testCanonicalSerializationOrderSameAsCpp9
  s    
z:SerializationTest.testCanonicalSerializationOrderSameAsCppc             C   s   t j }|jjd d|_d|j_|j }t j }|jjd d|_d|j_|j|}| j	t
|| | j	ddgt|j | j	d|j | j	d|jj d S )NZfoobarrj   r   r   r   i  )r   r:   rE   r|   r7   rV   rI   r   r  r0   r$   rG   )r   r  r  r  Zbytes_parsedr   r   r   'testMergeFromStringWhenFieldsAlreadySet@
  s    
z9SerializationTest.testMergeFromStringWhenFieldsAlreadySetc             C   s  t j }t j}t j}|j}|j}t j}d|j| _d|j| _d|j| _	|j
 }tj }| jd|jj j | jt||j| | jdt|j t j }	| jt|jd j|	j|jd j | jd|	j t j }
| jt|jd j|
j|jd j | jd|
j t j }| jt|jd j|j|jd j | jd|j	 t j }| jt||j| | jd|j| j | jd|j| j | jd|j| j	 | j|j t| | j|j t| d S )	Nr   rX   r   FrA   r   r?   r@   )r   r  TestMessageSetExtension1r  r  Zmessage_set_extension3r   r3   r   textr   r   r  r0   r   
GetOptionsmessage_set_wire_formatr$   r  rE  r   ZTestMessageSetExtension3r\   )r   r=   extension_message1Zextension_message2
extension1Z
extension2Z
extension3r  r  rv  rw  Zmessage3rv   r   r   r   testMessageSetWireFormatW
  sR    z*SerializationTest.testMessageSetWireFormatc             C   s   t j }|jj }d|_tj}tj }d|_|j |_	|jj }d|_tj}tj }d|_|j |_	|jj }d|_tj
 }d|_|j |_	|j }tj }| jt||j| tj}|j}| jd|j| j d S )Nii90  ii:0  irX   )r   r  rE  r}   r  r   r  r3   r   r   r  r   r  r0   r$   r  r  r   )r   r  rE  r  rv  r  r=   r  r   r   r   (testMessageSetWireFormatUnknownExtension
  s4    





z:SerializationTest.testMessageSetWireFormatUnknownExtensionc             C   sp   t j }tj| |j }t j }| jt||j| t j }d|_	|j }t j }| jt||j| d S )Nl   )
r   r:   r   re  r   r  r0   r$   r  r   )r   r=   r  rv   r   r   r   testUnknownFields
  s    
z#SerializationTest.testUnknownFieldsc             C   sR   y
|  W n0 |k
r: } z| j |t| dS d}~X nX | jdt| dS )zEThis method checks if the excpetion type and message are as expected.Nz%s not raised)r0   r   ZfailureException)r   Z	exc_classZcallable_objr  exr   r   r   _CheckRaises
  s    
zSerializationTest._CheckRaisesc             C   s6  t j }| jtj|jd |j }t j }| j|jd |j	| | j|jd d|_
| jtj|jd |j }d|_| jtj|jd |j }d|_|j }|j }t j }| jt||j| | jd|j
 | jd|j | jd|j | jt||j| | jd|j
 | jd|j | jd|j d S )NzHMessage protobuf_unittest.TestRequired is missing required fields: a,b,crM   r?   zFMessage protobuf_unittest.TestRequired is missing required fields: b,cr@   zDMessage protobuf_unittest.TestRequired is missing required fields: crA   )r   rV  r  r   r  r   r  r;   r<   r   rM   r  rK   r0   r$   r  )r   r=   partialrv   r  r   r   r   testSerializeUninitialized
  sL    
z,SerializationTest.testSerializeUninitializedc             C   s   t j }|j  d|j_| jtj|jd d|j_d|j_	|j  d|j
j _d|j
j _| jtj|jd d|j
d _d|j
d _	d|j
d _d|j
d _	|j  d S )Nr?   zoMessage protobuf_unittest.TestRequiredForeign is missing required fields: optional_message.b,optional_message.cr@   rA   zMessage protobuf_unittest.TestRequiredForeign is missing required fields: repeated_message[0].b,repeated_message[0].c,repeated_message[1].a,repeated_message[1].cr   )r   r   r   r  rM   r  r   r  r  rK   r   r}   )r   r=   r   r   r   $testSerializeUninitializedSubMessage  s*    z6SerializationTest.testSerializeUninitializedSubMessagec             C   s`   t j }t j }tj| |j }| j|j t| |j|}| j|j | | j|| d S )N)	r   r  r   SetAllPackedFieldsr   r0   r\   r$   r  )r   r  r  r  r  r   r   r   testSerializeAllPackedFields0  s    

z.SerializationTest.testSerializeAllPackedFieldsc             C   sL   t j }t j }tj| |j }|j|}| j|j | | j|| d S )N)r   r  r   ZSetAllPackedExtensionsr   r  r0   r\   )r   r  r  r  r  r   r   r    testSerializeAllPackedExtensions:  s    

z2SerializationTest.testSerializeAllPackedExtensionsc             C   s   t j }|jjddg |jjd |j }t j }|jjd |jjddg |jjd | jt	||j
| | jdddg|j | jdddg|j | jdg|j d S )Nr?   r@   g      @rA   g      ?g       @r   )r   r  r  r   r  r|   r   Zpacked_sint32r0   r$   r  )r   r  r  r  r   r   r   1testMergePackedFromStringWhenSomeFieldsAlreadySetC  s    zCSerializationTest.testMergePackedFromStringWhenSomeFieldsAlreadySetc             C   sP  t j }|jjddddg |jjddg |jjd |j }| j|j	 t
| t|}|j}| jdtjf|  | jd|j  | jd|j  | jd|j  | jd|j  | jd|j  | jd	tjf|  | jd
|j  | jd|j  | jdtjf|  | jd|j  | jd|j  | jd|j  | j|j  d S )Nr?   r@      rA   g      ?g     @@g       @Z   r   r   e   r"   r@   rA   rq   r  )r   r  r  r   r  Zpacked_floatr|   r   r0   r\   r$   r   r   r   ZWIRETYPE_LENGTH_DELIMITEDr*   r!   r#   r^   r%   )r   r=   r  rh   r  r   r   r   testPackedFieldsWireFormatU  s,    z,SerializationTest.testPackedFieldsWireFormatc             C   sZ   t j }tj| t j }|j }| jt||j| t j }tj	| | j|| d S )N)
r   TestUnpackedTypesr   SetAllUnpackedFieldsr  r   r0   r$   r  r  )r   unpackedpackedr  rC  r   r   r   testParsePackedFromUnpackedm  s    

z-SerializationTest.testParsePackedFromUnpackedc             C   sZ   t j }tj| t j }|j }| jt||j| t j }tj	| | j|| d S )N)
r   r  r   r  r"  r   r0   r$   r  r#  )r   r%  r$  r  rC  r   r   r   testParseUnpackedFromPackedy  s    

z-SerializationTest.testParseUnpackedFromPackedc             C   s   t j }| jt jjjd | jt jjd | jt jjd | jt jjd | jt jjd | jt jj	d | jt jj
d | jt jjd | jt jjd d S )	Nr?   r     r   r   r   r   3   )r   r:   r0   rQ   ZBB_FIELD_NUMBEROPTIONAL_INT32_FIELD_NUMBERZOPTIONALGROUP_FIELD_NUMBER$OPTIONAL_NESTED_MESSAGE_FIELD_NUMBERZ!OPTIONAL_NESTED_ENUM_FIELD_NUMBERREPEATED_INT32_FIELD_NUMBERZREPEATEDGROUP_FIELD_NUMBERZ$REPEATED_NESTED_MESSAGE_FIELD_NUMBERZ!REPEATED_NESTED_ENUM_FIELD_NUMBER)r   r=   r   r   r   testFieldNumbers  s    z"SerializationTest.testFieldNumbersc             C   s8  | j tjjjd | j tjjd | j tjjjd | j tjjd | j tjjd | j tj	d | j tj
jd | j tjd | j tjjd | j tjd | j tjjd | j tjd | j tjjd | j tjd | j tjjd | j tjd | j tjjd	 | j tjd	 | j tjjd
 | j tjd
 d S )Ni  i  r?   r  r(  r   r   r   r   r)  )r0   r   rV  rW  r  ZSINGLE_FIELD_NUMBERr  ZMULTI_FIELD_NUMBERr   Z%OPTIONAL_INT32_EXTENSION_FIELD_NUMBERZoptionalgroup_extensionZ$OPTIONALGROUP_EXTENSION_FIELD_NUMBERZ!optional_nested_message_extensionZ.OPTIONAL_NESTED_MESSAGE_EXTENSION_FIELD_NUMBERZoptional_nested_enum_extensionZ+OPTIONAL_NESTED_ENUM_EXTENSION_FIELD_NUMBERr   Z%REPEATED_INT32_EXTENSION_FIELD_NUMBERrS  Z$REPEATEDGROUP_EXTENSION_FIELD_NUMBERrt  Z.REPEATED_NESTED_MESSAGE_EXTENSION_FIELD_NUMBERZrepeated_nested_enum_extensionZ+REPEATED_NESTED_ENUM_EXTENSION_FIELD_NUMBER)r   r   r   r   testExtensionFieldNumbers  s0    

z+SerializationTest.testExtensionFieldNumbersc             C   s   t j}| j|jj|jjd  | j|j|jjj | j|j	j|jjd  | j|j
|j	jj | j|jj|jjd  | j|j|jjj d S )Nr7   rV   rB   )r   r:   r[  r7   r   r   r0   r*  r  rV   r+  rB   r,  )r   clsr   r   r   testFieldProperties  s    


z%SerializationTest.testFieldPropertiesc             C   s   t j }d|_| jt jjj|d t jjj|d | j|jd | jt
 |`W d Q R X yt jj	j| W n t
k
r   Y nX d S )Nr   r   )r   r:   r7   r0   __get____set__rY   r   rT   rK   rZ   )r   r  r   r   r   testFieldDataDescriptor  s    z)SerializationTest.testFieldDataDescriptorc             C   sZ  t jddddt jjddt jddt jjt jdddgd		}| j|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	d|j
 | j	d|j | j	d|j | j	d|j | j	d|jj | j	d|jj | j	t jj|j | j	t j|j | j	dddg|j d S )Nr?   rX   Ts   bar)rI   )rK   r@   rA   )	r7   r6   r   r   rV   rg   r   optional_foreign_enumrB   r7   r6   r   r   rV   rg   r   r4  )r   r:   rQ   rT   rR   r   r^   r  r<   r0   r7   r6   r   r   rV   rI   rg   rK   r   r4  rB   )r   r=   r   r   r   testInitKwargs  s:    

z SerializationTest.testInitKwargsc             C   s   dd }| j t|d d S )Nc              S   s   t jdd} d S )Nunknown)r6  )r   r  )Zunused_protor   r   r   (InitalizeEmptyMessageWithExtraKeywordArg  s    z`SerializationTest.testInitArgsUnknownFieldName.<locals>.InitalizeEmptyMessageWithExtraKeywordArgz9Protocol message TestEmptyMessage has no "unknown" field.)r  r   )r   r7  r   r   r   testInitArgsUnknownFieldName  s
    z.SerializationTest.testInitArgsUnknownFieldNamec             C   s   t jdddd}| j|j  | j|jd | j|jd | j|jd | j|jd  | jd|j | jd|j | jd|j d S )Nr?   )rM   r  rK   rM   r  rK   dummy2)	r   rV  r^   r  r<   r0   rM   r  rK   )r   r=   r   r   r   testInitRequiredKwargs  s    z(SerializationTest.testInitRequiredKwargsc             C   s   t jt jddddd}| j|j  | j|jd | j|jj  | j|jjd | j|jjd | j|jjd | j|jjd  | jt jdddd|j | jd|jj | jd|jj	 | jd|jj
 d S )	Nr?   )rM   r  rK   )r  r  rM   r  rK   r9  )r   r   rV  r^   r  r<   r  r0   rM   r  rK   )r   r=   r   r   r   testInitRequiredForeignKwargs  s    z/SerializationTest.testInitRequiredForeignKwargsc             C   sZ   t jdddgd}| j|j  | jd|jd  | jd|jd  | jd|jd  d S )Nr?   r@   rA   )rB   r   )r   r:   r^   r  r0   rB   )r   r=   r   r   r   testInitRepeatedKwargs  s
    z(SerializationTest.testInitRepeatedKwargsN)!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  r3  r5  r8  r:  r;  r<  r   r   r   r   r  	  s<   


6)3!
	r  c               @   s   e Zd Zdd Zdd ZdS )OptionsTestc             C   s<   t j }| jd|jj j tj }| jd|jj j d S )NTF)r   r  r0   r   r  r  r   r:   )r   r=   r   r   r   testMessageOptions  s    zOptionsTest.testMessageOptionsc             C   s   t j }d|_d|_x&|j D ]\}}| jd|j j qW t j }|j	j
d |jj
d x8|j D ],\}}| jd|j j | jtjj|j qfW d S )Nr?   g      @FT)r   r:   r7   r8   r   r0   r  r%  r  r  r|   r  r   r  r3  r  )r   r=   Zfield_descriptor_r   r   r   testPackedOptions$  s    
zOptionsTest.testPackedOptionsN)r&   r'   r(   r>  r@  r   r   r   r   r=    s   r=  c               @   s|   e Zd Zejej dko"ej dkdej	ddd Z
dd Zej	d	d
d Zej	d	dd Zej	d	dd ZdS )ClassAPITestr  r@   z6C++ implementation requires a call to MakeDescriptor()zMakeClass is not repeatablec          
   C   s|   t jdddd g g g g d}t jdddd g |gg g d}t jdddd g g g g d}t jd	d
dd g ||gg g d}tj| d S )NZleafzpackage.parent.child.leafr   )r  r  r  r  r  r  zpackage.parent.childZsiblingzpackage.parent.siblingparentzpackage.parent)r   r!  r   	MakeClass)r   Z	leaf_descZ
child_descZsibling_descZparent_descr   r   r   !testMakeClassWithNestedDescriptor8  s"    



z.ClassAPITest.testMakeClassWithNestedDescriptorc             C   s(   d| d }t j }tj|| |j S )a   Get a serialized representation of a test FileDescriptorProto.

    Args:
      name: All calls to this must use a unique message name, to avoid
          collisions in the cpp descriptor pool.
    Returns:
      A string containing the serialized form of a test FileDescriptorProto.
    zmessage_type {  name: "ai  "  field {    name: "flat"    number: 1    label: LABEL_REPEATED    type: TYPE_UINT32  }  field {    name: "bar"    number: 2    label: LABEL_OPTIONAL    type: TYPE_MESSAGE    type_name: "Bar"  }  nested_type {    name: "Bar"    field {      name: "baz"      number: 3      label: LABEL_OPTIONAL      type: TYPE_MESSAGE      type_name: "Baz"    }    nested_type {      name: "Baz"      enum_type {        name: "deep_enum"        value {          name: "VALUE_A"          number: 0        }      }      field {        name: "deep"        number: 4        label: LABEL_OPTIONAL        type: TYPE_UINT32      }    }  }})r   r  r	   Merger   )r   r  Zfile_descriptor_strfile_descriptorr   r   r   _GetSerializedFileDescriptorO  s    )z)ClassAPITest._GetSerializedFileDescriptorz MakeDescriptor is not repeatablec                s   t j dkrdS tj }|j| jd tj|jd  G  fdddt	j
tjtj}| }d}tj|| | j|jddd	g dS )
z7Test that the generated class can parse a flat message.rx   NAr   c                   s   e Zd Z ZdS )zSClassAPITest.testParsingFlatClassWithExplicitClassDeclaration.<locals>.MessageClassN)r&   r'   r(   r   r   )msg_descriptorr   r   MessageClass  s   rJ  zflat: 0 flat: 1 flat: 2 r?   r@   )r
   rz   r   r  r   rG  r   r4  r  r"  r#  r   r$  r   r%  r	   rE  r0   flat)r   rF  rJ  r  msg_strr   )rI  r   0testParsingFlatClassWithExplicitClassDeclaration  s    
 z=ClassAPITest.testParsingFlatClassWithExplicitClassDeclarationc             C   s`   t j }|j| jd tj|jd }tj|}| }d}t	j
|| | j|jdddg dS )z7Test that the generated class can parse a flat message.Br   zflat: 0 flat: 1 flat: 2 r?   r@   N)r   r  r   rG  r   r4  r  r   rC  r	   rE  r0   rK  )r   rF  rI  	msg_classr  rL  r   r   r   testParsingFlatClass  s    
z!ClassAPITest.testParsingFlatClassc             C   s^   t j }|j| jd tj|jd }tj|}| }d}t	j
|| | j|jjjd dS )z9Test that the generated class can parse a nested message.Cr   zbar {  baz {    deep: 4  }}r   N)r   r  r   rG  r   r4  r  r   rC  r	   rE  r0   r   r   deep)r   rF  rI  rO  r  rL  r   r   r   testParsingNestedClass  s    
z#ClassAPITest.testParsingNestedClassN)r&   r'   r(   r  r  r
   rz   r  r   r  rD  rG  rM  rP  rS  r   r   r   r   rA  5  s   8rA  __main__),r)   r~  rn   rO  r"  r   Z	unittest2r  ImportErrorZgoogle.protobufr   r   r   r   r   r   r   r	   r  r
   r   r   r   r   r   r   r   PY3r   r   objectr   ZTestCaser/   r  r  r  r  r  r  r=  rA  r&   mainr   r   r   r   <module>$   s   -
               

V
!

  :
    U

 
