U
     T‚g�8  ã                   @   s´   U d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZ ddlmZmZ dd	lmZmZmZ e	d
ƒZg Zed ed< eejƒG dd„ deƒƒZdS )zP
Things likely to be used by writers of unit tests.

Maintainer: Jonathan Lange
é    N)ÚCallableÚList)Úimplementer)Ú	ParamSpec)ÚdeferÚutils)Úfailure)ÚitrialÚutil)ÚFailTestÚSkipTestÚSynchronousTestCaseÚ_PÚ_wait_is_runningc                       sô   e Zd ZdZd1‡ fdd„	Zdd„ Ze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jdd„ ƒZdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zeeef ejejd'd(œ‡ fd)d*„Zd+d,„ Zd-d.„ Z e!fd/d0„Z"‡  Z#S )2ÚTestCasea=  
    A unit test. The atom of the unit testing universe.

    This class extends L{SynchronousTestCase} which extends C{unittest.TestCase}
    from the standard library. The main feature is the ability to return
    C{Deferred}s from tests and fixture methods and to have the suite wait for
    those C{Deferred}s to fire.  Also provides new assertions such as
    L{assertFailure}.

    @ivar timeout: A real number of seconds. If set, the test will
    raise an error if it takes longer than C{timeout} seconds.
    If not set, util.DEFAULT_TIMEOUT_DURATION is used.
    ÚrunTestc                    s   t ƒ  |¡ dS )aŸ  
        Construct an asynchronous test case for C{methodName}.

        @param methodName: The name of a method on C{self}. This method should
        be a unit test. That is, it should be a short method that calls some of
        the assert* methods. If C{methodName} is unspecified,
        L{SynchronousTestCase.runTest} will be used as the test method. This is
        mostly useful for testing Trial.
        N)ÚsuperÚ__init__)ÚselfÚ
methodName©Ú	__class__© ú</tmp/pip-unpacked-wheel-1_7di8ic/twisted/trial/_asynctest.pyr   1   s    
zTestCase.__init__c                    s&   ‡fdd„}‡ ‡fdd„}|  ||¡S )zÖ
        Fail if C{deferred} does not errback with one of C{expectedFailures}.
        Returns the original Deferred with callbacks added. You will need
        to return this Deferred from your test case.
        c                    s   ˆ   d| ›�¡‚d S )Nz$did not catch an error, instead got )ÚfailureException)Úignore©r   r   r   Ú_cbD   s    ÿz#TestCase.assertFailure.<locals>._cbc                    s.   | j ˆ Ž r| jS d ˆ t| ƒ¡}ˆ |¡‚d S )Nz
Expected: {!r}
Got:
{})ÚcheckÚvalueÚformatÚstrr   )r   Úoutput©ÚexpectedFailuresr   r   r   Ú_ebI   s    
 ÿz#TestCase.assertFailure.<locals>._eb)ÚaddCallbacks)r   Údeferredr$   r   r%   r   r#   r   ÚassertFailure=   s    	zTestCase.assertFailurec                    s    ddl m‰ ˆ ¡ ‰‡‡‡‡‡fdd„}t |tjtd�¡}tˆˆƒ}t	 
|¡rhtd |¡ƒ}t |¡S t tjˆ ¡ |¡}ˆ ˆ||¡‰ | ‡ fdd„¡ |S )	Nr   ©Úreactorc                    s”   t  ˆ›dˆ › dˆ› d�¡}t |¡}z|  |¡ W nZ t jk
rŽ   ˆ ¡  dˆ_ˆ ¡ }|d k	r~| 	|¡r~ˆ 
ˆ||¡ nˆ ˆ|¡ Y nX d S )Nz (z) still running at z secsT)r   ÚTimeoutErrorr   ÚFailureZerrbackZAlreadyCalledErrorÚcrashÚ	_timedOutÚgetTodoÚexpectedÚaddExpectedFailureÚaddError)ÚdÚeÚfÚtodo)r   r*   Úresultr   Útimeoutr   r   Ú	onTimeout[   s    ÿ
z TestCase._run.<locals>.onTimeout©Úcategoryz9{!r} is a generator function and therefore will never runc                    s   ˆ   ¡ rˆ  ¡ p| S ©N)ÚactiveÚcancel)Úx)Úcallr   r   Ú<lambda>€   ó    zTestCase._run.<locals>.<lambda>)Útwisted.internetr*   Ú
getTimeoutr   ÚsuppressWarningsr
   ÚsuppressÚDeprecationWarningÚgetattrÚinspectÚisgeneratorfunctionÚ	TypeErrorr    r   ZfailZmaybeDeferredZrunWithWarningsSuppressedÚ_getSuppressZ	callLaterÚaddBoth)r   r   r7   r9   ÚmethodÚexcr3   r   )r@   r   r*   r7   r   r8   r   Ú_runV   s.     
