U
    4Je>                     @   s  d Z dZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	l	mZ dd
lmZ ddlmZ ddlmZ ddlmZ dZdZede G dd dejZG dd deZG dd dejZG dd dejZG dd dejZedkre  dS )z1Unittest for google.protobuf.internal.descriptor.z#robinson@google.com (Will Robinson)    N)unittest_custom_options_pb2)unittest_import_pb2)unittest_pb2)descriptor_pb2)api_implementation)	test_util)
descriptor)descriptor_pool)symbol_database)text_formatz
name: 'TestEmptyMessage'
aO  syntax = "proto2";

package protobuf_unittest;

message NestedMessage {
  enum ForeignEnum {
    FOREIGN_FOO = 4;
    FOREIGN_BAR = 5;
    FOREIGN_BAZ = 6;
  }
  optional int32 bb = 1;
}

message ResponseMessage {
}

service Service {
  rpc CallMethod(.protobuf_unittest.NestedMessage) returns (.protobuf_unittest.ResponseMessage);
}

errorc                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	
e dkd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e	
e dkd#d$d% Zd&d' Zd(S ))DescriptorTestc                 C   s  t jddd}|jjdd}|jjddt jjt jjd |jjd	d}|j	jd
dd |j	jddd |j	jddd |jjdd |j
jdd}|jjdddd}|  | _| j| | j|j| _| jj|j | _| jj|j | _| jj|j | _| jj|j | _d S )Nsome/filename/some.protoprotobuf_unittest)namepackageNestedMessage)r   Zbb   )r   numbertypelabelForeignEnumFOREIGN_FOO   )r   r   ZFOREIGN_BAR   ZFOREIGN_BAZ   ZResponseMessageZServiceZ
CallMethodz .protobuf_unittest.NestedMessagez".protobuf_unittest.ResponseMessage)r   Z
input_typeoutput_type)r   FileDescriptorProtomessage_typeaddfieldZFieldDescriptorProtoZ
TYPE_INT32ZLABEL_OPTIONAL	enum_typevalueZservicemethodGetDescriptorPoolpoolZAddZFindFileByNamer   my_filemessage_types_by_name
my_messageenum_types_by_namemy_enumservices_by_name
my_servicemethods_by_name	my_method)self
file_protomessage_protoZ
enum_protoZservice_protoZmethod_proto r2   L/tmp/pip-unpacked-wheel-0oacnh65/google/protobuf/internal/descriptor_test.pysetUpR   sF    
zDescriptorTest.setUpc                 C   s
   t  jS N)r
   Defaultr%   r/   r2   r2   r3   r$   v   s    z DescriptorTest.GetDescriptorPoolc              	   C   s   |  | jddd |  | jjd jd j| jdd | t | jdd W 5 Q R X | t | jdd W 5 Q R X | t | j  W 5 Q R X d S )Nr   r   r   i  ZNoneEnum)	assertEqualr(   ZEnumValueNamer)   Zvalues_by_numberr   assertRaisesKeyError	TypeErrorr7   r2   r2   r3   testEnumValueNamey   s"    z DescriptorTest.testEnumValueNamec                 C   s   |  | j| jjd j d S Nr   )r8   r*   valuesr   r7   r2   r2   r3   testEnumFixups   s    zDescriptorTest.testEnumFixupsc                 C   s.   |  | j| jjd j |  | j| jj d S r=   )r8   r(   fieldscontaining_typer*   r7   r2   r2   r3   testContainingTypeFixups   s    z'DescriptorTest.testContainingTypeFixupsc                 C   s   |  | j| jj d S r5   )r8   r,   r.   Zcontaining_servicer7   r2   r2   r3   testContainingServiceFixups   s    z*DescriptorTest.testContainingServiceFixupspythonz<GetDebugString is only available with the cpp implementationc                 C   s   |  | j t d S r5   )r8   r&   ZGetDebugStringTEST_FILE_DESCRIPTOR_DEBUGr7   r2   r2   r3   testGetDebugString   s    z!DescriptorTest.testGetDebugStringc                 C   s   |  | j t  |  | jjd  t  |  | j t  |  | jj	d  t
  |  | j t  |  | j t  d S r=   )r8   r*   
