B
    u9au  ã               @   s\   d dl Z d dlZd dlZd dlmZ G dd„ de jƒZG dd„ de jƒZe	dkrXe  
¡  dS )é    N)Úsystemc               @   sL   e Zd ZdZdd„ Zdd„ Zdd„ ZdZd	d
„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚExceptionClassTestsz]Tests for anything relating to exception objects themselves (e.g.,
    inheritance hierarchy)c             C   s   |   tttƒ¡ d S )N)Ú
assertTrueÚ
issubclassÚ	ExceptionÚobject)Úself© r	   ú(/usr/lib/python3.7/test_baseexception.pyÚtest_builtins_new_style   s    z+ExceptionClassTests.test_builtins_new_stylec             C   s0   x*dD ]"}|   t||ƒd|jj|f ¡ qW d S )N)ÚargsÚ__str__Ú__repr__z%s missing %s attribute)r   ÚhasattrÚ	__class__Ú__name__)r   ZinsÚattrr	   r	   r
   Úverify_instance_interface   s    
z-ExceptionClassTests.verify_instance_interfacec          	   C   sˆ  t ƒ }xBtj ¡ D ]4}yt|tƒr.| |j¡ W q tk
rD   Y qX qW t	t
j t
j t¡d d¡ƒ}zø| ¡  ¡ }ytt|ƒ}W n" tk
r¨   |  d| ¡ Y nX |  ||d| ¡ | |¡ g }d}xŽ|D ]„}| ¡ }| d¡}	||	d d … }
d|
krJ|
 d¡}|
|d d	… }|
d |d … }
tƒ |krJ| |
¡ qÖd
|
krn|
 d
¡}|
d |d … }
ytt|
ƒ}W n$ tk
r    |  d|
 ¡ Y nX ||	k r¼| ||f¡ n,||	krèx |d	 d |	kræ| ¡  qÈW |  t||d	 d ƒd|j|d	 d jf ¡ y|  |ƒ ¡ W n tk
r<   Y nX |  |
|¡ | |
¡ |}|	}qÖW W d | ¡  X |  t|ƒdd| ¡ d S )Nr   zexception_hierarchy.txtzbase class %s not a built-inz%s not foundú-é   ú(é   éÿÿÿÿú[z%s not a built-in exceptionz%s is not a subclass of %sz%s not accounted for) ÚsetÚbuiltinsÚ__dict__Úvaluesr   ÚBaseExceptionÚaddr   Ú	TypeErrorÚopenÚosÚpathÚjoinÚsplitÚ__file__ÚreadlineÚrstripÚgetattrÚAttributeErrorÚfailZassertInÚdiscardÚrindexÚindexÚplatform_systemÚappendÚpopr   r   ÚcloseÚassertEqualÚlen)r   Zexc_setÚobject_Zinheritance_treeZsuperclass_nameÚlast_excZsuperclassesZ
last_depthZexc_lineÚdepthZexc_nameZparen_indexZplatform_nameZleft_bracketÚexcr	   r	   r
   Útest_inheritance   sp    













z$ExceptionClassTests.test_inheritance)Úlengthr   ÚstrÚreprc          	   C   s:   x4t | j|ƒD ]$\}\}}|  ||d|||f ¡ qW d S )Nz%s: %s != %s)ÚzipÚinterface_testsr3   )r   ÚresultsZ	test_nameZgivenZexpectedr	   r	   r
   Úinterface_test_driverT   s    z)ExceptionClassTests.interface_test_driverc             C   sZ   d}t |ƒ}t|jƒdg|jd |gt|ƒt|ƒgt|ƒd|jj|f gf}|  |¡ d S )NÚspamr   r   z%s(%r))r   r4   r   r;   r<   r   r   r@   )r   Úargr8   r?   r	   r	   r
   Útest_interface_single_argY   s    z-ExceptionClassTests.test_interface_single_argc             C   sd   d}t t|ƒƒ}t|Ž }t|jƒ|g|j|gt|ƒt|ƒgt|ƒ|jjt|jƒ gf}|  	|¡ d S )Né   )
ÚtupleÚranger   r4   r   r;   r<   r   r   r@   )r   Ú	arg_countr   r8   r?   r	   r	   r
   Útest_interface_multi_argb   s    z,ExceptionClassTests.test_interface_multi_argc             C   sJ   t ƒ }t|jƒdg|jtƒ gt|ƒdgt|ƒ|jjd gf}|  |¡ d S )Nr   Ú z())	r   r4   r   rE   r;   r<   r   r   r@   )r   r8   r?   r	   r	   r
   Útest_interface_no_argl   s
    
z)ExceptionClassTests.test_interface_no_argN)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r9   r>   r@   rC   rH   rJ   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S )Ú
UsageTestszTest usage of exceptionsc             C   s4   y|‚W n t k
r   dS X |  dt|ƒ ¡ dS )z6Make sure that raising 'object_' triggers a TypeError.Nz!TypeError expected for raising %s)r    r+   Útype)r   r5   r	   r	   r
   Úraise_failsx   s
    zUsageTests.raise_failsc             C   s¾   y"yt ‚W n |k
r   Y nX W n8 tk
r6   Y n& t k
rZ   |  dt|ƒ ¡ Y nX y$yt ‚W n |fk
r|   Y nX W n8 tk
r”   dS  t k
r¸   |  dt|ƒ ¡ Y nX dS )z,Catching 'object_' should raise a TypeError.z#TypeError expected when catching %sNz;TypeError expected when catching %s as specified in a tuple)r   r    r+   rO   )r   r5   r	   r	   r
   Úcatch_fails€   s&    

zUsageTests.catch_failsc             C   s*   G dd„ dt ƒ}|  |¡ |  |ƒ ¡ d S )Nc               @   s   e Zd ZdS )zDUsageTests.test_raise_new_style_non_exception.<locals>.NewStyleClassN)r   rK   rL   r	   r	   r	   r
   ÚNewStyleClassœ   s   rR   )r   rP   )r   rR   r	   r	   r
   Ú"test_raise_new_style_non_exception—   s    
z-UsageTests.test_raise_new_style_non_exceptionc             C   s   |   d¡ d S )NrA   )rP   )r   r	   r	   r
   Útest_raise_string¡   s    zUsageTests.test_raise_stringc             C   s*   G dd„ dt ƒ}|  |¡ |  |ƒ ¡ d S )Nc               @   s   e Zd ZdS )zAUsageTests.test_catch_non_BaseException.<locals>.NonBaseExceptionN)r   rK   rL   r	   r	   r	   r
   ÚNonBaseException¨   s   rU   )r   rQ   )r   rU   r	   r	   r
   Útest_catch_non_BaseException¥   s    
z'UsageTests.test_catch_non_BaseExceptionc             C   s   |   tƒ ¡ d S )N)rQ   r   )r   r	   r	   r
   Ú!test_catch_BaseException_instance­   s    z,UsageTests.test_catch_BaseException_instancec             C   s   |   d¡ d S )NrA   )rQ   )r   r	   r	   r
   Útest_catch_string±   s    zUsageTests.test_catch_stringN)r   rK   rL   rM   rP   rQ   rS   rT   rV   rW   rX   r	   r	   r	   r
   rN   t   s   
rN   Ú__main__)Zunittestr   r"   Úplatformr   r/   ZTestCaser   rN   r   Úmainr	   r	   r	   r
   Ú<module>   s   mB