U
    4Je2;                     @   s   d Z d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G dd dejZG dd dejZedkre  dS )zUnittest that directly tests the output of the pure-Python protocol
compiler.  See //google/protobuf/internal/reflection_test.py for a test which
further ensures that we can use Python protocol message objects as we expect.
z#robinson@google.com (Will Robinson)    N)test_bad_identifiers_pb2)unittest_custom_options_pb2)unittest_import_pb2)unittest_import_public_pb2)unittest_mset_pb2)unittest_mset_wire_format_pb2) unittest_no_generic_services_pb2)unittest_pb2)service)symbol_databasei    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/S )0GeneratorTestc                 C   s(   d}t j}| |jj|jj| j d S )Noptional_nested_message)r	   TestAllTypesassertEqualNestedMessage
DESCRIPTORfields_by_nameZmessage_type)self
field_nameZ
proto_type r   K/tmp/pip-unpacked-wheel-0oacnh65/google/protobuf/internal/generator_test.pytestNestedMessageDescriptor<   s    z)GeneratorTest.testNestedMessageDescriptorc                 C   s   |  dtj |  dtj |  dtj t }|  d|j |  dtjj |  d|j |  dtjj |  d|j |  dtjj d S )N                  )	r   r	   ZFOREIGN_FOOZFOREIGN_BARZFOREIGN_BAZr   FOOZBARZBAZr   protor   r   r   	testEnumsC   s    zGeneratorTest.testEnumsc                    s   t  }dd   fdd}| ||j | |jdk | ||j | |jdk  |  |j | ||j | |jdk | ||j | |jdk  |  |j | 	d|j
 d S )Nc                 S   s   | | kS Nr   valr   r   r   isnanX   s    z5GeneratorTest.testExtremeDefaultValues.<locals>.isnanc                    s    |  o | d S Nr   r   r#   r%   r   r   isinf[   s    z5GeneratorTest.testExtremeDefaultValues.<locals>.isinfr   z? ? ?? ?? ??? ??/ ??-)r	   ZTestExtremeDefaultValues
assertTrueZ
inf_doubleZneg_inf_doubleZ
nan_doubleZ	inf_floatZneg_inf_floatZ	nan_floatr   Zcpp_trigraph)r   messager(   r   r'   r   testExtremeDefaultValuesS   s    z&GeneratorTest.testExtremeDefaultValuesc                    s>   t jj}ddddd t fdd|jD }|  | d S )NFT)Zoptional_int32Zrepeated_int32r   Zdefault_int32c                    s"   g | ]}|j  kr|j |jfqS r   )nameZhas_default_value).0fZexpected_has_default_by_namer   r   
<listcomp>w   s   
z6GeneratorTest.testHasDefaultValues.<locals>.<listcomp>)r	   r   r   dictfieldsr   )r   descZhas_default_by_namer   r/   r   testHasDefaultValuesl   s    
z"GeneratorTest.testHasDefaultValuesc                 C   s.   |  tjjtjj |  tjjjtjj d S r"   )r   r	   optional_int32_extensioncontaining_typeTestAllExtensionsr   TestRequiredsingler   r   r   r   'testContainingTypeBehaviorForExtensions|   s    
z5GeneratorTest.testContainingTypeBehaviorForExtensionsc                 C   s*   |  tjjd  |  tjjjtjj d S r"   )r   r	   r5   Zextension_scoper8   r9   r   r:   r   r   r   testExtensionScope   s    
z GeneratorTest.testExtensionScopec                 C   sB   |  tjj |  tjjj tjj}|jd }|  |j  d S )Na)r)   r	   r5   Zis_extensionr8   r9   r   r   )r   Zmessage_descriptorZnon_extension_descriptorr   r   r   testIsExtension   s
    
zGeneratorTest.testIsExtensionc                 C   s   t  }| |j j d S r"   )r   ZTestMessageSetr)   r   
GetOptionsZmessage_set_wire_formatr   r   r   r   testOptions   s    zGeneratorTest.testOptionsc                 C   s*   t  }|jjd  }| |d k	 d S )NZAnEnum)r   ZTestMessageWithCustomOptionsr   enum_types_by_namer?   r)   )r   r    Zenum_optionsr   r   r   testMessageWithCustomOptions   s    z*GeneratorTest.testMessageWithCustomOptionsc                 C   s\   |  ttjjjttjjjtjjjtjjjg |  tj	jjg  |  tjjjjg  d S r"   )
r   setr	   r   r   Znested_typesr   OptionalGroupRepeatedGroupTestEmptyMessager:   r   r   r   testNestedTypes   s    
 zGeneratorTest.testNestedTypesc                 C   st   |  tjjjd k |  tjjjd k | tjjjjtjj | tjjjjtjj | tjjjjtjj d S r"   )	r)   r	   rF   r   r6   r   r   r   rE   r:   r   r   r   testContainingType   s$    


z GeneratorTest.testContainingTypec                 C   s*   |  tjjd k | tjjtjj d S r"   )r)   r	   _FOREIGNENUMr6   r   _TESTALLTYPES_NESTEDENUMr   r   r:   r   r   r   "testContainingTypeInEnumDescriptor   s    
z0GeneratorTest.testContainingTypeInEnumDescriptorc                 C   s|   |  tjjjjd tjjj}|  |jjd |  tjjjjd |  tj	jjd |  tj
jjd |  tjjjd d S )Nprotobuf_unittestZprotobuf_unittest_import)r   r	   r   r   filepackager   r   ZImportMessagerI   rJ   Z_IMPORTENUM)r   r3   r   r   r   testPackage   s,    


 zGeneratorTest.testPackagec                 C   sJ   |  tjjjg  |  tjjjdtfg |  tjjjdddtfg d S )Nr   )*   +   )i/  i  i   )r   r	   r   r   Zextension_rangesr7   MAX_EXTENSIONZTestMultipleExtensionRangesr:   r   r   r   testExtensionRange   s     z GeneratorTest.testExtensionRangec                 C   sr   |  tjjd |  tjjd | tjjd k |  tjjtjg |  tjjt	jg |  tjj