GetOptionsr   ZEnumOptionsr>   ZEnumValueOptionsr(   ZMessageOptionsr@   ZFieldOptionsr.   ZMethodOptionsr,   ZServiceOptionsr7   r2   r2   r3   testGetOptions   s$    zDescriptorTest.testGetOptionsc                 C   s  t j}t jj}|jd }|jd }|jd }|jd }|jd }t jj}|d}	|	 }
t j
}| d|
j|  |	 }t j}| d|j|  |	 }t j}| d	|j|  t j}| d
|j|  |	 }t j}| d|j|  |	 }t j}| d|j|  |	 }t j}| d|j|  |	 }t j}| d|j|  |		 }t j}| t j|j|  t jj}| |j | |j | |j | |j | |j | |j | |j d S )NZfield1ZAnOneofZAnEnumZANENUM_VAL2ZANENUM_VAL1Fool   `	 il   -a *   ii{   l`	 )r   
DESCRIPTORTestMessageWithCustomOptionsfields_by_nameoneofs_by_namer)   enum_values_by_nameZTestServiceWithCustomOptionsFindMethodByNamerG   	file_opt1r8   
Extensionsmessage_opt1
field_opt1
field_opt2
oneof_opt1	enum_opt1enum_value_opt1service_opt1method_opt1ZMETHODOPT1_VAL2DummyMessageContainingEnum
assertTruehas_optionsassertFalse)r/   file_descriptormessage_descriptorfield_descriptoroneof_descriptorenum_descriptorenum_value_descriptorZother_enum_value_descriptorservice_descriptormethod_descriptorfile_optionsrR   message_optionsrT   Zfield_optionsrU   rV   Zoneof_optionsrW   Zenum_optionsrX   Zenum_value_optionsrY   Zservice_optionsrZ   Zmethod_optionsr[   r2   r2   r3   testSimpleCustomOptions   sd    



z&DescriptorTest.testSimpleCustomOptionsc                 C   s4   t jj}t }|| | t|j	 d d S )N   )
r   rM   rL   r   DescriptorProtoCopyToProtor8   lenoptionsZ
ListFields)r/   ra   r1   r2   r2   r3   testCustomOptionsCopyTo   s    
z&DescriptorTest.testCustomOptionsCopyToc           
      C   s  d}d}d}d}d}d}t jj}| }| d|jt j  | ||jt j  | ||jt j  | d|jt j	  | d|jt j
  | ||jt j  | ||jt j  | d|jt j  | d|jt j  | ||jt j  | ||jt j  t jj}| }| d	|jt j  | ||jt j  | ||jt j  | ||jt j	  | ||jt j
  | ||jt j  | ||jt j  | ||jt j  | ||jt j  | ||jt j  | ||jt j  t jj}| }| d
|jt j  | d|jt j d | d|jt j  | d|jt j  | d|jt j  t j}	| |	j|jt j  t jj}| }| d|jt j d | d|jt j  t jj}| }| d|jt j d | d|jt j  d S )Ni   l         il    l    l    Fr   Tig$(@r   gYB?zHello, "World"s   Hello World      iif)r   ZCustomOptionMinIntegerValuesrL   rG   r8   rS   Zbool_optZ	int32_optZ	int64_optZ
uint32_optZ
uint64_optZ
sint32_optZ
sint64_optZfixed32_optZfixed64_optZsfixed32_optZsfixed64_optZCustomOptionMaxIntegerValuesZCustomOptionOtherValuesZassertAlmostEqualZ	float_optZ
double_optZ
string_optZ	bytes_optr\   ZTEST_OPTION_ENUM_TYPE2Zenum_optZSettingRealsFromPositiveIntsZSettingRealsFromNegativeInts)
r/   Z	kint32minZ	kint64minZ	kint32maxZ	kint64maxZ
kuint32maxZ
kuint64maxra   ri   Z
dummy_enumr2   r2   r3   testDifferentCustomOptionTypes   s    































z-DescriptorTest.testDifferentCustomOptionTypesc                 C   s  t jj}| }| d|jt j j | d|jt j jt j  | d|jt j jt j	 j
 | d|jt j j | d|jt j jt j  | d|jt j jj | d|jt j jjt j  | d|jt j jjt j	 j
 | d	|jt j jt j j | d
