o
    Óó·in<  ã                   @  sÈ   U d Z ddlm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 dd
lmZmZ ddlmZmZmZ edƒZg Zded< e	ejƒG dd„ deƒƒZdS )z4
Things likely to be used by writers of unit tests.
é    )ÚannotationsN)ÚCallableÚList)Úimplementer)Ú	ParamSpec)ÚdeferÚutils)Ú_T)Úfailure)ÚitrialÚutil)ÚFailTestÚSkipTestÚSynchronousTestCaseÚ_Pz
List[None]Ú_wait_is_runningc                      sÈ   e Zd ZdZd5‡ fdd„	Zd6d
d„Ze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d7‡ fd-d.„Zd/d0„ Zd1d2„ Zefd3d4„Z‡  ZS )8Ú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dlm} || _d| _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.
        r   ©ÚreactorFN)ÚsuperÚ__init__Útwisted.internetr   Ú_twistedPrivateSchedulerÚ_twistedPrivateNeedsTearDown)ÚselfÚ
methodNamer   ©Ú	__class__© ú^C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/trial/_asynctest.pyr   1   s   

zTestCase.__init__Údeferredúdefer.Deferred[_T]ÚexpectedFailuresútype[BaseException]Úreturnc                   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    Ú_cbR   s   ÿz#TestCase.assertFailure.<locals>._cbc                   s*   | j ˆ Ž r| jS d ˆ t| ƒ¡}ˆ |¡‚)Nz
Expected: {!r}
Got:
{})ÚcheckÚvalueÚformatÚstrr&   )r
   Úoutput©r#   r   r   r    Ú_ebW   s   
ÿ
z#TestCase.assertFailure.<locals>._ebN)ZaddCallbacks)r   r!   r#   r)   r0   r   r/   r    ÚassertFailureI   s   		zTestCase.assertFailurec                   sŠ   ˆ  ¡ ‰‡‡‡‡fdd„}t |tjtd�¡}t |¡r(td 	|¡ƒ}t
 |¡S t
 tjˆ ¡ |¡}ˆj ˆ||¡‰ | ‡ fdd„¡ |S )Nc                   s˜   t  ˆ›dˆ › dˆ› d�¡}t |¡}z|  |¡ W d S  t jyK   ˆj ¡  dˆ_ˆ 	¡ }|d urB| 
|¡rBˆ ˆ||¡ Y d S ˆ ˆ|¡ Y d S w )Nz (z) still running at z secsT)r   ÚTimeoutErrorr
   ÚFailureZerrbackZAlreadyCalledErrorr   ÚcrashÚ	_timedOutÚgetTodoÚexpectedÚaddExpectedFailureÚaddError)ÚdÚeÚfÚtodo)ÚfuncDescriptionÚresultr   Útimeoutr   r    Ú	onTimeoutg   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>)Ú
getTimeoutr   ÚsuppressWarningsr   ÚsuppressÚDeprecationWarningÚinspectÚisgeneratorfunctionÚ	TypeErrorr,   r   ZfailZmaybeDeferredZrunWithWarningsSuppressedÚ_getSuppressr   Z	callLaterÚaddBoth)r   Úfuncr>   r?   rA   Úexcr:   r   )rH   r>   r?   r   r@   r    Ú_rund   s    ÿ
ÿ
ÿzTestCase._runc                 O  s   | j |i |¤ŽS rD   )Úrun)r   ÚargsÚkwargsr   r   r    Ú__call__Œ   s   zTestCase.__call__c              	   Ã  sv   �z,z|   |¡I d H  W |  |¡I d H  n	|  |¡I d H  w W | jr,|  |¡I d H  d S d S | jr:|  |¡I d H  w w rD   )Ú_deferSetUpAndRunÚ_deferRunCleanupsr   Ú_deferTearDown©r   r?   r   r   r    Ú_deferSetUp�   s   €&
ÿÿzTestCase._deferSetUpc              
   Ã  sN  �z|   | jd|¡I dH  W nX ty, } z| | |  | j|¡¡ W Y d}~dS d}~w tyK } z| | t |¡¡ | 	¡  W Y d}~dS d}~w t
yf } z| | t |¡¡ W Y d}~dS d}~ww d| _z&|   t| | jƒ| j|¡I dH  |  ¡ dur‹| | |  ¡ ¡ W dS d| _W dS  t
y¦ } z
|  t |¡|¡ ‚ d}~ww )zc
        Execute the setUp and run part of a test. Teardown and cleanups are not executed.
        ÚsetUpNT)rU   r_   r   ÚaddSkipÚ_getSkipReasonÚKeyboardInterruptr9   r
   r3   ÚstopÚBaseExceptionr   ÚgetattrÚ_testMethodNamer6   ZaddUnexpectedSuccessÚ_passedÚ_ebDeferTestMethod©r   r?   r;   r   r   r    rZ   ™   s<   €€€€þ