t	jg d S )Ngoogle/protobuf/unittest.protorL   )r   r	   r   r,   rN   assertFalseZserialized_pbZdependenciesr   r   Zpublic_dependenciesr:   r   r   r   testFileDescriptor   s    



z GeneratorTest.testFileDescriptorc                 C   sR   |  ttd |  ttd |  ttd ttdrN| ttjtj d S )NZTestMessager   Ztest_extensionTestService)r)   hasattrr   rU   
issubclassrW   r
   ZServicer:   r   r   r   testNoGenericServices   s    

z#GeneratorTest.testNoGenericServicesc                 C   s6   t j}| t j|jt jj  | t jj|jk d S r"   )r	   r   r   Z_TESTALLTYPESZmessage_types_by_namer,   rU   Z_TESTALLTYPES_NESTEDMESSAGEr   	file_typer   r   r   testMessageTypesByName   s    z$GeneratorTest.testMessageTypesByNamec                 C   s"   t j}| t j|jt jj  d S r"   )r	   r   r   rI   rA   r,   r[   r   r   r   testEnumTypesByName   s
    z!GeneratorTest.testEnumTypesByNamec                 C   s"   t j}| t j|jt jj  d S r"   )r	   r   r   Zmy_extension_stringZextensions_by_namer,   r[   r   r   r   testExtensionsByName   s
    z"GeneratorTest.testExtensionsByNamec                 C   sD   t  }| d|jj t }| d|j | tjtjk d S r&   )	r	   r   r   Zoptional_public_import_messageer   ZPublicImportMessager)   r   )r   Zall_type_protoZpublic_import_protor   r   r   testPublicImports  s    zGeneratorTest.testPublicImportsc                 C   s\   t  }| |jt j d | |jt j d | |jt j d | |jt j d d S )NZfoobarZbazZqux)r   ZTestBadIdentifiersr   Z
Extensionsr*   Z
descriptorZ
reflectionr
   )r   r*   r   r   r   testBadIdentifiers  s    z GeneratorTest.testBadIdentifiersc                 C   s   t jj}| dt|j | d|jd j | d|jd j | ||jd j	 | |jd |j
d  tddddg}| |tdd	 |jd jD  |j D ]2\}}||kr| |jd |j q| |j qd S )
Nr   Zoneof_fieldr   Zoneof_uint32Zoneof_nested_messageZoneof_stringZoneof_bytesc                 S   s   g | ]
}|j qS r   )r,   )r-   fieldr   r   r   r0   )  s     z+GeneratorTest.testOneof.<locals>.<listcomp>)r	   r   r   r   lenZoneofsr,   indexZassertIsr6   Zoneofs_by_namerC   r2   r   itemsZcontaining_oneofZassertIsNone)r   r3   Znested_namesr   Z
field_descr   r   r   	testOneof  s$     zGeneratorTest.testOneofc                 C   s\   |  dtjtj |  dtjtj |  dtjtj |  dtjtj d S )NFOO1BAR1)r   r	   ZTestEnumWithDupValueNameri   ZFOO2rj   ZBAR2r:   r   r   r   testEnumWithDupValue0  s    z"GeneratorTest.testEnumWithDupValueN)__name__
__module____qualname__r   r!   r+   r4   r;   r<   r>   r@   rB   rG   rH   rK   rO   rS   rV   rZ   r]   r^   r_   ra   rc   rh   rl   r   r   r   r   r   :   s.   
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	SymbolDatabaseRegistrationTestz?Checks that messages, enums and files are correctly registered.c              	   C   s   |  tjt d |  tjjt d | t t d W 5 Q R X |  tjj	t d |  tjj
t d d S )Nzprotobuf_unittest.TestAllTypesz,protobuf_unittest.TestAllTypes.NestedMessagezprotobuf_unittest.NestedMessagez,protobuf_unittest.TestAllTypes.OptionalGroupz,protobuf_unittest.TestAllTypes.RepeatedGroup)r   r	   r   r   DefaultZ	GetSymbolr   assertRaisesKeyErrorrD   rE   r:   r   r   r   testGetSymbol>  s4     z,SymbolDatabaseRegistrationTest.testGetSymbolc                 C   s8   |  dt jdj |  dt jdj d S )Nzprotobuf_unittest.ForeignEnumz)protobuf_unittest.TestAllTypes.NestedEnum)r   r   rq   poolZFindEnumTypeByNameZ	full_namer:   r   r   r   r!   Q  s    

z(SymbolDatabaseRegistrationTest.testEnumsc                 C   s   |  dt jdj d S )NrT   )r   r   rq   ru   ZFindFileByNamer,   r:   r   r   r   testFindFileByName[  s    
z1SymbolDatabaseRegistrationTest.testFindFileByNameN)rm   rn   ro   __doc__rt   r!   rv   r   r   r   r   rp   ;  s   
rp   __main__)rw   
__author__ZunittestZgoogle.protobuf.internalr   Zgoogle.protobufr   r   r   r   r   r   r	   r
   r   rR   ZTestCaser   rp   rm   mainr   r   r   r   <module>#   s&     &