|jt j jt j jt j  | d|jt j jt j jt j	 j
 | d|jt jjj j | d|jt j jj | d|jt j j
 | d|jt j jj | d|jt j j d S )NrJ   iD  il  i  i  i  i  i  i  i  iI  i  iA  	         )r   ZVariousComplexOptionsrL   rG   r8   rS   Zcomplex_opt1ZfooZmoooZcorgeZmooZcomplex_opt2ZbazZgraultbarZgarplyZComplexOptionType2ZComplexOptionType4Zcomplex_opt4ZwaldoZfredZcomplex_opt3Zcomplexoptiontype5ZplughZcomplexopt6Zxyzzy)r/   r   ro   r2   r2   r3   testComplexExtensionOptionsC  s    


















z*DescriptorTest.testComplexExtensionOptionsc           	      C   sJ  t j}t jj}|jd }t jj}|jd }t jj}|d}| j	t j
 }| d|j | d|j | d|jj | d|jj	t j
 j | d|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 S )N	fieldnameZVALUEZMethodd   ZFileAnnotationZNestedFileAnnotationZFileExtensionAnnotationZEmbeddedMessageSetElementZMessageAnnotationZFieldAnnotationZEnumAnnotationZEnumValueAnnotationZServiceAnnotationZMethodAnnotation)r   rL   AggregateMessagerN   ZAggregateEnumvalues_by_nameZAggregateServicerQ   rG   rS   Zfileoptr8   issubfileZmsetZAggregateMessageSetElementZmessage_set_extensionmsgoptZfieldoptZenumoptZ
enumvaloptZ
serviceoptZ	methodopt)	r/   r`   ra   rb   rd   re   rf   rg   rh   r2   r2   r3   testAggregateOptionsu  sr    


z#DescriptorTest.testAggregateOptionsc                 C   s   t jjj}| d| jt j  |jd }| d| jt j	  t jj}|j
d }| d| jt j  |jd }| d| jt j  |jd }| d	| jt j  d S )
Ni  nested_fieldi  
NestedEnumi  ZNESTED_ENUM_VALUEi  nested_extensioni  )r   ZNestedOptionTyper   rL   r8   rG   rS   rT   rN   rU   r)   rX   rP   rY   extensions_by_namerV   )r/   Znested_messager   Zouter_messageZnested_enumZnested_enum_valuer   r2   r2   r3   testNestedOptions  s.    



z DescriptorTest.testNestedOptionsc                 C   s(   |  | jj| j |  | jj| j d S r5   )r8   r*   r   r&   r(   r7   r2   r2   r3   testFileDescriptorReferences  s    z+DescriptorTest.testFileDescriptorReferencesc                 C   s   |  | jjd |  | jjd |  | jj| j | | jj |  d| jj t	 }| j
| |  | jj|  |  tjjt  d S )Nr   r   Zproto2)r8   r&   r   r   r%   r_   r^   Zsyntaxr   r   rm   Zserialized_pbZSerializeToStringr   rL   r	   r6   )r/   r0   r2   r2   r3   testFileDescriptor  s    
z!DescriptorTest.testFileDescriptorzAImmutability of descriptors is only enforced in v2 implementationc              	   C   sH  t j}t jj}|jd }|jd }|jd }| t d |_W 5 Q R X | t d |jd< W 5 Q R X | t |j	
d  W 5 Q R X | t ||_W 5 Q R X | t d|_W 5 Q R X | t d|_W 5 Q R X | t d|_W 5 Q R X | t d|_W 5 Q R X | t}d|_W 5 Q R X | dt|j d S )Noptional_int32r   oneof_fieldZAnotherFTz&attribute is not writable: has_options)r   rL   TestAllTypesrN   r)   rO   r9   AttributeErrorr;   r@   appendrA   r^   r8   str	exception)r/   r`   ra   rb   rd   rc   er2   r2   r3   testImmutableCppDescriptor  s4    


z)DescriptorTest.testImmutableCppDescriptorc                 C   sl   t jj}|jd }| |jg  |jd }| |jg  |jd }| |jd  |jd }| |jd  d S )NZrepeated_int32Zrepeated_nested_messageZoptionalgroupZoptional_nested_message)r   r   rL   rN   r8   default_value)r/   ra   r    r2   r2   r3   testDefault  s    



zDescriptorTest.testDefaultN)__name__
__module____qualname__r4   r$   r<   r?   rB   rC   unittestskipIfr   TyperF   rH   rj   rp   rs   rx   r   r   r   r   r   r   r2   r2   r2   r3   r   P   s4   $