ÿ

ÿÿ
  ÿzTestCase._runc                 O   s   | j ||ŽS r<   )Úrun)r   ÚargsÚkwargsr   r   r   Ú__call__ƒ   s    zTestCase.__call__c                 C   s*   |   d|¡}|j| j| j|f|fd� |S )NÚsetUp©ZcallbackArgsZerrbackArgs)rP   r&   ÚdeferTestMethodÚ_ebDeferSetUp©r   Úignoredr7   r3   r   r   r   Ú
deferSetUp†   s    üzTestCase.deferSetUpc                 C   sN   |  t¡r$| | |  | j|j¡¡ n| | |¡ |  t¡rB| ¡  |  	d |¡S r<   )
r   r   ÚaddSkipÚ_getSkipReasonrU   r   r2   ÚKeyboardInterruptÚstopÚdeferRunCleanups©r   r   r7   r   r   r   rX   �   s    

zTestCase._ebDeferSetUpc                 C   sH   |   | j|¡}|j| j| j|f|fd� | | j|¡ | | j|¡ |S )NrV   )rP   Ú_testMethodNamer&   Ú_cbDeferTestMethodÚ_ebDeferTestMethodrM   r`   ÚdeferTearDownrY   r   r   r   rW   ™   s    üzTestCase.deferTestMethodc                 C   s(   |   ¡ d k	r| | |   ¡ ¡ nd| _|S )NT)r/   ZaddUnexpectedSuccessÚ_passed)r   rZ   r7   r   r   r   rc   ¥   s    zTestCase._cbDeferTestMethodc                 C   s    |   ¡ }|d k	r*| |¡r*| | ||¡ nr| | jt¡rF| | |¡ nV| t¡rf| | |¡ | 	¡  n6| t
¡r�| | |  t| | jƒ|j¡¡ n| | |¡ d S r<   )r/   r0   r1   r   r   r   Z
addFailurer^   r2   r_   r   r\   r]   rH   rb   r   )r   r5   r7   r6   r   r   r   rd   ¬   s    


 ÿzTestCase._ebDeferTestMethodc                 C   s   |   d|¡}| | j|¡ |S )NZtearDown)rP   Z
addErrbackÚ_ebDeferTearDownrY   r   r   r   re   ¼   s    zTestCase.deferTearDownc                 C   s(   |  | |¡ | t¡r| ¡  d| _d S ©NF)r2   r   r^   r_   rf   ra   r   r   r   rg   Á   s    
zTestCase._ebDeferTearDownc                 c   sx   g }t | jƒdkrX| j ¡ \}}}z|||ŽV  W q tk
rT   | t ¡ ¡ Y qX q|D ]}| | |¡ d| _q\dS )zf
        Run any scheduled cleanups and report errors (if any) to the result.
        object.
        r   FN)	ÚlenÚ	_cleanupsÚpopÚ	ExceptionÚappendr   r,   r2   rf   )r   rZ   r7   ZfailuresÚfuncrR   rS   r5   r   r   r   r`   Ç   s    zTestCase.deferRunCleanupsc                 C   s�   zt  | |¡ ¡ }|sd| _W n* tk
rH   | | t ¡ ¡ d| _Y nX | j 	¡ D ]}| | |¡ d| _qT|  
¡  |  ¡  | jrŒ| | ¡ d S rh   )r
   Ú_JanitorZpostCaseCleanuprf   ÚBaseExceptionr2   r   r,   Z	_observerZ	getErrorsZflushLoggedErrorsZ_removeObserverZ
addSuccess)r   r7   ÚcleanÚerrorr   r   r   Ú_cleanUpÙ   s    
zTestCase._cleanUpc                 C   s>   zt  | |¡ ¡  W n$ tk
r8   | | t ¡ ¡ Y nX d S r<   )r
   ro   ZpostClassCleanuprp   r2   r   r,   )r   r7   r   r   r   Ú_classCleanUpé   s    zTestCase._classCleanUpc                    s   ‡ ‡fdd„}|S )z•
        Create a method which wraps the reactor method C{name}. The new
        method issues a deprecation warning and calls the original.
        c                     s(   t jdˆ ˆ f dtd� ˆjˆ  | |ŽS )Nz{reactor.%s cannot be used inside unit tests. In the future, using %s will fail the test and may crash or hang the test run.é   )Ú
