o
    ¯b@7  ã                   @   sœ   U 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m	Z	 ddl
mZ ddlmZmZ ddlmZmZm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)ÚList)Úimplementer)ÚdeferÚutils)Úfailure)ÚitrialÚutil)ÚFailTestÚSkipTestÚSynchronousTestCaseÚ_wait_is_runningc                       sÜ   e Zd ZdZd/‡ 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‡ fd'd(„Zd)d*„ Zd+d,„ Zefd-d.„Z‡  ZS )0Ú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__© ú:/usr/lib/python3/dist-packages/twisted/trial/_asynctest.pyr   -   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| ›�¡‚)Nz$did not catch an error, instead got )ÚfailureException)Úignore©r   r   r   Ú_cb@   s   ÿz#TestCase.assertFailure.<locals>._cbc                    s*   | j ˆ Ž r| jS d ˆ t| ƒ¡}ˆ |¡‚)Nz
Expected: {!r}
Got:
{})ÚcheckÚvalueÚformatÚstrr   )r   Úoutput©ÚexpectedFailuresr   r   r   Ú_ebE   s   
ÿ
z#TestCase.assertFailure.<locals>._eb)ÚaddCallbacks)r   Údeferredr!   r   r"   r   r    r   ÚassertFailure9   s   	zTestCase.assertFailurec                    s    ddl m‰ ˆ ¡ ‰‡‡‡‡‡fdd„}t |tjtd�¡}tˆˆƒ}t	 
|¡r4td |¡ƒ}t |¡S t tjˆ ¡ |¡}ˆ ˆ||¡‰ | ‡ fdd„¡ |S )	Nr   ©Úreactorc                    s–   t  ˆ›dˆ › dˆ› d�¡}t |¡}z|  |¡ W d S  t jyJ   ˆ ¡  dˆ_ˆ ¡ }|d urA| 	|¡rAˆ 
ˆ||¡ Y d S ˆ ˆ|¡ Y d S w )Nz (z) still running at z secsT)r   ÚTimeoutErrorr   ÚFailureÚerrbackÚAlreadyCalledErrorÚcrashÚ	_timedOutÚgetTodoÚexpectedÚaddExpectedFailureÚaddError)ÚdÚeÚfÚtodo)r   r'   Úresultr   Útimeoutr   r   Ú	onTimeoutW   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>|   s    zTestCase._run.<locals>.<lambda>)Útwisted.internetr'   Ú
getTimeoutr   ÚsuppressWarningsr   ÚsuppressÚDeprecationWarningÚgetattrÚinspectÚisgeneratorfunctionÚ	TypeErrorr   r   ÚfailÚmaybeDeferredÚrunWithWarningsSuppressedÚ_getSuppressÚ	callLaterÚaddBoth)r   r   r6   r8   ÚmethodÚexcr2   r   )r?   r   r'   r6   r   r7   r   Ú_runR   s(   ÿ

ÿÿ
ÿzTestCase._runc                 O   s   | j |i |¤Ž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©ÚcallbackArgsÚerrbackArgs)rR   r#   ÚdeferTestMethodÚ_ebDeferSetUp©r   Úignoredr6   r2   r   r   r   Ú
deferSetUp‚   s   üzTestCase.deferSetUpc                 C   sN   |  t¡r| | |  | j|j¡¡ n| | |¡ |  t¡r!| ¡  |  	d |¡S r;   )
r   r
   ÚaddSkipÚ_getSkipReasonrW   r   r1   ÚKeyboardInterruptÚstopÚdeferRunCleanups©r   r   r6   r   r   r   r\   Œ   s   

zTestCase._ebDeferSetUpc                 C   sH   |   | j|¡}|j| j| j|f|fd� | | j|¡ | | j|¡ |S )NrX   )rR   Ú_testMethodNamer#   Ú_cbDeferTestMethodÚ_ebDeferTestMethodrO   rd   ÚdeferTearDownr]   r   r   r   r[   •   s   üzTestCase.deferTestMethodc                 C   s*   |   ¡ d ur| | |   ¡ ¡ |S d| _|S )NT)r.   ÚaddUnexpectedSuccessÚ_passed)r   r^   r6   r   r   r   rg   ¡   s
   ÿzTestCase._cbDeferTestMethodc                 C   s¨   |   ¡ }|d ur| |¡r| | ||¡ d S | | jt¡r%| | |¡ d S | t¡r6| | |¡ | 	¡  d S | t