6^21

r   c                   @   s   e Zd ZdZdd ZdS )NewDescriptorTestzARedo the same tests as above, but with a separate DescriptorPool.c                 C   s   t  S r5   )r	   ZDescriptorPoolr7   r2   r2   r3   r$     s    z#NewDescriptorTest.GetDescriptorPoolN)r   r   r   __doc__r$   r2   r2   r2   r3   r     s   r   c                   @   s`   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S )GeneratedDescriptorTestz:Tests for the properties of descriptors in generated code.c                 C   s   |  |jd |  |jd |  || |  |jd j| | ||g | ||d i | |j | |j | |j	 | |j
 | |j | |j | |j | |jd j |  |jg  d S )Nr   zprotobuf_unittest.TestAllTypesr   )r8   r   	full_namer@   rA   assertInCheckDescriptorSequenceCheckDescriptorMappingrN   Zfields_by_numberfields_by_camelcase_namer)   rP   rO   
enum_typesr|   extension_ranges)r/   ra   r2   r2   r3   CheckMessageDescriptor  s&    z.GeneratedDescriptorTest.CheckMessageDescriptorc                 C   s   |  |jd |  |jd |  |jd |  |jjd |  |jtj |  || |  |jjd | |  |jj	d | | 
||g | 
||d i |  d |j |  d |j | |j t dkr|  |j|j d S )Nr   optionalInt32z-protobuf_unittest.TestAllTypes.optional_int32r   cpp)r8   r   camelcase_namer   rA   r   r   rL   rN   r   r   Zextension_scoper!   r]   Zhas_presencer   r   id)r/   rb   r2   r2   r3   CheckFieldDescriptor  s2    
z,GeneratedDescriptorTest.CheckFieldDescriptorc                 C   s  |  |tjjj |  |g  |  |d | |dk | || t|}| || | t	|d | t	|t	| | |t	|d  |d  |d }| ||d  | 
|| | ||d | ||d tjjjd }| || | ||d | t|j| | t|jg  t|}| t|t|d d d  | tt| d|d< |  || t dkr| tt|t| n| t|d d d S )Nr   r   change valuerD   <)assertNotEqualr   TestAllExtensionsrL   r@   r_   r8   listassertGreaterrn   r   indexcountZNestedTestAllTypesZassertNotInr9   
ValueErrorreversedStopIterationnextr   r   r   )r/   sequenceexpected_listitemZ
other_itemZreversed_iteratorr2   r2   r3   r   0  s<    z/GeneratedDescriptorTest.CheckDescriptorSequencec              	   C   s  |  |tjjj |  |i  |  |d | |dk t| }| || | || | 	t
|d | t
|t
| tt| \}}| || | ||| | t |  W 5 Q R X t dkr| t|jg  n| d |g  tt| tt| f}| |tt|  d||< |  || ||= d|d< |  || | t|jd | t|jt
|d  t dkr| t
tt| t
t| n| t|d d d S )	Nr   r   rD   r   newZnew_keyZ	key_errorr   )r   r   r   rL   rN   r_   dictitemsr8   r   rn   r   iterr   getr9   r;   r   r   keysr>   r:   __getitem__r   )r/   mappingZexcepted_dictkeyr   r2   r2   r3   r   S  s@      &z.GeneratedDescriptorTest.CheckDescriptorMappingc                 C   s   t jj}| | |jd }| | |jd }| | t jjd }| d |j	 | t j
jjdg | t jjjdddg d S )Nr   r   r   )r       )rJ   +   )i/  i  )i   r   )r   r   rL   r   rN   r   r   r)   r8   rA   r   r   TestMultipleExtensionRanges)r/   ra   rb   rd   r2   r2   r3   testDescriptorz  s(    



z&GeneratedDescriptorTest.testDescriptorc                 C   sV   t j}| |j | |j | |j | |j | |j | t j	jj d S r5   )
r   rL   r   Zdependenciesr   r'   r)   r+   r   ZTestNestedExtension)r/   Zcontaining_filer2   r2   r3   testCppDescriptorContainer  s    z2GeneratedDescriptorTest.testCppDescriptorContainerc                 C   s0   t jjjd }t|j}~| dt|j d S )Nr   FOO)	r   r   rL   r)   r   r>   r8   r   r   )r/   enumZvalues_iterr2   r2   r3   #testCppDescriptorContainer_Iterator  s    
z;GeneratedDescriptorTest.testCppDescriptorContainer_Iteratorc                 C   sH   t jj}t jjj}| t|jd | d |jk | ||jk d S )N   )	r   r   rL   r   r8   rn   nested_typesr_   r]   )r/   ra   Znested_message_descriptorr2   r2   r3   "testDescriptorNestedTypesContainer  s    
z:GeneratedDescriptorTest.testDescriptorNestedTypesContainerc                 C   s\   t jjd }| |jd | |jd jd | |jt j | |jd | 	|j
 d S )NTestServicer   rI   )r   rL   r+   r8   r   methodsZassertIsr   r   r   r-   )r/   rf   r2   r2   r3   testServiceDescriptor  s    z-GeneratedDescriptorTest.testServiceDescriptorc                 C   sZ   t jj}|jd }| |j | ||j | d|j | d|j	 | d|j
 d S )Nr   z*protobuf_unittest.TestAllTypes.oneof_fieldr   )r   r   rL   rO   r_   r^   r8   rA   r   r   r   )r/   ra   rc   r2   r2   r3   testOneofDescriptor  s    