ÿ€þzTestCase._deferSetUpAndRunc                 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 rD   )r6   r7   r8   r*   r&   r   Z
addFailurerb   r9   rc   r   r`   ra   re   rf   r+   )r   r<   r?   r=   r   r   r    rh   ¸   s   

ÿzTestCase._ebDeferTestMethodc              
   Ã  s¢   �z|   | jd|¡I d H  W d S  ty2 } z| | t |¡¡ | ¡  d| _W Y d }~d S d }~w tyP } z| | t |¡¡ d| _W Y d }~d S d }~ww )NÚtearDownF)	rU   rj   rb   r9   r
   r3   rc   rg   rd   ri   r   r   r    r\   È   s   €€€þzTestCase._deferTearDownc                 #  sœ   � g }t | jƒdkr>| j ¡ \‰‰ ‰z|  ‡ ‡‡fdd„dˆj› �|¡V  W n ty6   | t ¡ ¡ Y nw t | jƒdks
|D ]}| 	| |¡ d| _
q@dS )zf
        Run any scheduled cleanups and report errors (if any) to the result.
        object.
        r   c                     s   ˆˆ i ˆ¤ŽS rD   r   r   ©rW   rS   rX   r   r    rI   Þ   s    z,TestCase._deferRunCleanups.<locals>.<lambda>zcleanup function FN)ÚlenÚ	_cleanupsÚpoprU   Ú__name__Ú	ExceptionÚappendr
   r3   r9   rg   )r   r?   Zfailuresr<   r   rk   r    r[   Ó   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 )NF)r   Ú_JanitorZpostCaseCleanuprg   rd   r9   r
   r3   Z	_observerZ	getErrorsZflushLoggedErrorsZ_removeObserverZ
addSuccess)r   r?   ÚcleanÚerrorr   r   r    Ú_cleanUpé   s"   €
þÿzTestCase._cleanUpc                 C  s<   zt  | |¡ ¡  W d S  ty   | | t ¡ ¡ Y d S w rD   )r   rr   ZpostClassCleanuprd   r9   r
   r3   r]   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.é   )Ú
stacklevelrC   )ÚwarningsÚwarnrM   Ú_reactorMethods)ÚaÚkw©Únamer   r   r    Ú_  s   þûz&TestCase._makeReactorMethod.<locals>._Nr   )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.
        )r4   Ziteraterc   N)r{   re   Ú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   Úmethodr   r   r    Ú_undeprecateReactor  s   
zTestCase._undeprecateReactorc              	   C  s†   ddl m} |  |¡ d| _z.tj |  |¡¡}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)r   r   rƒ   r5   r   ZDeferredZfromCoroutiner^   Ú_waitru   rv   r†   )r   r?   r   r:   r   r   r    Ú_runFixturesAndTest)  s   


ÿzTestCase._runFixturesAndTestr<   úCallable[_P, object]rW   ú_P.argsrX   ú	_P.kwargsÚNonec                   s   t ƒ j|g|¢R i |¤ŽS )ar  
        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.

        If the function takes more than C{timeout} settings, then the test will
        raise an error.
        N)r   Ú
addCleanup)r   r<   rW   rX   r   r   r    r�   @  s   zTestCase.addCleanupc                 C  s   |   ¡ S rD   )rQ   r(   r   r   r    ÚgetSuppressO  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.
        r@   z)'timeout' attribute needs to be a number.rB   N)
r   ZacquireAttributeÚ_parentsZDEFAULT_TIMEOUT_DURATIONÚfloatÚ
ValueErrorrP   ry   rz   rM   )r   r@   r   r   r    rJ   R  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 rD   )rq   )Úany)Úresultsr   r    rq   r  s   ÿzTestCase._wait.<locals>.appendc                   s   ˆd ur
ˆ   ¡  d S d S rD   ©r4   )Zign©r   r“   r   r    r4   v  s   ÿzTestCase._wait.<locals>.crashzreactor\.crash cannot be used.*)ÚmessagerC   c                     s   ˆ   ¡  d S rD   r”   r   r   r   r    rc   �  s   zTestCase._wait.<locals>.stopN)ÚRuntimeErrorr   r   r   rK   r   rL   rM   rq   rR   rn   rc   rV   r5   rb   )r   r:   Úrunningrq   r4   rc   r   r•   r    r‡   i  sL   ÿþÿþ


â

ý
zTestCase._wait)r   )r!   r"   r#   r$   r%   r"   )r<   r‰   rW   rŠ   rX   r‹   r%   rŒ   )ro   Ú
__module__Ú__qualname__Ú__doc__r   r1   ZfailUnlessFailurerU   rY   r^   rZ   rh   r\   r   ZinlineCallbacksr[   ru   rv   r�   rƒ   r†   rˆ   r�   rŽ   rJ   r   r‡   Ú__classcell__r   r   r   r    r   !   s.    
(

r   )r›   Ú
__future__r   rN   ry   Útypingr   r   Zzope.interfacer   Ztyping_extensionsr   r   r   r   Ztwisted.internet.deferr	   Ztwisted.pythonr
   Ztwisted.trialr   r   Ztwisted.trial._synctestr   r   r   r   r   Ú__annotations__Z	ITestCaser   r   r   r   r    Ú<module>   s    