¡rL| | |  t| | jƒ|j¡¡ d S | | |¡ d S r;   )r.   r/   r0   r   r   r	   Ú
addFailurerb   r1   rc   r
   r`   ra   rF   rf   r   )r   r4   r6   r5   r   r   r   rh   ¨   s   

ÿzTestCase._ebDeferTestMethodc                 C   s   |   d|¡}| | j|¡ |S )NÚtearDown)rR   Ú
addErrbackÚ_ebDeferTearDownr]   r   r   r   ri   ¸   s   zTestCase.deferTearDownc                 C   s(   |  | |¡ | t¡r| ¡  d| _d S ©NF)r1   r   rb   rc   rk   re   r   r   r   ro   ½   s   

zTestCase._ebDeferTearDownc              	   c   sv   � g }| j ddd… D ]\}}}z
||i |¤ŽV  W q ty*   | t ¡ ¡ Y qw |D ]}| | |¡ d| _q-dS )zf
        Run any scheduled cleanups and report errors (if any) to the result.
        object.
        NéÿÿÿÿF)Ú	_cleanupsÚ	ExceptionÚappendr   r)   r1   rk   )r   r^   r6   ÚfailuresÚfuncrT   rU   r4   r   r   r   rd   Ã   s   €ÿþzTestCase.deferRunCleanupsc                 C   s’   zt  | |¡ ¡ }|sd| _W n ty#   | | t ¡ ¡ d| _Y nw | j 	¡ D ]}| | |¡ d| _q)|  
¡  |  ¡  | jrG| | ¡ d S d S rp   )r   Ú_JanitorÚpostCaseCleanuprk   ÚBaseExceptionr1   r   r)   Ú	_observerÚ	getErrorsÚflushLoggedErrorsÚ_removeObserverÚ
addSuccess)r   r6   ÚcleanÚerrorr   r   r   Ú_cleanUpÔ   s"   €
þÿzTestCase._cleanUpc                 C   s<   zt  | |¡ ¡  W d S  ty   | | t ¡ ¡ Y d S w r;   )r   rw   ÚpostClassCleanupry   r1   r   r)   )r   r6   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ˆ  | i |¤Ž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ÚwarnrE   Ú_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,   Úiteraterc   N)rˆ   rF   Ú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)rˆ   Úitemsr�   )r   r'   rŒ   rP   r   r   r   Ú_undeprecateReactor	  s   
zTestCase._undeprecateReactorc              	   C   s€   ddl m} |  |¡ d| _z+|  d|¡}z|  |¡ W |  |¡ |  |¡ n|  |¡ |  |¡ w W |  |¡ dS |  |¡ w )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)	rA   r'   r‘   r-   r_   Ú_waitr�   rƒ   r“   )r   r6   r'   r2   r   r   r   Ú_runFixturesAndTest  s   


ÿzTestCase._runFixturesAndTestc                    s   t ƒ j|g|¢R i |¤Ž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   r4   rT   rU   r   r   r   r–   )  s   zTestCase.addCleanupc                 C   s   |   ¡ S r;   )rM   r   r   r   r   ÚgetSuppress3  s   zTestCase.getSuppressc              	   C   sH   t  | jdt j¡}zt|ƒW S  ttfy#   tjdt	d� t j Y S w )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.
        r7   z)'timeout' attribute needs to be a number.r9   )
r   ÚacquireAttributeÚ_parentsÚDEFAULT_TIMEOUT_DURATIONÚfloatÚ
ValueErrorrI   r†   r‡   rE   )r   r7   r   r   r   rB   6  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¡ z5| 	|¡ ˆrMW d‰| 
¡  dS | 	|¡ |ˆ _zˆ  ¡  W ˆ `nˆ `w ˆsf| jroW d‰| 
¡  dS tƒ ‚d‰| 
¡  w )zATake a Deferred that only ever callbacks. Block until it happens.z_wait is not reentrantr   r&   c                    s   ˆ d urˆ   | ¡ d S d S r;   )rt   )Úany)Úresultsr   r   rt   V  s   ÿzTestCase._wait.<locals>.appendc                    s   ˆd ur
ˆ   ¡  d S d S r;   ©r,   )Úign©r'   rž   r   r   r,   Z  s   ÿzTestCase._wait.<locals>.crashzreactor\.crash cannot be used.*)Úmessager:   c                      s   ˆ   ¡  d S r;   rŸ   r   r&   r   r   rc   e  s   zTestCase._wait.<locals>.stopN)ÚRuntimeErrorrA   r'   r   rC   r   rD   rE   rt   rO   Úpoprc   rS   r-   rb   )r   r2   Úrunningrt   r,   rc   r   r¡   r   r”   M  sL   ÿþÿþ


â

ý
zTestCase._wait)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   ÚfailUnlessFailurerR   rV   r_   r\   r[   rg   rh   ri   ro   r   ÚinlineCallbacksrd   r�   rƒ   rŽ   r‘   r“   r•   r–   r—   rB   r   r”   Ú__classcell__r   r   r   r   r      s4    -
	

r   )r©   rG   r†   Útypingr   Úzope.interfacer   rA   r   r   Útwisted.pythonr   Útwisted.trialr   r   Útwisted.trial._synctestr	   r
   r   r   Ú__annotations__Ú	ITestCaser   r   r   r   r   Ú<module>   s   