z+GeneratedDescriptorTest.testOneofDescriptorN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r2   r2   r2   r3   r     s   #'
r   c                   @   s   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d Zee dkddd ZdS )DescriptorCopyToProtoTestz)Tests for CopyTo functions of Descriptor.c                 C   s4   | }t || | ||dt|t|f  d S )Nz#Not equal,
Actual:
%s
Expected:
%s
)r   ZMerger8   r   )r/   Zactual_protoZexpected_classexpected_asciiZexpected_protor2   r2   r3   _AssertProtoEqual  s     z+DescriptorCopyToProtoTest._AssertProtoEqualc                 C   s"   | }| | | ||| d S r5   )rm   r   )r/   descZexpected_proto_classZexpected_proto_asciiactualr2   r2   r3   _InternalTestCopyToProto  s    
  z2DescriptorCopyToProtoTest._InternalTestCopyToProtoc                 C   s   |  tjjtjt d S r5   )r   r   TestEmptyMessagerL   r   rl   #TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCIIr7   r2   r2   r3   testCopyToProto_EmptyMessage  s
    z6DescriptorCopyToProtoTest.testCopyToProto_EmptyMessagec                 C   s   d}|  tjjjtj| d S )Nz
      name: 'NestedMessage'
      field: <
        name: 'bb'
        number: 1
        label: 1  # Optional
        type: 5  # TYPE_INT32
      >
      )r   r   r   r   rL   r   rl   )r/   ZTEST_NESTED_MESSAGE_ASCIIr2   r2   r3   testCopyToProto_NestedMessage  s    
