B
    ^ Çcï(  ã               @   s|   d Z ddlZddlmZ ddlmZ dZdd„ ZG d	d
„ d
eƒZG dd „ d eƒZ	G dd„ deƒZ
dd„ ZG dd„ deƒZdS )Ú	TestSuiteé    Né   )Úcase)ÚutilTc             C   s   t | |dd„ ƒ}|ƒ  d S )Nc               S   s   d S )N© r   r   r   úlib/python3.7/unittest/suite.pyÚ<lambda>   ó    z!_call_if_exists.<locals>.<lambda>)Úgetattr)ÚparentÚattrÚfuncr   r   r   Ú_call_if_exists   s    r   c               @   sn   e Zd ZdZdZ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dd„ ZdS )ÚBaseTestSuitezNA simple test suite that doesn't provide class or module shared fixtures.
    Tr   c             C   s   g | _ d| _|  |¡ d S )Nr   )Ú_testsÚ_removed_testsÚaddTests)ÚselfÚtestsr   r   r   Ú__init__   s    zBaseTestSuite.__init__c             C   s   dt  | j¡t| ƒf S )Nz<%s tests=%s>)r   ÚstrclassÚ	__class__Úlist)r   r   r   r   Ú__repr__   s    zBaseTestSuite.__repr__c             C   s    t || jƒstS t| ƒt|ƒkS )N)Ú
isinstancer   ÚNotImplementedr   )r   Úotherr   r   r   Ú__eq__   s    zBaseTestSuite.__eq__c             C   s
   t | jƒS )N)Úiterr   )r   r   r   r   Ú__iter__"   s    zBaseTestSuite.__iter__c             C   s(   | j }x| D ]}|r|| ¡ 7 }qW |S )N)r   ÚcountTestCases)r   ZcasesÚtestr   r   r   r    %   s
    
zBaseTestSuite.countTestCasesc             C   sL   t |ƒstd t|ƒ¡ƒ‚t|tƒr<t|tjt	fƒr<tdƒ‚| j
 |¡ d S )Nz{} is not callablezNTestCases and TestSuites must be instantiated before passing them to addTest())ÚcallableÚ	TypeErrorÚformatÚreprr   ÚtypeÚ
issubclassr   ZTestCaser   r   Úappend)r   r!   r   r   r   ÚaddTest,   s    zBaseTestSuite.addTestc             C   s.   t |tƒrtdƒ‚x|D ]}|  |¡ qW d S )Nz0tests must be an iterable of tests, not a string)r   Ústrr#   r)   )r   r   r!   r   r   r   r   6   s    

zBaseTestSuite.addTestsc             C   s:   x4t | ƒD ](\}}|jrP ||ƒ | jr
|  |¡ q
W |S )N)Ú	enumerateÚ
shouldStopÚ_cleanupÚ_removeTestAtIndex)r   ÚresultÚindexr!   r   r   r   Úrun<   s    zBaseTestSuite.runc             C   sN   y| j | }W n tk
r"   Y n(X t|dƒr@|  j| ¡ 7  _d| j |< dS )z2Stop holding a reference to the TestCase at index.r    N)r   r#   Úhasattrr   r    )r   r0   r!   r   r   r   r.   E   s    
z BaseTestSuite._removeTestAtIndexc             O   s   | j ||ŽS )N)r1   )r   ÚargsÚkwdsr   r   r   Ú__call__S   s    zBaseTestSuite.__call__c             C   s   x| D ]}|  ¡  qW dS )z7Run the tests without collecting errors in a TestResultN)Údebug)r   r!   r   r   r   r6   V   s    
zBaseTestSuite.debugN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r   r   r   r   r    r)   r   r1   r.   r5   r6   r   r   r   r   r      s   

	r   c               @   sR   e Zd ZdZ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S )r   a³  A test suite is a composite test consisting of a number of TestCases.

    For use, create an instance of TestSuite, then add test case instances.
    When all tests have been added, the suite can be passed to a test
    runner, such as TextTestRunner. It will run the individual test cases
    in the order in which they were added, aggregating the results. When
    subclassing, do not forget to call the base class constructor.
    Fc             C   sÖ   d}t |ddƒdkrd |_}x’t| ƒD ]†\}}|jr8P t|ƒrˆ|  ||¡ |  ||¡ |  ||¡ |j|_	t |jddƒs(t |ddƒrˆq(|s–||ƒ n| 
¡  | jr(|  |¡ q(W |rÒ|  d |¡ |  |¡ d|_|S )NFÚ_testRunEnteredTÚ_classSetupFailedÚ_moduleSetUpFailed)r
   r;   r+   r,   Ú_isnotsuiteÚ_tearDownPreviousClassÚ_handleModuleFixtureÚ_handleClassSetUpr   Ú_previousTestClassr6   r-   r.   Ú_handleModuleTearDown)r   r/   r6   ZtopLevelr0   r!   r   r   r   r1   f   s0    


zTestSuite.runc             C   s   t ƒ }|  |d¡ dS )z7Run the tests without collecting errors in a TestResultTN)Ú_DebugResultr1   )r   r6   r   r   r   r6   ‡   s    zTestSuite.debugc       	   
   C   sì   t |dd ƒ}|j}||krd S |jr(d S t |ddƒr8d S y
d|_W n tk
rV   Y nX t |dd ƒ}|d k	rèt|dƒ zdy
|ƒ  W nT tk
rÖ } z6t|tƒr ‚ d|_t	 
|¡}d| }|  |||¡ W d d }~X Y nX W d t|dƒ X d S )	NrB   Ú__unittest_skip__FÚ
setUpClassÚ_setupStdoutTzsetUpClass (%s)Ú_restoreStdout)r
   r   r=   r<   r#   r   Ú	Exceptionr   rD   r   r   Ú_addClassOrModuleLevelException)	r   r!   r/   ÚpreviousClassÚcurrentClassrF   ÚeÚ	classNameÚ	errorNamer   r   r   rA   Ž   s2    