stacklevelr;   )ÚwarningsÚwarnrG   Ú_reactorMethods)ÚaÚkw©Únamer   r   r   Ú_õ   s    þûz&TestCase._makeReactorMethod.<locals>._r   )r   r}   r~   r   r|   r   Ú_makeReactorMethodï   s    
zTestCase._makeReactorMethodc                 C   s6   i | _ dD ]&}t||ƒ| j |< t|||  |¡ƒ q
dS )zó
        Deprecate C{iterate}, C{crash} and C{stop} on C{reactor}. That is,
        each method is wrapped in a function that issues a deprecation
        warning, then calls the original.

        @param reactor: The Twisted reactor.
        )r-   Ziterater_   N)ry   rH   Úsetattrr   )r   r*   r}   r   r   r   Ú_deprecateReactor  s    zTestCase._deprecateReactorc                 C   s*   | j  ¡ D ]\}}t|||ƒ q
i | _ dS )z•
        Restore the deprecated reactor methods. Undoes what
        L{_deprecateReactor} did.

        @param reactor: The Twisted reactor.
        N)ry   Úitemsr€   )r   r*   r}   rN   r   r   r   Ú_undeprecateReactor  s    zTestCase._undeprecateReactorc              	   C   sd   ddl m} |  |¡ d| _z6|  d|¡}z|  |¡ W 5 |  |¡ |  |¡ X W 5 |  |¡ X dS )zÛ
        Really run C{setUp}, the test method, and C{tearDown}.  Any of these may
        return L{defer.Deferred}s. After they complete, do some reactor cleanup.

        @param result: A L{TestResult} object.
        r   r)   FN)	rC   r*   r�   r.   rƒ   r[   rs   rt   Ú_wait)r   r7   r*   r3   r   r   r   Ú_runFixturesAndTest  s    

zTestCase._runFixturesAndTestN)r5   rR   rS   Úreturnc                    s   t ƒ j|f|ž|ŽS )a	  
        Extend the base cleanup feature with support for cleanup functions which
        return Deferreds.

        If the function C{f} returns a Deferred, C{TestCase} will wait until the
        Deferred has fired before proceeding to the next function.
        )r   Ú
addCleanup)r   r5   rR   rS   r   r   r   r‡   0  s    
zTestCase.addCleanupc                 C   s   |   ¡ S r<   )rL   r   r   r   r   ÚgetSuppress<  s    zTestCase.getSuppressc              	   C   sN   t  | jdt j¡}z
t|ƒW S  ttfk
rH   tjdt	d� t j Y S X dS )ae  
        Returns the timeout value set on this test. Checks on the instance
        first, then the class, then the module, then packages. As soon as it
        finds something with a C{timeout} attribute, returns that. Returns
        L{util.DEFAULT_TIMEOUT_DURATION} if it cannot find anything. See
        L{TestCase} docstring for more details.
        r8   z)'timeout' attribute needs to be a number.r:   N)
r
   ZacquireAttributeÚ_parentsZDEFAULT_TIMEOUT_DURATIONÚfloatÚ
ValueErrorrK   rw   rx   rG   )r   r8   r   r   r   rD   ?  s      ÿ
 ÿzTestCase.getTimeoutc                    sâ   |rt dƒ‚ddlm‰  g ‰‡fdd„}‡ ‡fdd„}t |tjdtd	�¡}‡ fd
d„}t |tjdtd	�¡}| d¡ zV| 
|¡ ˆr�W ¢DdS | 
|¡ |ˆ _zˆ  ¡  W 5 ˆ `X ˆs¾| jrÆW ¢dS tƒ ‚W 5 d‰| 	¡  X dS )zATake a Deferred that only ever callbacks. Block until it happens.z_wait is not reentrantr   r)   c                    s   ˆ d k	rˆ   | ¡ d S r<   )rm   )Úany)Úresultsr   r   rm   _  s    zTestCase._wait.<locals>.appendc                    s   ˆd k	rˆ   ¡  d S r<   ©r-   )Zign©r*   r�   r   r   r-   c  s    zTestCase._wait.<locals>.crashzreactor\.crash cannot be used.*)Úmessager;   c                      s   ˆ   ¡  d S r<   rŽ   r   r)   r   r   r_   n  s    zTestCase._wait.<locals>.stopN)ÚRuntimeErrorrC   r*   r   rE   r
   rF   rG   rm   rk   rM   r_   rQ   r.   r^   )r   r3   Zrunningrm   r-   r_   r   r�   r   r„   V  sH     ÿþ ÿþ




zTestCase._wait)r   )$Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   ZfailUnlessFailurerP   rT   r[   rX   rW   rc   rd   re   rg   r   ZinlineCallbacksr`   rs   rt   r   r�   rƒ   r…   r   r   ÚobjectrR   rS   r‡   rˆ   rD   r   r„   Ú__classcell__r   r   r   r   r   !   s:   -
	

  þr   )r•   rI   rw   Útypingr   r   Zzope.interfacer   Ztyping_extensionsr   rC   r   r   Ztwisted.pythonr   Ztwisted.trialr	   r
   Ztwisted.trial._synctestr   r   r   r   r   Ú__annotations__Z	ITestCaser   r   r   r   r   Ú<module>   s   