z7DescriptorCopyToProtoTest.testCopyToProto_NestedMessagec                 C   s   d}|  tjjtj| d S )Nz
      name: 'TestForeignNested'
      field: <
        name: 'foreign_nested'
        number: 1
        label: 1  # Optional
        type: 11  # TYPE_MESSAGE
        type_name: '.protobuf_unittest.TestAllTypes.NestedMessage'
      >
      )r   r   ZTestForeignNestedrL   r   rl   )r/   ZTEST_FOREIGN_NESTED_ASCIIr2   r2   r3   $testCopyToProto_ForeignNestedMessage  s    z>DescriptorCopyToProtoTest.testCopyToProto_ForeignNestedMessagec                 C   s   d}|  tjjtj| d S )Nz
      name: 'ForeignEnum'
      value: <
        name: 'FOREIGN_FOO'
        number: 4
      >
      value: <
        name: 'FOREIGN_BAR'
        number: 5
      >
      value: <
        name: 'FOREIGN_BAZ'
        number: 6
      >
      )r   r   r   rL   r   ZEnumDescriptorProto)r/   ZTEST_FOREIGN_ENUM_ASCIIr2   r2   r3   testCopyToProto_ForeignEnum  s    z5DescriptorCopyToProtoTest.testCopyToProto_ForeignEnumc                 C   s   d}|  tjjtj| d S )Na  
      name: 'TestDeprecatedFields'
      field: <
        name: 'deprecated_int32'
        number: 1
        label: 1  # Optional
        type: 5  # TYPE_INT32
        options: <
          deprecated: true
        >
      >
      field {
        name: "deprecated_int32_in_oneof"
        number: 2
        label: LABEL_OPTIONAL
        type: TYPE_INT32
        options {
          deprecated: true
        }
        oneof_index: 0
      }
      oneof_decl {
        name: "oneof_fields"
      }
      )r   r   ZTestDeprecatedFieldsrL   r   rl   )r/   ZTEST_DEPRECATED_FIELDS_ASCIIr2   r2   r3   testCopyToProto_Options	  s    z1DescriptorCopyToProtoTest.testCopyToProto_Optionsc                 C   s   d}|  tjjtj| d S )Nz}
      name: 'TestEmptyMessageWithExtensions'
      extension_range: <
        start: 1
        end: 536870912
      >
      )r   r   ZTestEmptyMessageWithExtensionsrL   r   rl   )r/   Z(TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCIIr2   r2   r3   testCopyToProto_AllExtensions)  s    z7DescriptorCopyToProtoTest.testCopyToProto_AllExtensionsc                 C   s   d}|  tjjtj| d S )Na  
      name: 'TestMultipleExtensionRanges'
      extension_range: <
        start: 42
        end: 43
      >
      extension_range: <
        start: 4143
        end: 4244
      >
      extension_range: <
        start: 65536
        end: 536870912
      >
      )r   r   r   rL   r   rl   )r/   Z*TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCIIr2   r2   r3   !testCopyToProto_SeveralExtensions7  s    z;DescriptorCopyToProtoTest.testCopyToProto_SeveralExtensionsc                 C   s   d}|  tjtj| d S )Na7  
      name: 'google/protobuf/unittest_import.proto'
      package: 'protobuf_unittest_import'
      dependency: 'google/protobuf/unittest_import_public.proto'
      message_type: <
        name: 'ImportMessage'
        field: <
          name: 'd'
          number: 1
          label: 1  # Optional
          type: 5  # TYPE_INT32
        >
      >
      enum_type: <
        name: 'ImportEnum'
        value: <
          name: 'IMPORT_FOO'
          number: 7
        >
        value: <
          name: 'IMPORT_BAR'
          number: 8
        >
        value: <
          name: 'IMPORT_BAZ'
          number: 9
        >
      >
      enum_type: <
        name: 'ImportEnumForMap'
        value: <
          name: 'UNKNOWN'
          number: 0
        >
        value: <
          name: 'FOO'
          number: 1
        >
        value: <
          name: 'BAR'
          number: 2
        >
      >
      options: <
        java_package: 'com.google.protobuf.test'
        optimize_for: 1  # SPEED
      
        cc_enable_arenas: true
      >
      public_dependency: 0
    )r   r   rL   r   r   )r/   Z%UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCIIr2   r2   r3   testCopyToProto_FileDescriptorM  s    5z8DescriptorCopyToProtoTest.testCopyToProto_FileDescriptorc                 C   s   d}|  tjjtj| d S )NaM  
      name: 'TestService'
      method: <
        name: 'Foo'
        input_type: '.protobuf_unittest.FooRequest'
        output_type: '.protobuf_unittest.FooResponse'
      >
      method: <
        name: 'Bar'
        input_type: '.protobuf_unittest.BarRequest'
        output_type: '.protobuf_unittest.BarResponse'
      >
      )r   r   r   rL   r   ZServiceDescriptorProto)r/   ZTEST_SERVICE_ASCIIr2   r2   r3   !testCopyToProto_ServiceDescriptor  s    z;DescriptorCopyToProtoTest.testCopyToProto_ServiceDescriptorc                 C   s&   d}t jjd}| |tj| d S )Nz}
      name: 'Foo'
      input_type: '.protobuf_unittest.FooRequest'
      output_type: '.protobuf_unittest.FooResponse'
    rI   )r   r   rL   rQ   r   r   ZMethodDescriptorProto)r/   r   rg   r2   r2   r3    testCopyToProto_MethodDescriptor  s    z:DescriptorCopyToProtoTest.testCopyToProto_MethodDescriptorrD   z!Pure python does not raise error.c                 C   sb   t  }| ttjjj| | ttjjj| | ttj	jj| t 
 }| ttjj| d S r5   )r   r   r9   r;   r   r   rL   rm   r   r   rl   r   )r/   r0   protor2   r2   r3   testCopyToProto_TypeError  s$    z3DescriptorCopyToProtoTest.testCopyToProto_TypeErrorN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r2   r2   r2   r3   r     s$   	 ;