$zTestSuite._handleClassSetUpc             C   s"   d }t |dd ƒ}|d k	r|j}|S )NrB   )r
   r8   )r   r/   ÚpreviousModulerK   r   r   r   Ú_get_previous_module®   s
    zTestSuite._get_previous_modulec       	   
   C   sÜ   |   |¡}|jj}||krd S |  |¡ d|_ytj| }W n tk
rP   d S X t|dd ƒ}|d k	rØt	|dƒ zZy
|ƒ  W nJ t
k
rÆ } z,t|tƒrš‚ d|_d| }|  |||¡ W d d }~X Y nX W d t	|dƒ X d S )NFÚsetUpModulerG   TzsetUpModule (%s)rH   )rQ   r   r8   rC   r=   ÚsysÚmodulesÚKeyErrorr
   r   rI   r   rD   rJ   )	r   r!   r/   rP   ZcurrentModuleÚmodulerR   rM   rO   r   r   r   r@   ¶   s,    




$zTestSuite._handleModuleFixturec             C   sL   t |ƒ}t|dd ƒ}|d k	r8t|tjƒr8||t|ƒƒ n| |t ¡ ¡ d S )NÚaddSkip)	Ú_ErrorHolderr
   r   r   ZSkipTestr*   ZaddErrorrS   Úexc_info)r   r/   Z	exceptionrO   ÚerrorrW   r   r   r   rJ   Ò   s
    z)TestSuite._addClassOrModuleLevelExceptionc          
   C   sÈ   |   |¡}|d krd S |jr d S ytj| }W n tk
rB   d S X t|dd ƒ}|d k	rÄt|dƒ zTy
|ƒ  W nD tk
r² } z&t|t	ƒrŒ‚ d| }|  
|||¡ W d d }~X Y nX W d t|dƒ X d S )NÚtearDownModulerG   ztearDownModule (%s)rH   )rQ   r=   rS   rT   rU   r
   r   rI   r   rD   rJ   )r   r/   rP   rV   r[   rM   rO   r   r   r   rC   Ú   s(    



$zTestSuite._handleModuleTearDownc       	   
   C   sÜ   t |dd ƒ}|j}||krd S t |ddƒr.d S t |ddƒr>d S t |ddƒrNd S t |dd ƒ}|d k	rØt|dƒ z^y
|ƒ  W nN tk
rÆ } z0t|tƒr–‚ t |¡}d| }|  |||¡ W d d }~X Y nX W d t|d	ƒ X d S )
NrB   r<   Fr=   rE   ÚtearDownClassrG   ztearDownClass (%s)rH   )	r
   r   r   rI   r   rD   r   r   rJ   )	r   r!   r/   rK   rL   r\   rM   rN   rO   r   r   r   r?   ó   s,    



$z TestSuite._tearDownPreviousClassN)F)r7   r8   r9   r:   r1   r6   rA   rQ   r@   rJ   rC   r?   r   r   r   r   r   \   s   
! c               @   sT   e Zd 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S )rX   z¾
    Placeholder for a TestCase inside a result. As far as a TestResult
    is concerned, this looks exactly like a unit test. Used to insert
    arbitrary errors into a test suite run.
    Nc             C   s
   || _ d S )N)Údescription)r   r]   r   r   r   r     s    z_ErrorHolder.__init__c             C   s   | j S )N)r]   )r   r   r   r   Úid  s    z_ErrorHolder.idc             C   s   d S )Nr   )r   r   r   r   ÚshortDescription   s    z_ErrorHolder.shortDescriptionc             C   s   d| j f S )Nz<ErrorHolder description=%r>)r]   )r   r   r   r   r   #  s    z_ErrorHolder.__repr__c             C   s   |   ¡ S )N)r^   )r   r   r   r   Ú__str__&  s    z_ErrorHolder.__str__c             C   s   d S )Nr   )r   r/   r   r   r   r1   )  s    z_ErrorHolder.runc             C   s
   |   |¡S )N)r1   )r   r/   r   r   r   r5   .  s    z_ErrorHolder.__call__c             C   s   dS )Nr   r   )r   r   r   r   r    1  s    z_ErrorHolder.countTestCases)r7   r8   r9   r:   ZfailureExceptionr   r^   r_   r   r`   r1   r5   r    r   r   r   r   rX     s   rX   c             C   s&   yt | ƒ W n tk
r    dS X dS )z?A crude way to tell apart testcases and suites with duck-typingTF)r   r#   )r!   r   r   r   r>   4  s
    r>   c               @   s   e Zd ZdZdZdZdZdS )rD   zCUsed by the TestSuite to hold previous class when running in debug.NF)r7   r8   r9   r:   rB   r=   r,   r   r   r   r   rD   =  s   rD   )r:   rS   Ú r   r   Z
__unittestr   Úobjectr   r   rX   r>   rD   r   r   r   r   Ú<module>   s   L 3&	