r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )MakeDescriptorTestc           	   	   C   s  t  }d|_|j }|j|_|j }d|_|j }d|_|j }d|_d|_|j	 }d|_d|_t
jj|_t
jj|_|j	 }d|_d	|_t
jj|_t
jj|_d|_|j	 }d|_d
|_t
jj|_t
jj|_d|_t
|}| |jd jt
jj | |jd jt
jj | |jd jj| | |jd jd jd | |jd jd j|jd jd  | |j | |jd j t  dkr| !t" d|jd _W 5 Q R X d S )NZFoo2ZSubr   BARr   r   uint64_fieldrk   Znested_message_field	bar_fieldzFoo2.Sub.FOOr   zFoo2.Sub.bar_fieldr   F)#r   r   r   r   r   nested_typer!   r"   r   r    r   FieldDescriptorLABEL_REQUIREDr   TYPE_UINT64r   ZTYPE_MESSAGE	type_name	TYPE_ENUMMakeDescriptorr8   r@   cpp_typeCPPTYPE_UINT64ZCPPTYPE_MESSAGErA   r   r   r   r_   r^   r   r   r9   r   )	r/   file_descriptor_protor   r   r!   enum_type_valr    
enum_fieldresultr2   r2   r3   "testMakeDescriptorWithNestedFields  sb    













z5MakeDescriptorTest.testMakeDescriptorWithNestedFieldsc                 C   s   t  }d|_|j }|j|_|j }d|_|j }d|_d|_|j }d|_d|_t	j
j|_t	j
j|_|j }d|_d|_t	j
j|_t	j
j|_d	|_t	|}| |jd
 jt	j
j d S )NrI   r   r   r   r   r   rk   r   zFoo.FOOr   )r   r   r   r   r   r!   r"   r   r    r   r   r   r   r   r   r   r   r   r8   r@   r   r   )r/   r   r   r!   r   r    r   r   r2   r2   r3   &testMakeDescriptorWithUnsignedIntField  s0    









z9MakeDescriptorTest.testMakeDescriptorWithUnsignedIntFieldc                 C   sF   t  }tj}|j| t|}| }| 	d|j
tj j d S )Ne   )r   rl   r   r{   rL   rm   r   r   rG   r8   rS   r   r}   )r/   descriptor_protoZaggregate_messageZreformed_descriptorro   r2   r2   r3   testMakeDescriptorWithOptions	  s    
z0MakeDescriptorTest.testMakeDescriptorWithOptionsc                 C   s   t  }d|_dddddg}dddddg}tt|D ]"}|j }|d |_|| |_q6t	|}tt|D ]}| 
|j| j||  qpd S )	NBarZfoo_fooZFooBarZfooBazZfooFooZfoobarZfooBarr   )r   rl   r   rangern   r    r   r   r   r   r8   r@   r   )r/   r   namesZcamelcase_namesr   r    r   r2   r2   r3   testCamelcaseName  s    


z$MakeDescriptorTest.testCamelcaseNamec                 C   s   t  }d|_ddddddg}dddddd	g}tt|D ]"}|j }|d
 |_|| |_q:d	|_t	
|}tt|D ]}| |j| j||  qzd S )NZTestJsonName
field_nameZ	fieldNameZ	FieldNameZ_field_nameZ
FIELD_NAME	json_nameZ	FIELDNAMEz@typer   )r   rl   r   r   rn   r    r   r   r  r   r   r8   r@   )r/   r   r   Z
json_namesr   r    r   r2   r2   r3   testJsonName!  s,        


zMakeDescriptorTest.testJsonNameN)r   r   r   r   r   r   r   r  r2   r2   r2   r3   r     s
   /
r   __main__)r   
__author__r   warningsZgoogle.protobufr   r   r   r   Zgoogle.protobuf.internalr   r   r   r	   r
   r   r   rE   simplefilterDeprecationWarningZTestCaser   r   r   r   r   r   mainr2   r2   r2   r3   <module>   s:      ( >  v
